compat.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. # util/compat.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """Handle Python version/platform incompatibilities."""
  8. import collections
  9. import contextlib
  10. import inspect
  11. import operator
  12. import platform
  13. import sys
  14. py312 = sys.version_info >= (3, 12)
  15. py311 = sys.version_info >= (3, 11)
  16. py310 = sys.version_info >= (3, 10)
  17. py39 = sys.version_info >= (3, 9)
  18. py38 = sys.version_info >= (3, 8)
  19. py37 = sys.version_info >= (3, 7)
  20. py3k = sys.version_info >= (3, 0)
  21. py2k = sys.version_info < (3, 0)
  22. pypy = platform.python_implementation() == "PyPy"
  23. cpython = platform.python_implementation() == "CPython"
  24. win32 = sys.platform.startswith("win")
  25. osx = sys.platform.startswith("darwin")
  26. arm = "aarch" in platform.machine().lower()
  27. is64bit = sys.maxsize > 2 ** 32
  28. has_refcount_gc = bool(cpython)
  29. contextmanager = contextlib.contextmanager
  30. dottedgetter = operator.attrgetter
  31. namedtuple = collections.namedtuple
  32. next = next # noqa
  33. FullArgSpec = collections.namedtuple(
  34. "FullArgSpec",
  35. [
  36. "args",
  37. "varargs",
  38. "varkw",
  39. "defaults",
  40. "kwonlyargs",
  41. "kwonlydefaults",
  42. "annotations",
  43. ],
  44. )
  45. class nullcontext(object):
  46. """Context manager that does no additional processing.
  47. Vendored from Python 3.7.
  48. """
  49. def __init__(self, enter_result=None):
  50. self.enter_result = enter_result
  51. def __enter__(self):
  52. return self.enter_result
  53. def __exit__(self, *excinfo):
  54. pass
  55. try:
  56. import threading
  57. except ImportError:
  58. import dummy_threading as threading # noqa
  59. def inspect_getfullargspec(func):
  60. """Fully vendored version of getfullargspec from Python 3.3."""
  61. if inspect.ismethod(func):
  62. func = func.__func__
  63. if not inspect.isfunction(func):
  64. raise TypeError("{!r} is not a Python function".format(func))
  65. co = func.__code__
  66. if not inspect.iscode(co):
  67. raise TypeError("{!r} is not a code object".format(co))
  68. nargs = co.co_argcount
  69. names = co.co_varnames
  70. nkwargs = co.co_kwonlyargcount if py3k else 0
  71. args = list(names[:nargs])
  72. kwonlyargs = list(names[nargs : nargs + nkwargs])
  73. nargs += nkwargs
  74. varargs = None
  75. if co.co_flags & inspect.CO_VARARGS:
  76. varargs = co.co_varnames[nargs]
  77. nargs = nargs + 1
  78. varkw = None
  79. if co.co_flags & inspect.CO_VARKEYWORDS:
  80. varkw = co.co_varnames[nargs]
  81. return FullArgSpec(
  82. args,
  83. varargs,
  84. varkw,
  85. func.__defaults__,
  86. kwonlyargs,
  87. func.__kwdefaults__ if py3k else None,
  88. func.__annotations__ if py3k else {},
  89. )
  90. if py38:
  91. from importlib import metadata as importlib_metadata
  92. else:
  93. import importlib_metadata # noqa
  94. def importlib_metadata_get(group):
  95. ep = importlib_metadata.entry_points()
  96. if hasattr(ep, "select"):
  97. return ep.select(group=group)
  98. else:
  99. return ep.get(group, ())
  100. if py3k:
  101. import base64
  102. import builtins
  103. import configparser
  104. import itertools
  105. import pickle
  106. from functools import reduce
  107. from io import BytesIO as byte_buffer
  108. from io import StringIO
  109. from itertools import zip_longest
  110. from time import perf_counter
  111. from urllib.parse import (
  112. quote_plus,
  113. unquote_plus,
  114. parse_qsl,
  115. quote,
  116. unquote,
  117. )
  118. string_types = (str,)
  119. binary_types = (bytes,)
  120. binary_type = bytes
  121. text_type = str
  122. int_types = (int,)
  123. iterbytes = iter
  124. long_type = int
  125. itertools_filterfalse = itertools.filterfalse
  126. itertools_filter = filter
  127. itertools_imap = map
  128. exec_ = getattr(builtins, "exec")
  129. import_ = getattr(builtins, "__import__")
  130. print_ = getattr(builtins, "print")
  131. def b(s):
  132. return s.encode("latin-1")
  133. def b64decode(x):
  134. return base64.b64decode(x.encode("ascii"))
  135. def b64encode(x):
  136. return base64.b64encode(x).decode("ascii")
  137. def decode_backslashreplace(text, encoding):
  138. return text.decode(encoding, errors="backslashreplace")
  139. def cmp(a, b):
  140. return (a > b) - (a < b)
  141. def raise_(
  142. exception, with_traceback=None, replace_context=None, from_=False
  143. ):
  144. r"""implement "raise" with cause support.
  145. :param exception: exception to raise
  146. :param with_traceback: will call exception.with_traceback()
  147. :param replace_context: an as-yet-unsupported feature. This is
  148. an exception object which we are "replacing", e.g., it's our
  149. "cause" but we don't want it printed. Basically just what
  150. ``__suppress_context__`` does but we don't want to suppress
  151. the enclosing context, if any. So for now we make it the
  152. cause.
  153. :param from\_: the cause. this actually sets the cause and doesn't
  154. hope to hide it someday.
  155. """
  156. if with_traceback is not None:
  157. exception = exception.with_traceback(with_traceback)
  158. if from_ is not False:
  159. exception.__cause__ = from_
  160. elif replace_context is not None:
  161. # no good solution here, we would like to have the exception
  162. # have only the context of replace_context.__context__ so that the
  163. # intermediary exception does not change, but we can't figure
  164. # that out.
  165. exception.__cause__ = replace_context
  166. try:
  167. raise exception
  168. finally:
  169. # credit to
  170. # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
  171. # as the __traceback__ object creates a cycle
  172. del exception, replace_context, from_, with_traceback
  173. def u(s):
  174. return s
  175. def ue(s):
  176. return s
  177. from typing import TYPE_CHECKING
  178. # Unused. Kept for backwards compatibility.
  179. callable = callable # noqa
  180. from abc import ABC
  181. def _qualname(fn):
  182. return fn.__qualname__
  183. else:
  184. import base64
  185. import ConfigParser as configparser # noqa
  186. import itertools
  187. from StringIO import StringIO # noqa
  188. from cStringIO import StringIO as byte_buffer # noqa
  189. from itertools import izip_longest as zip_longest # noqa
  190. from time import clock as perf_counter # noqa
  191. from urllib import quote # noqa
  192. from urllib import quote_plus # noqa
  193. from urllib import unquote # noqa
  194. from urllib import unquote_plus # noqa
  195. from urlparse import parse_qsl # noqa
  196. from abc import ABCMeta
  197. class ABC(object):
  198. __metaclass__ = ABCMeta
  199. try:
  200. import cPickle as pickle
  201. except ImportError:
  202. import pickle # noqa
  203. string_types = (basestring,) # noqa
  204. binary_types = (bytes,)
  205. binary_type = str
  206. text_type = unicode # noqa
  207. int_types = int, long # noqa
  208. long_type = long # noqa
  209. callable = callable # noqa
  210. cmp = cmp # noqa
  211. reduce = reduce # noqa
  212. b64encode = base64.b64encode
  213. b64decode = base64.b64decode
  214. itertools_filterfalse = itertools.ifilterfalse
  215. itertools_filter = itertools.ifilter
  216. itertools_imap = itertools.imap
  217. def b(s):
  218. return s
  219. def exec_(func_text, globals_, lcl=None):
  220. if lcl is None:
  221. exec("exec func_text in globals_")
  222. else:
  223. exec("exec func_text in globals_, lcl")
  224. def iterbytes(buf):
  225. return (ord(byte) for byte in buf)
  226. def import_(*args):
  227. if len(args) == 4:
  228. args = args[0:3] + ([str(arg) for arg in args[3]],)
  229. return __import__(*args)
  230. def print_(*args, **kwargs):
  231. fp = kwargs.pop("file", sys.stdout)
  232. if fp is None:
  233. return
  234. for arg in enumerate(args):
  235. if not isinstance(arg, basestring): # noqa
  236. arg = str(arg)
  237. fp.write(arg)
  238. def u(s):
  239. # this differs from what six does, which doesn't support non-ASCII
  240. # strings - we only use u() with
  241. # literal source strings, and all our source files with non-ascii
  242. # in them (all are tests) are utf-8 encoded.
  243. return unicode(s, "utf-8") # noqa
  244. def ue(s):
  245. return unicode(s, "unicode_escape") # noqa
  246. def decode_backslashreplace(text, encoding):
  247. try:
  248. return text.decode(encoding)
  249. except UnicodeDecodeError:
  250. # regular "backslashreplace" for an incompatible encoding raises:
  251. # "TypeError: don't know how to handle UnicodeDecodeError in
  252. # error callback"
  253. return repr(text)[1:-1].decode()
  254. def safe_bytestring(text):
  255. # py2k only
  256. if not isinstance(text, string_types):
  257. return unicode(text).encode( # noqa: F821
  258. "ascii", errors="backslashreplace"
  259. )
  260. elif isinstance(text, unicode): # noqa: F821
  261. return text.encode("ascii", errors="backslashreplace")
  262. else:
  263. return text
  264. exec(
  265. "def raise_(exception, with_traceback=None, replace_context=None, "
  266. "from_=False):\n"
  267. " if with_traceback:\n"
  268. " raise type(exception), exception, with_traceback\n"
  269. " else:\n"
  270. " raise exception\n"
  271. )
  272. TYPE_CHECKING = False
  273. def _qualname(meth):
  274. """return __qualname__ equivalent for a method on a class"""
  275. for cls in meth.im_class.__mro__:
  276. if meth.__name__ in cls.__dict__:
  277. break
  278. else:
  279. return meth.__name__
  280. return "%s.%s" % (cls.__name__, meth.__name__)
  281. if py3k:
  282. def _formatannotation(annotation, base_module=None):
  283. """vendored from python 3.7"""
  284. if getattr(annotation, "__module__", None) == "typing":
  285. return repr(annotation).replace("typing.", "")
  286. if isinstance(annotation, type):
  287. if annotation.__module__ in ("builtins", base_module):
  288. return annotation.__qualname__
  289. return annotation.__module__ + "." + annotation.__qualname__
  290. return repr(annotation)
  291. def inspect_formatargspec(
  292. args,
  293. varargs=None,
  294. varkw=None,
  295. defaults=None,
  296. kwonlyargs=(),
  297. kwonlydefaults={},
  298. annotations={},
  299. formatarg=str,
  300. formatvarargs=lambda name: "*" + name,
  301. formatvarkw=lambda name: "**" + name,
  302. formatvalue=lambda value: "=" + repr(value),
  303. formatreturns=lambda text: " -> " + text,
  304. formatannotation=_formatannotation,
  305. ):
  306. """Copy formatargspec from python 3.7 standard library.
  307. Python 3 has deprecated formatargspec and requested that Signature
  308. be used instead, however this requires a full reimplementation
  309. of formatargspec() in terms of creating Parameter objects and such.
  310. Instead of introducing all the object-creation overhead and having
  311. to reinvent from scratch, just copy their compatibility routine.
  312. Ultimately we would need to rewrite our "decorator" routine completely
  313. which is not really worth it right now, until all Python 2.x support
  314. is dropped.
  315. """
  316. kwonlydefaults = kwonlydefaults or {}
  317. annotations = annotations or {}
  318. def formatargandannotation(arg):
  319. result = formatarg(arg)
  320. if arg in annotations:
  321. result += ": " + formatannotation(annotations[arg])
  322. return result
  323. specs = []
  324. if defaults:
  325. firstdefault = len(args) - len(defaults)
  326. for i, arg in enumerate(args):
  327. spec = formatargandannotation(arg)
  328. if defaults and i >= firstdefault:
  329. spec = spec + formatvalue(defaults[i - firstdefault])
  330. specs.append(spec)
  331. if varargs is not None:
  332. specs.append(formatvarargs(formatargandannotation(varargs)))
  333. else:
  334. if kwonlyargs:
  335. specs.append("*")
  336. if kwonlyargs:
  337. for kwonlyarg in kwonlyargs:
  338. spec = formatargandannotation(kwonlyarg)
  339. if kwonlydefaults and kwonlyarg in kwonlydefaults:
  340. spec += formatvalue(kwonlydefaults[kwonlyarg])
  341. specs.append(spec)
  342. if varkw is not None:
  343. specs.append(formatvarkw(formatargandannotation(varkw)))
  344. result = "(" + ", ".join(specs) + ")"
  345. if "return" in annotations:
  346. result += formatreturns(formatannotation(annotations["return"]))
  347. return result
  348. else:
  349. from inspect import formatargspec as _inspect_formatargspec
  350. def inspect_formatargspec(*spec, **kw):
  351. # convert for a potential FullArgSpec from compat.getfullargspec()
  352. return _inspect_formatargspec(*spec[0:4], **kw) # noqa
  353. # Fix deprecation of accessing ABCs straight from collections module
  354. # (which will stop working in 3.8).
  355. if py3k:
  356. import collections.abc as collections_abc
  357. else:
  358. import collections as collections_abc # noqa
  359. if py37:
  360. import dataclasses
  361. def dataclass_fields(cls):
  362. """Return a sequence of all dataclasses.Field objects associated
  363. with a class."""
  364. if dataclasses.is_dataclass(cls):
  365. return dataclasses.fields(cls)
  366. else:
  367. return []
  368. def local_dataclass_fields(cls):
  369. """Return a sequence of all dataclasses.Field objects associated with
  370. a class, excluding those that originate from a superclass."""
  371. if dataclasses.is_dataclass(cls):
  372. super_fields = set()
  373. for sup in cls.__bases__:
  374. super_fields.update(dataclass_fields(sup))
  375. return [
  376. f for f in dataclasses.fields(cls) if f not in super_fields
  377. ]
  378. else:
  379. return []
  380. else:
  381. def dataclass_fields(cls):
  382. return []
  383. def local_dataclass_fields(cls):
  384. return []
  385. def raise_from_cause(exception, exc_info=None):
  386. r"""legacy. use raise\_()"""
  387. if exc_info is None:
  388. exc_info = sys.exc_info()
  389. exc_type, exc_value, exc_tb = exc_info
  390. cause = exc_value if exc_value is not exception else None
  391. reraise(type(exception), exception, tb=exc_tb, cause=cause)
  392. def reraise(tp, value, tb=None, cause=None):
  393. r"""legacy. use raise\_()"""
  394. raise_(value, with_traceback=tb, from_=cause)
  395. def with_metaclass(meta, *bases, **kw):
  396. """Create a base class with a metaclass.
  397. Drops the middle class upon creation.
  398. Source: https://lucumr.pocoo.org/2013/5/21/porting-to-python-3-redux/
  399. """
  400. class metaclass(meta):
  401. __call__ = type.__call__
  402. __init__ = type.__init__
  403. def __new__(cls, name, this_bases, d):
  404. if this_bases is None:
  405. cls = type.__new__(cls, name, (), d)
  406. else:
  407. cls = meta(name, bases, d)
  408. if hasattr(cls, "__init_subclass__") and hasattr(
  409. cls.__init_subclass__, "__func__"
  410. ):
  411. cls.__init_subclass__.__func__(cls, **kw)
  412. return cls
  413. return metaclass("temporary_class", None, {})
  414. if py3k:
  415. from datetime import timezone
  416. else:
  417. from datetime import datetime
  418. from datetime import timedelta
  419. from datetime import tzinfo
  420. class timezone(tzinfo):
  421. """Minimal port of python 3 timezone object"""
  422. __slots__ = "_offset"
  423. def __init__(self, offset):
  424. if not isinstance(offset, timedelta):
  425. raise TypeError("offset must be a timedelta")
  426. if not self._minoffset <= offset <= self._maxoffset:
  427. raise ValueError(
  428. "offset must be a timedelta "
  429. "strictly between -timedelta(hours=24) and "
  430. "timedelta(hours=24)."
  431. )
  432. self._offset = offset
  433. def __eq__(self, other):
  434. if type(other) != timezone:
  435. return False
  436. return self._offset == other._offset
  437. def __hash__(self):
  438. return hash(self._offset)
  439. def __repr__(self):
  440. return "sqlalchemy.util.%s(%r)" % (
  441. self.__class__.__name__,
  442. self._offset,
  443. )
  444. def __str__(self):
  445. return self.tzname(None)
  446. def utcoffset(self, dt):
  447. return self._offset
  448. def tzname(self, dt):
  449. return self._name_from_offset(self._offset)
  450. def dst(self, dt):
  451. return None
  452. def fromutc(self, dt):
  453. if isinstance(dt, datetime):
  454. if dt.tzinfo is not self:
  455. raise ValueError("fromutc: dt.tzinfo " "is not self")
  456. return dt + self._offset
  457. raise TypeError(
  458. "fromutc() argument must be a datetime instance" " or None"
  459. )
  460. @staticmethod
  461. def _timedelta_to_microseconds(timedelta):
  462. """backport of timedelta._to_microseconds()"""
  463. return (
  464. timedelta.days * (24 * 3600) + timedelta.seconds
  465. ) * 1000000 + timedelta.microseconds
  466. @staticmethod
  467. def _divmod_timedeltas(a, b):
  468. """backport of timedelta.__divmod__"""
  469. q, r = divmod(
  470. timezone._timedelta_to_microseconds(a),
  471. timezone._timedelta_to_microseconds(b),
  472. )
  473. return q, timedelta(0, 0, r)
  474. @staticmethod
  475. def _name_from_offset(delta):
  476. if not delta:
  477. return "UTC"
  478. if delta < timedelta(0):
  479. sign = "-"
  480. delta = -delta
  481. else:
  482. sign = "+"
  483. hours, rest = timezone._divmod_timedeltas(
  484. delta, timedelta(hours=1)
  485. )
  486. minutes, rest = timezone._divmod_timedeltas(
  487. rest, timedelta(minutes=1)
  488. )
  489. result = "UTC%s%02d:%02d" % (sign, hours, minutes)
  490. if rest.seconds:
  491. result += ":%02d" % (rest.seconds,)
  492. if rest.microseconds:
  493. result += ".%06d" % (rest.microseconds,)
  494. return result
  495. _maxoffset = timedelta(hours=23, minutes=59)
  496. _minoffset = -_maxoffset
  497. timezone.utc = timezone(timedelta(0))