Skip to content

Commit

Permalink
optimize import module
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Nov 8, 2024
1 parent c725ca9 commit 0e71dcd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/__globals__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

from httpout import worker, context # noqa: F401
from httpout import ne # noqa: F401
from httpout import app # noqa: F401
2 changes: 1 addition & 1 deletion httpout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2024 nggit

__version__ = '0.0.46'
__version__ = '0.0.47'
__all__ = ('HTTPOut',)

from .httpout import HTTPOut # noqa: E402
37 changes: 22 additions & 15 deletions httpout/httpout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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__
Expand Down Expand Up @@ -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__']
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_import_error(self):
)
self.assertEqual(
body,
b'5A\r\n<ul><li>ImportError: cannot import name &#x27;ne&#x27; '
b'5B\r\n<ul><li>ImportError: cannot import name &#x27;app&#x27; '
b'from &#x27;httpout&#x27;</li></ul>\n\r\n0\r\n\r\n'
)

Expand Down

0 comments on commit 0e71dcd

Please sign in to comment.