Blame SOURCES/00216-pep466-fix-load-verify-locs-unicode.patch

057d67
057d67
# HG changeset patch
057d67
# User Benjamin Peterson <benjamin@python.org>
057d67
# Date 1409232801 14400
057d67
# Node ID 97081a80f487841d81aeed55d398a1dba1faca00
057d67
# Parent  3ae399c6ecf685086ebf07e17717955f21e14cb8
057d67
fix load_verify_locations on unicode paths (closes #22244)
057d67
057d67
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
057d67
--- a/Lib/test/test_ssl.py
057d67
+++ b/Lib/test/test_ssl.py
057d67
@@ -850,11 +850,14 @@ class ContextTests(unittest.TestCase):
057d67
         ctx.load_verify_locations(cafile=CERTFILE, capath=None)
057d67
         ctx.load_verify_locations(BYTES_CERTFILE)
057d67
         ctx.load_verify_locations(cafile=BYTES_CERTFILE, capath=None)
057d67
+        ctx.load_verify_locations(cafile=BYTES_CERTFILE.decode('utf-8'))
057d67
         self.assertRaises(TypeError, ctx.load_verify_locations)
057d67
         self.assertRaises(TypeError, ctx.load_verify_locations, None, None, None)
057d67
         with self.assertRaises(IOError) as cm:
057d67
             ctx.load_verify_locations(WRONGCERT)
057d67
         self.assertEqual(cm.exception.errno, errno.ENOENT)
057d67
+        with self.assertRaises(IOError):
057d67
+            ctx.load_verify_locations(u'')
057d67
         with self.assertRaisesRegexp(ssl.SSLError, "PEM lib"):
057d67
             ctx.load_verify_locations(BADCERT)
057d67
         ctx.load_verify_locations(CERTFILE, CAPATH)
057d67
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
057d67
--- a/Modules/_ssl.c
057d67
+++ b/Modules/_ssl.c
057d67
@@ -2628,17 +2628,33 @@ load_verify_locations(PySSLContext *self
057d67
     }
057d67
 
057d67
     if (cafile) {
057d67
-        cafile_bytes = PyString_AsEncodedObject(
057d67
-            cafile, Py_FileSystemDefaultEncoding, "strict");
057d67
-        if (!cafile_bytes) {
057d67
-            goto error;
057d67
+        if (PyString_Check(cafile)) {
057d67
+            Py_INCREF(cafile);
057d67
+            cafile_bytes = cafile;
057d67
+        } else {
057d67
+            PyObject *u = PyUnicode_FromObject(cafile);
057d67
+            if (!u)
057d67
+                goto error;
057d67
+            cafile_bytes = PyUnicode_AsEncodedString(
057d67
+                u, Py_FileSystemDefaultEncoding, NULL);
057d67
+            Py_DECREF(u);
057d67
+            if (!cafile_bytes)
057d67
+                goto error;
057d67
         }
057d67
     }
057d67
     if (capath) {
057d67
-        capath_bytes = PyString_AsEncodedObject(
057d67
-            capath, Py_FileSystemDefaultEncoding, "strict");
057d67
-        if (!capath_bytes) {
057d67
-            goto error;
057d67
+        if (PyString_Check(capath)) {
057d67
+            Py_INCREF(capath);
057d67
+            capath_bytes = capath;
057d67
+        } else {
057d67
+            PyObject *u = PyUnicode_FromObject(capath);
057d67
+            if (!u)
057d67
+                goto error;
057d67
+            capath_bytes = PyUnicode_AsEncodedString(
057d67
+                u, Py_FileSystemDefaultEncoding, NULL);
057d67
+            Py_DECREF(u);
057d67
+            if (!capath_bytes)
057d67
+                goto error;
057d67
         }
057d67
     }
057d67
 
057d67