Show More
@@ -82,14 +82,17 b' from warnings import warn' | |||||
82 | from logging import error |
|
82 | from logging import error | |
83 | import IPython.core.hooks |
|
83 | import IPython.core.hooks | |
84 |
|
84 | |||
85 | from typing import List as ListType, Tuple, Optional |
|
85 | from typing import List as ListType, Tuple, Optional, Callable | |
86 | from ast import AST |
|
86 | from ast import AST, stmt | |
|
87 | ||||
87 |
|
88 | |||
88 | # NoOpContext is deprecated, but ipykernel imports it from here. |
|
89 | # NoOpContext is deprecated, but ipykernel imports it from here. | |
89 | # See https://github.com/ipython/ipykernel/issues/157 |
|
90 | # See https://github.com/ipython/ipykernel/issues/157 | |
90 | # (2016, let's try to remove than in IPython 8.0) |
|
91 | # (2016, let's try to remove than in IPython 8.0) | |
91 | from IPython.utils.contexts import NoOpContext |
|
92 | from IPython.utils.contexts import NoOpContext | |
92 |
|
93 | |||
|
94 | sphinxify: Optional[Callable] | |||
|
95 | ||||
93 | try: |
|
96 | try: | |
94 | import docrepr.sphinxify as sphx |
|
97 | import docrepr.sphinxify as sphx | |
95 |
|
98 | |||
@@ -207,14 +210,20 b' def _ast_asyncify(cell:str, wrapper_name:str) -> ast.Module:' | |||||
207 | is updated only on `local()` calls. |
|
210 | is updated only on `local()` calls. | |
208 | """ |
|
211 | """ | |
209 |
|
212 | |||
210 | from ast import Expr, Await, Return |
|
213 | from ast import Expr, Await, Return, stmt, FunctionDef, Try, AsyncFunctionDef | |
211 | if sys.version_info >= (3,8): |
|
214 | if sys.version_info >= (3,8): | |
212 | return ast.parse(cell) |
|
215 | return ast.parse(cell) | |
213 | tree = ast.parse(_asyncify(cell)) |
|
216 | tree = ast.parse(_asyncify(cell)) | |
214 |
|
217 | |||
215 | function_def = tree.body[0] |
|
218 | function_def = tree.body[0] | |
|
219 | if sys.version_info > (3, 8): | |||
|
220 | assert isinstance(function_def, FunctionDef), function_def | |||
|
221 | else: | |||
|
222 | assert isinstance(function_def, (FunctionDef, AsyncFunctionDef)), function_def | |||
|
223 | ||||
216 | function_def.name = wrapper_name |
|
224 | function_def.name = wrapper_name | |
217 | try_block = function_def.body[0] |
|
225 | try_block = function_def.body[0] | |
|
226 | assert isinstance(try_block, Try) | |||
218 | lastexpr = try_block.body[-1] |
|
227 | lastexpr = try_block.body[-1] | |
219 | if isinstance(lastexpr, (Expr, Await)): |
|
228 | if isinstance(lastexpr, (Expr, Await)): | |
220 | try_block.body[-1] = Return(lastexpr.value) |
|
229 | try_block.body[-1] = Return(lastexpr.value) | |
@@ -312,7 +321,7 b' class ExecutionResult(object):' | |||||
312 | """ |
|
321 | """ | |
313 | execution_count = None |
|
322 | execution_count = None | |
314 | error_before_exec = None |
|
323 | error_before_exec = None | |
315 | error_in_exec = None |
|
324 | error_in_exec: Optional[BaseException] = None | |
316 | info = None |
|
325 | info = None | |
317 | result = None |
|
326 | result = None | |
318 |
|
327 | |||
@@ -3278,8 +3287,14 b' class InteractiveShell(SingletonConfigurable):' | |||||
3278 | ast.fix_missing_locations(node) |
|
3287 | ast.fix_missing_locations(node) | |
3279 | return node |
|
3288 | return node | |
3280 |
|
3289 | |||
3281 | async def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr', |
|
3290 | async def run_ast_nodes( | |
3282 | compiler=compile, result=None): |
|
3291 | self, | |
|
3292 | nodelist: ListType[stmt], | |||
|
3293 | cell_name: str, | |||
|
3294 | interactivity="last_expr", | |||
|
3295 | compiler=compile, | |||
|
3296 | result=None, | |||
|
3297 | ): | |||
3283 | """Run a sequence of AST nodes. The execution mode depends on the |
|
3298 | """Run a sequence of AST nodes. The execution mode depends on the | |
3284 | interactivity parameter. |
|
3299 | interactivity parameter. | |
3285 |
|
3300 | |||
@@ -3318,6 +3333,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
3318 | if not nodelist: |
|
3333 | if not nodelist: | |
3319 | return |
|
3334 | return | |
3320 |
|
3335 | |||
|
3336 | ||||
3321 | if interactivity == 'last_expr_or_assign': |
|
3337 | if interactivity == 'last_expr_or_assign': | |
3322 | if isinstance(nodelist[-1], _assign_nodes): |
|
3338 | if isinstance(nodelist[-1], _assign_nodes): | |
3323 | asg = nodelist[-1] |
|
3339 | asg = nodelist[-1] |
General Comments 0
You need to be logged in to leave comments.
Login now