from that IOLoop's thread. add_callback() may be used to transfer
control from other threads to the IOLoop's thread.
"""
+ if not self._callbacks:
+ self._wake()
self._callbacks.append(stack_context.wrap(callback))
- self._wake()
def _wake(self):
try:
import sys
import threading
-NoneType = type(None)
-
class _State(threading.local):
def __init__(self):
self.contexts = ()
different execution context (either in a different thread or
asynchronously in the same thread).
'''
+ if fn is None or fn.__class__ is _StackContextWrapper:
+ return fn
# functools.wraps doesn't appear to work on functools.partial objects
#@functools.wraps(fn)
def wrapped(callback, contexts, *args, **kwargs):
callback(*args, **kwargs)
else:
callback(*args, **kwargs)
- if isinstance(fn, (_StackContextWrapper, NoneType)):
- return fn
return _StackContextWrapper(wrapped, fn, _state.contexts)
@contextlib.contextmanager
HTTP specification. If the value is not a string, we convert it to
a string. All header values are then encoded as UTF-8.
"""
- if isinstance(value, datetime.datetime):
- t = calendar.timegm(value.utctimetuple())
- value = email.utils.formatdate(t, localtime=False, usegmt=True)
- elif isinstance(value, int) or isinstance(value, long):
- value = str(value)
- else:
+ if isinstance(value, basestring):
value = utf8(value)
# If \n is allowed into the header, it is possible to inject
# additional headers or split the request. Also cap length to
safe_value = re.sub(b(r"[\x00-\x1f]"), b(" "), value)[:4000]
if safe_value != value:
raise ValueError("Unsafe header value %r", value)
+ elif isinstance(value, datetime.datetime):
+ t = calendar.timegm(value.utctimetuple())
+ value = email.utils.formatdate(t, localtime=False, usegmt=True)
+ elif isinstance(value, int) or isinstance(value, long):
+ value = str(value)
+ else:
+ raise TypeError("Unsupported header value %r" % value)
self._headers[name] = value
_ARG_DEFAULT = []