Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e8f395

Browse files
authored
bpo-25404: SSLContext.load_dh_params() non-ASCII path (pythonGH-3459)
SSLContext.load_dh_params() now supports non-ASCII path. Signed-off-by: Christian Heimes <[email protected]>
1 parent 8d4d173 commit 6e8f395

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Lib/test/test_ssl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import os
1515
import errno
1616
import pprint
17-
import tempfile
17+
import shutil
1818
import urllib2
1919
import traceback
2020
import weakref
@@ -1000,6 +1000,10 @@ def test_load_dh_params(self):
10001000
self.assertEqual(cm.exception.errno, errno.ENOENT)
10011001
with self.assertRaises(ssl.SSLError) as cm:
10021002
ctx.load_dh_params(CERTFILE)
1003+
with support.temp_dir() as d:
1004+
fname = os.path.join(d, u'dhpäräm.pem')
1005+
shutil.copy(DHFILE, fname)
1006+
ctx.load_dh_params(fname)
10031007

10041008
@skip_if_broken_ubuntu_ssl
10051009
def test_session_stats(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SSLContext.load_dh_params() now supports non-ASCII path.

Modules/_ssl.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,13 +2983,25 @@ load_dh_params(PySSLContext *self, PyObject *filepath)
29832983
{
29842984
BIO *bio;
29852985
DH *dh;
2986-
char *path = PyBytes_AsString(filepath);
2987-
if (!path) {
2988-
return NULL;
2986+
PyObject *filepath_bytes = NULL;
2987+
2988+
if (PyString_Check(filepath)) {
2989+
Py_INCREF(filepath);
2990+
filepath_bytes = filepath;
2991+
} else {
2992+
PyObject *u = PyUnicode_FromObject(filepath);
2993+
if (!u)
2994+
return NULL;
2995+
filepath_bytes = PyUnicode_AsEncodedString(
2996+
u, Py_FileSystemDefaultEncoding, NULL);
2997+
Py_DECREF(u);
2998+
if (!filepath_bytes)
2999+
return NULL;
29893000
}
29903001

2991-
bio = BIO_new_file(path, "r");
3002+
bio = BIO_new_file(PyBytes_AS_STRING(filepath_bytes), "r");
29923003
if (bio == NULL) {
3004+
Py_DECREF(filepath_bytes);
29933005
ERR_clear_error();
29943006
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, filepath);
29953007
return NULL;
@@ -2998,6 +3010,7 @@ load_dh_params(PySSLContext *self, PyObject *filepath)
29983010
PySSL_BEGIN_ALLOW_THREADS
29993011
dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
30003012
BIO_free(bio);
3013+
Py_DECREF(filepath_bytes);
30013014
PySSL_END_ALLOW_THREADS
30023015
if (dh == NULL) {
30033016
if (errno != 0) {

0 commit comments

Comments
 (0)