From 7120bb59d165103d8f2cffd0726709591b572174 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Sun, 19 Dec 2021 16:42:06 +0100 Subject: Accept pathlib.Path as a valid path (#1027) And also whatever supports the protocol. Way more pythonic now! --- src/OpenSSL/_util.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/OpenSSL/_util.py') 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): -- cgit v1.2.1