[issue43069] Python fails to read a script whose path is `/dev/fd/X`

Ciprian Dorin Craciun report at bugs.python.org
Fri Jan 29 17:32:21 EST 2021


New submission from Ciprian Dorin Craciun <ciprian.craciun at gmail.com>:

Sometimes (especially from wrapper scripts) one would like to call Python with a script that should be read from a file-descriptor, like `python3 /dev/fd/9`;  most likely the backing is a file in `TMPDIR` (perhaps unlinked, like `bash`'s "here documents").

However on OSX (I've tried it on `10.15.7` and `10.13.6`), for some reason if that script is too "large" (more than a couple of characters) it just bails out, even if that file-descriptor is in fact backed by a file.

For example:
~~~~
echo 'print("1234567890")' >/tmp/x.py && python3 /dev/fd/9 9</tmp/x.py
# correctly prints 1234567890

echo 'print("12345678901234567890")' >/tmp/x.py && python3 /dev/fd/9 9</tmp/x.py
# prints nothing, no error, no exit code, etc.

# alternative variant 1, with `bash` "here-documents"
python3 /dev/fd/9 9<<<'print("12345678901234567890")'
# still prints nothing.

# alternative variant 2, that uses `/dev/stdin` instead of `/dev/fd/N`
python3 /dev/stdin <<<'print("12345678901234567890")'
# still prints nothing.

# alternative variant 3, that uses `open` and `exec`
python3 -c 'exec(open("/dev/fd/9").read())' 9<<<'print("12345678901234567890")'
# correctly prints 12345678901234567890
~~~~

The file `/tmp/x.py` is just a simple script that prints that token.

I've tried both Python 3.9.1, 3.8.2 and even 2.7.18 and 2.7.16, all with the same results.  (This makes me think it's actually an OSX issue?)

On Linux this works flawlesly.  Furthermore if one uses something else like `cat`, `bash` or anything else it works.  Thus it is something related with Python on OSX.

Also as seen from the examples, this is not a "shell issue" or something similar;  it just seems to hit a corner case when the script path is `/dev/fd/...` or `/dev/stdin`

----------
components: Interpreter Core, macOS
messages: 385955
nosy: ciprian.craciun, ned.deily, ronaldoussoren
priority: normal
severity: normal
status: open
title: Python fails to read a script whose path is `/dev/fd/X`
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43069>
_______________________________________


More information about the Python-bugs-list mailing list