Skip to content

pyrepl fails with an ugly traceback when ioctls are not allowed on tty stdio #134466

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

Open
glyph opened this issue May 21, 2025 · 2 comments
Open
Labels
stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@glyph
Copy link
Contributor

glyph commented May 21, 2025

Bug report

Bug description:

If you remove the ability to write ioctls to tty stdio, for example on macOS by doing

sandbox-exec -p '(version 1) (import "system.sb") (allow process-exec) (allow file-read* (subpath "/"))' python3.13

Then PyREPL fails:

Python 3.13.3 (v3.13.3:6280bb54784, Apr  8 2025, 10:47:54) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/reader.py", line 584, in prepare
    self.console.prepare()
    ~~~~~~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/unix_console.py", line 342, in prepare
    tcsetattr(self.input_fd, termios.TCSADRAIN, raw)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/fancy_termios.py", line 57, in tcsetattr
    termios.tcsetattr(fd, when, attrs.as_list())
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
termios.error: (1, 'Operation not permitted')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/simple_interact.py", line 142, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/readline.py", line 389, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/reader.py", line 744, in readline
    self.prepare()
    ~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/historical_reader.py", line 306, in prepare
    super().prepare()
    ~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/reader.py", line 593, in prepare
    self.restore()
    ~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/reader.py", line 607, in restore
    self.console.restore()
    ~~~~~~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/unix_console.py", line 372, in restore
    self.__maybe_write_code(self._rmkx)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/unix_console.py", line 713, in __maybe_write_code
    self.__write_code(fmt, *args)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/unix_console.py", line 709, in __write_code
    self.__buffer.append((curses.tparm(fmt, *args), 1))
    ^^^^^^^^^^^^^
AttributeError: 'UnixConsole' object has no attribute '_UnixConsole__buffer'. Did you mean: '_UnixConsole__move'?

You can work around this, by re-enabling ioctls:

sandbox-exec -p '(version 1) (import "system.sb") (allow process-exec) (allow file-read* (subpath "/")) (allow file-ioctl (regex "^/dev/ttys[0-9]*"))' python3.13

but pyrepl should probably be robust to this and fall back to a regular console.

As requested, notifying @ambv

CPython versions tested on:

3.13

Operating systems tested on:

macOS

@glyph glyph added the type-bug An unexpected behavior, bug, or error label May 21, 2025
@tomasr8 tomasr8 added the topic-repl Related to the interactive shell label May 21, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label May 23, 2025
@feoh
Copy link
Contributor

feoh commented May 24, 2025

I wrote a naive patch adding an except clause for termios.error in reader.py and unix_consolele.py, but I can't figure out what the right way to handle the tcsetattr in fancy_termios.py because there's no restore() to call from there.

feoh@2da12f9

@sergey-miryanov
Copy link
Contributor

If you don't mind, I would suggest you firstly fix the second exception and move __buffer declaration to the top of prepare function or even to __init__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants