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

Avoid using __import__ and eval #2043

Merged
merged 5 commits into from
May 22, 2019
Merged

Conversation

c-bata
Copy link
Contributor

@c-bata c-bata commented May 20, 2019

See https://docs.python.org/3/library/functions.html#__import__

$ python -V
Python 3.7.2
$ gunicorn examples.echo:app
[2019-05-20 23:09:16 +0900] [26697] [INFO] Starting gunicorn 19.9.0
[2019-05-20 23:09:16 +0900] [26697] [INFO] Listening at: http://127.0.0.1:8000 (26697)
[2019-05-20 23:09:16 +0900] [26697] [INFO] Using worker: sync
[2019-05-20 23:09:16 +0900] [26700] [INFO] Booting worker with pid: 26700

gunicorn/util.py Outdated
else:
__import__(module)
mod = sys.modules[module]
except (ImportError, ModuleNotFoundError):
Copy link
Contributor Author

@c-bata c-bata May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message when specified filepath to python file.

$ python -V
Python 3.7.2
$ gunicorn examples/echo.py:app
[2019-05-20 23:21:06 +0900] [27295] [INFO] Starting gunicorn 19.9.0
[2019-05-20 23:21:06 +0900] [27295] [INFO] Listening at: http://127.0.0.1:8000 (27295)
[2019-05-20 23:21:06 +0900] [27295] [INFO] Using worker: sync
[2019-05-20 23:21:06 +0900] [27298] [INFO] Booting worker with pid: 27298
[2019-05-20 23:21:06 +0900] [27298] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/util.py", line 368, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'examples/echo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/arbiter.py", line 585, in spawn_worker
    worker.init_process()
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/workers/base.py", line 133, in init_process
    self.load_wsgi()
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/workers/base.py", line 142, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/app/base.py", line 65, in wsgi
    self.callable = self.load()
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/Users/c-bata/src/github.com/benoitc/gunicorn/gunicorn/util.py", line 372, in import_app
    raise ModuleNotFoundError(msg % (module.rsplit(".", 1)[0], obj))
ModuleNotFoundError: Failed to find application, did you mean 'examples/echo:app'?
[2019-05-20 23:21:06 +0900] [27298] [INFO] Worker exiting (pid: 27298)
[2019-05-20 23:21:07 +0900] [27295] [INFO] Shutting down: Master
[2019-05-20 23:21:07 +0900] [27295] [INFO] Reason: Worker failed to boot.

gunicorn/util.py Outdated
__import__(module)
except ImportError:
if importlib:
mod = importlib.import_module(module)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ python -V
Python 3.7.2
$ gunicorn examples.echo:app
[2019-05-20 23:09:16 +0900] [26697] [INFO] Starting gunicorn 19.9.0
[2019-05-20 23:09:16 +0900] [26697] [INFO] Listening at: http://127.0.0.1:8000 (26697)
[2019-05-20 23:09:16 +0900] [26697] [INFO] Using worker: sync
[2019-05-20 23:09:16 +0900] [26700] [INFO] Booting worker with pid: 26700

@c-bata c-bata changed the title Avoid using __import__, use importlib instead Avoid using __import__ and eval May 20, 2019
app = eval(obj, vars(mod))
except NameError:
app = getattr(mod, obj)
except AttributeError:
Copy link
Contributor Author

@c-bata c-bata May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message when failing to find application object.

$ python -V
Python 3.7.2
$ gunicorn examples.echo:foo
[2019-05-20 23:19:04 +0900] [27282] [INFO] Starting gunicorn 19.9.0
[2019-05-20 23:19:04 +0900] [27282] [INFO] Listening at: http://127.0.0.1:8000 (27282)
[2019-05-20 23:19:04 +0900] [27282] [INFO] Using worker: sync
[2019-05-20 23:19:04 +0900] [27285] [INFO] Booting worker with pid: 27285
Failed to find application object 'foo' in 'examples.echo'
[2019-05-20 23:19:04 +0900] [27285] [INFO] Worker exiting (pid: 27285)
[2019-05-20 23:19:05 +0900] [27282] [INFO] Shutting down: Master
[2019-05-20 23:19:05 +0900] [27282] [INFO] Reason: App failed to load.

Copy link
Collaborator

@tilgovi tilgovi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this looks good! Thanks!

gunicorn/util.py Outdated
except ImportError:
# ModuleNotFoundError (a subclass of ImportError) is new in version 3.6.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be dropped.

Copy link
Contributor Author

@c-bata c-bata May 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to tell ModuleNotFound exception is also handled in this clause. But It's ok to remove this comment. Should I remove this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not necessary to have the comment. There's only one statement in the try clause, so even if the error is more general than needed on Python 3.6 there's not much need to justify it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I've just removed this line.

@berkerpeksag berkerpeksag merged commit 3701ad9 into benoitc:master May 22, 2019
@berkerpeksag
Copy link
Collaborator

Nice cleanup, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants