From 0e71dcdc2c8b5d055e64211c7120726ec4250f46 Mon Sep 17 00:00:00 2001 From: nggit <12218311+nggit@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:57:55 +0700 Subject: [PATCH] optimize import module --- examples/__globals__.py | 4 ++-- examples/import.py | 2 +- httpout/__init__.py | 2 +- httpout/httpout.py | 37 ++++++++++++++++++++++--------------- tests/test_http.py | 2 +- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/examples/__globals__.py b/examples/__globals__.py index d970596..9893ba9 100644 --- a/examples/__globals__.py +++ b/examples/__globals__.py @@ -1,10 +1,10 @@ import __main__ -from httpout import app, __globals__ +from httpout import app, modules # just for testing. the only thing that matters here is the `app` :) -assert __main__ is __globals__ +assert __main__ is modules['__globals__'] # in routes it should be available as `__globals__.counter` # you can't access this from inside the middleware, btw diff --git a/examples/import.py b/examples/import.py index b1e17b8..2a08db3 100644 --- a/examples/import.py +++ b/examples/import.py @@ -1,3 +1,3 @@ from httpout import worker, context # noqa: F401 -from httpout import ne # noqa: F401 +from httpout import app # noqa: F401 diff --git a/httpout/__init__.py b/httpout/__init__.py index 50008f1..95c57a5 100644 --- a/httpout/__init__.py +++ b/httpout/__init__.py @@ -1,6 +1,6 @@ # Copyright (c) 2024 nggit -__version__ = '0.0.46' +__version__ = '0.0.47' __all__ = ('HTTPOut',) from .httpout import HTTPOut # noqa: E402 diff --git a/httpout/httpout.py b/httpout/httpout.py index 790c068..4808a93 100644 --- a/httpout/httpout.py +++ b/httpout/httpout.py @@ -45,15 +45,20 @@ async def _on_worker_start(self, **worker): # provides __globals__, a worker-level context worker['__globals__'] = new_module('__globals__') + worker['modules'] = {'__globals__': worker['__globals__']} def wait(coro, timeout=None): return asyncio.run_coroutine_threadsafe(coro, loop).result(timeout) def load_module(name, globals, level=0): - if (globals['__name__'] != '__globals__' and - name in globals['__main__'].__server__['modules']): + if globals['__name__'] == '__globals__': + modules = worker['modules'] + else: + modules = globals['__main__'].__server__['modules'] + + if name in modules: # already imported - return globals['__main__'].__server__['modules'][name] + return modules[name] module = new_module(name, level, document_root) @@ -66,9 +71,10 @@ def load_module(name, globals, level=0): module.print = globals['__main__'].print module.run = globals['__main__'].run module.wait = wait - globals['__main__'].__server__['modules'][name] = module + modules[name] = module exec_module(module) + return module py_import = builtins.__import__ @@ -108,7 +114,7 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0): if name == 'httpout' or name.startswith('httpout.'): if globals['__name__'] == '__globals__': - module = worker['__globals__'] + module = worker['modules'][globals['__name__']] else: module = globals['__main__'].__server__['modules'][ globals['__name__'] @@ -121,19 +127,20 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0): if child in module.__dict__: continue - if (globals['__name__'] == '__globals__' or - child not in module.__server__): - if child not in worker: - raise ImportError( - f'cannot import name \'{child}\' ' - f'from \'{name}\'' - ) - - module.__dict__[child] = worker[child] - else: + if (globals['__name__'] != '__globals__' and + child in module.__server__): module.__dict__[child] = module.__server__[ child ] + elif child in worker and ( + child != 'app' or + globals['__name__'] == '__globals__'): + module.__dict__[child] = worker[child] + else: + raise ImportError( + f'cannot import name \'{child}\' ' + f'from \'{name}\'' + ) return module diff --git a/tests/test_http.py b/tests/test_http.py index 72a471f..69ed170 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -81,7 +81,7 @@ def test_import_error(self): ) self.assertEqual( body, - b'5A\r\n