| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ec219ba commit d5cacbb
34 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,6 @@ Module Objects | |||
| 7 | 7 | ||
| 8 | 8 | .. index:: object: module | |
| 9 | 9 | ||
| 10 | - There are only a few functions special to module objects. | ||
| 11 | - | ||
| 12 | 10 | ||
| 13 | 11 | .. c:var:: PyTypeObject PyModule_Type | |
| 14 | 12 | ||
@@ -109,6 +107,14 @@ There are only a few functions special to module objects. | |||
| 109 | 107 | unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead. | |
| 110 | 108 | ||
| 111 | 109 | ||
| 110 | + Per-interpreter module state | ||
| 111 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 112 | + | ||
| 113 | + Single-phase initialization creates singleton modules that can store additional | ||
| 114 | + information as part of the interpreter, allow that state to be retrieved later | ||
| 115 | + with only a reference to the module definition, rather than to the module | ||
| 116 | + itself. | ||
| 117 | + | ||
| 112 | 118 | .. c:function:: void* PyModule_GetState(PyObject *module) | |
| 113 | 119 | ||
| 114 | 120 | Return the "state" of the module, that is, a pointer to the block of memory | |
@@ -146,27 +152,6 @@ There are only a few functions special to module objects. | |||
| 146 | 152 | Initializing C modules | |
| 147 | 153 | ^^^^^^^^^^^^^^^^^^^^^^ | |
| 148 | 154 | ||
| 149 | - These functions are usually used in the module initialization function. | ||
| 150 | - | ||
| 151 | - .. c:function:: PyObject* PyModule_Create(PyModuleDef *module) | ||
| 152 | - | ||
| 153 | - Create a new module object, given the definition in *module*. This behaves | ||
| 154 | - like :c:func:`PyModule_Create2` with *module_api_version* set to | ||
| 155 | - :const:`PYTHON_API_VERSION`. | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - .. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version) | ||
| 159 | - | ||
| 160 | - Create a new module object, given the definition in *module*, assuming the | ||
| 161 | - API version *module_api_version*. If that version does not match the version | ||
| 162 | - of the running interpreter, a :exc:`RuntimeWarning` is emitted. | ||
| 163 | - | ||
| 164 | - .. note:: | ||
| 165 | - | ||
| 166 | - Most uses of this function should be using :c:func:`PyModule_Create` | ||
| 167 | - instead; only use this if you are sure you need it. | ||
| 168 | - | ||
| 169 | - | ||
| 170 | 155 | .. c:type:: PyModuleDef | |
| 171 | 156 | ||
| 172 | 157 | This struct holds all information that is needed to create a module object. | |
@@ -210,9 +195,10 @@ These functions are usually used in the module initialization function. | |||
| 210 | 195 | A pointer to a table of module-level functions, described by | |
| 211 | 196 | :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present. | |
| 212 | 197 | ||
| 213 | - .. c:member:: inquiry m_reload | ||
| 198 | + .. c:member:: PyModuleDef_Slot* m_slots | ||
| 214 | 199 | ||
| 215 | - Currently unused, should be *NULL*. | ||
| 200 | + An array of slot definitions for multi-phase initialization, terminated by | ||
| 201 | + a *NULL* entry. | ||
| 216 | 202 | ||
| 217 | 203 | .. c:member:: traverseproc m_traverse | |
| 218 | 204 | ||
@@ -229,14 +215,68 @@ These functions are usually used in the module initialization function. | |||
| 229 | 215 | A function to call during deallocation of the module object, or *NULL* if | |
| 230 | 216 | not needed. | |
| 231 | 217 | ||
| 218 | + The module initialization function may create and return the module object | ||
| 219 | + directly. This is referred to as "single-phase initialization", and uses one | ||
| 220 | + of the following two module creation functions: | ||
| 221 | + | ||
| 222 | + .. c:function:: PyObject* PyModule_Create(PyModuleDef *module) | ||
| 223 | + | ||
| 224 | + Create a new module object, given the definition in *module*. This behaves | ||
| 225 | + like :c:func:`PyModule_Create2` with *module_api_version* set to | ||
| 226 | + :const:`PYTHON_API_VERSION`. | ||
| 227 | + | ||
| 228 | + | ||
| 229 | + .. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version) | ||
| 230 | + | ||
| 231 | + Create a new module object, given the definition in *module*, assuming the | ||
| 232 | + API version *module_api_version*. If that version does not match the version | ||
| 233 | + of the running interpreter, a :exc:`RuntimeWarning` is emitted. | ||
| 234 | + | ||
| 235 | + .. note:: | ||
| 236 | + | ||
| 237 | + Most uses of this function should be using :c:func:`PyModule_Create` | ||
| 238 | + instead; only use this if you are sure you need it. | ||
| 239 | + | ||
| 240 | + | ||
| 241 | + Alternatively, the module initialization function may instead return a | ||
| 242 | + :c:type:`PyModuleDef` instance with a non-empty ``m_slots`` array. This is | ||
| 243 | + referred to as "multi-phase initialization", and ``PyModuleDef`` instance | ||
| 244 | + should be initialized with the following function: | ||
| 245 | + | ||
| 246 | + .. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *module) | ||
| 247 | + | ||
| 248 | + Ensures a module definition is a properly initialized Python object that | ||
| 249 | + correctly reports its type and reference count. | ||
| 250 | + | ||
| 251 | + .. XXX (ncoghlan): It's not clear if it makes sense to document PyModule_ExecDef | ||
| 252 | + PyModule_FromDefAndSpec or PyModule_FromDefAndSpec2 here, as end user code | ||
| 253 | + generally shouldn't be calling those. | ||
| 254 | + | ||
| 255 | + The module initialization function (if using single phase initialization) or | ||
| 256 | + a function called from a module execution slot (if using multiphase | ||
| 257 | + initialization), can use the following functions to help initialize the module | ||
| 258 | + state: | ||
| 259 | + | ||
| 260 | + .. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring) | ||
| 261 | + | ||
| 262 | + Set the docstring for *module* to *docstring*. Return ``-1`` on error, ``0`` | ||
| 263 | + on success. | ||
| 264 | + | ||
| 265 | + .. c:function:: int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions) | ||
| 266 | + | ||
| 267 | + Add the functions from the ``NULL`` terminated *functions* array to *module*. | ||
| 268 | + Refer to the :c:type:`PyMethodDef` documentation for details on individual | ||
| 269 | + entries (due to the lack of a shared module namespace, module level | ||
| 270 | + "functions" implemented in C typically receive the module as their first | ||
| 271 | + parameter, making them similar to instance methods on Python classes). | ||
| 272 | + | ||
| 232 | 273 | ||
| 233 | 274 | .. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value) | |
| 234 | 275 | ||
| 235 | 276 | Add an object to *module* as *name*. This is a convenience function which can | |
| 236 | 277 | be used from the module's initialization function. This steals a reference to | |
| 237 | 278 | *value*. Return ``-1`` on error, ``0`` on success. | |
| 238 | 279 | ||
| 239 | - | ||
| 240 | 280 | .. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value) | |
| 241 | 281 | ||
| 242 | 282 | Add an integer constant to *module* as *name*. This convenience function can be | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,12 @@ generically as an :term:`importer`) to participate in the import process. | |||
| 55 | 55 | :pep:`451` | |
| 56 | 56 | A ModuleSpec Type for the Import System | |
| 57 | 57 | ||
| 58 | + :pep:`488` | ||
| 59 | + Elimination of PYO files | ||
| 60 | + | ||
| 61 | + :pep:`489` | ||
| 62 | + Multi-phase extension module initialization | ||
| 63 | + | ||
| 58 | 64 | :pep:`3120` | |
| 59 | 65 | Using UTF-8 as the Default Source Encoding | |
| 60 | 66 | ||
@@ -756,9 +762,9 @@ find and load modules. | |||
| 756 | 762 | Only class methods are defined by this class to alleviate the need for | |
| 757 | 763 | instantiation. | |
| 758 | 764 | ||
| 759 | - .. note:: | ||
| 760 | - Due to limitations in the extension module C-API, for now | ||
| 761 | - BuiltinImporter does not implement :meth:`Loader.exec_module`. | ||
| 765 | + .. versionchanged:: 3.5 | ||
| 766 | + As part of :pep:`489`, the builtin importer now implements | ||
| 767 | + :meth:`Loader.create_module` and :meth:`Loader.exec_module` | ||
| 762 | 768 | ||
| 763 | 769 | ||
| 764 | 770 | .. class:: FrozenImporter | |
@@ -973,14 +979,18 @@ find and load modules. | |||
| 973 | 979 | ||
| 974 | 980 | Path to the extension module. | |
| 975 | 981 | ||
| 976 | - .. method:: load_module(name=None) | ||
| 982 | + .. method:: create_module(spec) | ||
| 983 | + | ||
| 984 | + Creates the module object from the given specification in accordance | ||
| 985 | + with :pep:`489`. | ||
| 986 | + | ||
| 987 | + .. versionadded:: 3.5 | ||
| 988 | + | ||
| 989 | + .. method:: exec_module(module) | ||
| 977 | 990 | ||
| 978 | - Loads the extension module if and only if *fullname* is the same as | ||
| 979 | - :attr:`name` or is ``None``. | ||
| 991 | + Initializes the given module object in accordance with :pep:`489`. | ||
| 980 | 992 | ||
| 981 | - .. note:: | ||
| 982 | - Due to limitations in the extension module C-API, for now | ||
| 983 | - ExtensionFileLoader does not implement :meth:`Loader.exec_module`. | ||
| 993 | + .. versionadded:: 3.5 | ||
| 984 | 994 | ||
| 985 | 995 | .. method:: is_package(fullname) | |
| 986 | 996 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,6 +93,7 @@ Implementation improvements: | |||
| 93 | 93 | (:issue:`19977`). | |
| 94 | 94 | ||
| 95 | 95 | * :pep:`488`, the elimination of ``.pyo`` files. | |
| 96 | + * :pep:`489`, multi-phase initialization of extension modules. | ||
| 96 | 97 | ||
| 97 | 98 | Significantly Improved Library Modules: | |
| 98 | 99 | ||
@@ -265,6 +266,21 @@ updated API to help with this change. | |||
| 265 | 266 | :pep:`488` -- Elimination of PYO files | |
| 266 | 267 | ||
| 267 | 268 | ||
| 269 | + PEP 489: Multi-phase extension module initialization | ||
| 270 | + ---------------------------------------------------- | ||
| 271 | + | ||
| 272 | + :pep:`489` updates extension module initialization to take advantage of the | ||
| 273 | + two step module loading mechanism introduced by :pep:`451` in Python 3.4. | ||
| 274 | + | ||
| 275 | + This change brings the import semantics of extension modules that opt-in to | ||
| 276 | + using the new mechanism much closer to those of Python source and bytecode | ||
| 277 | + modules, including the ability to any valid identifier as a module name, | ||
| 278 | + rather than being restricted to ASCII. | ||
| 279 | + | ||
| 280 | + .. seealso:: | ||
| 281 | + | ||
| 282 | + :pep:`488` -- Multi-phase extension module initialization | ||
| 283 | + | ||
| 268 | 284 | Other Language Changes | |
| 269 | 285 | ====================== | |
| 270 | 286 | ||
@@ -667,7 +683,7 @@ time | |||
| 667 | 683 | tkinter | |
| 668 | 684 | ------- | |
| 669 | 685 | ||
| 670 | - * The :module:`tkinter._fix` module used for setting up the Tcl/Tk environment | ||
| 686 | + * The :mod:`tkinter._fix` module used for setting up the Tcl/Tk environment | ||
| 671 | 687 | on Windows has been replaced by a private function in the :module:`_tkinter` | |
| 672 | 688 | module which makes no permanent changes to environment variables. | |
| 673 | 689 | (Contributed by Zachary Ware in :issue:`20035`.) | |
@@ -1012,7 +1028,6 @@ Changes in the Python API | |||
| 1012 | 1028 | program depends on patching the module level variable to capture the debug | |
| 1013 | 1029 | output, you will need to update it to capture sys.stderr instead. | |
| 1014 | 1030 | ||
| 1015 | - | ||
| 1016 | 1031 | Changes in the C API | |
| 1017 | 1032 | -------------------- | |
| 1018 | 1033 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,13 +12,13 @@ extern "C" { | |||
| 12 | 12 | /* If PY_SSIZE_T_CLEAN is defined, each functions treats #-specifier | |
| 13 | 13 | to mean Py_ssize_t */ | |
| 14 | 14 | #ifdef PY_SSIZE_T_CLEAN | |
| 15 | - #define PyArg_Parse _PyArg_Parse_SizeT | ||
| 16 | - #define PyArg_ParseTuple _PyArg_ParseTuple_SizeT | ||
| 17 | - #define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT | ||
| 18 | - #define PyArg_VaParse _PyArg_VaParse_SizeT | ||
| 19 | - #define PyArg_VaParseTupleAndKeywords _PyArg_VaParseTupleAndKeywords_SizeT | ||
| 20 | - #define Py_BuildValue _Py_BuildValue_SizeT | ||
| 21 | - #define Py_VaBuildValue _Py_VaBuildValue_SizeT | ||
| 15 | + #define PyArg_Parse _PyArg_Parse_SizeT | ||
| 16 | + #define PyArg_ParseTuple _PyArg_ParseTuple_SizeT | ||
| 17 | + #define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT | ||
| 18 | + #define PyArg_VaParse _PyArg_VaParse_SizeT | ||
| 19 | + #define PyArg_VaParseTupleAndKeywords _PyArg_VaParseTupleAndKeywords_SizeT | ||
| 20 | + #define Py_BuildValue _Py_BuildValue_SizeT | ||
| 21 | + #define Py_VaBuildValue _Py_VaBuildValue_SizeT | ||
| 22 | 22 | #else | |
| 23 | 23 | PyAPI_FUNC(PyObject *) _Py_VaBuildValue_SizeT(const char *, va_list); | |
| 24 | 24 | #endif | |
@@ -49,6 +49,9 @@ PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long); | |||
| 49 | 49 | PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *); | |
| 50 | 50 | #define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c) | |
| 51 | 51 | #define PyModule_AddStringMacro(m, c) PyModule_AddStringConstant(m, #c, c) | |
| 52 | + PyAPI_FUNC(int) PyModule_SetDocString(PyObject *, const char *); | ||
| 53 | + PyAPI_FUNC(int) PyModule_AddFunctions(PyObject *, PyMethodDef *); | ||
| 54 | + PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def); | ||
| 52 | 55 | ||
| 53 | 56 | #define Py_CLEANUP_SUPPORTED 0x20000 | |
| 54 | 57 | ||
@@ -67,35 +70,35 @@ PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char | |||
| 67 | 70 | Please add a line or two to the top of this log for each API | |
| 68 | 71 | version change: | |
| 69 | 72 | ||
| 70 | - 22-Feb-2006 MvL 1013 PEP 353 - long indices for sequence lengths | ||
| 73 | + 22-Feb-2006 MvL 1013 PEP 353 - long indices for sequence lengths | ||
| 71 | 74 | ||
| 72 | - 19-Aug-2002 GvR 1012 Changes to string object struct for | ||
| 73 | - interning changes, saving 3 bytes. | ||
| 75 | + 19-Aug-2002 GvR 1012 Changes to string object struct for | ||
| 76 | + interning changes, saving 3 bytes. | ||
| 74 | 77 | ||
| 75 | - 17-Jul-2001 GvR 1011 Descr-branch, just to be on the safe side | ||
| 78 | + 17-Jul-2001 GvR 1011 Descr-branch, just to be on the safe side | ||
| 76 | 79 | ||
| 77 | 80 | 25-Jan-2001 FLD 1010 Parameters added to PyCode_New() and | |
| 78 | 81 | PyFrame_New(); Python 2.1a2 | |
| 79 | 82 | ||
| 80 | 83 | 14-Mar-2000 GvR 1009 Unicode API added | |
| 81 | 84 | ||
| 82 | - 3-Jan-1999 GvR 1007 Decided to change back! (Don't reuse 1008!) | ||
| 85 | + 3-Jan-1999 GvR 1007 Decided to change back! (Don't reuse 1008!) | ||
| 83 | 86 | ||
| 84 | - 3-Dec-1998 GvR 1008 Python 1.5.2b1 | ||
| 87 | + 3-Dec-1998 GvR 1008 Python 1.5.2b1 | ||
| 85 | 88 | ||
| 86 | - 18-Jan-1997 GvR 1007 string interning and other speedups | ||
| 89 | + 18-Jan-1997 GvR 1007 string interning and other speedups | ||
| 87 | 90 | ||
| 88 | - 11-Oct-1996 GvR renamed Py_Ellipses to Py_Ellipsis :-( | ||
| 91 | + 11-Oct-1996 GvR renamed Py_Ellipses to Py_Ellipsis :-( | ||
| 89 | 92 | ||
| 90 | - 30-Jul-1996 GvR Slice and ellipses syntax added | ||
| 93 | + 30-Jul-1996 GvR Slice and ellipses syntax added | ||
| 91 | 94 | ||
| 92 | - 23-Jul-1996 GvR For 1.4 -- better safe than sorry this time :-) | ||
| 95 | + 23-Jul-1996 GvR For 1.4 -- better safe than sorry this time :-) | ||
| 93 | 96 | ||
| 94 | - 7-Nov-1995 GvR Keyword arguments (should've been done at 1.3 :-( ) | ||
| 97 | + 7-Nov-1995 GvR Keyword arguments (should've been done at 1.3 :-( ) | ||
| 95 | 98 | ||
| 96 | - 10-Jan-1995 GvR Renamed globals to new naming scheme | ||
| 99 | + 10-Jan-1995 GvR Renamed globals to new naming scheme | ||
| 97 | 100 | ||
| 98 | - 9-Jan-1995 GvR Initial version (incompatible with older API) | ||
| 101 | + 9-Jan-1995 GvR Initial version (incompatible with older API) | ||
| 99 | 102 | */ | |
| 100 | 103 | ||
| 101 | 104 | /* The PYTHON_ABI_VERSION is introduced in PEP 384. For the lifetime of | |
@@ -105,21 +108,34 @@ PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char | |||
| 105 | 108 | #define PYTHON_ABI_STRING "3" | |
| 106 | 109 | ||
| 107 | 110 | #ifdef Py_TRACE_REFS | |
| 108 | - /* When we are tracing reference counts, rename PyModule_Create2 so | ||
| 111 | + /* When we are tracing reference counts, rename module creation functions so | ||
| 109 | 112 | modules compiled with incompatible settings will generate a | |
| 110 | 113 | link-time error. */ | |
| 111 | 114 | #define PyModule_Create2 PyModule_Create2TraceRefs | |
| 115 | + #define PyModule_FromDefAndSpec2 PyModule_FromDefAndSpec2TraceRefs | ||
| 112 | 116 | #endif | |
| 113 | 117 | ||
| 114 | 118 | PyAPI_FUNC(PyObject *) PyModule_Create2(struct PyModuleDef*, | |
| 115 | 119 | int apiver); | |
| 116 | 120 | ||
| 117 | 121 | #ifdef Py_LIMITED_API | |
| 118 | 122 | #define PyModule_Create(module) \ | |
| 119 | - PyModule_Create2(module, PYTHON_ABI_VERSION) | ||
| 123 | + PyModule_Create2(module, PYTHON_ABI_VERSION) | ||
| 120 | 124 | #else | |
| 121 | 125 | #define PyModule_Create(module) \ | |
| 122 | - PyModule_Create2(module, PYTHON_API_VERSION) | ||
| 126 | + PyModule_Create2(module, PYTHON_API_VERSION) | ||
| 127 | + #endif | ||
| 128 | + | ||
| 129 | + PyAPI_FUNC(PyObject *) PyModule_FromDefAndSpec2(PyModuleDef *def, | ||
| 130 | + PyObject *spec, | ||
| 131 | + int module_api_version); | ||
| 132 | + | ||
| 133 | + #ifdef Py_LIMITED_API | ||
| 134 | + #define PyModule_FromDefAndSpec(module, spec) \ | ||
| 135 | + PyModule_FromDefAndSpec2(module, spec, PYTHON_ABI_VERSION) | ||
| 136 | + #else | ||
| 137 | + #define PyModule_FromDefAndSpec(module, spec) \ | ||
| 138 | + PyModule_FromDefAndSpec2(module, spec, PYTHON_API_VERSION) | ||
| 123 | 139 | #endif | |
| 124 | 140 | ||
| 125 | 141 | #ifndef Py_LIMITED_API | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,9 @@ PyAPI_FUNC(void) _PyModule_ClearDict(PyObject *); | |||
| 30 | 30 | PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*); | |
| 31 | 31 | PyAPI_FUNC(void*) PyModule_GetState(PyObject*); | |
| 32 | 32 | ||
| 33 | + PyAPI_FUNC(PyObject *) PyModuleDef_Init(struct PyModuleDef*); | ||
| 34 | + PyTypeObject PyModuleDef_Type; | ||
| 35 | + | ||
| 33 | 36 | typedef struct PyModuleDef_Base { | |
| 34 | 37 | PyObject_HEAD | |
| 35 | 38 | PyObject* (*m_init)(void); | |
@@ -44,18 +47,29 @@ typedef struct PyModuleDef_Base { | |||
| 44 | 47 | NULL, /* m_copy */ \ | |
| 45 | 48 | } | |
| 46 | 49 | ||
| 50 | + typedef struct PyModuleDef_Slot{ | ||
| 51 | + int slot; | ||
| 52 | + void *value; | ||
| 53 | + } PyModuleDef_Slot; | ||
| 54 | + | ||
| 47 | 55 | typedef struct PyModuleDef{ | |
| 48 | 56 | PyModuleDef_Base m_base; | |
| 49 | 57 | const char* m_name; | |
| 50 | 58 | const char* m_doc; | |
| 51 | 59 | Py_ssize_t m_size; | |
| 52 | 60 | PyMethodDef *m_methods; | |
| 53 | - inquiry m_reload; | ||
| 61 | + PyModuleDef_Slot* m_slots; | ||
| 54 | 62 | traverseproc m_traverse; | |
| 55 | 63 | inquiry m_clear; | |
| 56 | 64 | freefunc m_free; | |
| 57 | 65 | }PyModuleDef; | |
| 58 | 66 | ||
| 67 | + #define Py_mod_create 1 | ||
| 68 | + #define Py_mod_exec 2 | ||
| 69 | + | ||
| 70 | + #ifndef Py_LIMITED_API | ||
| 71 | + #define _Py_mod_LAST_SLOT 2 | ||
| 72 | + #endif | ||
| 59 | 73 | ||
| 60 | 74 | #ifdef __cplusplus | |
| 61 | 75 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments