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

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