Skip to content

Commit

Permalink
Refactor a bit of uniformity.
Browse files Browse the repository at this point in the history
Not-reiniting user_ns may also help fix issues on windows.
  • Loading branch information
Carreau committed Oct 29, 2022
1 parent 17ac9ea commit 9bfc216
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
22 changes: 11 additions & 11 deletions IPython/core/interactiveshell.py
Expand Up @@ -270,6 +270,16 @@ def __repr__(self):
return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
(name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))

@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 "
"as it is likely to crash IPython. If you know what you are doing, "
"you can use builtins' open."
)

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

class InteractiveShell(SingletonConfigurable):
"""An enhanced, interactive shell for Python."""
Expand Down Expand Up @@ -1255,18 +1265,7 @@ 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 "
"as it is likely to crash IPython. If you know what you are doing, "
"you can use builtins' open."
)

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

user_ns["open"] = modified_open

return user_module, user_ns

Expand Down Expand Up @@ -1336,6 +1335,7 @@ def init_user_ns(self):

ns['exit'] = self.exiter
ns['quit'] = self.exiter
ns["open"] = _modified_open

# Sync what we've added so far to user_ns_hidden so these aren't seen
# by %who
Expand Down
3 changes: 0 additions & 3 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -104,17 +104,14 @@ def test_syntax_error(self):
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)

Expand Down

0 comments on commit 9bfc216

Please sign in to comment.