Skip to content

Commit

Permalink
raise an error when user tries to open a standard stream
Browse files Browse the repository at this point in the history
Fixes #13718
  • Loading branch information
osherdp committed Oct 1, 2022
1 parent 4f6e132 commit 2b682be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions IPython/core/interactiveshell.py
Expand Up @@ -1239,6 +1239,15 @@ def prepare_user_module(self, user_module=None, user_ns=None):
if user_ns is None:
user_ns = user_module.__dict__

@functools.wraps(io_open)
def modified_open(file, *args, **kwargs):
if file in {0, 1, 2}:
raise ValueError(f"IPython won't let you open fd={file} by default")

return io_open(file, *args, **kwargs)

user_ns["open"] = modified_open

return user_module, user_ns

def init_sys_modules(self):
Expand Down
15 changes: 15 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -103,6 +103,21 @@ def test_syntax_error(self):
res = ip.run_cell("raise = 3")
self.assertIsInstance(res.error_before_exec, SyntaxError)

def test_open_standard_input_stream(self):
ip.init_create_namespaces()
res = ip.run_cell("open(0)")
self.assertIsInstance(res.error_in_exec, ValueError)

def test_open_standard_output_stream(self):
ip.init_create_namespaces()
res = ip.run_cell("open(1)")
self.assertIsInstance(res.error_in_exec, ValueError)

def test_open_standard_error_stream(self):
ip.init_create_namespaces()
res = ip.run_cell("open(2)")
self.assertIsInstance(res.error_in_exec, ValueError)

def test_In_variable(self):
"Verify that In variable grows with user input (GH-284)"
oldlen = len(ip.user_ns['In'])
Expand Down

0 comments on commit 2b682be

Please sign in to comment.