diff -r ae51329f9893 Lib/test/test_imp.py --- a/Lib/test/test_imp.py Wed Aug 22 14:40:35 2012 +0200 +++ b/Lib/test/test_imp.py Wed Aug 22 17:38:09 2012 +0200 @@ -197,6 +197,18 @@ class ImportTests(unittest.TestCase): self.assertIn(path, err.exception.path) self.assertEqual(name, err.exception.name) + @unittest.skipUnless(support.TESTFN_UNENCODABLE is not None, + 'requires an unencodable filename') + def test_load_dynamic_nonascii(self): + # Check that the right encodings are used to decode arguments + # of the exception + path = support.TESTFN_UNENCODABLE + name = 'extension' + with self.assertRaises(ImportError) as err: + imp.load_dynamic(name, path_bytes) + self.assertEqual(name, err.exception.name) + #self.assertEqual(path, err.exception.path) + class ReloadTests(unittest.TestCase): diff -r ae51329f9893 Python/dynload_shlib.c --- a/Python/dynload_shlib.c Wed Aug 22 14:40:35 2012 +0200 +++ b/Python/dynload_shlib.c Wed Aug 22 17:38:09 2012 +0200 @@ -135,13 +135,13 @@ dl_funcptr _PyImport_GetDynLoadFunc(cons const char *error = dlerror(); if (error == NULL) error = "unknown dlopen() error"; - error_ob = PyUnicode_FromString(error); - path = PyUnicode_FromString(pathname); + error_ob = PyUnicode_DecodeLocale(error, "surrogateescape"); + path = PyUnicode_DecodeFSDefault(pathname); mod_name = PyUnicode_FromString(shortname); PyErr_SetImportError(error_ob, mod_name, path); - Py_DECREF(error_ob); - Py_DECREF(path); - Py_DECREF(mod_name); + Py_XDECREF(error_ob); + Py_XDECREF(path); + Py_XDECREF(mod_name); return NULL; } if (fp != NULL && nhandles < 128)