You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In IDLE, sys.stdout and sys.stderr can write any pickleable object #57741
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2012-07-10.18:38:56.361>created_at=<Date2011-12-05.14:07:42.625>labels= ['expert-IDLE', 'type-crash']
title='In IDLE,\tsys.stdout and sys.stderr can write any pickleable object'updated_at=<Date2012-07-10.18:38:56.348>user='https://bugs.python.org/RamchandraApte'
In IDLE, sys.stdout.write and sys.stderr can write any pickleable object such as 100 when they should only allow strings.
IDLE seems to be pickling the object.
>>> import sys
>>> sys.stdout.write(100)
100
>>> sys.stdout.write(sys)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
sys.stdout.write(sys)
_pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup builtins.module failed
The error above is more detailed in IDLE 2.7.
While in Python on the command-line:
>>> import sys
>>> sys.stdout.write(100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>>
The error above in Python 2.7:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object
We should like the IDLE shell to give the same results as the standard shell. See bpo-7163
On 3.2.2 on Win7, the problem is worse: sys.stdout.write(100) crashes IDLE -- as in it just fades away after a short (1/2 sec?) delay.
sys.stdout.write(sys)
gives me the error reported.
The difference comes from this in the Command Window:
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp437'>
versus this in IDLE:
>>> import sys
>>> sys.stdout
<idlelib.rpc.RPCProxy object at 0x00000000031D0208>
changed the title [-]In IDLE, sys.stdout.write and sys.stderr can write any pickleable object[/-][+]In IDLE, sys.stdout and sys.stderr can write any pickleable object[/+]on Dec 14, 2011
loewis commented on Dec 14, 2011
loewismannequin
Mannequin
More actions
We should like the IDLE shell to give the same results as the standard shell.
I disagree that this should be an absolute principle. Two standard
shells may not give the same result due to running in different
environments, so forcing IDLE to give the same result as a specific
standard shell is not reasonable when taken absolutely.
changed the title [-]In IDLE, sys.stdout and sys.stderr can write any pickleable object[/-][+]In IDLE, sys.stdout.write and sys.stderr can write any pickleable object[/+]on Dec 14, 2011
In English, "We should like" is far from an absolute statement. "IDLE must" would be such. In any case, I hope you agree that crashing is bad. It is also not good if IDLE, as a development environment, enables code that violates the doc specifications and that will not run in the standard interpreter in standard batch mode with no shell.
I also so not understand you reverting the title to the arguably incorrect non-parallel construction.
loewis commented on Dec 14, 2011
loewismannequin
Mannequin
More actions
I also so not understand you reverting the title to the arguably incorrect non-parallel construction.
That is an (unfortunate) side effect of replying by email.
RamchandraApte commented on Dec 15, 2011
RamchandraAptemannequin
MannequinAuthor
More actions
One reason to fix this bug:
People may develop code that calls sys.std{out,err}.write with the number 200.
like sys.stdout.write(200)
In IDLE the code works well but in Python from the command-line it fails to run.
Creating a bug in the code.
Thanks Martin. I was thinking of adding special-casing inside the rpcproxy class, but the wrapping is pretty clean and clear. Works great in 3.3 Win7-64.
The 2.7 message 'expected a character buffer object' is technically correct*, but opaque, especially to beginners. I actually prefer the 3.x style message, except that 'str' should be expanded to 'string'. (We especially should not say 'character buffer object' until we test for exactly that.)
*My impression is that 2.7 unicode is not a character buffer object, but gets auto converted to str/bytes, which is.
On the technically correct front, bytearray in 2.7 is a character buffer object that .write() can write, but it is not a subclass of basestring. So the widened test in the followup patch http://hg.python.org/cpython/rev/2993f566c82e
should be widened further.
if not isinstance(s, basestring):
raise TypeError('must be str, not ' + type(s).\_\_name__)
+ if not isinstance(s, (basestring, bytearray)):
+ raise TypeError('must be string, not ' + type(s).__name__)
Are there any other 'character buffer' classes that should be included? What *is* the test that 2.7 .write() uses to determine what is such? Can it even be written in Python (rather than C)?
loewis commented on Jul 9, 2012
loewismannequin
Mannequin
More actions
I'm losing interest in this issue. It already got more attention than it deserves. Terry, feel free to make whatever change you consider desirable.
I tested and pushed the change to allow bytestrings. If anyone is printing other character buffer objects in 2.7 with Idle, they can explicitly convert to str/bytes, patch run.py, and/or post here or on a new issue for further expansion. I am otherwise done with this too.
sys.stdin has a write method, but it should raise "io.UnsupportedOperation: not writable" when passed a string. It looks like IDLE has allowed writes to stdin even before Martin's patch. I'll open a separate issue for this case.
serwy commented on Jul 10, 2012
serwymannequin
Mannequin
More actions
Running "input" from IDLE now raises an error. Attached is a fix to _RPCFile to allow readline (and isatty) to function properly.
Activity
RamchandraApte commentedon Dec 5, 2011
loewis commentedon Dec 5, 2011
Why do you think this is a bug? Is this behavior causing problems?
RamchandraApte commentedon Dec 6, 2011
It is different behaviour than usual.
I agree it is of low importance.
terryjreedy commentedon Dec 12, 2011
We should like the IDLE shell to give the same results as the standard shell. See bpo-7163
On 3.2.2 on Win7, the problem is worse: sys.stdout.write(100) crashes IDLE -- as in it just fades away after a short (1/2 sec?) delay.
sys.stdout.write(sys)
gives me the error reported.
The difference comes from this in the Command Window: >>> sys.stdout <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp437'> versus this in IDLE: >>> import sys >>> sys.stdout <idlelib.rpc.RPCProxy object at 0x00000000031D0208>serwy commentedon Dec 13, 2011
The crash can be prevented by using bpo-13582.
The RPCProxy object would need to be subclassed so that it raises TypeError for the "write" method when it is not given a string.
[-]In IDLE, sys.stdout.write and sys.stderr can write any pickleable object[/-][+]In IDLE, sys.stdout and sys.stderr can write any pickleable object[/+]loewis commentedon Dec 14, 2011
I disagree that this should be an absolute principle. Two standard
shells may not give the same result due to running in different
environments, so forcing IDLE to give the same result as a specific
standard shell is not reasonable when taken absolutely.
[-]In IDLE, sys.stdout and sys.stderr can write any pickleable object[/-][+]In IDLE, sys.stdout.write and sys.stderr can write any pickleable object[/+]terryjreedy commentedon Dec 14, 2011
In English, "We should like" is far from an absolute statement. "IDLE must" would be such. In any case, I hope you agree that crashing is bad. It is also not good if IDLE, as a development environment, enables code that violates the doc specifications and that will not run in the standard interpreter in standard batch mode with no shell.
I also so not understand you reverting the title to the arguably incorrect non-parallel construction.
loewis commentedon Dec 14, 2011
That is an (unfortunate) side effect of replying by email.
RamchandraApte commentedon Dec 15, 2011
One reason to fix this bug:
People may develop code that calls sys.std{out,err}.write with the number 200.
like sys.stdout.write(200)
In IDLE the code works well but in Python from the command-line it fails to run.
Creating a bug in the code.
15 remaining items
serwy commentedon Jul 9, 2012
I just pulled Martin's patches and they fix the issue. Indirection to the rescue!
There is one very slight problem though with the error message raised on 2.7 since it is different than 3.3. Attached is a patch to fix it.
loewis commentedon Jul 9, 2012
Ah, this indicates that we should support any buffer object...
terryjreedy commentedon Jul 9, 2012
Thanks Martin. I was thinking of adding special-casing inside the rpcproxy class, but the wrapping is pretty clean and clear. Works great in 3.3 Win7-64.
The 2.7 message 'expected a character buffer object' is technically correct*, but opaque, especially to beginners. I actually prefer the 3.x style message, except that 'str' should be expanded to 'string'. (We especially should not say 'character buffer object' until we test for exactly that.)
*My impression is that 2.7 unicode is not a character buffer object, but gets auto converted to str/bytes, which is.
On the technically correct front, bytearray in 2.7 is a character buffer object that .write() can write, but it is not a subclass of basestring. So the widened test in the followup patch
http://hg.python.org/cpython/rev/2993f566c82e
should be widened further.
+ if not isinstance(s, (basestring, bytearray)):
+ raise TypeError('must be string, not ' + type(s).__name__)
Are there any other 'character buffer' classes that should be included? What *is* the test that 2.7 .write() uses to determine what is such? Can it even be written in Python (rather than C)?
loewis commentedon Jul 9, 2012
I'm losing interest in this issue. It already got more attention than it deserves. Terry, feel free to make whatever change you consider desirable.
python-dev commentedon Jul 9, 2012
New changeset 4f891f44ec15 by Terry Jan Reedy in branch '2.7':
bpo-13532: Allow bytearrays to be written also.
http://hg.python.org/cpython/rev/4f891f44ec15
terryjreedy commentedon Jul 9, 2012
I tested and pushed the change to allow bytestrings. If anyone is printing other character buffer objects in 2.7 with Idle, they can explicitly convert to str/bytes, patch run.py, and/or post here or on a new issue for further expansion. I am otherwise done with this too.
serhiy-storchaka commentedon Jul 10, 2012
write to sys.stdin?
serwy commentedon Jul 10, 2012
sys.stdin has a write method, but it should raise "io.UnsupportedOperation: not writable" when passed a string. It looks like IDLE has allowed writes to stdin even before Martin's patch. I'll open a separate issue for this case.
serwy commentedon Jul 10, 2012
Running "input" from IDLE now raises an error. Attached is a fix to _RPCFile to allow readline (and isatty) to function properly.
serhiy-storchaka commentedon Jul 10, 2012
I think the issue is far from a solution.
loewis commentedon Jul 10, 2012
Folks: one issue at a time - pretty pretty please.
This issue was "In IDLE, sys.stdout and sys.stderr can write any pickleable object". Will anybody dispute that this issue is fixed?
Roger: please create a new issue for input being broken now. Your patch "shouldn't" be necessary, as __getattr__ should deal with this.
Serhiy: please create a new issue for readable and writable returning an incorrect result.