This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients ezio.melotti, ishimoto, loewis, tim.golden, vstinner
Date 2012-07-25.23:23:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1343258617.83.0.691035505447.issue15441@psf.upfronthosting.co.za>
In-reply-to
Content
> 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...)
History
Date User Action Args
2012-07-25 23:23:37vstinnersetrecipients: + vstinner, loewis, ishimoto, tim.golden, ezio.melotti
2012-07-25 23:23:37vstinnersetmessageid: <1343258617.83.0.691035505447.issue15441@psf.upfronthosting.co.za>
2012-07-25 23:23:37vstinnerlinkissue15441 messages
2012-07-25 23:23:37vstinnercreate