This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: compile() doesn't work on ImportFrom with level=None
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Janosch.Gräf, amaury.forgeotdarc, benjamin.peterson, berker.peksag, python-dev, terry.reedy, vstinner
Priority: normal Keywords: patch

Created on 2011-11-20 00:04 by Janosch.Gräf, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue13436.patch amaury.forgeotdarc, 2011-11-20 20:41 review
issue13436.py berker.peksag, 2016-04-29 16:33
Messages (8)
msg147972 - (view) Author: Janosch Gräf (Janosch.Gräf) Date: 2011-11-20 00:04
The documentation for ast says that arguments that are marked with a '?' in the abstract grammar are optional and can therefore be None.
When I try to compile a Module node which contains an ImportFrom node with attribute level=None compile() throws an exception:

Module(body=[ImportFrom(module='time', names=[alias(name='sleep', asname=None), alias(name='time', asname=None)], level=None, lineno=0, col_offset=0)])

Traceback (most recent call last):
  File "g0.py", line 423, in <module>
    p.main()
  File "g0.py", line 65, in main
    self.reproduce("g1.pyc")
  File "g0.py", line 85, in reproduce
    co = self.generate_bytecode(st, genome)
  File "g0.py", line 243, in generate_bytecode
    co = compile(st, id, "exec")
ValueError: invalid integer value: ��������

So, I tried to set level=0:
Module(body=[ImportFrom(module='time', names=[alias(name='sleep', asname=None), alias(name='time', asname=None)], level=0, lineno=0, col_offset=0)])

and everything worked fine.

BTW: The unprintable bytes in the error message are:
ef bf bd ef bf bd ef bf bd ef bf bd ef bf bd ef bf bd ef bf bd ef bf bd
msg148007 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-20 20:41
Here is a patch for the bad error message (PyBytes_AS_BYTES after PyObject_Repr, bah)
msg148020 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-11-21 00:24
LGTM.
msg148143 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-22 21:02
New changeset ffcdfc534942 by Amaury Forgeot d'Arc in branch '3.2':
Issue #13436: Fix a bogus error message when an AST object was passed
http://hg.python.org/cpython/rev/ffcdfc534942

New changeset 470f7d7c57ce by Amaury Forgeot d'Arc in branch '3.2':
Issue #13436: commit regenerated Python-ast.c
http://hg.python.org/cpython/rev/470f7d7c57ce
msg148144 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-22 21:04
I fixed the bogus error message, but "level=None" is still not allowed, whereas the docs promise that optional values can be None.
msg148371 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-11-26 00:15
Does this apply to 2.7 as well?

I believe msg148146 is due to a commit message typo.
msg264509 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-04-29 16:33
level=None is now allowed: https://hg.python.org/cpython/file/default/Python/Python-ast.c#l5224 (see line 5224 to 5232) I converted the example in msg147972 to a Python script. test_bad_integer is still useful as it uses the required 'lineno' field. I will also commit issue13436.py as a test case.
msg264511 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-04-29 16:50
New changeset 59638baee25e by Berker Peksag in branch 'default':
Issue #13436: Add a test to make sure that ast.ImportFrom(level=None) works
https://hg.python.org/cpython/rev/59638baee25e
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57645
2016-04-29 16:50:20berker.peksagsetstatus: open -> closed
resolution: out of date
stage: resolved
2016-04-29 16:50:07python-devsetnosy: + python-dev
messages: + msg264511
2016-04-29 16:33:41berker.peksagsetfiles: + issue13436.py
nosy: + berker.peksag
messages: + msg264509

2013-10-08 17:29:33georg.brandlsetnosy: - docs@python, python-dev
components: - Documentation
2013-10-08 17:28:41georg.brandlsetmessages: - msg148146
2011-11-26 00:15:57terry.reedysetnosy: + terry.reedy, vstinner

messages: + msg148371
versions: + Python 3.2, Python 3.3, - Python 3.1
2011-11-22 21:30:58python-devsetmessages: + msg148146
2011-11-22 21:04:57amaury.forgeotdarcsetmessages: + msg148144
2011-11-22 21:02:29python-devsetnosy: + python-dev
messages: + msg148143
2011-11-21 00:24:36benjamin.petersonsetassignee: docs@python ->
2011-11-21 00:24:28benjamin.petersonsetmessages: + msg148020
2011-11-20 20:43:09amaury.forgeotdarcsetnosy: + benjamin.peterson
2011-11-20 20:41:24amaury.forgeotdarcsetfiles: + issue13436.patch

nosy: + amaury.forgeotdarc
messages: + msg148007

keywords: + patch
2011-11-20 00:04:38Janosch.Gräfcreate