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 The framework that calls the importers is explained in PEP 451
[#pep-0451-loading]_. [#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): class BuiltinImporter:
_imp.exec_dynamic(module) def create_module(self, spec):
module = _imp.create_builtin(spec)
def load_module(self, name): def exec_module(self, module):
# use a backwards compatibility shim _imp.exec_dynamic(module)
_load_module_shim(self, name)
importlib/_bootstrap_external.py:: def load_module(self, name):
# use a backwards compatibility shim
_load_module_shim(self, name)
class ExtensionFileLoader: importlib/_bootstrap_external.py:
def create_module(self, spec):
module = _imp.create_dynamic(spec)
def exec_module(self, module): ::
_imp.exec_dynamic(module)
def load_module(self, name): class ExtensionFileLoader:
# use a backwards compatibility shim def create_module(self, spec):
_load_module_shim(self, name) module = _imp.create_dynamic(spec)
Python/import.c (the _imp module):: def exec_module(self, module):
_imp.exec_dynamic(module)
def create_dynamic(spec): def load_module(self, name):
name = spec.name # use a backwards compatibility shim
path = spec.origin _load_module_shim(self, name)
# Find an already loaded module that used single-phase init. Python/import.c (the _imp module):
# For multi-phase initialization, mod is NULL, so a new module
# is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
return _PyImport_LoadDynamicModuleWithSpec(spec) ::
def exec_dynamic(module): def create_dynamic(spec):
if not isinstance(module, types.ModuleType): name = spec.name
# non-modules are skipped -- PyModule_GetDef fails on them path = spec.origin
return
def = PyModule_GetDef(module) # Find an already loaded module that used single-phase init.
state = PyModule_GetState(module) # For multi-phase initialization, mod is NULL, so a new module
if state is NULL: # is always created.
PyModule_ExecDef(module, def) mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
def create_builtin(spec): return _PyImport_LoadDynamicModuleWithSpec(spec)
name = spec.name
# Find an already loaded module that used single-phase init. def exec_dynamic(module):
# For multi-phase initialization, mod is NULL, so a new module if not isinstance(module, types.ModuleType):
# is always created. # non-modules are skipped -- PyModule_GetDef fails on them
mod = _PyImport_FindExtensionObject(name, name) return
if mod:
return mod
for initname, initfunc in PyImport_Inittab: def = PyModule_GetDef(module)
if name == initname: state = PyModule_GetState(module)
m = initfunc() if state is NULL:
if isinstance(m, PyModuleDef): PyModule_ExecDef(module, def)
def = m
return PyModule_FromDefAndSpec(def, spec)
else:
# fall back to single-phase initialization
module = m
_PyImport_FixupExtensionObject(module, name, name)
return module
Python/importdl.c:: def create_builtin(spec):
name = spec.name
def _PyImport_LoadDynamicModuleWithSpec(spec): # Find an already loaded module that used single-phase init.
path = spec.origin # For multi-phase initialization, mod is NULL, so a new module
package, dot, name = spec.name.rpartition('.') # is always created.
mod = _PyImport_FindExtensionObject(name, name)
if mod:
return mod
# see the "Non-ASCII module names" section for export_hook_name for initname, initfunc in PyImport_Inittab:
hook_name = export_hook_name(name) 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 Python/importdl.c:
# from shared library
exportfunc = _find_shared_funcptr(hook_name, path)
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): m = exportfunc()
name = spec.name if isinstance(m, PyModuleDef):
create = None def = m
for slot, value in def.m_slots: return PyModule_FromDefAndSpec(def, spec)
if slot == Py_mod_create:
create = value
if create:
m = create(spec, def)
else:
m = PyModule_New(name)
if isinstance(m, types.ModuleType): module = m
m.md_state = None
m.md_def = def
if def.m_methods: # fall back to single-phase initialization
PyModule_AddFunctions(m, def.m_methods) ....
if def.m_doc:
PyModule_SetDocString(m, def.m_doc)
def PyModule_ExecDef(module, def): Objects/moduleobject.c:
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: def PyModule_FromDefAndSpec(def, spec):
if slot == Py_mod_exec: name = spec.name
value(module) 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 Module Creation Phase