Skip to content

GH-87235: Make sure "python /dev/fd/9 9</path/to/script.py" works on macOS - #99768

Merged
ronaldoussoren merged 3 commits into
python:mainfrom
ronaldoussoren:gh-87235-devfd-import
Nov 27, 2022
Merged

GH-87235: Make sure "python /dev/fd/9 9</path/to/script.py" works on macOS#99768
ronaldoussoren merged 3 commits into
python:mainfrom
ronaldoussoren:gh-87235-devfd-import

Conversation

@ronaldoussoren

@ronaldoussorenronaldoussoren commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

On macOS all file descriptors for a particular file in /dev/fd share the same file offset, that is open("/dev/fd/9", "r") behaves more like dup(9) than a regular open.

This causes problems when a user tries to run "/dev/fd/9" as a script because zipimport changes the file offset to try to read a zipfile directory. Therefore change zipimport to reset the file offset after trying to read the zipfile directory.

…ks on macOS
On macOS all file descriptors for a particular file in /dev/fd
share the same file offset, that is ``open("/dev/fd/9", "r")`` behaves
more like ``dup(9)`` than a regular open.
This causes problems when a user tries to run "/dev/fd/9" as a script
because zipimport changes the file offset to try to read a zipfile
directory. Therefore change zipimport to reset the file offset after
trying to read the zipfile directory.
Comment threadLib/zipimport.py
# to not cause problems when some runs 'python3 /dev/fd/9 9<some_script'
start_offset = fp.tell()
try:
fp.seek(-END_CENTRAL_DIR_SIZE, 2)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

The PR looks messy here. I just indented the body of "with fp" one level deeper to add the code to reset the file offset, that is:

start_offset=fp.tell()
try:
<ORIGINALCODE>finally:
fp.seek(start_offset)

@brettcannon
brettcannon requested review from serhiy-storchaka and removed request for brettcannonNovember 25, 2022 19:07

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like a hack to me, but I do not know a better solution.

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @ronaldoussoren for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-99816 is a backport of this pull request to the 3.11 branch.

@bedevere-botbedevere-bot removed the needs backport to 3.11 only security fixes label Nov 27, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 27, 2022
…ks on macOS (pythonGH-99768)
On macOS all file descriptors for a particular file in /dev/fd
share the same file offset, that is ``open("/dev/fd/9", "r")`` behaves
more like ``dup(9)`` than a regular open.
This causes problems when a user tries to run "/dev/fd/9" as a script
because zipimport changes the file offset to try to read a zipfile
directory. Therefore change zipimport to reset the file offset after
trying to read the zipfile directory.
(cherry picked from commit d08fb25)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 27, 2022
…ks on macOS (pythonGH-99768)
On macOS all file descriptors for a particular file in /dev/fd
share the same file offset, that is ``open("/dev/fd/9", "r")`` behaves
more like ``dup(9)`` than a regular open.
This causes problems when a user tries to run "/dev/fd/9" as a script
because zipimport changes the file offset to try to read a zipfile
directory. Therefore change zipimport to reset the file offset after
trying to read the zipfile directory.
(cherry picked from commit d08fb25)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
@bedevere-bot

Copy link
Copy Markdown

GH-99817 is a backport of this pull request to the 3.10 branch.

@bedevere-botbedevere-bot removed the needs backport to 3.10 only security fixes label Nov 27, 2022
miss-islington added a commit that referenced this pull request Nov 27, 2022
…macOS (GH-99768)
On macOS all file descriptors for a particular file in /dev/fd
share the same file offset, that is ``open("/dev/fd/9", "r")`` behaves
more like ``dup(9)`` than a regular open.
This causes problems when a user tries to run "/dev/fd/9" as a script
because zipimport changes the file offset to try to read a zipfile
directory. Therefore change zipimport to reset the file offset after
trying to read the zipfile directory.
(cherry picked from commit d08fb25)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
ambv added a commit that referenced this pull request Mar 28, 2023
…rks on macOS (GH-99768) (#99817)
On macOS all file descriptors for a particular file in /dev/fd
share the same file offset, that is ``open("/dev/fd/9", "r")`` behaves
more like ``dup(9)`` than a regular open.
This causes problems when a user tries to run "/dev/fd/9" as a script
because zipimport changes the file offset to try to read a zipfile
directory. Therefore change zipimport to reset the file offset after
trying to read the zipfile directory.
(cherry picked from commit d08fb25)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
* Regen zipimport
---------
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ronaldoussoren@miss-islington@bedevere-bot@serhiy-storchaka