Printing strings with null-bytes in Pythonwin?
Dinu C. Gherman
gherman at darwin.in-berlin.de
Wed Sep 15 07:23:14 EDT 1999
Is is possible that Pythonwin has a problem with strings
containing null-bytes? Or is the behaviour below to be
expected as one cannot display null bytes in any meaning-
ful way, so to say?
If I run the following code from a script:
str = ['\000'] * 4
# str = ['a'] * 4
print `str`
from string import join
print join(str, '')
I get this traceback, pointing to a file winout.py which
does not seem to mention any problems with null-bytes:
"""
Traceback (innermost last):
File "C:\Program
Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 237, in
RunScript
exec codeObject in __main__.__dict__
File "C:\Usr\gherman\Misc\nullbytes.py", line 5, in ?
print join(str, '')
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 207, in write
return self.template.write(msg)
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 396, in write
self.HandleOutput(message)
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 381, in HandleOutput
return self.QueueFlush()
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 364, in QueueFlush
self.currentView.ReplaceSel(self.outputQueue.get_nowait())
TypeError: ReplaceSel, argument 1: expected string without null bytes,
string found
"""
If you write the same in an interactive Pythonwin window
you'll get the following, indicating that the problem only
occurs when you actually try to print the string containing
null-bytes:
>>> str = ['\000'] * 4
>>> print `str`
['\000', '\000', '\000', '\000']
>>> from string import join
>>> join(str, '')
'\000\000\000\000'
>>> print join(str, '')
Traceback (innermost last):
File "C:\Program Files\Python\Pythonwin\pywin\framework\interact.py",
line 372, in keyhandler_enter
self.DrawPrompt()
File "C:\Program Files\Python\Pythonwin\pywin\framework\interact.py",
line 120, in DrawPrompt
self.flush()
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 212, in flush
return self.template.flush()
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 399, in flush
self.QueueFlush()
File "C:\Program Files\Python\Pythonwin\pywin\framework\winout.py",
line 364, in QueueFlush
self.currentView.ReplaceSel(self.outputQueue.get_nowait())
TypeError: ReplaceSel, argument 1: expected string without null bytes,
string found
>>>
If the reasoning at the beginning is the official one (null-
bytes cannot be displayed anyway), it's interesting to note
the difference to a Python interpreter running on Linux,
where you get as boring a result as can be expected, but at
least it does not fail:
>>> str = ['\000'] * 4
>>> print `str`
['\000', '\000', '\000', '\000']
>>> from string import join
>>> join(str, '')
'\000\000\000\000'
>>> print join(str, '')
>>>
Just curious,
Dinu
--
Dinu C. Gherman
................................................................
"An average of more than 15 % of adults in 12 industrialized
countries are functionally illiterate; in Ireland, the United
Kingdom and the United States, the rates are over 20 %."
(The State of the World's Children 1999,
UNICEF, http://www.unicef.org/sowc99)
More information about the Python-list
mailing list