STINNER Victor <victor.stin...@gmail.com> added the comment:

> haypo: how is this meant to fix the bug?
> Won't it now cause a WindowsError, when a successful
> operation is expected?

Oh, I was referring to the new test proposed in the attached patch 
(issue15441.patch):

+    def test_chdir_invalid_filename(self):
+        self.assertRaises(WindowsError, os.chdir, b'\xe7w\xf0')

os.chdir() in a non existent directory with a bytes name should raise an 
OSError, not a UnicodeDecodeError.

--

About the original issue: it looks like mkdir(bytes) decodes internally the 
directory name and ignore undecodable bytes. On Windows 7, mkdir(b"\xe7w\xf0") 
creates a directory called "\u8f42" (b"\xe7w", b"\xf0" suffix has been 
dropped). It is not possible to change the directory to "b"\xe7w\xf0", but it 
works with "b"\xe7w" or "\u8f42".

There are 2 issues:

 * On Windows, os.chdir(bytes) should not raise a UnicodeDecodeError on the 
directory does not exist
 * test_nonascii_abspath() can be skipped on Windows if 
os.fsdecode(b"\xe7w\xf0") fails, or b"\xe7w" name should be used instead

My patch is not the best solution because it looses information (if the 
filename contains undecodable bytes). I realized that OSError.filename is not 
necessary a str, bytes is also accepted. win32_error_object() can be used. The 
following patch pass the original bytes object to OSError constructor instead:


diff -r 43ae2a243eca Modules/posixmodule.c
--- a/Modules/posixmodule.c     Thu Jul 26 00:47:15 2012 +0200
+++ b/Modules/posixmodule.c     Thu Jul 26 01:19:14 2012 +0200
@@ -1138,11 +1138,10 @@ static PyObject *
 path_error(char *function_name, path_t *path)
 {
 #ifdef MS_WINDOWS
-    if (path->narrow)
-        return win32_error(function_name, path->narrow);
-    if (path->wide)
-        return win32_error_unicode(function_name, path->wide);
-    return win32_error(function_name, NULL);
+    return PyErr_SetExcFromWindowsErrWithFilenameObject(
+                PyExc_OSError,
+                0,
+                path->object);
 #else
     return path_posix_error(function_name, path);
 #endif

(sorry, I failed to attach a patch, I have an issue with my file chooser...)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15441>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to