summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/langhelpers.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-08-04 15:03:50 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-08-04 15:03:50 -0400
commite0237aac2ba992a5c31206fea52f55969db65ba0 (patch)
tree0cd40a9905ada7ff8c4823c579fdfcc7dc8ef1e9 /lib/sqlalchemy/util/langhelpers.py
parentb79a5c25bc131bfdeaeadbf41ed1253e93161bee (diff)
downloadsqlalchemy-e0237aac2ba992a5c31206fea52f55969db65ba0.tar.gz
- don't need resolve, don't need import for this. just look in sys.modules,
since we are dealing with cycles in any case.
Diffstat (limited to 'lib/sqlalchemy/util/langhelpers.py')
-rw-r--r--lib/sqlalchemy/util/langhelpers.py58
1 files changed, 16 insertions, 42 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index ef1ff881d..430c630a8 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -688,62 +688,36 @@ class importlater(object):
except evaluted upon attribute access to "somesubmod".
- importlater() currently requires that resolve_all() be
- called, typically at the bottom of a package's __init__.py.
- This is so that __import__ still called only at
- module import time, and not potentially within
- a non-main thread later on.
+ importlater() relies on sys.modules being populated with the
+ target package, and the target package containing the target module,
+ by time the attribute is evaulated.
"""
- _unresolved = set()
-
- def __init__(self, path, addtl=None):
+ def __init__(self, path, addtl):
self._il_path = path
self._il_addtl = addtl
- importlater._unresolved.add(self)
-
- @classmethod
- def resolve_all(cls):
- for m in list(importlater._unresolved):
- m._resolve()
@property
def _full_path(self):
- if self._il_addtl:
- return self._il_path + "." + self._il_addtl
- else:
- return self._il_path
+ return self._il_path + "." + self._il_addtl
@memoized_property
def module(self):
- if self in importlater._unresolved:
- raise ImportError(
- "importlater.resolve_all() hasn't "
- "been called (this is %s %s)"
- % (self._il_path, self._il_addtl))
-
- m = self._initial_import
- if self._il_addtl:
- m = getattr(m, self._il_addtl)
- else:
- for token in self._il_path.split(".")[1:]:
- m = getattr(m, token)
- return m
-
- def _resolve(self):
- importlater._unresolved.discard(self)
- if self._il_addtl:
- self._initial_import = compat.import_(
- self._il_path, globals(), locals(),
- [self._il_addtl])
+ try:
+ m = sys.modules[self._il_path]
+ except KeyError:
+ raise KeyError("Module %s hasn't been installed" % self._il_path)
else:
- self._initial_import = compat.import_(self._il_path)
+ try:
+ return getattr(m, self._il_addtl)
+ except AttributeError:
+ raise KeyError(
+ "Module %s hasn't been installed into %s" %
+ (self._il_addtl, self._il_path)
+ )
def __getattr__(self, key):
- if key == 'module':
- raise ImportError("Could not resolve module %s"
- % self._full_path)
try:
attr = getattr(self.module, key)
except AttributeError: