Re: Forcing prompt to be on newline when embedding Python with

2017-01-06 Thread H Krishnan
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

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


Re: Forcing prompt to be on newline when embedding Python with

2017-01-06 Thread eryk sun
On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan  wrote:
> 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.

In Python 2 the newline is written depending on the value of 
sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine, which 
resets the file's softspace to 0 via PyFile_SoftSpace and writes a newline if 
the previous value was non-zero. Next displayhook writes the repr, sets the 
softspace to 1 and calls Py_FlushLine again.

The result you're seeing could occur if your filelike object doesn't have a 
dict or a property to allow setting the "softspace" attribute, as the following 
toy example demonstrates:

import sys

class File(object):
def __init__(self, file):
self._file = file
self._sp_enabled = True
self.softspace = 0

def write(self, string):
return self._file.write(string)

def __getattribute__(self, name):
value = object.__getattribute__(self, name)
if name == 'softspace':
if not self._sp_enabled:
raise AttributeError
self._file.write('[get softspace <- %d]\n' % value)
return value

def __setattr__(self, name, value):
if name == 'softspace':
if not self._sp_enabled:
raise AttributeError
self._file.write('[set softspace -> %d]\n' % value)
object.__setattr__(self, name, value)

softspace enabled:

>>> sys.stdout = File(sys.stdout)
[set softspace -> 0]
[get softspace <- 0]
[set softspace -> 0]
>>> 42
[get softspace <- 0]
[set softspace -> 0]
42[get softspace <- 0]
[set softspace -> 1]
[get softspace <- 1]
[set softspace -> 0]

[get softspace <- 0]
[set softspace -> 0]

softspace disabled:

>>> sys.stdout._sp_enabled = False
>>> 42
42>>> 42
42>>>

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


Re: Forcing prompt to be on newline when embedding Python with

2017-01-06 Thread H Krishnan
Hello Mr.Eryk,

Thanks for the detailed explanation. After I added attribute support to my 
extension class for stdio, the problem was resolved.

Regards,
Krishnan


On Fri, Jan 6, 2017 at 9:24 AM, eryk sun  wrote:

> On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan  wrote:
> > 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.
>
> In Python 2 the newline is written depending on the value of
> sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine,
> which resets the file's softspace to 0 via PyFile_SoftSpace and writes
> a newline if the previous value was non-zero. Next displayhook writes
> the repr, sets the softspace to 1 and calls Py_FlushLine again.
>
> The result you're seeing could occur if your filelike object doesn't
> have a dict or a property to allow setting the "softspace" attribute,
> as the following toy example demonstrates:
>
> import sys
>
> class File(object):
> def __init__(self, file):
> self._file = file
> self._sp_enabled = True
> self.softspace = 0
>
> def write(self, string):
> return self._file.write(string)
>
> def __getattribute__(self, name):
> value = object.__getattribute__(self, name)
> if name == 'softspace':
> if not self._sp_enabled:
> raise AttributeError
> self._file.write('[get softspace <- %d]\n' % value)
> return value
>
> def __setattr__(self, name, value):
> if name == 'softspace':
> if not self._sp_enabled:
> raise AttributeError
> self._file.write('[set softspace -> %d]\n' % value)
> object.__setattr__(self, name, value)
>
> softspace enabled:
>
> >>> sys.stdout = File(sys.stdout)
> [set softspace -> 0]
> [get softspace <- 0]
> [set softspace -> 0]
> >>> 42
> [get softspace <- 0]
> [set softspace -> 0]
> 42[get softspace <- 0]
> [set softspace -> 1]
> [get softspace <- 1]
> [set softspace -> 0]
>
> [get softspace <- 0]
> [set softspace -> 0]
>
> softspace disabled:
>
> >>> sys.stdout._sp_enabled = False
> >>> 42
> 42>>> 42
> 42>>>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: Forcing prompt to be on newline when embedding Python with

2017-01-06 Thread eryk sun
On Thu, Jan 5, 2017 at 7:09 AM, H Krishnan  wrote:
>
> I am working on embedding Python in my application.

You forgot to tell us the version of Python that you're embedding.

> 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?

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__).

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


Re: Forcing prompt to be on newline when embedding Python with stdin/out redirection

2017-01-06 Thread H Krishnan
Hello Mr.Eryk,

Thanks for the detailed explanation. After I added attribute support to my
extension class for stdio, the problem was resolved.

Regards,
Krishnan


On Fri, Jan 6, 2017 at 9:24 AM, eryk sun  wrote:

> On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan  wrote:
> > 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.
>
> In Python 2 the newline is written depending on the value of
> sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine,
> which resets the file's softspace to 0 via PyFile_SoftSpace and writes
> a newline if the previous value was non-zero. Next displayhook writes
> the repr, sets the softspace to 1 and calls Py_FlushLine again.
>
> The result you're seeing could occur if your filelike object doesn't
> have a dict or a property to allow setting the "softspace" attribute,
> as the following toy example demonstrates:
>
> import sys
>
> class File(object):
> def __init__(self, file):
> self._file = file
> self._sp_enabled = True
> self.softspace = 0
>
> def write(self, string):
> return self._file.write(string)
>
> def __getattribute__(self, name):
> value = object.__getattribute__(self, name)
> if name == 'softspace':
> if not self._sp_enabled:
> raise AttributeError
> self._file.write('[get softspace <- %d]\n' % value)
> return value
>
> def __setattr__(self, name, value):
> if name == 'softspace':
> if not self._sp_enabled:
> raise AttributeError
> self._file.write('[set softspace -> %d]\n' % value)
> object.__setattr__(self, name, value)
>
> softspace enabled:
>
> >>> sys.stdout = File(sys.stdout)
> [set softspace -> 0]
> [get softspace <- 0]
> [set softspace -> 0]
> >>> 42
> [get softspace <- 0]
> [set softspace -> 0]
> 42[get softspace <- 0]
> [set softspace -> 1]
> [get softspace <- 1]
> [set softspace -> 0]
>
> [get softspace <- 0]
> [set softspace -> 0]
>
> softspace disabled:
>
> >>> sys.stdout._sp_enabled = False
> >>> 42
> 42>>> 42
> 42>>>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forcing prompt to be on newline when embedding Python with stdin/out redirection

2017-01-05 Thread eryk sun
On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan  wrote:
> 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.

In Python 2 the newline is written depending on the value of
sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine,
which resets the file's softspace to 0 via PyFile_SoftSpace and writes
a newline if the previous value was non-zero. Next displayhook writes
the repr, sets the softspace to 1 and calls Py_FlushLine again.

The result you're seeing could occur if your filelike object doesn't
have a dict or a property to allow setting the "softspace" attribute,
as the following toy example demonstrates:

import sys

class File(object):
def __init__(self, file):
self._file = file
self._sp_enabled = True
self.softspace = 0

def write(self, string):
return self._file.write(string)

def __getattribute__(self, name):
value = object.__getattribute__(self, name)
if name == 'softspace':
if not self._sp_enabled:
raise AttributeError
self._file.write('[get softspace <- %d]\n' % value)
return value

def __setattr__(self, name, value):
if name == 'softspace':
if not self._sp_enabled:
raise AttributeError
self._file.write('[set softspace -> %d]\n' % value)
object.__setattr__(self, name, value)

softspace enabled:

>>> sys.stdout = File(sys.stdout)
[set softspace -> 0]
[get softspace <- 0]
[set softspace -> 0]
>>> 42
[get softspace <- 0]
[set softspace -> 0]
42[get softspace <- 0]
[set softspace -> 1]
[get softspace <- 1]
[set softspace -> 0]

[get softspace <- 0]
[set softspace -> 0]

softspace disabled:

>>> sys.stdout._sp_enabled = False
>>> 42
42>>> 42
42>>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forcing prompt to be on newline when embedding Python with stdin/out redirection

2017-01-05 Thread H Krishnan
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forcing prompt to be on newline when embedding Python with stdin/out redirection

2017-01-05 Thread eryk sun
On Thu, Jan 5, 2017 at 7:09 AM, H Krishnan  wrote:
>
> I am working on embedding Python in my application.

You forgot to tell us the version of Python that you're embedding.

> 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?

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__).
-- 
https://mail.python.org/mailman/listinfo/python-list


Forcing prompt to be on newline when embedding Python with stdin/out redirection

2017-01-05 Thread H Krishnan
Hi,

I am working on embedding Python in my application. 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?

Thanks,
Krishnan
-- 
https://mail.python.org/mailman/listinfo/python-list