Skip to content

Commit bf08f2f

Browse files
committed
Enable async stuff in stdlib modules
1 parent 577cea9 commit bf08f2f

File tree

3 files changed

+342
-343
lines changed

3 files changed

+342
-343
lines changed

Lib/_collections_abc.py

Lines changed: 135 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
dict_valueiterator = type(iter({}.values()))
4141
dict_itemiterator = type(iter({}.items()))
4242
list_iterator = type(iter([]))
43-
# list_reverseiterator = type(iter(reversed([])))
43+
list_reverseiterator = type(iter(reversed([])))
4444
range_iterator = type(iter(range(0)))
4545
longrange_iterator = type(iter(range(1 << 1000)))
4646
set_iterator = type(iter(set()))
@@ -53,13 +53,14 @@
5353
dict_items = type({}.items())
5454
## misc ##
5555
mappingproxy = type(type.__dict__)
56-
# generator = type((lambda: (yield))())
56+
generator = type((lambda: (yield))())
5757
## coroutine ##
58-
# async def _coro(): pass
59-
# _coro = _coro()
60-
# coroutine = type(_coro)
61-
# _coro.close() # Prevent ResourceWarning
62-
# del _coro
58+
async def _coro(): pass
59+
_coro = _coro()
60+
coroutine = type(_coro)
61+
_coro.close() # Prevent ResourceWarning
62+
del _coro
63+
# XXX RustPython TODO: async generators
6364
# ## asynchronous generator ##
6465
# async def _ag(): yield
6566
# _ag = _ag()
@@ -96,145 +97,145 @@ def __subclasshook__(cls, C):
9697
return NotImplemented
9798

9899

99-
# class Awaitable(metaclass=ABCMeta):
100+
class Awaitable(metaclass=ABCMeta):
100101

101-
# __slots__ = ()
102+
__slots__ = ()
102103

103-
# @abstractmethod
104-
# def __await__(self):
105-
# yield
104+
@abstractmethod
105+
def __await__(self):
106+
yield
106107

107-
# @classmethod
108-
# def __subclasshook__(cls, C):
109-
# if cls is Awaitable:
110-
# return _check_methods(C, "__await__")
111-
# return NotImplemented
108+
@classmethod
109+
def __subclasshook__(cls, C):
110+
if cls is Awaitable:
111+
return _check_methods(C, "__await__")
112+
return NotImplemented
112113

113114

114-
# class Coroutine(Awaitable):
115+
class Coroutine(Awaitable):
115116

116-
# __slots__ = ()
117+
__slots__ = ()
117118

118-
# @abstractmethod
119-
# def send(self, value):
120-
# """Send a value into the coroutine.
121-
# Return next yielded value or raise StopIteration.
122-
# """
123-
# raise StopIteration
119+
@abstractmethod
120+
def send(self, value):
121+
"""Send a value into the coroutine.
122+
Return next yielded value or raise StopIteration.
123+
"""
124+
raise StopIteration
124125

125-
# @abstractmethod
126-
# def throw(self, typ, val=None, tb=None):
127-
# """Raise an exception in the coroutine.
128-
# Return next yielded value or raise StopIteration.
129-
# """
130-
# if val is None:
131-
# if tb is None:
132-
# raise typ
133-
# val = typ()
134-
# if tb is not None:
135-
# val = val.with_traceback(tb)
136-
# raise val
137-
138-
# def close(self):
139-
# """Raise GeneratorExit inside coroutine.
140-
# """
141-
# try:
142-
# self.throw(GeneratorExit)
143-
# except (GeneratorExit, StopIteration):
144-
# pass
145-
# else:
146-
# raise RuntimeError("coroutine ignored GeneratorExit")
126+
@abstractmethod
127+
def throw(self, typ, val=None, tb=None):
128+
"""Raise an exception in the coroutine.
129+
Return next yielded value or raise StopIteration.
130+
"""
131+
if val is None:
132+
if tb is None:
133+
raise typ
134+
val = typ()
135+
if tb is not None:
136+
val = val.with_traceback(tb)
137+
raise val
147138

148-
# @classmethod
149-
# def __subclasshook__(cls, C):
150-
# if cls is Coroutine:
151-
# return _check_methods(C, '__await__', 'send', 'throw', 'close')
152-
# return NotImplemented
139+
def close(self):
140+
"""Raise GeneratorExit inside coroutine.
141+
"""
142+
try:
143+
self.throw(GeneratorExit)
144+
except (GeneratorExit, StopIteration):
145+
pass
146+
else:
147+
raise RuntimeError("coroutine ignored GeneratorExit")
153148

149+
@classmethod
150+
def __subclasshook__(cls, C):
151+
if cls is Coroutine:
152+
return _check_methods(C, '__await__', 'send', 'throw', 'close')
153+
return NotImplemented
154154

155-
# Coroutine.register(coroutine)
156155

156+
Coroutine.register(coroutine)
157157

158-
# class AsyncIterable(metaclass=ABCMeta):
159158

160-
# __slots__ = ()
159+
class AsyncIterable(metaclass=ABCMeta):
161160

162-
# @abstractmethod
163-
# def __aiter__(self):
164-
# return AsyncIterator()
161+
__slots__ = ()
165162

166-
# @classmethod
167-
# def __subclasshook__(cls, C):
168-
# if cls is AsyncIterable:
169-
# return _check_methods(C, "__aiter__")
170-
# return NotImplemented
163+
@abstractmethod
164+
def __aiter__(self):
165+
return AsyncIterator()
171166

167+
@classmethod
168+
def __subclasshook__(cls, C):
169+
if cls is AsyncIterable:
170+
return _check_methods(C, "__aiter__")
171+
return NotImplemented
172172

173-
# class AsyncIterator(AsyncIterable):
174173

175-
# __slots__ = ()
174+
class AsyncIterator(AsyncIterable):
176175

177-
# @abstractmethod
178-
# async def __anext__(self):
179-
# """Return the next item or raise StopAsyncIteration when exhausted."""
180-
# raise StopAsyncIteration
176+
__slots__ = ()
181177

182-
# def __aiter__(self):
183-
# return self
178+
@abstractmethod
179+
async def __anext__(self):
180+
"""Return the next item or raise StopAsyncIteration when exhausted."""
181+
raise StopAsyncIteration
184182

185-
# @classmethod
186-
# def __subclasshook__(cls, C):
187-
# if cls is AsyncIterator:
188-
# return _check_methods(C, "__anext__", "__aiter__")
189-
# return NotImplemented
183+
def __aiter__(self):
184+
return self
185+
186+
@classmethod
187+
def __subclasshook__(cls, C):
188+
if cls is AsyncIterator:
189+
return _check_methods(C, "__anext__", "__aiter__")
190+
return NotImplemented
190191

191192

192-
# class AsyncGenerator(AsyncIterator):
193+
class AsyncGenerator(AsyncIterator):
194+
195+
__slots__ = ()
196+
197+
async def __anext__(self):
198+
"""Return the next item from the asynchronous generator.
199+
When exhausted, raise StopAsyncIteration.
200+
"""
201+
return await self.asend(None)
202+
203+
@abstractmethod
204+
async def asend(self, value):
205+
"""Send a value into the asynchronous generator.
206+
Return next yielded value or raise StopAsyncIteration.
207+
"""
208+
raise StopAsyncIteration
209+
210+
@abstractmethod
211+
async def athrow(self, typ, val=None, tb=None):
212+
"""Raise an exception in the asynchronous generator.
213+
Return next yielded value or raise StopAsyncIteration.
214+
"""
215+
if val is None:
216+
if tb is None:
217+
raise typ
218+
val = typ()
219+
if tb is not None:
220+
val = val.with_traceback(tb)
221+
raise val
193222

194-
# __slots__ = ()
223+
async def aclose(self):
224+
"""Raise GeneratorExit inside coroutine.
225+
"""
226+
try:
227+
await self.athrow(GeneratorExit)
228+
except (GeneratorExit, StopAsyncIteration):
229+
pass
230+
else:
231+
raise RuntimeError("asynchronous generator ignored GeneratorExit")
195232

196-
# async def __anext__(self):
197-
# """Return the next item from the asynchronous generator.
198-
# When exhausted, raise StopAsyncIteration.
199-
# """
200-
# return await self.asend(None)
201-
202-
# @abstractmethod
203-
# async def asend(self, value):
204-
# """Send a value into the asynchronous generator.
205-
# Return next yielded value or raise StopAsyncIteration.
206-
# """
207-
# raise StopAsyncIteration
208-
209-
# @abstractmethod
210-
# async def athrow(self, typ, val=None, tb=None):
211-
# """Raise an exception in the asynchronous generator.
212-
# Return next yielded value or raise StopAsyncIteration.
213-
# """
214-
# if val is None:
215-
# if tb is None:
216-
# raise typ
217-
# val = typ()
218-
# if tb is not None:
219-
# val = val.with_traceback(tb)
220-
# raise val
221-
222-
# async def aclose(self):
223-
# """Raise GeneratorExit inside coroutine.
224-
# """
225-
# try:
226-
# await self.athrow(GeneratorExit)
227-
# except (GeneratorExit, StopAsyncIteration):
228-
# pass
229-
# else:
230-
# raise RuntimeError("asynchronous generator ignored GeneratorExit")
231-
232-
# @classmethod
233-
# def __subclasshook__(cls, C):
234-
# if cls is AsyncGenerator:
235-
# return _check_methods(C, '__aiter__', '__anext__',
236-
# 'asend', 'athrow', 'aclose')
237-
# return NotImplemented
233+
@classmethod
234+
def __subclasshook__(cls, C):
235+
if cls is AsyncGenerator:
236+
return _check_methods(C, '__aiter__', '__anext__',
237+
'asend', 'athrow', 'aclose')
238+
return NotImplemented
238239

239240

240241
# AsyncGenerator.register(async_generator)
@@ -274,20 +275,20 @@ def __subclasshook__(cls, C):
274275
return _check_methods(C, '__iter__', '__next__')
275276
return NotImplemented
276277

277-
# Iterator.register(bytes_iterator)
278-
# Iterator.register(bytearray_iterator)
278+
Iterator.register(bytes_iterator)
279+
Iterator.register(bytearray_iterator)
279280
# Iterator.register(callable_iterator)
280-
# Iterator.register(dict_keyiterator)
281-
# Iterator.register(dict_valueiterator)
282-
# Iterator.register(dict_itemiterator)
283-
# Iterator.register(list_iterator)
284-
# Iterator.register(list_reverseiterator)
285-
# Iterator.register(range_iterator)
286-
# Iterator.register(longrange_iterator)
287-
# Iterator.register(set_iterator)
288-
# Iterator.register(str_iterator)
289-
# Iterator.register(tuple_iterator)
290-
# Iterator.register(zip_iterator)
281+
Iterator.register(dict_keyiterator)
282+
Iterator.register(dict_valueiterator)
283+
Iterator.register(dict_itemiterator)
284+
Iterator.register(list_iterator)
285+
Iterator.register(list_reverseiterator)
286+
Iterator.register(range_iterator)
287+
Iterator.register(longrange_iterator)
288+
Iterator.register(set_iterator)
289+
Iterator.register(str_iterator)
290+
Iterator.register(tuple_iterator)
291+
Iterator.register(zip_iterator)
291292

292293

293294
class Reversible(Iterable):
@@ -353,8 +354,7 @@ def __subclasshook__(cls, C):
353354
'send', 'throw', 'close')
354355
return NotImplemented
355356

356-
# Generator.register(generator)
357-
357+
Generator.register(generator)
358358

359359
class Sized(metaclass=ABCMeta):
360360

0 commit comments

Comments
 (0)