From d1102636532722852ed47cab044ee4525cc16f2f Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Sat, 12 May 2018 14:46:39 -0700 Subject: [PATCH 1/2] Fix test for Python 3.7+ A bare string is interpreted as a module docstring and does not trigger the usual side-effect. --- IPython/core/compilerop.py | 20 +++++++++++++++++++- IPython/core/interactiveshell.py | 2 -- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/IPython/core/compilerop.py b/IPython/core/compilerop.py index 3dc083c90a7..19b55961122 100644 --- a/IPython/core/compilerop.py +++ b/IPython/core/compilerop.py @@ -90,13 +90,31 @@ def __init__(self): # stdlib that call it outside our control go through our codepath # (otherwise we'd lose our tracebacks). linecache.checkcache = check_linecache_ipython + + + def _fix_module_ds(self, module): + """ + Starting in python 3.7 the AST for mule have changed, and if + the first expressions encountered is a string it is attached to the + `docstring` attribute of the `Module` ast node. + + This breaks IPython, as if this string is the only expression, IPython + will not return it as the result of the current cell. + """ + from ast import Str, Expr, Module, fix_missing_locations + docstring = getattr(module, 'docstring', None) + if not docstring: + return module + new_body=[Expr(Str(docstring, lineno=1, col_offset=0), lineno=1, col_offset=0)] + new_body.extend(module.body) + return fix_missing_locations(Module(new_body)) def ast_parse(self, source, filename='', symbol='exec'): """Parse code to an AST with the current compiler flags active. Arguments are exactly the same as ast.parse (in the standard library), and are passed to the built-in compile function.""" - return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1) + return self._fix_module_ds(compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)) def reset_compiler_flags(self): """Reset compiler flags to default state.""" diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index ff450cef962..4593c6b99c4 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2865,7 +2865,6 @@ def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='la """ if not nodelist: return - if interactivity == 'last_expr_or_assign': if isinstance(nodelist[-1], _assign_nodes): asg = nodelist[-1] @@ -2895,7 +2894,6 @@ def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='la to_run_exec, to_run_interactive = [], nodelist else: raise ValueError("Interactivity was %r" % interactivity) - try: for i, node in enumerate(to_run_exec): mod = ast.Module([node]) From 9299591b9274d6471bf8dbbf262da05d440afe6f Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Sat, 12 May 2018 15:53:10 -0700 Subject: [PATCH 2/2] test on 3.7 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 77bbe82637f..99c867f1091 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: python python: - "nightly" + - "3.7-dev" - 3.6 - 3.5 - 3.4