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

Blank lines in ._pth file are not ignored #73512

Closed
zooba opened this issue Jan 19, 2017 · 16 comments
Closed

Blank lines in ._pth file are not ignored #73512

zooba opened this issue Jan 19, 2017 · 16 comments
Assignees
Labels
3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error

Comments

@zooba
Copy link
Member

zooba commented Jan 19, 2017

BPO 29326
Nosy @pfmoore, @tjguk, @bitdancer, @zware, @zooba, @mhsmith, @ammaraskar, @izbyshev, @excitoon
PRs
  • bpo-33698: Fixed skipping of empty paths while reading pth [WIP]. #7243
  • Files
  • empty_path.diff
  • underpth_blank_lines.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/zooba'
    closed_at = <Date 2017-02-08.05:06:13.782>
    created_at = <Date 2017-01-19.16:54:32.081>
    labels = ['type-bug', '3.7', 'OS-windows']
    title = 'Blank lines in ._pth file are not ignored'
    updated_at = <Date 2019-05-29.00:18:03.147>
    user = 'https://github.com/zooba'

    bugs.python.org fields:

    activity = <Date 2019-05-29.00:18:03.147>
    actor = 'excitoon'
    assignee = 'steve.dower'
    closed = True
    closed_date = <Date 2017-02-08.05:06:13.782>
    closer = 'ammar2'
    components = ['Windows']
    creation = <Date 2017-01-19.16:54:32.081>
    creator = 'steve.dower'
    dependencies = []
    files = ['46365', '46433']
    hgrepos = []
    issue_num = 29326
    keywords = ['patch']
    message_count = 16.0
    messages = ['285809', '285931', '285960', '286363', '286432', '287007', '287013', '287018', '318033', '318126', '318182', '318184', '318196', '318198', '318199', '318200']
    nosy_count = 10.0
    nosy_names = ['paul.moore', 'tim.golden', 'r.david.murray', 'python-dev', 'zach.ware', 'steve.dower', 'Malcolm Smith', 'ammar2', 'izbyshev', 'excitoon']
    pr_nums = ['7243']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29326'
    versions = ['Python 3.6', 'Python 3.7']

    @zooba
    Copy link
    Member Author

    zooba commented Jan 19, 2017

    If a python._pth file includes a blank line, it gets treated as '\n' which is then appended to the directory and used as an entry in sys.path.

    Empty lines should be ignored completely.

    @zooba zooba added 3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error labels Jan 19, 2017
    @ammaraskar
    Copy link
    Member

    I've attached a patch that makes the site package ignore empty lines in .pth files. Along with a test for this behavior. I'm not really familiar with the site machinery so I hope the test and code are fine.

    @zooba
    Copy link
    Member Author

    zooba commented Jan 21, 2017

    The fix for this issue is needed in PC/getpathp.c as this is a special feature in path generation on Windows.

    I'm not aware of any such issue with .pth files - the underscore in ._pth is deliberate.

    @izbyshev
    Copy link
    Mannequin

    izbyshev mannequin commented Jan 27, 2017

    I've encountered this issue too. (FYI, the official 3.6.0 embeddable zip from https://www.python.org/downloads/windows/ does contain a blank line in its ._pth, so all its users get an invalid entry in sys.path).

    The patch is attached. I couldn't fix tests in more straightforward way because both of them were broken:

    • Both of them generated ._pth with 'import site'
    • Both of them checked for zero exit code instead of non-zero one, but both passed because predicates in sys.exit() were incorrect

    I've strengthened the check in 'nosite' test because it is trivial to fully emulate sys.path calculation logic in this case. I've preserved weaker checks in the other test because I didn't want to emulate site.py logic.

    @zooba
    Copy link
    Member Author

    zooba commented Jan 29, 2017

    I haven't had a chance to try the patch (underpth_blank_lines.diff) or apply it, but it looks right.

    If one of the other core devs gets to apply this before I do, feel free.

    Alexey, you don't need to sign a CLA for this fix, but if you're going to contribute more then you may want to: https://docs.python.org/devguide/committing.html#contributor-licensing-agreements It's all online, and it'll help save the core developers from having to evaluate whether you need it or not (which I can do, because it comes up often for me in my day job, but not everyone has that experience). And thanks for this patch!

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 4, 2017

    New changeset 54fea351e3f9 by Steve Dower in branch '3.6':
    Issue bpo-29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
    https://hg.python.org/cpython/rev/54fea351e3f9

    New changeset a0ff777ab153 by Steve Dower in branch 'default':
    Issue bpo-29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
    https://hg.python.org/cpython/rev/a0ff777ab153

    @zooba zooba self-assigned this Feb 4, 2017
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 5, 2017

    New changeset 6d90b135e36c5b2794a29f650ebebf8bbd96f3fb by Steve Dower in branch 'master':
    Issue bpo-29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
    6d90b13

    New changeset 3ba099c135cdbced6c2ff168a90a689a83b376db by Steve Dower in branch 'master':
    Issue bpo-29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
    3ba099c

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 5, 2017

    New changeset 6d90b135e36c5b2794a29f650ebebf8bbd96f3fb by Steve Dower in branch '3.6':
    Issue bpo-29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
    6d90b13

    @mhsmith
    Copy link
    Mannequin

    mhsmith mannequin commented May 29, 2018

    I'm not aware of any such issue with .pth files - the underscore in ._pth is deliberate.

    An identical issue *does* exist for .pth files. As you can see from examining site.addpackage, it does not ignore blank lines as the documentation says. Ammar's patch should also be applied.

    @zooba
    Copy link
    Member Author

    zooba commented May 29, 2018

    Please create a new issue. This one is closed.

    @excitoon
    Copy link
    Mannequin

    excitoon mannequin commented May 30, 2018

    Hi.

    I guess this issue has to be reopen because Alexey erroneously broke adding empty paths to sys.path along with his patch.

    Official embeddable Pythons do contain a blank line in its ._pth, and this allowed users to import modules from script directory.

    Kind regards,
    Vladimir.

    @excitoon
    Copy link
    Mannequin

    excitoon mannequin commented May 30, 2018

    I shall attach pull request soon.

    @zooba
    Copy link
    Member Author

    zooba commented May 30, 2018

    The python._pth file generated for the embeddable distribution includes ".", which allows you to import from the dist directory. The blank line should be ignored.

    If you want to misuse the embeddable distribution by not restricting its search paths, delete the ._pth file.

    @excitoon
    Copy link
    Mannequin

    excitoon mannequin commented May 30, 2018

    Hi Steve.

    I'll try to explain what is my motivation.

    I need a reliable way to run Python (not matter embedded or not) in isolated mode, but still having current directory in sys.path (empty entry).

    Ironically I could misuse normal mode to simulate isolated mode with specified %PYTHONPATH% (which is not much handy, by the way), but hiding Windows Registry keys would look pretty hard (it is impossible to make a bat/cmd wrapper). Possibility to search %PYTHONPATH% in HKLM entries makes it even worse.

    Empty entry in sys.path is a extremely popular thing, and many software depend on it. It looks like a bug that isolated mode cannot support such entry.

    # How would I fix it.

    Possibly we should allow another keyword for adding an empty entry (like we have an exclusion for import site).

    Ideas are appreciated.

    @mhsmith
    Copy link
    Mannequin

    mhsmith mannequin commented May 30, 2018

    FYI: I have created bpo-33689 for the non-Windows-specific issue.

    @bitdancer
    Copy link
    Member

    Vladimir, please open a new issue for this feature request. This issue is closed.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants