Skip to content

Commit 00a7af8

Browse files
committed
Reformat and cleanup some more py2 code.
1 parent 4a940b5 commit 00a7af8

File tree

9 files changed

+153
-198
lines changed

9 files changed

+153
-198
lines changed

src/lazy_object_proxy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
except ImportError:
2121
__version__ = '1.7.1'
2222

23-
__all__ = "Proxy",
23+
__all__ = ("Proxy",)

src/lazy_object_proxy/compat.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import sys
2-
3-
PY2 = sys.version_info[0] == 2
4-
PY3 = sys.version_info[0] == 3
5-
6-
if PY3:
7-
string_types = str, bytes
8-
else:
9-
string_types = basestring, # noqa: F821
1+
string_types = str, bytes
102

113

124
def with_metaclass(meta, *bases):

src/lazy_object_proxy/simple.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,10 @@ def __wrapped__(self):
9696
def __repr__(self, __getattr__=object.__getattribute__):
9797
if '__wrapped__' in self.__dict__:
9898
return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
99-
type(self).__name__, id(self),
100-
self.__wrapped__, id(self.__wrapped__),
101-
self.__factory__
99+
type(self).__name__, id(self), self.__wrapped__, id(self.__wrapped__), self.__factory__
102100
)
103101
else:
104-
return '<{} at 0x{:x} with factory {!r}>'.format(
105-
type(self).__name__, id(self),
106-
self.__factory__
107-
)
102+
return '<{} at 0x{:x} with factory {!r}>'.format(type(self).__name__, id(self), self.__factory__)
108103

109104
def __fspath__(self):
110105
wrapped = self.__wrapped__

src/lazy_object_proxy/slots.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def __resolved__(self, __getattr__=object.__getattribute__):
9494
return True
9595

9696
@property
97-
def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__,
98-
__delattr__=object.__delattr__):
97+
def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__, __delattr__=object.__delattr__):
9998
try:
10099
return __getattr__(self, '__target__')
101100
except AttributeError:
@@ -146,22 +145,18 @@ def __str__(self):
146145
return str(self.__wrapped__)
147146

148147
if PY3:
148+
149149
def __bytes__(self):
150150
return bytes(self.__wrapped__)
151151

152152
def __repr__(self, __getattr__=object.__getattribute__):
153153
try:
154154
target = __getattr__(self, '__target__')
155155
except AttributeError:
156-
return '<{} at 0x{:x} with factory {!r}>'.format(
157-
type(self).__name__, id(self),
158-
self.__factory__
159-
)
156+
return '<{} at 0x{:x} with factory {!r}>'.format(type(self).__name__, id(self), self.__factory__)
160157
else:
161158
return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
162-
type(self).__name__, id(self),
163-
target, id(target),
164-
self.__factory__
159+
type(self).__name__, id(self), target, id(target), self.__factory__
165160
)
166161

167162
def __fspath__(self):
@@ -179,6 +174,7 @@ def __reversed__(self):
179174
return reversed(self.__wrapped__)
180175

181176
if PY3:
177+
182178
def __round__(self):
183179
return round(self.__wrapped__)
184180

@@ -379,6 +375,7 @@ def __int__(self):
379375
return int(self.__wrapped__)
380376

381377
if PY2:
378+
382379
def __long__(self):
383380
return long(self.__wrapped__) # noqa
384381

src/lazy_object_proxy/utils.py

+45-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1-
# flake8: noqa
2-
try:
3-
from .utils_py3 import __aenter__
4-
from .utils_py3 import __aexit__
5-
from .utils_py3 import __aiter__
6-
from .utils_py3 import __anext__
7-
from .utils_py3 import __await__
8-
from .utils_py3 import await_
9-
except (ImportError, SyntaxError):
10-
await_ = None
1+
from collections.abc import Awaitable
2+
from inspect import CO_ITERABLE_COROUTINE
3+
from types import CoroutineType
4+
from types import GeneratorType
5+
6+
7+
async def do_await(obj):
8+
return await obj
9+
10+
11+
def do_yield_from(gen):
12+
return (yield from gen)
13+
14+
15+
def await_(obj):
16+
obj_type = type(obj)
17+
if (
18+
obj_type is CoroutineType
19+
or obj_type is GeneratorType
20+
and bool(obj.gi_code.co_flags & CO_ITERABLE_COROUTINE)
21+
or isinstance(obj, Awaitable)
22+
):
23+
return do_await(obj).__await__()
24+
else:
25+
return do_yield_from(obj)
26+
27+
28+
def __aiter__(self):
29+
return self.__wrapped__.__aiter__()
30+
31+
32+
async def __anext__(self):
33+
return await self.__wrapped__.__anext__()
34+
35+
36+
def __await__(self):
37+
return await_(self.__wrapped__)
38+
39+
40+
def __aenter__(self):
41+
return self.__wrapped__.__aenter__()
42+
43+
44+
def __aexit__(self, *args, **kwargs):
45+
return self.__wrapped__.__aexit__(*args, **kwargs)
1146

1247

1348
def identity(obj):

src/lazy_object_proxy/utils_py3.py

-44
This file was deleted.

tests/compat.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
if PY3:
77
import builtins
8+
89
exec_ = getattr(builtins, "exec")
910
del builtins
1011

1112
else:
13+
1214
def exec_(_code_, _globs_=None, _locs_=None):
1315
"""Execute code in a namespace."""
1416
if _globs_ is None:
@@ -21,6 +23,8 @@ def exec_(_code_, _globs_=None, _locs_=None):
2123
_locs_ = _globs_
2224
exec("""exec _code_ in _globs_, _locs_""")
2325

24-
exec_("""def reraise(tp, value, tb=None):
26+
exec_(
27+
"""def reraise(tp, value, tb=None):
2528
raise tp, value, tb
26-
""")
29+
"""
30+
)

0 commit comments

Comments
 (0)