Show More
@@ -15,6 +15,7 b' import abc' | |||||
15 | import ast |
|
15 | import ast | |
16 | import atexit |
|
16 | import atexit | |
17 | import builtins as builtin_mod |
|
17 | import builtins as builtin_mod | |
|
18 | import dis | |||
18 | import functools |
|
19 | import functools | |
19 | import inspect |
|
20 | import inspect | |
20 | import os |
|
21 | import os | |
@@ -3304,6 +3305,29 b' class InteractiveShell(SingletonConfigurable):' | |||||
3304 | ast.fix_missing_locations(node) |
|
3305 | ast.fix_missing_locations(node) | |
3305 | return node |
|
3306 | return node | |
3306 |
|
3307 | |||
|
3308 | def _update_code_co_name(self, code): | |||
|
3309 | """Python 3.10 changed the behaviour so that whenever a code object | |||
|
3310 | is assembled in the compile(ast) the co_firstlineno would be == 1. | |||
|
3311 | ||||
|
3312 | This makes pydevd/debugpy think that all cells invoked are the same | |||
|
3313 | since it caches information based on (co_firstlineno, co_name, co_filename). | |||
|
3314 | ||||
|
3315 | Given that, this function changes the code 'co_name' to be unique | |||
|
3316 | based on the first real lineno of the code (which also has a nice | |||
|
3317 | side effect of customizing the name so that it's not always <module>). | |||
|
3318 | ||||
|
3319 | See: https://github.com/ipython/ipykernel/issues/841 | |||
|
3320 | """ | |||
|
3321 | if not hasattr(code, "replace"): | |||
|
3322 | # It may not be available on older versions of Python (only | |||
|
3323 | # available for 3.8 onwards). | |||
|
3324 | return code | |||
|
3325 | try: | |||
|
3326 | first_real_line = next(dis.findlinestarts(code))[1] | |||
|
3327 | except StopIteration: | |||
|
3328 | return code | |||
|
3329 | return code.replace(co_name="<cell line: %s>" % (first_real_line,)) | |||
|
3330 | ||||
3307 | async def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr', |
|
3331 | async def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr', | |
3308 | compiler=compile, result=None): |
|
3332 | compiler=compile, result=None): | |
3309 | """Run a sequence of AST nodes. The execution mode depends on the |
|
3333 | """Run a sequence of AST nodes. The execution mode depends on the | |
@@ -3415,6 +3439,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
3415 | mod = ast.Interactive([node]) |
|
3439 | mod = ast.Interactive([node]) | |
3416 | with compiler.extra_flags(getattr(ast, 'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0) if self.autoawait else 0x0): |
|
3440 | with compiler.extra_flags(getattr(ast, 'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0) if self.autoawait else 0x0): | |
3417 | code = compiler(mod, cell_name, mode) |
|
3441 | code = compiler(mod, cell_name, mode) | |
|
3442 | code = self._update_code_co_name(code) | |||
3418 | asy = compare(code) |
|
3443 | asy = compare(code) | |
3419 | if (await self.run_code(code, result, async_=asy)): |
|
3444 | if (await self.run_code(code, result, async_=asy)): | |
3420 | return True |
|
3445 | return True |
General Comments 0
You need to be logged in to leave comments.
Login now