diff options
author | Federico Caselli <cfederico87@gmail.com> | 2020-03-07 19:17:07 +0100 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-07 17:50:45 -0500 |
commit | eda6dbbf387def2063d1b6719b64b20f9e7f2ab4 (patch) | |
tree | 4af5f41edfac169b0fdc6d6cab0fce4e8bf776cf /test/base/test_utils.py | |
parent | 851fb8f5a661c66ee76308181118369c8c4df9e0 (diff) | |
download | sqlalchemy-eda6dbbf387def2063d1b6719b64b20f9e7f2ab4.tar.gz |
Simplified module pre-loading strategy and made it linter friendly
Introduced a modules registry to register modules that should be lazily loaded
in the package init. This ensures that they are in the system module cache,
avoiding potential thread safety issues as when importing them directly
in the function that uses them. The module registry is used to obtain
these modules directly, ensuring that the all the lazily loaded modules
are resolved at the proper time
This replaces dependency_for decorator and the dependencies decorator logic,
removing the need to pass the resolved modules as arguments of the
decodated functions and removes possible errors caused by linters.
Fixes: #4689
Fixes: #4656
Change-Id: I2e291eba4297867fc0ddb5d875b9f7af34751d01
Diffstat (limited to 'test/base/test_utils.py')
-rw-r--r-- | test/base/test_utils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/base/test_utils.py b/test/base/test_utils.py index 183e157e5..4392df013 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -3,6 +3,7 @@ import copy import datetime import inspect +import sys from sqlalchemy import exc from sqlalchemy import sql @@ -3209,3 +3210,35 @@ class TimezoneTest(fixtures.TestBase): repr(timezone(datetime.timedelta(hours=5))), "sqlalchemy.util.timezone(%r)" % (datetime.timedelta(hours=5)), ) + + +class TestModuleRegistry(fixtures.TestBase): + def test_modules_are_loaded(self): + to_restore = [] + for m in ("xml.dom", "wsgiref.simple_server"): + to_restore.append((m, sys.modules.pop(m, None))) + try: + mr = langhelpers._ModuleRegistry() + + ret = mr.preload_module( + "xml.dom", "wsgiref.simple_server", "sqlalchemy.sql.util" + ) + o = object() + is_(ret(o), o) + + is_false(hasattr(mr, "xml_dom")) + mr.import_prefix("xml") + is_true("xml.dom" in sys.modules) + is_(sys.modules["xml.dom"], mr.xml_dom) + + is_true("wsgiref.simple_server" not in sys.modules) + mr.import_prefix("wsgiref") + is_true("wsgiref.simple_server" in sys.modules) + is_(sys.modules["wsgiref.simple_server"], mr.wsgiref_simple_server) + + mr.import_prefix("sqlalchemy") + is_(sys.modules["sqlalchemy.sql.util"], mr.sql_util) + finally: + for name, mod in to_restore: + if mod is not None: + sys.modules[name] = mod |