summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-116322: Rename PyModule_ExperimentalSetGIL to PyUnstable_Module_SetGIL ↵Petr Viktorin2024-05-061-1/+1
| | | | (GH-118645)
* gh-118527: Use deferred reference counting for C functions on modules (#118529)Sam Gross2024-05-031-0/+1
| | | | | This addresses a scaling bottleneck in the free-threaded build when calling functions like `math.floor()` concurrently from multiple threads.
* gh-116322: Add Py_mod_gil module slot (#116882)Brett Simmers2024-05-031-0/+35
| | | | | | | | | | | | | | This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL. PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148. A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If `-v` is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).
* gh-95754: Better error when script shadows a standard library or third party ↵Shantanu2024-04-231-53/+191
| | | | module (#113769)
* gh-117376: Partial implementation of deferred reference counting (#117696)Sam Gross2024-04-121-18/+19
| | | | | This marks objects as using deferred refrence counting using the `ob_gc_bits` field in the free-threaded build and collects those objects during GC.
* gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438)Serhiy Storchaka2024-03-071-3/+7
|
* gh-95754: Better AttributeError on partially initialised module (#112577)Shantanu2023-12-211-2/+22
| | | Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-112660: Do not clear arbitrary errors on import (GH-112661)Serhiy Storchaka2023-12-071-32/+25
| | | | | Previously arbitrary errors could be cleared during formatting error messages for ImportError or AttributeError for modules. Now all unexpected errors are reported.
* gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827)Serhiy Storchaka2023-11-141-16/+13
|
* gh-108082: Use PyErr_FormatUnraisable() (GH-111580)Serhiy Storchaka2023-11-021-6/+5
| | | | | | Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-108308: Use PyDict_GetItemRef() in moduleobject.c (#108381)Victor Stinner2023-08-231-28/+40
| | | | | | | | Replace PyDict_GetItemWithError() with PyDict_GetItemRef() which returns a strong reference. Cleanup also the function: move variable definition to their first assignation, rename variable names to use name longer than 1 character.
* gh-106869: Use new PyMemberDef constant names (#106871)Victor Stinner2023-07-251-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Remove '#include "structmember.h"'. * If needed, add <stddef.h> to get offsetof() function. * Update Parser/asdl_c.py to regenerate Python/Python-ast.c. * Replace: * T_SHORT => Py_T_SHORT * T_INT => Py_T_INT * T_LONG => Py_T_LONG * T_FLOAT => Py_T_FLOAT * T_DOUBLE => Py_T_DOUBLE * T_STRING => Py_T_STRING * T_OBJECT => _Py_T_OBJECT * T_CHAR => Py_T_CHAR * T_BYTE => Py_T_BYTE * T_UBYTE => Py_T_UBYTE * T_USHORT => Py_T_USHORT * T_UINT => Py_T_UINT * T_ULONG => Py_T_ULONG * T_STRING_INPLACE => Py_T_STRING_INPLACE * T_BOOL => Py_T_BOOL * T_OBJECT_EX => Py_T_OBJECT_EX * T_LONGLONG => Py_T_LONGLONG * T_ULONGLONG => Py_T_ULONGLONG * T_PYSSIZET => Py_T_PYSSIZET * T_NONE => _Py_T_NONE * READONLY => Py_READONLY * PY_AUDIT_READ => Py_AUDIT_READ * READ_RESTRICTED => Py_AUDIT_READ * PY_WRITE_RESTRICTED => _Py_WRITE_RESTRICTED * RESTRICTED => (READ_RESTRICTED | _Py_WRITE_RESTRICTED)
* gh-106719: Fix __annotations__ getter and setter in the type and module ↵Serhiy Storchaka2023-07-181-27/+21
| | | | | types (GH-106720) No longer suppress arbitrary errors. Simplify the code.
* gh-106521: Remove _PyObject_LookupAttr() function (GH-106642)Serhiy Storchaka2023-07-121-1/+1
|
* gh-106320: Remove private _PyErr C API functions (#106356)Victor Stinner2023-07-031-2/+3
| | | | Remove private _PyErr C API functions: move them to the internal C API (pycore_pyerrors.h).
* gh-106320: Create pycore_modsupport.h header file (#106355)Victor Stinner2023-07-031-1/+2
| | | | | | | | | | Remove the following functions from the C API, move them to the internal C API: add a new pycore_modsupport.h internal header file: * PyModule_CreateInitialized() * _PyArg_NoKwnames() * _Py_VaBuildStack() No longer export these functions.
* gh-105927: finalize_modules_clear_weaklist() uses _PyWeakref_GET_REF() (#105971)Victor Stinner2023-06-211-1/+1
| | | | | finalize_modules_clear_weaklist() now holds a strong reference to the module longer than before: replace PyWeakref_GET_OBJECT() with _PyWeakref_GET_REF().
* gh-99113: Add a check for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED (gh-104206)Eric Snow2023-05-061-1/+7
| | | Py_MOD_PER_INTERPRETER_GIL_SUPPORTED is a new supported value for Py_mod_multiple_interpreters, added in gh-104205.
* gh-99113: Add Py_MOD_PER_INTERPRETER_GIL_SUPPORTED (gh-104205)Eric Snow2023-05-051-1/+2
| | | Here we are doing no more than adding the value for Py_mod_multiple_interpreters and using it for stdlib modules. We will start checking for it in gh-104206 (once PyInterpreterState.ceval.own_gil is added in gh-104204).
* gh-104108: Add the Py_mod_multiple_interpreters Module Def Slot (gh-104148)Eric Snow2023-05-051-0/+30
| | | I'll be adding a value to indicate support for per-interpreter GIL in gh-99114.
* gh-104066: Improve performance of hasattr for module objects (#104063)Itamar Ostricher2023-05-041-29/+62
|
* gh-101758: Clean Up Uses of Import State (gh-101919)Eric Snow2023-02-151-22/+3
| | | | | | | | | | | | | | | This change is almost entirely moving code around and hiding import state behind internal API. We introduce no changes to behavior, nor to non-internal API. (Since there was already going to be a lot of churn, I took this as an opportunity to re-organize import.c into topically-grouped sections of code.) The motivation is to simplify a number of upcoming changes. Specific changes: * move existing import-related code to import.c, wherever possible * add internal API for interacting with import state (both global and per-interpreter) * use only API outside of import.c (to limit churn there when changing the location, etc.) * consolidate the import-related state of PyInterpreterState into a single struct field (this changes layout slightly) * add macros for import state in import.c (to simplify changing the location) * group code in import.c into sections *remove _PyState_AddModule() https://github.com/python/cpython/issues/101758
* gh-99947: Ensure unreported errors are chained for SystemError during import ↵Sebastian Berg2022-12-231-4/+5
| | | | (GH-99946)
* gh-81057: Move More Globals in Core Code to _PyRuntimeState (gh-99516)Eric Snow2022-11-161-0/+2
| | | https://github.com/python/cpython/issues/81057
* gh-81057: Move the Extension Modules Cache to _PyRuntimeState (gh-99355)Eric Snow2022-11-111-3/+2
| | | | | We also move the closely related max_module_number and add comments documenting the group of struct members. https://github.com/python/cpython/issues/81057
* gh-99300: Use Py_NewRef() in Objects/ directory (#99354)Victor Stinner2022-11-101-6/+3
| | | | Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
* gh-98627: Use a Switch in PyModule_FromDefAndSpec2() (gh-98734)Eric Snow2022-10-271-14/+18
| | | This helps simplify some changes in follow-up PRs. It also matches what we're doing in PyModule_ExecDef().
* no-issue: Add assertion to PyModule_GetName for understanding (GH-32236)Dong-hee Na2022-04-021-1/+3
|
* bpo-46891: Fix creating a new instance of a module subclass with slots ↵Mark Shannon2022-03-031-1/+2
| | | | (GH-31643)
* bpo-45459: C API uses type names rather than structure names (GH-31528)Victor Stinner2022-02-241-6/+6
| | | | Thanks to the new pytypedefs.h, it becomes to use type names like PyObject rather like structure names like "struct _object".
* bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized ↵Eric Snow2022-02-081-37/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | global objects. (gh-30928) We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
* bpo-45439: Move _PyObject_CallNoArgs() to pycore_call.h (GH-28895)Victor Stinner2021-10-121-0/+1
| | | | | | | * Move _PyObject_CallNoArgs() to pycore_call.h (internal C API). * _ssl, _sqlite and _testcapi extensions now call the public PyObject_CallNoArgs() function, rather than _PyObject_CallNoArgs(). * _lsprof extension is now built with Py_BUILD_CORE_MODULE macro defined to get access to internal _PyObject_CallNoArgs().
* bpo-45439: Rename _PyObject_CallNoArg() to _PyObject_CallNoArgs() (GH-28891)Victor Stinner2021-10-111-1/+1
| | | | | Fix typo in the private _PyObject_CallNoArg() function name: rename it to _PyObject_CallNoArgs() to be consistent with the public function PyObject_CallNoArgs().
* bpo-44717: improve AttributeError on circular imports of submodules (GH-27338)Filipe Laíns2021-07-241-0/+30
|
* Revert "bpo-44717: improve AttributeError on circular imports of submodules ↵Pablo Galindo Salgado2021-07-241-30/+0
| | | | | (GH-27299)" (GH-27331) This reverts commit 8072a1181dd64135f700b44372fbf7bf91e68072.
* bpo-44717: improve AttributeError on circular imports of submodules (GH-27299)Filipe Laíns2021-07-241-0/+30
| | | | | Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
* bpo-43770: Cleanup PyModuleDef_Init() (GH-26879)Victor Stinner2021-06-231-2/+1
| | | | | PyModuleDef_Init() no longer tries to make PyModule_Type type: it's already done by _PyTypes_Init() at Python startup. Replace PyType_Ready() call with an assertion.
* bpo-44486: Make sure that modules always have a dictionary. (GH-26847)Mark Shannon2021-06-231-41/+61
| | | * Make sure that modules always have a dictionary.
* bpo-43901: Fix refleaks in test_module (GH-25754)Pablo Galindo2021-04-301-4/+11
|
* Two minor fixes for accessing a module's name. (#25658)larryhastings2021-04-301-6/+2
| | | | | | | | | | | | | | | | | While working on another issue, I noticed two minor nits in the C implementation of the module object. Both are related to getting a module's name. First, the C function module_dir() (module.__dir__) starts by ensuring the module dict is valid. If the module dict is invalid, it wants to format an exception using the name of the module, which it gets from PyModule_GetName(). However, PyModule_GetName() gets the name of the module from the dict. So getting the name in this circumstance will never succeed. When module_dir() wants to format the error but can't get the name, it knows that PyModule_GetName() must have already raised an exception. So it leaves that exception alone and returns an error. The end result is that the exception raised here is kind of useless and misleading: dir(module) on a module with no __dict__ raises SystemError("nameless module"). I changed the code to actually raise the exception it wanted to raise, just without a real module name: TypeError("<module>.__dict__ is not a dictionary"). This seems more useful, and would do a better job putting the programmer who encountered this on the right track of figuring out what was going on. Second, the C API function PyModule_GetNameObject() checks to see if the module has a dict. If m->md_dict is not NULL, it calls _PyDict_GetItemIdWithError(). However, it's possible for m->md_dict to be None. And if you call _PyDict_GetItemIdWithError(Py_None, ...) it will *crash*. Unfortunately, this crash was due to my own bug in the other branch. Fixing my code made the crash go away. I assert that this is still possible at the API level. The fix is easy: add a PyDict_Check() to PyModule_GetNameObject(). Unfortunately, I don't know how to add a unit test for this. Having changed module_dir() above, I can't find any other interfaces callable from Python that eventually call PyModule_GetNameObject(). So I don't know how to trick the runtime into reproducing this error. Since both these changes are minor--each entails only a small edit to only one line--I didn't bother with a news item.
* bpo-43901: Lazy-create an empty annotations dict in all unannotated user ↵larryhastings2021-04-301-3/+69
| | | | | classes and modules (#25623) Change class and module objects to lazy-create empty annotations dicts on demand. The annotations dicts are stored in the object's `__dict__` for backwards compatibility.
* bpo-40137: Add pycore_moduleobject.h internal header (GH-25507)Victor Stinner2021-04-211-15/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add pycore_moduleobject.h internal header file with static inline functions to access module members: * _PyModule_GetDict() * _PyModule_GetDef() * _PyModule_GetState() These functions don't check at runtime if their argument has a valid type and can be inlined even if Python is not built with LTO. _PyType_GetModuleByDef() uses _PyModule_GetDef(). Replace PyModule_GetState() with _PyModule_GetState() in the extension modules, considered as performance sensitive: * _abc * _functools * _operator * _pickle * _queue * _random * _sre * _struct * _thread * _winapi * array * posix The following extensions are now built with the Py_BUILD_CORE_MODULE macro defined, to be able to use the internal pycore_moduleobject.h header: _abc, array, _operator, _queue, _sre, _struct.
* bpo-42923: Dump extension modules on fatal error (GH-24207)Victor Stinner2021-01-181-0/+13
| | | | | | | The Py_FatalError() function and the faulthandler module now dump the list of extension modules on a fatal error. Add _Py_DumpExtensionModules() and _PyModule_IsExtension() internal functions.
* bpo-42519: Replace PyMem_MALLOC() with PyMem_Malloc() (GH-23586)Victor Stinner2020-12-011-3/+3
| | | | | | | | | | | No longer use deprecated aliases to functions: * Replace PyMem_MALLOC() with PyMem_Malloc() * Replace PyMem_REALLOC() with PyMem_Realloc() * Replace PyMem_FREE() with PyMem_Free() * Replace PyMem_Del() with PyMem_Free() * Replace PyMem_DEL() with PyMem_Free() Modify also the PyMem_DEL() macro to use directly PyMem_Free().
* bpo-42006: Stop using PyDict_GetItem, PyDict_GetItemString and ↵Serhiy Storchaka2020-10-261-7/+21
| | | | | | | | | | | _PyDict_GetItemId. (GH-22648) These functions are considered not safe because they suppress all internal errors and can return wrong result. PyDict_GetItemString and _PyDict_GetItemId can also silence current exception in rare cases. Remove no longer used _PyDict_GetItemId. Add _PyDict_ContainsId and rename _PyDict_Contains into _PyDict_Contains_KnownHash.
* bpo-40268: Remove unused structmember.h includes (GH-19530)Victor Stinner2020-04-151-3/+3
| | | | | | If only offsetof() is needed: include stddef.h instead. When structmember.h is used, add a comment explaining that PyMemberDef is used.
* bpo-40268: Remove a few pycore_pystate.h includes (GH-19510)Victor Stinner2020-04-141-1/+1
|
* bpo-40268: Rename _PyInterpreterState_GET_UNSAFE() (GH-19509)Victor Stinner2020-04-141-2/+2
| | | | | | | Rename _PyInterpreterState_GET_UNSAFE() to _PyInterpreterState_GET() for consistency with _PyThreadState_GET() and to have a shorter name (help to fit into 80 columns). Add also "assert(tstate != NULL);" to the function.
* bpo-40268: Include explicitly pycore_interp.h (GH-19505)Victor Stinner2020-04-141-0/+1
| | | | pycore_pystate.h no longer includes pycore_interp.h: it's now included explicitly in files accessing PyInterpreterState.
* bpo-40268: Add _PyInterpreterState_GetConfig() (GH-19492)Victor Stinner2020-04-131-2/+2
| | | | | | | | Don't access PyInterpreterState.config member directly anymore, but use new functions: * _PyInterpreterState_GetConfig() * _PyInterpreterState_SetConfig() * _Py_GetConfig()