Printing strings with null-bytes in Pythonwin?

Gordon McMillan gmcm at hypernet.com
Wed Sep 15 08:58:57 EDT 1999


Dinu C. Gherman writes:

> 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:
> 
[snip]
> TypeError: ReplaceSel, argument 1: expected string without null
> bytes, string found """

It doesn't?

> 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:
[snip]
> >>> print join(str, '')
[snip]
> 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, '')
> 
> >>>

Hmm, see if you can see the difference between what you're 
doing and what I'm doing (which behaves identically in both 
places):

PythonWin 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit 
(Intel)] on win32
Copyright 1991-1995 Stichting 
Mathematisch Centrum, Amsterdam
Portions Copyright 1994-
1999 Mark Hammond (MHammond at skippinet.com.au)
>>> s = ['\000']*4
>>> print `s`
['\000', '\000', '\000', '\000']
>>> import string
>>> print string.join(s, '')

>>> print `string.join(s, '')`
'\000\000\000\000'
>>> str
<built-in function str>
>>> 

Oops, I gave it away... (And it's not a platform difference, it's 
the things Pythonwin does to emulate stdout from a GUI).

- Gordon




More information about the Python-list mailing list