Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix when __globals__.py doesn't exist #49

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.53'
__version__ = '0.0.54'
__all__ = ('HTTPOut',)

from .httpout import HTTPOut # noqa: E402
14 changes: 6 additions & 8 deletions httpout/httpout.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ async def _on_worker_start(self, **worker):
sys.path.insert(0, document_root)

# provides __globals__, a worker-level context
worker['__globals__'] = new_module('__globals__')
module = new_module('__globals__')
worker['__globals__'] = module or ModuleType('__globals__')
worker['modules'] = {'__globals__': worker['__globals__']}
py_import = builtins.__import__

def wait(coro, timeout=None):
return asyncio.run_coroutine_threadsafe(coro, loop).result(timeout)
Expand Down Expand Up @@ -77,8 +79,6 @@ def load_module(name, globals, level=0):

return module

py_import = builtins.__import__

def ho_import(name, globals=None, locals=None, fromlist=(), level=0):
if (name not in sys.builtin_module_names and
globals is not None and '__file__' in globals and
Expand Down Expand Up @@ -147,17 +147,15 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0):
return py_import(name, globals, locals, fromlist, level)

builtins.__import__ = ho_import
builtins.__globals__ = worker['__globals__']

g.wait = wait
g.caches = {}
g.executor = MultiThreadExecutor(thread_pool_size)
g.executor.start()

if worker['__globals__']:
builtins.__globals__ = worker['__globals__']
exec_module(worker['__globals__'])
else:
builtins.__globals__ = ModuleType('__globals__')
if module:
exec_module(module)

async def _on_worker_stop(self, **worker):
g = worker['globals']
Expand Down
Loading