summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSalvo 'LtWorf' Tomaselli <ltworf@users.noreply.github.com>2021-12-19 16:42:06 +0100
committerGitHub <noreply@github.com>2021-12-19 10:42:06 -0500
commit7120bb59d165103d8f2cffd0726709591b572174 (patch)
tree360f4aa100be0b411af69cb61c3d06ddb888ac52
parentdb092ce0c542aaea1407961be063b509a0f37551 (diff)
downloadpyopenssl-7120bb59d165103d8f2cffd0726709591b572174.tar.gz
Accept pathlib.Path as a valid path (#1027)
And also whatever supports the protocol. Way more pythonic now!
-rw-r--r--src/OpenSSL/_util.py15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/OpenSSL/_util.py b/src/OpenSSL/_util.py
index 662239b..6bd43a5 100644
--- a/src/OpenSSL/_util.py
+++ b/src/OpenSSL/_util.py
@@ -1,3 +1,4 @@
+import os
import sys
import warnings
@@ -89,19 +90,19 @@ def native(s):
def path_string(s):
"""
- Convert a Python string to a :py:class:`bytes` string identifying the same
+ Convert a Python path to a :py:class:`bytes` string identifying the same
path and which can be passed into an OpenSSL API accepting a filename.
- :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
+ :param s: A path (valid for os.fspath).
:return: An instance of :py:class:`bytes`.
"""
- if isinstance(s, bytes):
- return s
- elif isinstance(s, str):
- return s.encode(sys.getfilesystemencoding())
+ strpath = os.fspath(s) # returns str or bytes
+
+ if isinstance(strpath, str):
+ return strpath.encode(sys.getfilesystemencoding())
else:
- raise TypeError("Path must be represented as bytes or unicode string")
+ return strpath
def byte_string(s):