Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import fails when /usr/libexec/platform-python is python 2.7 #17

Closed
1 of 2 tasks
ian-hurst opened this issue Oct 30, 2024 · 4 comments · Fixed by #18
Closed
1 of 2 tasks

Import fails when /usr/libexec/platform-python is python 2.7 #17

ian-hurst opened this issue Oct 30, 2024 · 4 comments · Fixed by #18
Assignees

Comments

@ian-hurst
Copy link
Contributor

ian-hurst commented Oct 30, 2024

What happened? What is the problem?

Just as the title says, when I try to import rpm on a system where platform-python is 2.7 (RHEL/CentOS 7.x), rpm-shim unexpectedly raises a subprocess.CalledProcessError:

>>> import rpm
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named machinery
Traceback (most recent call last):
  File "/home/ihurst/git/rpm-shim/rpm/__init__.py", line 222, in <module>
    _shim_module_initializing_
NameError: name '_shim_module_initializing_' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ihurst/git/rpm-shim/rpm/__init__.py", line 225, in <module>
    initialize()
  File "/home/ihurst/git/rpm-shim/rpm/__init__.py", line 201, in initialize
    for entry in get_system_sitepackages_and_suffixes():
  File "/home/ihurst/git/rpm-shim/rpm/__init__.py", line 75, in get_system_sitepackages_and_suffixes
    sitepackages_and_suffixes = get_sitepackages_and_suffixes(interpreter)
  File "/home/ihurst/git/rpm-shim/rpm/__init__.py", line 61, in get_sitepackages_and_suffixes
    output = subprocess.check_output([interpreter], input=script.encode())
  File "/usr/lib64/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib64/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/usr/libexec/platform-python']' returned non-zero exit status 1.

This is a new issue introduced by my own commit here c06a940

So sorry about this. RHEL 7 is end of life, of course, so this wasn't even a scenario to test. But I actually have production CentOS 7 systems still (via paid support from tuxcare), so it's relevant to me.

What did you expect to happen?

The expected behavior is the import rpm does not raise a CalledProcessError when it tries/usr/libexec/platform-python interpreter. It needn't succeed to import the rpm module, but it should proceed to the next interpreter when it fails, rather than raising.

The root cause is python 2.7's importlib doesn't implement machinery.

A workaround might be to catch the ImportError and return a dict with empty keys, like this:

diff --git a/rpm/__init__.py b/rpm/__init__.py
index 64f346f..f5a09e5 100644
--- a/rpm/__init__.py
+++ b/rpm/__init__.py
@@ -39,8 +39,13 @@ def get_system_sitepackages_and_suffixes() -> List[Dict[str, List[str]]]:
         script = textwrap.dedent(
             """
             import importlib
-            import importlib.machinery
             import json
+            import sys
+            try:
+                import importlib.machinery
+            except ImportError:
+                print(json.dumps({"sitepackages": [], "suffixes": []}))
+                sys.exit()
             import site
             print(
                 json.dumps(

Other approaches exit too, but this was my first thought.

Example URL(s)

No response

Steps to reproduce

1. On CentOS 7 or equivalent, create a virtualenv with any python 3+
2. Activate the virtualenv and run `python -c 'import rpm'`
3. See the exception raised

Workaround

  • There is an existing workaround that can be used until this issue is fixed.

Participation

  • I am willing to submit a pull request for this issue. (Packit team is happy to help!)
@ian-hurst
Copy link
Contributor Author

I wrote a proposed fix here: #18

@ian-hurst ian-hurst changed the title Import fails on CentOS 7 Import fails when /usr/libexec/platform-python is python 2.7 Oct 30, 2024
@mfocko mfocko moved this from new to in-review in Packit Kanban Board Oct 31, 2024
@github-project-automation github-project-automation bot moved this from in-review to done in Packit Kanban Board Oct 31, 2024
@ian-hurst
Copy link
Contributor Author

@nforro just wondering, can we get a new release that includes this fix? Thanks!

@nforro
Copy link
Member

nforro commented Nov 7, 2024

Oh, right, sorry, I was going to do that but it slipped my mind.

@ian-hurst
Copy link
Contributor Author

All good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants