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

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