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

Treat .pth injected paths as extras. #370

Merged
merged 5 commits into from
Mar 7, 2017
Merged

Conversation

kwlzn
Copy link
Contributor

@kwlzn kwlzn commented Mar 7, 2017

Fixes #302

before:

[omerta pex (kwlzn/osxtras)]$ pex --version
pex 1.2.3
[omerta pex (kwlzn/osxtras)]$ pex six -o /tmp/six_broke.pex
[omerta pex (kwlzn/osxtras)]$ PEX_VERBOSE=9 PEX_PYTHON=/usr/bin/python /tmp/six_broke.pex 
pex: Detected PEX_PYTHON, re-exec to /usr/bin/python
...
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from six import wraps
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name wraps
>>> import six; six.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc'
>>>

after:

[omerta pex (kwlzn/osxtras)]$ tox -e py27-package
...
[omerta pex (kwlzn/osxtras)]$ ./dist/pex27 six -o /tmp/six.pex
...
[omerta pex (kwlzn/osxtras)]$ PEX_VERBOSE=9 PEX_PYTHON=/usr/bin/python /tmp/six.pex 
pex: Detected PEX_PYTHON, re-exec to /usr/bin/python
...
pex: Found .pth file: /Library/Python/2.7/site-packages/Extras.pth
pex: Found site extra: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
pex: Found site extra: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
...
pex: Tainted path element: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
pex: Tainted path element: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
...
pex: Scrubbing from site-packages: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
pex: Scrubbing from site-packages: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
...
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from six import wraps
>>> import six; six.__file__
'/Users/kwilson/.pex/install/six-1.10.0-py2.py3-none-any.whl.a99dfb27e60da3957f6667853b91ea52e173da0a/six-1.10.0-py2.py3-none-any.whl/six.pyc'
>>>

pex/pex.py Outdated
if not os.path.exists(dir_path):
continue

pth_filenames = (f for f in os.listdir(dir_path) if f.endswith(os.extsep + 'pth'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never understood why os.extsep exists. This isn't something that changes across systems. I think .pth would be more readable and technically more precise. Also, it would be more uniform since we don't use extsep anywhere today, and we use . all over the place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm - fixed.

pex/util.py Outdated
try:
f = open(filename, 'rU') # noqa
except IOError:
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why site.py does this, but is this still what we want to do in our case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it hurts - otherwise it's possible we accidentally bomb trying to open a potentially problematic pth file that we're trying to passively consume.

continue
except Exception:
# Defer error handling to the higher level site.py logic invoked at startup.
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. We're not deferring error handling to site.py any more, since we've copy-pasted this out of startup code and into regular userland code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error handling I'm referring to here is the dumping of the traceback and other sys.stderr logging that happens in this case in site.py here.

I don't think we strictly need to do the import bits in our function - but they're basically no-ops afaict - and without testing the import it's possible we'd consume subsequent lines that site.py itself wouldn't respect because of how it breaks out of the processing loop in this case - so I left it in because it felt more correct.

open to stronger opinions on better handling of this tho.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I misunderstood what gets called when. Seems fine.

@@ -174,3 +174,35 @@ def test_distributionhelper_egg_assert():
'setuptools'
)
assert len(d.resource_listdir('/')) > 3


@mock.patch('os.path.exists', autospec=True, spec_set=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thing this change fixes the problem that makes mock a pain on OSX... :)

continue
except Exception:
# Defer error handling to the higher level site.py logic invoked at startup.
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I misunderstood what gets called when. Seems fine.

@kwlzn kwlzn merged commit 6b3fbdf into pex-tool:master Mar 7, 2017
@benjyw
Copy link
Collaborator

benjyw commented Mar 7, 2017

Yay! Thanks for fixing this. It was unbelievably irritating.

butlern pushed a commit to butlern/pex that referenced this pull request Jun 30, 2017
Fixes pex-tool#302

before:

    [omerta pex (kwlzn/osxtras)]$ pex --version
    pex 1.2.3
    [omerta pex (kwlzn/osxtras)]$ pex six -o /tmp/six_broke.pex
    [omerta pex (kwlzn/osxtras)]$ PEX_VERBOSE=9 PEX_PYTHON=/usr/bin/python /tmp/six_broke.pex 
    pex: Detected PEX_PYTHON, re-exec to /usr/bin/python
    ...
    Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
    [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from six import wraps
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    ImportError: cannot import name wraps
    >>> import six; six.__file__
    '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc'
    >>>

after:

    [omerta pex (kwlzn/osxtras)]$ tox -e py27-package
    ...
    [omerta pex (kwlzn/osxtras)]$ ./dist/pex27 six -o /tmp/six.pex
    ...
    [omerta pex (kwlzn/osxtras)]$ PEX_VERBOSE=9 PEX_PYTHON=/usr/bin/python /tmp/six.pex 
    pex: Detected PEX_PYTHON, re-exec to /usr/bin/python
    ...
    pex: Found .pth file: /Library/Python/2.7/site-packages/Extras.pth
    pex: Found site extra: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
    pex: Found site extra: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
    ...
    pex: Tainted path element: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
    pex: Tainted path element: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
    ...
    pex: Scrubbing from site-packages: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
    pex: Scrubbing from site-packages: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
    ...
    Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
    [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from six import wraps
    >>> import six; six.__file__
    '/Users/kwilson/.pex/install/six-1.10.0-py2.py3-none-any.whl.a99dfb27e60da3957f6667853b91ea52e173da0a/six-1.10.0-py2.py3-none-any.whl/six.pyc'
    >>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants