Author mbussonn
Recipients docs@python, mbussonn
Date 2018-05-13.00:57:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1526173050.51.0.682650639539.issue33477@psf.upfronthosting.co.za>
In-reply-to
Content
In recent Python the following 

>>> from ast import PyCF_ONLY_AST
>>> compile("'a'", 'whatever', 'exec', PyCF_ONLY_AST).body

In 3.6 it return

   [<_ast.Expr at 0x10b7441d0>] # that contail Str('a')

While on master:

   []

This is inconveninent for alternative repl like IPython, where basically if the user is entering a single string, the result is nothing in Python 3.7+, while it _does_ return something on earlier Python [1]. 

The documentation of `compile` says:

>  ... it can be 'exec' if source consists of a sequence of statements,

Which is not technically true any more as the first statement, if a string, will be removed. 

What's happening here is that since Python 3.7 if the _first_ statement is actually an expression containing a lonely string it is assign to the module docstring. So that's basically assuming you are parsing a module, and that the docstring make sens in this context, while in a REPL you are parsing a sucesssion of statements, in which case there is no need for a docstring that make no sens in this context.


This is _usually_ not an issue, unless this lonely statement is also the last, and what the user wants to execute in a REPL, in which case it has no side effect.

I don't have any objection to the new behavior, though I was wondering if this kind of side effect was anticipated. 

If that affect IPython, it will likely effect other alternative REPLs.

Thus, I believe it would be good to have this at least documented a tiny bit better and added in what's new, and potentially clarified in the `exec` docs.

I could argue that now the  `exec` name may be a tiny bit unsuitable for the new behavior, and would love if this could be optional.

1: https://github.com/ipython/ipython/issues/11133#issuecomment-388591332
History
Date User Action Args
2018-05-13 00:57:30mbussonnsetrecipients: + mbussonn, docs@python
2018-05-13 00:57:30mbussonnsetmessageid: <1526173050.51.0.682650639539.issue33477@psf.upfronthosting.co.za>
2018-05-13 00:57:30mbussonnlinkissue33477 messages
2018-05-13 00:57:27mbussonncreate