util.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. """Miscellaneous utility functions and classes.
  2. This module is used internally by Tornado. It is not necessarily expected
  3. that the functions and classes defined here will be useful to other
  4. applications, but they are documented here in case they are.
  5. The one public-facing part of this module is the `Configurable` class
  6. and its `~Configurable.configure` method, which becomes a part of the
  7. interface of its subclasses, including `.AsyncHTTPClient`, `.IOLoop`,
  8. and `.Resolver`.
  9. """
  10. import array
  11. import asyncio
  12. from inspect import getfullargspec
  13. import os
  14. import re
  15. import typing
  16. import zlib
  17. from typing import (
  18. Any,
  19. Optional,
  20. Dict,
  21. Mapping,
  22. List,
  23. Tuple,
  24. Match,
  25. Callable,
  26. Type,
  27. Sequence,
  28. )
  29. if typing.TYPE_CHECKING:
  30. # Additional imports only used in type comments.
  31. # This lets us make these imports lazy.
  32. import datetime # noqa: F401
  33. from types import TracebackType # noqa: F401
  34. from typing import Union # noqa: F401
  35. import unittest # noqa: F401
  36. # Aliases for types that are spelled differently in different Python
  37. # versions. bytes_type is deprecated and no longer used in Tornado
  38. # itself but is left in case anyone outside Tornado is using it.
  39. bytes_type = bytes
  40. unicode_type = str
  41. basestring_type = str
  42. # versionchanged:: 6.2
  43. # no longer our own TimeoutError, use standard asyncio class
  44. TimeoutError = asyncio.TimeoutError
  45. class ObjectDict(Dict[str, Any]):
  46. """Makes a dictionary behave like an object, with attribute-style access."""
  47. def __getattr__(self, name: str) -> Any:
  48. try:
  49. return self[name]
  50. except KeyError:
  51. raise AttributeError(name)
  52. def __setattr__(self, name: str, value: Any) -> None:
  53. self[name] = value
  54. class GzipDecompressor:
  55. """Streaming gzip decompressor.
  56. The interface is like that of `zlib.decompressobj` (without some of the
  57. optional arguments, but it understands gzip headers and checksums.
  58. """
  59. def __init__(self) -> None:
  60. # Magic parameter makes zlib module understand gzip header
  61. # http://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib
  62. # This works on cpython and pypy, but not jython.
  63. self.decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS)
  64. def decompress(self, value: bytes, max_length: int = 0) -> bytes:
  65. """Decompress a chunk, returning newly-available data.
  66. Some data may be buffered for later processing; `flush` must
  67. be called when there is no more input data to ensure that
  68. all data was processed.
  69. If ``max_length`` is given, some input data may be left over
  70. in ``unconsumed_tail``; you must retrieve this value and pass
  71. it back to a future call to `decompress` if it is not empty.
  72. """
  73. return self.decompressobj.decompress(value, max_length)
  74. @property
  75. def unconsumed_tail(self) -> bytes:
  76. """Returns the unconsumed portion left over"""
  77. return self.decompressobj.unconsumed_tail
  78. def flush(self) -> bytes:
  79. """Return any remaining buffered data not yet returned by decompress.
  80. Also checks for errors such as truncated input.
  81. No other methods may be called on this object after `flush`.
  82. """
  83. return self.decompressobj.flush()
  84. def import_object(name: str) -> Any:
  85. """Imports an object by name.
  86. ``import_object('x')`` is equivalent to ``import x``.
  87. ``import_object('x.y.z')`` is equivalent to ``from x.y import z``.
  88. >>> import tornado.escape
  89. >>> import_object('tornado.escape') is tornado.escape
  90. True
  91. >>> import_object('tornado.escape.utf8') is tornado.escape.utf8
  92. True
  93. >>> import_object('tornado') is tornado
  94. True
  95. >>> import_object('tornado.missing_module')
  96. Traceback (most recent call last):
  97. ...
  98. ImportError: No module named missing_module
  99. """
  100. if name.count(".") == 0:
  101. return __import__(name)
  102. parts = name.split(".")
  103. obj = __import__(".".join(parts[:-1]), fromlist=[parts[-1]])
  104. try:
  105. return getattr(obj, parts[-1])
  106. except AttributeError:
  107. raise ImportError("No module named %s" % parts[-1])
  108. def exec_in(
  109. code: Any, glob: Dict[str, Any], loc: Optional[Optional[Mapping[str, Any]]] = None
  110. ) -> None:
  111. if isinstance(code, str):
  112. # exec(string) inherits the caller's future imports; compile
  113. # the string first to prevent that.
  114. code = compile(code, "<string>", "exec", dont_inherit=True)
  115. exec(code, glob, loc)
  116. def raise_exc_info(
  117. exc_info: Tuple[Optional[type], Optional[BaseException], Optional["TracebackType"]]
  118. ) -> typing.NoReturn:
  119. try:
  120. if exc_info[1] is not None:
  121. raise exc_info[1].with_traceback(exc_info[2])
  122. else:
  123. raise TypeError("raise_exc_info called with no exception")
  124. finally:
  125. # Clear the traceback reference from our stack frame to
  126. # minimize circular references that slow down GC.
  127. exc_info = (None, None, None)
  128. def errno_from_exception(e: BaseException) -> Optional[int]:
  129. """Provides the errno from an Exception object.
  130. There are cases that the errno attribute was not set so we pull
  131. the errno out of the args but if someone instantiates an Exception
  132. without any args you will get a tuple error. So this function
  133. abstracts all that behavior to give you a safe way to get the
  134. errno.
  135. """
  136. if hasattr(e, "errno"):
  137. return e.errno # type: ignore
  138. elif e.args:
  139. return e.args[0]
  140. else:
  141. return None
  142. _alphanum = frozenset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  143. def _re_unescape_replacement(match: Match[str]) -> str:
  144. group = match.group(1)
  145. if group[0] in _alphanum:
  146. raise ValueError("cannot unescape '\\\\%s'" % group[0])
  147. return group
  148. _re_unescape_pattern = re.compile(r"\\(.)", re.DOTALL)
  149. def re_unescape(s: str) -> str:
  150. r"""Unescape a string escaped by `re.escape`.
  151. May raise ``ValueError`` for regular expressions which could not
  152. have been produced by `re.escape` (for example, strings containing
  153. ``\d`` cannot be unescaped).
  154. .. versionadded:: 4.4
  155. """
  156. return _re_unescape_pattern.sub(_re_unescape_replacement, s)
  157. class Configurable:
  158. """Base class for configurable interfaces.
  159. A configurable interface is an (abstract) class whose constructor
  160. acts as a factory function for one of its implementation subclasses.
  161. The implementation subclass as well as optional keyword arguments to
  162. its initializer can be set globally at runtime with `configure`.
  163. By using the constructor as the factory method, the interface
  164. looks like a normal class, `isinstance` works as usual, etc. This
  165. pattern is most useful when the choice of implementation is likely
  166. to be a global decision (e.g. when `~select.epoll` is available,
  167. always use it instead of `~select.select`), or when a
  168. previously-monolithic class has been split into specialized
  169. subclasses.
  170. Configurable subclasses must define the class methods
  171. `configurable_base` and `configurable_default`, and use the instance
  172. method `initialize` instead of ``__init__``.
  173. .. versionchanged:: 5.0
  174. It is now possible for configuration to be specified at
  175. multiple levels of a class hierarchy.
  176. """
  177. # Type annotations on this class are mostly done with comments
  178. # because they need to refer to Configurable, which isn't defined
  179. # until after the class definition block. These can use regular
  180. # annotations when our minimum python version is 3.7.
  181. #
  182. # There may be a clever way to use generics here to get more
  183. # precise types (i.e. for a particular Configurable subclass T,
  184. # all the types are subclasses of T, not just Configurable).
  185. __impl_class = None # type: Optional[Type[Configurable]]
  186. __impl_kwargs = None # type: Dict[str, Any]
  187. def __new__(cls, *args: Any, **kwargs: Any) -> Any:
  188. base = cls.configurable_base()
  189. init_kwargs = {} # type: Dict[str, Any]
  190. if cls is base:
  191. impl = cls.configured_class()
  192. if base.__impl_kwargs:
  193. init_kwargs.update(base.__impl_kwargs)
  194. else:
  195. impl = cls
  196. init_kwargs.update(kwargs)
  197. if impl.configurable_base() is not base:
  198. # The impl class is itself configurable, so recurse.
  199. return impl(*args, **init_kwargs)
  200. instance = super().__new__(impl)
  201. # initialize vs __init__ chosen for compatibility with AsyncHTTPClient
  202. # singleton magic. If we get rid of that we can switch to __init__
  203. # here too.
  204. instance.initialize(*args, **init_kwargs)
  205. return instance
  206. @classmethod
  207. def configurable_base(cls):
  208. # type: () -> Type[Configurable]
  209. """Returns the base class of a configurable hierarchy.
  210. This will normally return the class in which it is defined.
  211. (which is *not* necessarily the same as the ``cls`` classmethod
  212. parameter).
  213. """
  214. raise NotImplementedError()
  215. @classmethod
  216. def configurable_default(cls):
  217. # type: () -> Type[Configurable]
  218. """Returns the implementation class to be used if none is configured."""
  219. raise NotImplementedError()
  220. def _initialize(self) -> None:
  221. pass
  222. initialize = _initialize # type: Callable[..., None]
  223. """Initialize a `Configurable` subclass instance.
  224. Configurable classes should use `initialize` instead of ``__init__``.
  225. .. versionchanged:: 4.2
  226. Now accepts positional arguments in addition to keyword arguments.
  227. """
  228. @classmethod
  229. def configure(cls, impl, **kwargs):
  230. # type: (Union[None, str, Type[Configurable]], Any) -> None
  231. """Sets the class to use when the base class is instantiated.
  232. Keyword arguments will be saved and added to the arguments passed
  233. to the constructor. This can be used to set global defaults for
  234. some parameters.
  235. """
  236. base = cls.configurable_base()
  237. if isinstance(impl, str):
  238. impl = typing.cast(Type[Configurable], import_object(impl))
  239. if impl is not None and not issubclass(impl, cls):
  240. raise ValueError("Invalid subclass of %s" % cls)
  241. base.__impl_class = impl
  242. base.__impl_kwargs = kwargs
  243. @classmethod
  244. def configured_class(cls):
  245. # type: () -> Type[Configurable]
  246. """Returns the currently configured class."""
  247. base = cls.configurable_base()
  248. # Manually mangle the private name to see whether this base
  249. # has been configured (and not another base higher in the
  250. # hierarchy).
  251. if base.__dict__.get("_Configurable__impl_class") is None:
  252. base.__impl_class = cls.configurable_default()
  253. if base.__impl_class is not None:
  254. return base.__impl_class
  255. else:
  256. # Should be impossible, but mypy wants an explicit check.
  257. raise ValueError("configured class not found")
  258. @classmethod
  259. def _save_configuration(cls):
  260. # type: () -> Tuple[Optional[Type[Configurable]], Dict[str, Any]]
  261. base = cls.configurable_base()
  262. return (base.__impl_class, base.__impl_kwargs)
  263. @classmethod
  264. def _restore_configuration(cls, saved):
  265. # type: (Tuple[Optional[Type[Configurable]], Dict[str, Any]]) -> None
  266. base = cls.configurable_base()
  267. base.__impl_class = saved[0]
  268. base.__impl_kwargs = saved[1]
  269. class ArgReplacer:
  270. """Replaces one value in an ``args, kwargs`` pair.
  271. Inspects the function signature to find an argument by name
  272. whether it is passed by position or keyword. For use in decorators
  273. and similar wrappers.
  274. """
  275. def __init__(self, func: Callable, name: str) -> None:
  276. self.name = name
  277. try:
  278. self.arg_pos = self._getargnames(func).index(name) # type: Optional[int]
  279. except ValueError:
  280. # Not a positional parameter
  281. self.arg_pos = None
  282. def _getargnames(self, func: Callable) -> List[str]:
  283. try:
  284. return getfullargspec(func).args
  285. except TypeError:
  286. if hasattr(func, "func_code"):
  287. # Cython-generated code has all the attributes needed
  288. # by inspect.getfullargspec, but the inspect module only
  289. # works with ordinary functions. Inline the portion of
  290. # getfullargspec that we need here. Note that for static
  291. # functions the @cython.binding(True) decorator must
  292. # be used (for methods it works out of the box).
  293. code = func.func_code # type: ignore
  294. return code.co_varnames[: code.co_argcount]
  295. raise
  296. def get_old_value(
  297. self, args: Sequence[Any], kwargs: Dict[str, Any], default: Any = None
  298. ) -> Any:
  299. """Returns the old value of the named argument without replacing it.
  300. Returns ``default`` if the argument is not present.
  301. """
  302. if self.arg_pos is not None and len(args) > self.arg_pos:
  303. return args[self.arg_pos]
  304. else:
  305. return kwargs.get(self.name, default)
  306. def replace(
  307. self, new_value: Any, args: Sequence[Any], kwargs: Dict[str, Any]
  308. ) -> Tuple[Any, Sequence[Any], Dict[str, Any]]:
  309. """Replace the named argument in ``args, kwargs`` with ``new_value``.
  310. Returns ``(old_value, args, kwargs)``. The returned ``args`` and
  311. ``kwargs`` objects may not be the same as the input objects, or
  312. the input objects may be mutated.
  313. If the named argument was not found, ``new_value`` will be added
  314. to ``kwargs`` and None will be returned as ``old_value``.
  315. """
  316. if self.arg_pos is not None and len(args) > self.arg_pos:
  317. # The arg to replace is passed positionally
  318. old_value = args[self.arg_pos]
  319. args = list(args) # *args is normally a tuple
  320. args[self.arg_pos] = new_value
  321. else:
  322. # The arg to replace is either omitted or passed by keyword.
  323. old_value = kwargs.get(self.name)
  324. kwargs[self.name] = new_value
  325. return old_value, args, kwargs
  326. def timedelta_to_seconds(td):
  327. # type: (datetime.timedelta) -> float
  328. """Equivalent to ``td.total_seconds()`` (introduced in Python 2.7)."""
  329. return td.total_seconds()
  330. def _websocket_mask_python(mask: bytes, data: bytes) -> bytes:
  331. """Websocket masking function.
  332. `mask` is a `bytes` object of length 4; `data` is a `bytes` object of any length.
  333. Returns a `bytes` object of the same length as `data` with the mask applied
  334. as specified in section 5.3 of RFC 6455.
  335. This pure-python implementation may be replaced by an optimized version when available.
  336. """
  337. mask_arr = array.array("B", mask)
  338. unmasked_arr = array.array("B", data)
  339. for i in range(len(data)):
  340. unmasked_arr[i] = unmasked_arr[i] ^ mask_arr[i % 4]
  341. return unmasked_arr.tobytes()
  342. if os.environ.get("TORNADO_NO_EXTENSION") or os.environ.get("TORNADO_EXTENSION") == "0":
  343. # These environment variables exist to make it easier to do performance
  344. # comparisons; they are not guaranteed to remain supported in the future.
  345. _websocket_mask = _websocket_mask_python
  346. else:
  347. try:
  348. from tornado.speedups import websocket_mask as _websocket_mask
  349. except ImportError:
  350. if os.environ.get("TORNADO_EXTENSION") == "1":
  351. raise
  352. _websocket_mask = _websocket_mask_python
  353. def doctests():
  354. # type: () -> unittest.TestSuite
  355. import doctest
  356. return doctest.DocTestSuite()