Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/patsy/compat.py : 44%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of Patsy
2# Copyright (C) 2012 Nathaniel Smith <njs@pobox.com>
3# See file LICENSE.txt for license information.
5# This file contains compatibility code for supporting old versions of Python
6# and numpy. (If we can concentrate it here, hopefully it'll make it easier to
7# get rid of weird hacks once we drop support for old versions).
9##### Numpy
11import os
12# To force use of the compat code, set this env var to a non-empty value:
13optional_dep_ok = not os.environ.get("PATSY_AVOID_OPTIONAL_DEPENDENCIES")
15##### Python standard library
17# The Python license requires that all derivative works contain a "brief
18# summary of the changes made to Python". Both for license compliance, and for
19# our own sanity, therefore, please add a note at the top of any snippets you
20# add here explaining their provenance, any changes made, and what versions of
21# Python require them:
23# OrderedDict is only available in Python 2.7+. compat_ordereddict.py has
24# comments at the top.
25import collections
26if optional_dep_ok and hasattr(collections, "OrderedDict"):
27 from collections import OrderedDict
28else:
29 from patsy.compat_ordereddict import OrderedDict
31# 'raise from' available in Python 3+
32import sys
33from patsy import PatsyError
34def call_and_wrap_exc(msg, origin, f, *args, **kwargs):
35 try:
36 return f(*args, **kwargs)
37 except Exception as e:
38 if sys.version_info[0] >= 3:
39 new_exc = PatsyError("%s: %s: %s"
40 % (msg, e.__class__.__name__, e),
41 origin)
42 # Use 'exec' to hide this syntax from the Python 2 parser:
43 exec("raise new_exc from e")
44 else:
45 # In python 2, we just let the original exception escape -- better
46 # than destroying the traceback. But if it's a PatsyError, we can
47 # at least set the origin properly.
48 if isinstance(e, PatsyError):
49 e.set_origin(origin)
50 raise