Terry J. Reedy <tjre...@udel.edu> added the comment:

[For future reference, if I were not closing this, I would ask the following:  
Does 'on terminal mean that you started Python in interactive mode and then 
entered lines in response to the '>>> prompt'?  If so, did you start Python 
from an actual (non-Python) terminal or console? or from an icon or start menu? 
 What OS?  When you ran a script, did you run it from the system console or 
from the file itself?  However, I now consider this a moot point.]

The test for the code module is test.test_code_module, as test_code
tests code objects.  It created a code.InteractiveConsole and calls .interact.  
There are no direct unittests of any of the other methods, but most, if not 
all, including .push, are called directly or indirecty within .interact.  This 
is all within a script, which you say has problmes.  Input is fed to the 
interact loop with lines such as

        self.infunc.side_effect = ["try: ham\nexcept: eggs\n",
                                    EOFError('Finished')]

I cannot see that you have identified a real problem.  Your iitests.py fails 
because it is buggy.  It creates a new interactive console for each line.  When 
"    print('i', i)" is pushed to a new console, in response to >>>, there 
*should* be an indentation error.  Similarly, 'sleep(1)' in a new console 
*should* fail with a NameError.

In iiteswok.py, which you say works, only one console is created.  After adding 
'ii.push("")', to tell the console that the statement is complete, and removing 
unneeded code, the following works for me when run as a script

import code
ii = code.InteractiveConsole()
ii.push("for i in range(3):")
ii.push("    print ('i', i)")
ii.push("")

# prints

i 0
i 1
i 2

Adding

ii.push("from time import sleep")
ii.push("print('start')")
ii.push("sleep(1)")
ii.push("print('stop')")

prints 'start' and 'stop' with a one second delay.

----------
nosy: +terry.reedy
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
title: InteractiveConsole behaves differently when used on terminal and used 
within script -> InteractiveConsole behaves differently on terminal, within 
script

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33269>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to