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: compiler.compile fails on "if" statement in attached file
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Jim.Jewett, benjamin.peterson, mark.dickinson, meador.inge, menegazzobr, terry.reedy
Priority: normal Keywords:

Created on 2012-03-01 18:03 by menegazzobr, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
small_with_error.py menegazzobr, 2012-03-01 18:02
Messages (8)
msg154707 - (view) Author: Fabio Menegazzo (menegazzobr) Date: 2012-03-01 18:02
compiler.compile fails on "if" statement in attached file.

When executing the code

    compiler.compile(contents, '<string>', 'exec')

passing the attached file contents, the following error is raised:

    ValueError: chr() arg not in range(256)

This won't fail when using the builtin "compile". Also removing the "if" statement or any line before it, the contents are compiled successfully.
msg154803 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-02 21:40
Please provide a *minimal* example that exhibits the problem. 'Minimal' means that if a single line is removed, the problem disappears.
msg155290 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2012-03-10 04:53
If I understood correctly, that *is* the minimal case, which suggests some sort of size problem. 

That said, I could not duplicate on 2.6.2, nor with py_compile.compiler in 3.2.2; I am not currently sufficiently motivated to install another version just for this.
msg155304 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-03-10 11:52
Here's the trackback I get with Python 2.7.  Look's like something's assuming that POP_JUMP_IF_FALSE target of 65541 fits in 16 bits.


Python 2.7.2 (default, Jan 13 2012, 17:11:09) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('small_with_error.py') as f:
...     contents = f.read()
... 
>>> import compiler
>>> compiler.compile(contents, '<string>', 'exec')
POP_JUMP_IF_FALSE 65541
114 5 256
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pycodegen.py", line 65, in compile
    gen.compile()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pycodegen.py", line 117, in compile
    self.code = gen.getCode()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pycodegen.py", line 248, in getCode
    return self.graph.getCode()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pyassem.py", line 313, in getCode
    self.makeByteCode()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pyassem.py", line 518, in makeByteCode
    lnotab.addCode(self.opnum[opname], lo, hi)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/pyassem.py", line 612, in addCode
    self.code.append(chr(arg))
ValueError: chr() arg not in range(256)
msg155305 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-03-10 12:26
Interestingly, dis.dis also fails to decompile the .pyc bytecode generated by importing the file.  (This was after adding dummy AddUnit, MakeCustomaryToBase and MakeBaseToCustomary definitions to the top of the file to prevent the import raising an exception.)


>>> with open('small_with_error.pyc', 'r') as f:
...     bytecode = f.read()
... 
>>> bytecode = bytecode[8:]
>>> dis.dis(bytecode)

<... lots of output snipped ...>

       65686 LOAD_GLOBAL         4 (4)
       65689 STOP_CODE      
       65690 STOP_CODE      
       65691 POP_JUMP_IF_TRUE 27749
       65694 BUILD_TUPLE      1140
       65697 STOP_CODE      
       65698 STOP_CODE      
       65699 STOP_CODE      
       65700 STORE_GLOBAL    26482 (26482)
       65703 POP_JUMP_IF_TRUE  1652
       65706 STOP_CODE      
       65707 STOP_CODE      
       65708 STOP_CODE      
       65709 COMPARE_OP      24951
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/dis.py", line 45, in dis
    disassemble_string(x)
  File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/dis.py", line 147, in disassemble_string
    print '(' + cmp_op[oparg] + ')',
IndexError: tuple index out of range
msg155306 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-03-10 12:41
D'oh.  Ignore that last message;   all it shows is that I'm stupid.
msg155308 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-03-10 12:59
Okay, so the issue is simply that the compiler module doesn't have logic for emitting the EXTENDED_ARGS[1] bytecode instruction to properly deal with a jump target that won't fit in two bytes.  Given that the compiler module is deprecated, I propose closing this as 'won't fix'.

[1] http://docs.python.org/library/dis.html#opcode-EXTENDED_ARG
msg155321 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-10 16:30
I agree. 'deprecated' pretty much means "We do not think this is worth the effort of maintaining it. Don't use it unless it is already in old code."
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58377
2012-03-10 16:30:09terry.reedysetstatus: open -> closed
resolution: wont fix
messages: + msg155321
2012-03-10 12:59:14mark.dickinsonsetmessages: + msg155308
2012-03-10 12:41:19mark.dickinsonsetmessages: + msg155306
2012-03-10 12:26:59mark.dickinsonsetmessages: + msg155305
2012-03-10 11:52:12mark.dickinsonsetnosy: + mark.dickinson
messages: + msg155304
2012-03-10 04:53:51Jim.Jewettsetnosy: + Jim.Jewett
messages: + msg155290
2012-03-05 04:57:20meador.ingesetnosy: + meador.inge
2012-03-03 01:43:21pitrousetnosy: + benjamin.peterson
2012-03-02 21:40:30terry.reedysetnosy: + terry.reedy
messages: + msg154803
2012-03-01 18:03:05menegazzobrcreate