Skip to content

Commit 7120bb5

Browse files
authored
Accept pathlib.Path as a valid path (#1027)
And also whatever supports the protocol. Way more pythonic now!
1 parent db092ce commit 7120bb5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/OpenSSL/_util.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23
import warnings
34

@@ -89,19 +90,19 @@ def native(s):
8990

9091
def path_string(s):
9192
"""
92-
Convert a Python string to a :py:class:`bytes` string identifying the same
93+
Convert a Python path to a :py:class:`bytes` string identifying the same
9394
path and which can be passed into an OpenSSL API accepting a filename.
9495
95-
:param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
96+
:param s: A path (valid for os.fspath).
9697
9798
:return: An instance of :py:class:`bytes`.
9899
"""
99-
if isinstance(s, bytes):
100-
return s
101-
elif isinstance(s, str):
102-
return s.encode(sys.getfilesystemencoding())
100+
strpath = os.fspath(s) # returns str or bytes
101+
102+
if isinstance(strpath, str):
103+
return strpath.encode(sys.getfilesystemencoding())
103104
else:
104-
raise TypeError("Path must be represented as bytes or unicode string")
105+
return strpath
105106

106107

107108
def byte_string(s):

0 commit comments

Comments
 (0)