Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept pathlib.Path as a valid path #1027

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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