##// END OF EJS Templates
Refactor a bit of uniformity....
Matthias Bussonnier -
Show More
@@ -270,6 +270,16 b' class ExecutionResult(object):'
270 270 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
271 271 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))
272 272
273 @functools.wraps(io_open)
274 def _modified_open(file, *args, **kwargs):
275 if file in {0, 1, 2}:
276 raise ValueError(
277 f"IPython won't let you open fd={file} by default "
278 "as it is likely to crash IPython. If you know what you are doing, "
279 "you can use builtins' open."
280 )
281
282 return io_open(file, *args, **kwargs)
273 283
274 284 class InteractiveShell(SingletonConfigurable):
275 285 """An enhanced, interactive shell for Python."""
@@ -1255,19 +1265,6 b' class InteractiveShell(SingletonConfigurable):'
1255 1265 if user_ns is None:
1256 1266 user_ns = user_module.__dict__
1257 1267
1258 @functools.wraps(io_open)
1259 def modified_open(file, *args, **kwargs):
1260 if file in {0, 1, 2}:
1261 raise ValueError(
1262 f"IPython won't let you open fd={file} by default "
1263 "as it is likely to crash IPython. If you know what you are doing, "
1264 "you can use builtins' open."
1265 )
1266
1267 return io_open(file, *args, **kwargs)
1268
1269 user_ns["open"] = modified_open
1270
1271 1268 return user_module, user_ns
1272 1269
1273 1270 def init_sys_modules(self):
@@ -1336,6 +1333,7 b' class InteractiveShell(SingletonConfigurable):'
1336 1333
1337 1334 ns['exit'] = self.exiter
1338 1335 ns['quit'] = self.exiter
1336 ns["open"] = _modified_open
1339 1337
1340 1338 # Sync what we've added so far to user_ns_hidden so these aren't seen
1341 1339 # by %who
@@ -104,17 +104,14 b' class InteractiveShellTestCase(unittest.TestCase):'
104 104 self.assertIsInstance(res.error_before_exec, SyntaxError)
105 105
106 106 def test_open_standard_input_stream(self):
107 ip.init_create_namespaces()
108 107 res = ip.run_cell("open(0)")
109 108 self.assertIsInstance(res.error_in_exec, ValueError)
110 109
111 110 def test_open_standard_output_stream(self):
112 ip.init_create_namespaces()
113 111 res = ip.run_cell("open(1)")
114 112 self.assertIsInstance(res.error_in_exec, ValueError)
115 113
116 114 def test_open_standard_error_stream(self):
117 ip.init_create_namespaces()
118 115 res = ip.run_cell("open(2)")
119 116 self.assertIsInstance(res.error_in_exec, ValueError)
120 117
General Comments 0
You need to be logged in to leave comments. Login now