Re: Forcing prompt to be on newline when embedding Python with
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
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 <eryk...@gmail.com> wrote: > On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan <hetch...@gmail.com> 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
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 <eryk...@gmail.com> wrote: > On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan <hetch...@gmail.com> 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
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
Forcing prompt to be on newline when embedding Python with stdin/out redirection
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
[issue8415] namedtuple vs tuple
H Krishnan hetch...@gmail.com added the comment: Sorry, I didn't know about python-ideas. Actually, there is a way to do this without breaking any existing code. namedtuple could support an optional additional argument, say, useIterableCtr, which is by default False, and the class template could be appropriately modified based on this argument. I couldn't find the PEP for this, but I notice that other people have also suggested iterable as argument in the ActiveState recipe page for this. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8415 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8415] namedtuple vs tuple
New submission from H Krishnan hetch...@gmail.com: Named tuples and tuples have different creation behavior. Changing a tuple to a namedtuple will involve changing the usage as well. For example: ntuple = collections.namedtuple(ntuple, a,b) ntuple(1,2) ntuple(a=1, b=2) tuple(1,2) Traceback (most recent call last): File stdin, line 1, in module TypeError: tuple() takes at most 1 argument (2 given) tuple([1,2]) (1, 2) ntuple([1,2]) Traceback (most recent call last): File stdin, line 1, in module TypeError: __new__() takes exactly 3 arguments (2 given) Because of this, to create a tuple object given a 'tuple class', we need to do something like: def makeTuple(tupleCls, *args): if hasattr(tupleCls, _fields): return tupleCls(*args) else: return tupleCls(args) My suggestion: A namedtuple should also accept a single iterable as argument, in which case, the iterable will be broken up and assigned to individual fields. This will break an existing behaviour of namedtuple: if only one field is present in the namedtuple and an iterable is passed to the namedtuple, that field is currently assigned the iterable. However, namedtuples are seldom used for single fields and so this may not be that important. -- components: None messages: 103289 nosy: hkrishnan severity: normal status: open title: namedtuple vs tuple type: feature request versions: Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8415 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Python 2.6 and sys.exit()
Hello, I have the following in exit.py: import sys sys.exit(0) I now try 'python -i exit.py': In 2.5, the script exits as I would expect. In 2.6, the following error is printed: Traceback (most recent call last): File exit.py, line 2, in module sys.exit(0) SystemExit: 0 I couldn't find anything related to this in What's new in 2.6. Is there any way I can get 2.6 to behave like 2.5? Thank you for your help, Regards, H. Krishnan -- http://mail.python.org/mailman/listinfo/python-list