Show More
@@ -90,13 +90,31 b' class CachingCompiler(codeop.Compile):' | |||
|
90 | 90 | # stdlib that call it outside our control go through our codepath |
|
91 | 91 | # (otherwise we'd lose our tracebacks). |
|
92 | 92 | linecache.checkcache = check_linecache_ipython |
|
93 | ||
|
94 | ||
|
95 | def _fix_module_ds(self, module): | |
|
96 | """ | |
|
97 | Starting in python 3.7 the AST for mule have changed, and if | |
|
98 | the first expressions encountered is a string it is attached to the | |
|
99 | `docstring` attribute of the `Module` ast node. | |
|
100 | ||
|
101 | This breaks IPython, as if this string is the only expression, IPython | |
|
102 | will not return it as the result of the current cell. | |
|
103 | """ | |
|
104 | from ast import Str, Expr, Module, fix_missing_locations | |
|
105 | docstring = getattr(module, 'docstring', None) | |
|
106 | if not docstring: | |
|
107 | return module | |
|
108 | new_body=[Expr(Str(docstring, lineno=1, col_offset=0), lineno=1, col_offset=0)] | |
|
109 | new_body.extend(module.body) | |
|
110 | return fix_missing_locations(Module(new_body)) | |
|
93 | 111 | |
|
94 | 112 | def ast_parse(self, source, filename='<unknown>', symbol='exec'): |
|
95 | 113 | """Parse code to an AST with the current compiler flags active. |
|
96 | 114 | |
|
97 | 115 | Arguments are exactly the same as ast.parse (in the standard library), |
|
98 | 116 | and are passed to the built-in compile function.""" |
|
99 | return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1) | |
|
117 | return self._fix_module_ds(compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)) | |
|
100 | 118 | |
|
101 | 119 | def reset_compiler_flags(self): |
|
102 | 120 | """Reset compiler flags to default state.""" |
@@ -2865,7 +2865,6 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2865 | 2865 | """ |
|
2866 | 2866 | if not nodelist: |
|
2867 | 2867 | return |
|
2868 | ||
|
2869 | 2868 | if interactivity == 'last_expr_or_assign': |
|
2870 | 2869 | if isinstance(nodelist[-1], _assign_nodes): |
|
2871 | 2870 | asg = nodelist[-1] |
@@ -2895,7 +2894,6 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2895 | 2894 | to_run_exec, to_run_interactive = [], nodelist |
|
2896 | 2895 | else: |
|
2897 | 2896 | raise ValueError("Interactivity was %r" % interactivity) |
|
2898 | ||
|
2899 | 2897 | try: |
|
2900 | 2898 | for i, node in enumerate(to_run_exec): |
|
2901 | 2899 | mod = ast.Module([node]) |
General Comments 0
You need to be logged in to leave comments.
Login now