Skip to content

Commit d60711e

Browse files
committed
utils: Better pathlib.Path support
- Expose existing is_pathlike utility. - Support pathlib.Path with io utils.
1 parent c81a5b4 commit d60711e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/robot/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
parse_time)
6868
from .robottypes import (FALSE_STRINGS, Mapping, MutableMapping, TRUE_STRINGS,
6969
is_bytes, is_dict_like, is_falsy, is_integer,
70-
is_list_like, is_number, is_string, is_truthy,
71-
is_unicode, type_name, unicode)
70+
is_list_like, is_number, is_pathlike, is_string,
71+
is_truthy, is_unicode, type_name, unicode)
7272
from .setter import setter, SetterAwareType
7373
from .sortable import Sortable
7474
from .text import (cut_long_message, format_assign_message,

src/robot/utils/robotio.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
from .error import get_error_message
2323
from .platform import PY3
24+
from .robottypes import is_pathlike
2425

2526

2627
def file_writer(path=None, encoding='UTF-8', newline=None, usage=None):
2728
if path:
29+
if is_pathlike(path):
30+
path = str(path)
2831
create_destination_directory(path, usage)
2932
try:
3033
f = io.open(path, 'w', encoding=encoding, newline=newline)
@@ -47,6 +50,8 @@ def file_writer(path=None, encoding='UTF-8', newline=None, usage=None):
4750

4851
def binary_file_writer(path=None):
4952
if path:
53+
if is_pathlike(path):
54+
path = str(path)
5055
return io.open(path, 'wb')
5156
f = io.BytesIO()
5257
getvalue = f.getvalue
@@ -55,6 +60,8 @@ def binary_file_writer(path=None):
5560

5661

5762
def create_destination_directory(path, usage=None):
63+
if is_pathlike(path):
64+
path = str(path)
5865
directory = os.path.dirname(path)
5966
if directory and not os.path.exists(directory):
6067
try:

0 commit comments

Comments
 (0)