PEP 489: Tweak code formatting.

This commit is contained in:
Berker Peksag 2015-05-21 17:10:14 +03:00
parent 0751f33dab
commit e597818e47
1 changed files with 110 additions and 100 deletions

View File

@ -191,136 +191,146 @@ are left out, and C code is presented with a concise Python-like syntax.
The framework that calls the importers is explained in PEP 451
[#pep-0451-loading]_.
importlib/_bootstrap.py::
importlib/_bootstrap.py:
class BuiltinImporter:
def create_module(self, spec):
module = _imp.create_builtin(spec)
::
def exec_module(self, module):
_imp.exec_dynamic(module)
class BuiltinImporter:
def create_module(self, spec):
module = _imp.create_builtin(spec)
def load_module(self, name):
# use a backwards compatibility shim
_load_module_shim(self, name)
def exec_module(self, module):
_imp.exec_dynamic(module)
importlib/_bootstrap_external.py::
def load_module(self, name):
# use a backwards compatibility shim
_load_module_shim(self, name)
class ExtensionFileLoader:
def create_module(self, spec):
module = _imp.create_dynamic(spec)
importlib/_bootstrap_external.py:
def exec_module(self, module):
_imp.exec_dynamic(module)
::
def load_module(self, name):
# use a backwards compatibility shim
_load_module_shim(self, name)
class ExtensionFileLoader:
def create_module(self, spec):
module = _imp.create_dynamic(spec)
Python/import.c (the _imp module)::
def exec_module(self, module):
_imp.exec_dynamic(module)
def create_dynamic(spec):
name = spec.name
path = spec.origin
def load_module(self, name):
# use a backwards compatibility shim
_load_module_shim(self, name)
# Find an already loaded module that used single-phase init.
# For multi-phase initialization, mod is NULL, so a new module
# is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
Python/import.c (the _imp module):
return _PyImport_LoadDynamicModuleWithSpec(spec)
::
def exec_dynamic(module):
if not isinstance(module, types.ModuleType):
# non-modules are skipped -- PyModule_GetDef fails on them
return
def create_dynamic(spec):
name = spec.name
path = spec.origin
def = PyModule_GetDef(module)
state = PyModule_GetState(module)
if state is NULL:
PyModule_ExecDef(module, def)
# Find an already loaded module that used single-phase init.
# For multi-phase initialization, mod is NULL, so a new module
# is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
def create_builtin(spec):
name = spec.name
return _PyImport_LoadDynamicModuleWithSpec(spec)
# Find an already loaded module that used single-phase init.
# For multi-phase initialization, mod is NULL, so a new module
# is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
def exec_dynamic(module):
if not isinstance(module, types.ModuleType):
# non-modules are skipped -- PyModule_GetDef fails on them
return
for initname, initfunc in PyImport_Inittab:
if name == initname:
m = initfunc()
if isinstance(m, PyModuleDef):
def = m
return PyModule_FromDefAndSpec(def, spec)
else:
# fall back to single-phase initialization
module = m
_PyImport_FixupExtensionObject(module, name, name)
return module
def = PyModule_GetDef(module)
state = PyModule_GetState(module)
if state is NULL:
PyModule_ExecDef(module, def)
Python/importdl.c::
def create_builtin(spec):
name = spec.name
def _PyImport_LoadDynamicModuleWithSpec(spec):
path = spec.origin
package, dot, name = spec.name.rpartition('.')
# Find an already loaded module that used single-phase init.
# For multi-phase initialization, mod is NULL, so a new module
# is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
# see the "Non-ASCII module names" section for export_hook_name
hook_name = export_hook_name(name)
for initname, initfunc in PyImport_Inittab:
if name == initname:
m = initfunc()
if isinstance(m, PyModuleDef):
def = m
return PyModule_FromDefAndSpec(def, spec)
else:
# fall back to single-phase initialization
module = m
_PyImport_FixupExtensionObject(module, name, name)
return module
# call platform-specific function for loading exported function
# from shared library
exportfunc = _find_shared_funcptr(hook_name, path)
Python/importdl.c:
m = exportfunc()
if isinstance(m, PyModuleDef):
def = m
return PyModule_FromDefAndSpec(def, spec)
::
module = m
def _PyImport_LoadDynamicModuleWithSpec(spec):
path = spec.origin
package, dot, name = spec.name.rpartition('.')
# fall back to single-phase initialization
....
# see the "Non-ASCII module names" section for export_hook_name
hook_name = export_hook_name(name)
Objects/moduleobject.c::
# call platform-specific function for loading exported function
# from shared library
exportfunc = _find_shared_funcptr(hook_name, path)
def PyModule_FromDefAndSpec(def, spec):
name = spec.name
create = None
for slot, value in def.m_slots:
if slot == Py_mod_create:
create = value
if create:
m = create(spec, def)
else:
m = PyModule_New(name)
m = exportfunc()
if isinstance(m, PyModuleDef):
def = m
return PyModule_FromDefAndSpec(def, spec)
if isinstance(m, types.ModuleType):
m.md_state = None
m.md_def = def
module = m
if def.m_methods:
PyModule_AddFunctions(m, def.m_methods)
if def.m_doc:
PyModule_SetDocString(m, def.m_doc)
# fall back to single-phase initialization
....
def PyModule_ExecDef(module, def):
if isinstance(module, types.module_type):
if module.md_state is NULL:
# allocate a block of zeroed-out memory
module.md_state = _alloc(module.md_size)
Objects/moduleobject.c:
if def.m_slots is NULL:
return
::
for slot, value in def.m_slots:
if slot == Py_mod_exec:
value(module)
def PyModule_FromDefAndSpec(def, spec):
name = spec.name
create = None
for slot, value in def.m_slots:
if slot == Py_mod_create:
create = value
if create:
m = create(spec, def)
else:
m = PyModule_New(name)
if isinstance(m, types.ModuleType):
m.md_state = None
m.md_def = def
if def.m_methods:
PyModule_AddFunctions(m, def.m_methods)
if def.m_doc:
PyModule_SetDocString(m, def.m_doc)
def PyModule_ExecDef(module, def):
if isinstance(module, types.module_type):
if module.md_state is NULL:
# allocate a block of zeroed-out memory
module.md_state = _alloc(module.md_size)
if def.m_slots is NULL:
return
for slot, value in def.m_slots:
if slot == Py_mod_exec:
value(module)
Module Creation Phase