Forcing prompt to be on newline when embedding Python with

H Krishnan hetchkay at gmail.com
Thu Jan 5 13:36:48 EST 2017


Thanks for your help.

>
> >
> > I am working on embedding Python in my application.
>
> You forgot to tell us the version of Python that you're embedding.
>
> I am using Python2.7.


> > I have redirected sys.stdin and sys.stdout to call methods from a Qt
> TextEdit
> > widget. Everything works fine except that the Python prompt does not
> always
> > come in a new line:
> >
> >>>> dir()
> > ['__builtins__', '__doc__', '__name__', '__package__']>>>
> >
> > Why doesn't the prompt appear in a new line as with the default stdout?
>
> Are you using code.InteractiveConsole / code.interact?
>
> I am using code.InteractiveConsole().interact().


> If not, in what mode do you compile, Py_file_input ("exec") or
> Py_single_input ("single")? The latter executes PRINT_EXPR:
>
>     >>> dis.dis(compile('1', '', 'single'))
>       1           0 LOAD_CONST               0 (1)
>                   3 PRINT_EXPR
>                   4 LOAD_CONST               1 (None)
>                   7 RETURN_VALUE
>
> PRINT_EXPR in turn calls sys.displayhook on the value it pops from the
> stack. The default hook writes the repr of the value and a newline to
> sys.stdout, and it also references the value as "_" in the builtins
> module (2.x __builtin__).
>

I tried replacing sys.displayhook with a function that does not print newline 
but the newline still got inserted. So, I am not sure where the newline is 
coming from. In any case, I could override sys.displayhook to add a newline at 
the end and that seems to resolve my problem.


> --
> https://mail.python.org/mailman/listinfo/python-list
>

Thanks,
Krishnan




More information about the Python-list mailing list