Skip to content

Commit

Permalink
Accept pathlib.Path as a valid path (#1027)
Browse files Browse the repository at this point in the history
And also whatever supports the protocol.

Way more pythonic now!
  • Loading branch information
ltworf authored Dec 19, 2021
1 parent db092ce commit 7120bb5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/OpenSSL/_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import warnings

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7120bb5

Please sign in to comment.