##// END OF EJS Templates
Set co_name for cells run line by line. Fixes https://github.com/ipython/ipykernel/issues/841
Fabio Zadrozny -
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
@@ -3141,6 +3142,29 b' class InteractiveShell(SingletonConfigurable):'
3141 ast.fix_missing_locations(node)
3142 ast.fix_missing_locations(node)
3142 return node
3143 return node
3143
3144
3145 def _update_code_co_name(self, code):
3146 """Python 3.10 changed the behaviour so that whenever a code object
3147 is assembled in the compile(ast) the co_firstlineno would be == 1.
3148
3149 This makes pydevd/debugpy think that all cells invoked are the same
3150 since it caches information based on (co_firstlineno, co_name, co_filename).
3151
3152 Given that, this function changes the code 'co_name' to be unique
3153 based on the first real lineno of the code (which also has a nice
3154 side effect of customizing the name so that it's not always <module>).
3155
3156 See: https://github.com/ipython/ipykernel/issues/841
3157 """
3158 if not hasattr(code, "replace"):
3159 # It may not be available on older versions of Python (only
3160 # available for 3.8 onwards).
3161 return code
3162 try:
3163 first_real_line = next(dis.findlinestarts(code))[1]
3164 except StopIteration:
3165 return code
3166 return code.replace(co_name="<cell line: %s>" % (first_real_line,))
3167
3144 async def run_ast_nodes(
3168 async def run_ast_nodes(
3145 self,
3169 self,
3146 nodelist: ListType[stmt],
3170 nodelist: ListType[stmt],
@@ -3239,6 +3263,7 b' class InteractiveShell(SingletonConfigurable):'
3239 else 0x0
3263 else 0x0
3240 ):
3264 ):
3241 code = compiler(mod, cell_name, mode)
3265 code = compiler(mod, cell_name, mode)
3266 code = self._update_code_co_name(code)
3242 asy = compare(code)
3267 asy = compare(code)
3243 if await self.run_code(code, result, async_=asy):
3268 if await self.run_code(code, result, async_=asy):
3244 return True
3269 return True
General Comments 0
You need to be logged in to leave comments. Login now