1 # Copyright (c) 2010-2012 extras developers. See LICENSE for details.
3 """Extensions to the Python standard library."""
13 # same format as sys.version_info: "A tuple containing the five components of
14 # the version number: major, minor, micro, releaselevel, and serial. All
15 # values except releaselevel are integers; the release level is 'alpha',
16 # 'beta', 'candidate', or 'final'. The version_info value corresponding to the
17 # Python version 2.0 is (2, 0, 0, 'final', 0)." Additionally we use a
18 # releaselevel of 'dev' for unreleased under-development code.
20 # If the releaselevel is 'alpha' then the major/minor/micro components are not
21 # established at this point, and setup.py will use a version of next-$(revno).
22 # If the releaselevel is 'final', then the tarball will be major.minor.micro.
23 # Otherwise it is major.minor.micro~$(revno).
25 __version__ = (0, 0, 1, 'final', 0)
28 def try_import(name, alternative=None, error_callback=None):
29 """Attempt to import ``name``. If it fails, return ``alternative``.
31 When supporting multiple versions of Python or optional dependencies, it
32 is useful to be able to try to import a module.
34 :param name: The name of the object to import, e.g. ``os.path`` or
36 :param alternative: The value to return if no module can be imported.
38 :param error_callback: If non-None, a callable that is passed the ImportError
39 when the module cannot be loaded.
41 module_segments = name.split('.')
43 while module_segments:
44 module_name = '.'.join(module_segments)
46 module = __import__(module_name)
48 last_error = sys.exc_info()[1]
54 if last_error is not None and error_callback is not None:
55 error_callback(last_error)
57 nonexistent = object()
58 for segment in name.split('.')[1:]:
59 module = getattr(module, segment, nonexistent)
60 if module is nonexistent:
61 if last_error is not None and error_callback is not None:
62 error_callback(last_error)
67 _RAISE_EXCEPTION = object()
68 def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None):
69 """Attempt to import modules.
71 Tries to import the first module in ``module_names``. If it can be
72 imported, we return it. If not, we go on to the second module and try
73 that. The process continues until we run out of modules to try. If none
74 of the modules can be imported, either raise an exception or return the
75 provided ``alternative`` value.
77 :param module_names: A sequence of module names to try to import.
78 :param alternative: The value to return if no module can be imported.
79 If unspecified, we raise an ImportError.
80 :param error_callback: If None, called with the ImportError for *each*
81 module that fails to load.
82 :raises ImportError: If none of the modules can be imported and no
83 alternative value was specified.
85 module_names = list(module_names)
86 for module_name in module_names:
87 module = try_import(module_name, error_callback=error_callback)
90 if alternative is _RAISE_EXCEPTION:
92 "Could not import any of: %s" % ', '.join(module_names))
96 def safe_hasattr(obj, attr, _marker=object()):
97 """Does 'obj' have an attribute 'attr'?
99 Use this rather than built-in hasattr, as the built-in swallows exceptions
100 in some versions of Python and behaves unpredictably with respect to
103 return getattr(obj, attr, _marker) is not _marker