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: code.runsource() parsing bug
Type: Stage:
Components: None Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, smcallis
Priority: normal Keywords:

Created on 2009-07-14 05:19 by smcallis, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg90511 - (view) Author: Sean (smcallis) Date: 2009-07-14 05:19
I'm writing a little pre-processor that just parses python snippets out
of a file and passes them to a code.InteractiveInterpreter, and I'm
noticing some somewhat nasty stuff in how runsource handles some code.

sys.version:
'2.6.2 (r262:71600, Jul 12 2009, 11:52:33) \n[GCC 4.0.1 (Apple Inc.
build 5465)]'     

Doesn't work:
 interpreter.runsource('print("Foo")\nprint "Bar"')  

Works: 
 interpreter.runsource('print("Foo");print "Bar"')

Doesn't work:
 interpreter.runsource('print("Foo");\nprint "Bar"')  
  

Exact output:
>>> interpreter.runsource('print("Foo")\nprint "Bar"')                 
                                            
Foo                                                                    
                                            
False                                                                  
                                            
>>> interpreter.runsource('print("Foo");print "Bar"')                  
                                            
Foo                                                                    
                                            
Bar                                                                    
                                            
False                                                                  
                                            
>>> interpreter.runsource('print("Foo");\nprint "Bar"')                
                                            
Foo                                                                    
                                            
False                                                                  
                                            
>>>     


Is this a known issue? I didn't see it while searching the bug database...
msg90520 - (view) Author: Sean (smcallis) Date: 2009-07-14 21:16
Seems to me it's probably an issue with the newlines, but from
help(code.compile_command):

    Compile a command and determine whether it is incomplete.
    
    Arguments:
    
    source -- the source string; <b>may contain \n characters</b>
    filename -- optional filename from which source was read; default
                "<input>"
    symbol -- optional grammar start symbol; "single" (default) or
msg111968 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-07-29 17:03
code.runsource() uses the "single" start symbol by default, which will parse only a single statement.  Only your second snippet is a single statement, while the others are two statements.
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50729
2010-07-29 17:03:46georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg111968

resolution: not a bug
2009-07-14 21:16:31smcallissetmessages: + msg90520
2009-07-14 05:19:56smcalliscreate