Skip to content

Commit 89c240b

Browse files
committed
Add fspath support. Ref ionelmc#38.
1 parent caceb70 commit 89c240b

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

src/lazy_object_proxy/cext.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ static PyMethodDef Proxy_methods[] = {
12821282
{ "__reversed__", (PyCFunction)Proxy_reversed, METH_NOARGS, 0 },
12831283
{ "__reduce__", (PyCFunction)Proxy_reduce, METH_NOARGS, 0 },
12841284
{ "__reduce_ex__", (PyCFunction)Proxy_reduce, METH_O, 0 },
1285+
{ "__fspath__", (PyCFunction)Proxy_str, METH_NOARGS, 0 },
12851286
#if PY_MAJOR_VERSION >= 3
12861287
{ "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
12871288
#endif

src/lazy_object_proxy/simple.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __repr__(self, __getattr__=object.__getattribute__):
100100
self.__factory__
101101
)
102102

103+
__fspath__ = make_proxy_method(str)
103104
__reversed__ = make_proxy_method(reversed)
104105

105106
if PY3:

src/lazy_object_proxy/slots.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ def __repr__(self, __getattr__=object.__getattribute__):
152152
self.__factory__
153153
)
154154

155+
def __fspath__(self):
156+
return str(self.__wrapped__)
157+
155158
def __reversed__(self):
156159
return reversed(self.__wrapped__)
157160

tests/test_lazy_object_proxy.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gc
44
import imp
5+
import os
56
import pickle
67
import platform
78
import sys
@@ -1943,3 +1944,8 @@ def __init__(self, func, **lazy_attr):
19431944
proxy = LazyProxy(lambda: called.append(1) or Foo(), name='bar')
19441945
assert proxy.name == 'bar'
19451946
assert not called
1947+
1948+
1949+
@pytest.mark.skipif(not hasattr(os, "fspath"), reason="No os.fspath support.")
1950+
def test_fspath(lazy_object_proxy):
1951+
assert os.fspath(lazy_object_proxy.Proxy(lambda: '/tmp')) == '/tmp'

0 commit comments

Comments
 (0)