11"""A Future class similar to the one in PEP 3148."""
22
3- __all__ = ['CancelledError' , 'TimeoutError' ,
4- 'InvalidStateError' ,
5- 'Future' , 'wrap_future' , 'isfuture'
6- ]
3+ __all__ = ['CancelledError' , 'TimeoutError' , 'InvalidStateError' ,
4+ 'Future' , 'wrap_future' , 'isfuture' ]
75
8- import concurrent .futures . _base
6+ import concurrent .futures
97import logging
10- import reprlib
118import sys
129import traceback
1310
11+ from . import base_futures
1412from . import compat
1513from . import events
1614
17- # States for Future.
18- _PENDING = 'PENDING'
19- _CANCELLED = 'CANCELLED'
20- _FINISHED = 'FINISHED'
2115
22- Error = concurrent .futures ._base .Error
23- CancelledError = concurrent .futures .CancelledError
24- TimeoutError = concurrent .futures .TimeoutError
16+ CancelledError = base_futures .CancelledError
17+ InvalidStateError = base_futures .InvalidStateError
18+ TimeoutError = base_futures .TimeoutError
19+ isfuture = base_futures .isfuture
2520
26- STACK_DEBUG = logging .DEBUG - 1 # heavy-duty debugging
21+
22+ _PENDING = base_futures ._PENDING
23+ _CANCELLED = base_futures ._CANCELLED
24+ _FINISHED = base_futures ._FINISHED
2725
2826
29- class InvalidStateError (Error ):
30- """The operation is not allowed in this state."""
27+ STACK_DEBUG = logging .DEBUG - 1 # heavy-duty debugging
3128
3229
3330class _TracebackLogger :
@@ -110,56 +107,6 @@ def __del__(self):
110107 self .loop .call_exception_handler ({'message' : msg })
111108
112109
113- def isfuture (obj ):
114- """Check for a Future.
115-
116- This returns True when obj is a Future instance or is advertising
117- itself as duck-type compatible by setting _asyncio_future_blocking.
118- See comment in Future for more details.
119- """
120- return getattr (obj , '_asyncio_future_blocking' , None ) is not None
121-
122-
123- def _format_callbacks (cb ):
124- """helper function for Future.__repr__"""
125- size = len (cb )
126- if not size :
127- cb = ''
128-
129- def format_cb (callback ):
130- return events ._format_callback_source (callback , ())
131-
132- if size == 1 :
133- cb = format_cb (cb [0 ])
134- elif size == 2 :
135- cb = '{}, {}' .format (format_cb (cb [0 ]), format_cb (cb [1 ]))
136- elif size > 2 :
137- cb = '{}, <{} more>, {}' .format (format_cb (cb [0 ]),
138- size - 2 ,
139- format_cb (cb [- 1 ]))
140- return 'cb=[%s]' % cb
141-
142-
143- def _future_repr_info (future ):
144- # (Future) -> str
145- """helper function for Future.__repr__"""
146- info = [future ._state .lower ()]
147- if future ._state == _FINISHED :
148- if future ._exception is not None :
149- info .append ('exception={!r}' .format (future ._exception ))
150- else :
151- # use reprlib to limit the length of the output, especially
152- # for very long strings
153- result = reprlib .repr (future ._result )
154- info .append ('result={}' .format (result ))
155- if future ._callbacks :
156- info .append (_format_callbacks (future ._callbacks ))
157- if future ._source_traceback :
158- frame = future ._source_traceback [- 1 ]
159- info .append ('created at %s:%s' % (frame [0 ], frame [1 ]))
160- return info
161-
162-
163110class Future :
164111 """This class is *almost* compatible with concurrent.futures.Future.
165112
@@ -212,7 +159,7 @@ def __init__(self, *, loop=None):
212159 if self ._loop .get_debug ():
213160 self ._source_traceback = traceback .extract_stack (sys ._getframe (1 ))
214161
215- _repr_info = _future_repr_info
162+ _repr_info = base_futures . _future_repr_info
216163
217164 def __repr__ (self ):
218165 return '<%s %s>' % (self .__class__ .__name__ , ' ' .join (self ._repr_info ()))
@@ -247,10 +194,10 @@ def cancel(self):
247194 if self ._state != _PENDING :
248195 return False
249196 self ._state = _CANCELLED
250- self .__schedule_callbacks ()
197+ self ._schedule_callbacks ()
251198 return True
252199
253- def __schedule_callbacks (self ):
200+ def _schedule_callbacks (self ):
254201 """Internal: Ask the event loop to call all callbacks.
255202
256203 The callbacks are scheduled to be called as soon as possible. Also
@@ -352,7 +299,7 @@ def set_result(self, result):
352299 raise InvalidStateError ('{}: {!r}' .format (self ._state , self ))
353300 self ._result = result
354301 self ._state = _FINISHED
355- self .__schedule_callbacks ()
302+ self ._schedule_callbacks ()
356303
357304 def set_exception (self , exception ):
358305 """Mark the future done and set an exception.
@@ -369,7 +316,7 @@ def set_exception(self, exception):
369316 "and cannot be raised into a Future" )
370317 self ._exception = exception
371318 self ._state = _FINISHED
372- self .__schedule_callbacks ()
319+ self ._schedule_callbacks ()
373320 if compat .PY34 :
374321 self ._log_traceback = True
375322 else :
0 commit comments