Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I handle Python unicode strings with null-bytes the 'right' way?
    primarykey
    data
    text
    <p><strong>Question</strong></p> <p>It seems that PyWin32 is comfortable with giving null-terminated unicode strings as return values. I would like to deal with these strings the 'right' way.</p> <p>Let's say I'm getting a string like: <code>u'C:\\Users\\Guest\\MyFile.asy\x00\x00sy'</code>. This appears to be a C-style null-terminated string hanging out in a Python unicode object. I want to trim this bad boy down to a regular ol' string of characters that I could, for example, display in a window title bar.</p> <p>Is trimming the string off at the first null byte the right way to deal with it?</p> <p>I didn't expect to get a return value like this, so I wonder if I'm missing something important about how Python, Win32, and unicode play together... or if this is just a PyWin32 bug.</p> <p><strong>Background</strong></p> <p>I'm using the Win32 file chooser function <a href="http://docs.activestate.com/activepython/2.7/pywin32/win32gui__GetSaveFileNameW_meth.html" rel="nofollow"><code>GetOpenFileNameW</code></a> from the PyWin32 package. According to the documentation, this function returns a tuple containing the full filename path as a Python unicode object.</p> <p>When I open the dialog with an existing path and filename set, I get a strange return value. </p> <p>For example I had the default set to: <code>C:\\Users\\Guest\\MyFileIsReallyReallyReallyAwesome.asy</code></p> <p>In the dialog I changed the name to <code>MyFile.asy</code> and clicked save.</p> <p>The full path part of the return value was: u'C:\Users\Guest\MyFile.asy\x00wesome.asy'`</p> <p>I expected it to be: <code>u'C:\\Users\\Guest\\MyFile.asy'</code></p> <p>The function is returning a recycled buffer without trimming off the terminating bytes. Needless to say, the rest of my code wasn't set up for handling a C-style null-terminated string.</p> <p><strong>Demo Code</strong></p> <p>The following code demonstrates null-terminated string in return value from GetSaveFileNameW.</p> <p>Directions: In the dialog change the filename to 'MyFile.asy' then click Save. Observe what is printed to the console. The output I get is <code>u'C:\\Users\\Guest\\MyFile.asy\x00wesome.asy'</code>.</p> <pre><code>import win32gui, win32con if __name__ == "__main__": initial_dir = 'C:\\Users\\Guest' initial_file = 'MyFileIsReallyReallyReallyAwesome.asy' filter_string = 'All Files\0*.*\0' (filename, customfilter, flags) = \ win32gui.GetSaveFileNameW(InitialDir=initial_dir, Flags=win32con.OFN_EXPLORER, File=initial_file, DefExt='txt', Title="Save As", Filter=filter_string, FilterIndex=0) print repr(filename) </code></pre> <p>Note: If you don't shorten the filename enough (for example, if you try MyFileIsReally.asy) the string will be complete without a null byte.</p> <p><strong>Environment</strong></p> <p>Windows 7 Professional 64-bit (no service pack), Python 2.7.1, PyWin32 Build 216</p> <p><strong>UPDATE: PyWin32 Tracker Artifact</strong></p> <p>Based on the comments and answers I have received so far, this is likely a pywin32 bug so I filed a <a href="https://sourceforge.net/tracker/?func=detail&amp;aid=3277647&amp;group_id=78018&amp;atid=551954" rel="nofollow">tracker artifact</a>.</p> <p><strong>UPDATE 2: Fixed!</strong></p> <p>Mark Hammond reported in the tracker artifact that this is indeed a bug. A fix was checked in to rev f3fdaae5e93d, so hopefully that will make the next release.</p> <p>I think Aleksi Torhamo's answer below is the best solution for versions of PyWin32 before the fix.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload