modernize the way the config module is loaded

This change load the module  as suggested in the Python docs : https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly which add the `__file__` attribute back and others possibly missing.

This change remove the support of python 3.4
This commit is contained in:
benoitc 2019-11-22 11:07:25 +01:00 committed by Benoit Chesneau
parent 441977f57c
commit ad4ff8cb3c

View File

@ -2,7 +2,7 @@
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
import importlib.machinery
import importlib.util
import os
import sys
import traceback
@ -97,9 +97,10 @@ class Application(BaseApplication):
try:
module_name = '__config__'
mod = types.ModuleType(module_name)
loader = importlib.machinery.SourceFileLoader(module_name, filename)
loader.exec_module(mod)
spec = importlib.util.spec_from_file_location(module_name, filename)
mod = importlib.util.module_from_spec(spec)
sys.modules[module_name] = mod
spec.loader.exec_module(mod)
except Exception:
print("Failed to read config file: %s" % filename, file=sys.stderr)
traceback.print_exc()