Commit graph

117 commits

Author SHA1 Message Date
Jelle Zijlstra dc40226ea1
[3.12] gh-119311: Fix name mangling with PEP 695 generic classes (#119464) (#119644)
* [3.12] gh-119311: Fix name mangling with PEP 695 generic classes (#119464)

Fixes #119311. Fixes #119395.

(cherry picked from commit a9a74da4a0)
2024-06-04 19:55:45 +00:00
Eric Snow 0d5fe2c7b4
[3.12] gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters (gh-119331) (gh-119425)
_PyArg_Parser holds static global data generated for modules by Argument Clinic.  The _PyArg_Parser.kwtuple field is a tuple object, even though it's stored within a static global.  In some cases the tuple is statically allocated and thus it's okay that it gets shared by multiple interpreters.  However, in other cases the tuple is set lazily, allocated from the heap using the active interprepreter at the point the tuple is needed.

This is a problem once that interpreter is destroyed since _PyArg_Parser.kwtuple becomes at dangling pointer, leading to crashes.  It isn't a problem if the tuple is allocated under the main interpreter, since its lifetime is bound to the lifetime of the runtime.  The solution here is to temporarily switch to the main interpreter.  The alternative would be to always statically allocate the tuple.

This change also fixes a bug where only the most recent parser was added to the global linked list.

(cherry picked from commit 81865002ae)
2024-05-22 22:26:58 +00:00
Miss Islington (bot) 27941b14ab
[3.12] gh-102468: Document PyCFunction_New* and PyCMethod_New (GH-112557) (GH-114119)
gh-102468: Document `PyCFunction_New*` and `PyCMethod_New` (GH-112557)
(cherry picked from commit a482bc67ee)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
2024-01-17 10:21:08 +01:00
Miss Islington (bot) 6f90399c23
[3.12] gh-113696: Docs: Annotate PyObject_CallOneArg and PyObject_CallNoArgs as returning a strong reference (GH-113697) (#113698)
(cherry picked from commit 1ae7ceba29)

Co-authored-by: Jamie Phan <jamie@ordinarylab.dev>
2024-01-05 01:17:15 +01:00
Eric Snow 1e1a30f9f4
[3.12] gh-110310: Add a Per-Interpreter XID Registry for Heap Types (gh-110311) (gh-110714)
We do the following:

* add a per-interpreter XID registry (PyInterpreterState.xidregistry)
* put heap types there (keep static types in _PyRuntimeState.xidregistry)
* clear the registries during interpreter/runtime finalization
* avoid duplicate entries in the registry (when _PyCrossInterpreterData_RegisterClass() is called more than once for a type)
* use Py_TYPE() instead of PyObject_Type() in _PyCrossInterpreterData_Lookup()

The per-interpreter registry helps preserve isolation between interpreters.  This is important when heap types are registered, which is something we haven't been doing yet but I will likely do soon.

(cherry-picked from commit 80dc39e1dc)
2023-11-28 02:36:29 +00:00
Eric Snow 0122b4d7c9
[3.12] gh-105716: Support Background Threads in Subinterpreters Consistently (gh-109921) (gh-110707)
The existence of background threads running on a subinterpreter was preventing interpreters from getting properly destroyed, as well as impacting the ability to run the interpreter again. It also affected how we wait for non-daemon threads to finish.

We add PyInterpreterState.threads.main, with some internal C-API functions.

(cherry-picked from commit 1dd9dee45d)
2023-11-27 19:01:05 -07:00
Eric Snow 82ae5a609d
[3.12] gh-109793: Allow Switching Interpreters During Finalization (gh-109794) (gh-110705)
Essentially, we should check the thread ID rather than the thread state pointer.
2023-11-28 00:58:02 +00:00
Eric Snow 4f71f1680d
[3.12] gh-106931: Intern Statically Allocated Strings Globally (gh-107272) (gh-110713)
We tried this before with a dict and for all interned strings.  That ran into problems due to interpreter isolation.  However, exclusively using a per-interpreter cache caused some inconsistency that can eliminate the benefit of interning.  Here we circle back to using a global cache, but only for statically allocated strings.  We also use a more-basic _Py_hashtable_t for that global cache instead of a dict.

Ideally we would only have the global cache, but the optional isolation of each interpreter's allocator means that a non-static string object must not outlive its interpreter.  Thus we would have to store a copy of each such interned string in the global cache, tied to the main interpreter.

(cherry-picked from commit b72947a8d2)
2023-11-27 23:51:12 +00:00
Eric Snow 313554457e
[3.12] gh-109853: Fix sys.path[0] For Subinterpreters (gh-109994) (gh-110701)
This change makes sure sys.path[0] is set properly for subinterpreters.  Before, it wasn't getting set at all.

This change does not address the broader concerns from gh-109853.

(cherry-picked from commit a040a32ea2)
2023-11-27 22:21:12 +00:00
Mark Shannon 7ee021f999
[3.12] GH-108390: Prevent non-local events being set with sys.monitoring.set_local_events() (GH-108420) (#108899)
* GH-108390: Prevent non-local events being set with `sys.monitoring.set_local_events()` (GH-108420)

* Restore generated objects

* Restore size of monitoring arrays in code object for 3.12 ABI compatibility.

* Update ABI file
2023-09-05 11:29:38 +00:00
Miss Islington (bot) 00bfed7cba
[3.12] gh-91051: fix segfault when using all 8 type watchers (GH-107853) (#107876)
* gh-91051: fix segfault when using all 8 type watchers (GH-107853)
(cherry picked from commit 66e4edd734)

Co-authored-by: Carl Meyer <carl@oddbird.net>
2023-08-16 11:58:54 +02:00
Eric Snow da151fdc7a
[3.12] gh-105699: Use a _Py_hashtable_t for the PyModuleDef Cache (gh-106974) (gh-107412)
gh-105699: Use a _Py_hashtable_t for the PyModuleDef Cache (gh-106974)

This fixes a crasher due to a race condition, triggered infrequently when two isolated (own GIL) subinterpreters simultaneously initialize their sys or builtins modules.  The crash happened due the combination of the "detached" thread state we were using and the "last holder" logic we use for the GIL.  It turns out it's tricky to use the same thread state for different threads.  Who could have guessed?

We solve the problem by eliminating the one object we were still sharing between interpreters.  We replace it with a low-level hashtable, using the "raw" allocator to avoid tying it to the main interpreter.

We also remove the accommodations for "detached" thread states, which were a dubious idea to start with.

(cherry picked from commit 8ba4df91ae)
2023-07-28 23:16:12 +00:00
Mark Shannon 3b1a4c1842
[3.12] GH-106897: Add RERAISE event to sys.monitoring. (GH-107291) (GH-107346)
* Ensures that exception handling events are balanced. Each [re]raise event has a matching unwind/handled event.
2023-07-28 09:48:35 +01:00
Pablo Galindo Salgado 20315d141a
[3.12] gh-106140: Reorder some more fields to facilitate out-of-process inspection (GH-106148) (#106155)
(cherry picked from commit 9126a6a9ce)
2023-06-27 18:33:01 +00:00
Miss Islington (bot) ac80beb1ec
[3.12] gh-106140: Reorder some fields to facilitate out-of-process inspection (GH-106143) (#106147)
gh-106140: Reorder some fields to facilitate out-of-process inspection (GH-106143)
(cherry picked from commit 2d5a1c2811)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2023-06-27 17:44:37 +00:00
Eric Snow 33d3069c45
[3.12] gh-104812: Run Pending Calls in any Thread (gh-104813) (gh-105752)
For a while now, pending calls only run in the main thread (in the main interpreter).  This PR changes things to allow any thread run a pending call, unless the pending call was explicitly added for the main thread to run.
(cherry picked from commit 757b402)
2023-06-14 00:50:08 +00:00
Miss Islington (bot) 77c03a3b72
[3.12] gh-100227: Lock Around Modification of the Global Allocators State (gh-105516) (gh-105532)
The risk of a race with this state is relatively low, but we play it safe anyway. We do avoid using the lock in performance-sensitive cases where the risk of a race is very, very low.
(cherry picked from commit 68dfa49627)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
2023-06-08 22:35:53 +00:00
Eric Snow b08ea9a561
[3.12] gh-100227: Lock Around Adding Global Audit Hooks (gh-105515) (gh-105525)
The risk of a race with this state is relatively low, but we play it safe anyway.
(cherry picked from commit e822a676f1)
2023-06-08 21:05:47 +00:00
Miss Islington (bot) 2ad2bd8b14
[3.12] gh-100227: Lock Around Use of the Global "atexit" State (gh-105514) (gh-105517)
The risk of a race with this state is relatively low, but we play it safe anyway.
(cherry picked from commit 7799c8e678)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
2023-06-08 19:27:44 +00:00
Miss Islington (bot) c38ceb032d
[3.12] gh-105020: Share tp_bases and tp_mro Between Interpreters For All Static Builtin Types (gh-105115) (gh-105124)
In gh-103912 we added tp_bases and tp_mro to each PyInterpreterState.types.builtins entry.  However, doing so ignored the fact that both PyTypeObject fields are public API, and not documented as internal (as opposed to tp_subclasses).  We address that here by reverting back to shared objects, making them immortal in the process.
(cherry picked from commit 7be667d)

Co-authored-by: Eric Snow ericsnowcurrently@gmail.com
2023-06-01 22:24:55 +00:00
Miss Islington (bot) 9ae49e3f3b
gh-88745: Add _winapi.CopyFile2 and update shutil.copy2 to use it (GH-105055)
(cherry picked from commit cda1bd3c9d)

Co-authored-by: Steve Dower <steve.dower@python.org>
2023-05-30 20:33:17 +01:00
Miss Islington (bot) 7899fac3c5
[3.12] gh-104799: Move location of type_params AST fields (GH-104828) (#104974)
gh-104799: Move location of type_params AST fields (GH-104828)
(cherry picked from commit ba73473f4c)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-05-30 14:33:01 +00:00
Petr Viktorin b44beac5e1
[3.12] gh-87891: Add ABI check to CI (#104793)
Backport the workflow change and fix-ups:
- GH-92442 (e89c01eac7)
- GH-94129 (0dadb2249a)
- GH-98556 (194588decc)

Co-Authored-By: sterliakov <50529348+sterliakov@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2023-05-23 10:56:14 +02:00
Petr Viktorin cd9a56c2b0
gh-103509: PEP 697 -- Limited C API for Extending Opaque Types (GH-103511)
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
2023-05-04 09:56:53 +02:00
Irit Katriel 3f9285a8c5
gh-102755: Add PyErr_DisplayException(exc) (#102756) 2023-03-16 22:18:04 +00:00
Erlend E. Aasland 02d9f1504b
gh-101578: Amend exception docs (#102057)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
2023-02-21 09:15:49 +01:00
Erlend E. Aasland 60bbed7f17
gh-101578: Amend PyErr_{Set,Get}RaisedException docs (#101962)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
2023-02-19 21:22:29 +01:00
Mark Shannon feec49c407
GH-101578: Normalize the current exception (GH-101607)
* Make sure that the current exception is always normalized.

* Remove redundant type and traceback fields for the current exception.

* Add new API functions: PyErr_GetRaisedException, PyErr_SetRaisedException

* Add new API functions: PyException_GetArgs, PyException_SetArgs
2023-02-08 09:31:12 +00:00
Zackery Spytz 62a5dc13e9
bpo-43327: Fix the docs for PyImport_ImportFrozenModuleObject() (#24659)
The docs stated that PyImport_ImportFrozenModuleObject() returns a
new reference, but it actually returns an int.

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
2022-11-27 11:57:41 +05:30
Petr Viktorin 4d82f628c4
gh-47146: Soft-deprecate structmember.h, expose its contents via Python.h (GH-99014)
The ``structmember.h`` header is deprecated, though it continues to be available
and there are no plans to remove it. There are no deprecation warnings. Old code
can stay unchanged (unless the extra include and non-namespaced macros bother
you greatly). Specifically, no uses in CPython are updated -- that would just be
unnecessary churn.
The ``structmember.h`` header is deprecated, though it continues to be
available and there are no plans to remove it.

Its contents are now available just by including ``Python.h``,
with a ``Py`` prefix added if it was missing:

- `PyMemberDef`, `PyMember_GetOne` and`PyMember_SetOne`
- Type macros like `Py_T_INT`, `Py_T_DOUBLE`, etc.
  (previously ``T_INT``, ``T_DOUBLE``, etc.)
- The flags `Py_READONLY` (previously ``READONLY``) and
  `Py_AUDIT_READ` (previously all uppercase)

Several items are not exposed from ``Python.h``:

- `T_OBJECT` (use `Py_T_OBJECT_EX`)
- `T_NONE` (previously undocumented, and pretty quirky)
- The macro ``WRITE_RESTRICTED`` which does nothing.
- The macros ``RESTRICTED`` and ``READ_RESTRICTED``, equivalents of
  `Py_AUDIT_READ`.
- In some configurations, ``<stddef.h>`` is not included from ``Python.h``.
  It should be included manually when using ``offsetof()``.

The deprecated header continues to provide its original
contents under the original names.
Your old code can stay unchanged, unless the extra include and non-namespaced
macros bother you greatly.

There is discussion on the issue to rename `T_PYSSIZET` to `PY_T_SSIZE` or
similar. I chose not to do that -- users will probably copy/paste that with any
spelling, and not renaming it makes migration docs simpler.


Co-Authored-By: Alexander Belopolsky <abalkin@users.noreply.github.com>
Co-Authored-By: Matthias Braun <MatzeB@users.noreply.github.com>
2022-11-22 08:25:43 +01:00
David Hewitt e98923c0be
gh-98410: move getbufferproc and releasebufferproc to buffer.h (#31158)
This adds them to the Limited API.
2022-10-31 15:01:32 +01:00
Wenzel Jakob e60892f9db
gh-98586: Add vector call APIs to the Limited API (GH-98587)
Expose the facilities for making vector calls through Python's limited API.
2022-10-27 11:45:42 +02:00
Pablo Galindo Salgado e34c82abeb
GH-93503: Add thread-specific APIs to set profiling and tracing functions in the C-API (#93504)
* gh-93503: Add APIs to set profiling and tracing functions in all threads in the C-API

* Use a separate API

* Fix NEWS entry

* Add locks around the loop

* Document ignoring exceptions

* Use the new APIs in the sys module

* Update docs
2022-08-24 23:21:39 +01:00
Petr Viktorin 656dad702d
gh-93274: Expose receiving vectorcall in the Limited API (GH-95717) 2022-08-08 14:12:05 +02:00
Wenzel Jakob 5e34b494a0
gh-60074: add new stable API function PyType_FromMetaclass (GH-93012)
Added a new stable API function ``PyType_FromMetaclass``, which mirrors
the behavior of ``PyType_FromModuleAndSpec`` except that it takes an
additional metaclass argument. This is, e.g., useful for language
binding tools that need to store additional information in the type
object.
2022-05-27 10:27:39 +02:00
Victor Stinner 059b5baf98
gh-85858: Remove PyUnicode_InternImmortal() function (#92579)
Remove the PyUnicode_InternImmortal() function and the
SSTATE_INTERNED_IMMORTAL macro.

The PyUnicode_InternImmortal() function is still exported in the
stable ABI. The function is removed from the API.

PyASCIIObject.state.interned size is now a single bit, rather than 2
bits.

Keep SSTATE_NOT_INTERNED and SSTATE_INTERNED_MORTAL macros for
backward compatibility, but no longer use them internally since the
interned member is now a single bit and so can only have two values
(interned or not interned).

Update stats of _PyUnicode_ClearInterned().
2022-05-13 13:40:22 +02:00
Inada Naoki f9c9354a7a
gh-92536: PEP 623: Remove wstr and legacy APIs from Unicode (GH-92537) 2022-05-12 14:48:38 +09:00
Irit Katriel 5d421d7342
gh-90501: Add PyErr_GetHandledException and PyErr_SetHandledException (GH-30531) 2022-04-15 19:57:47 +01:00
Petr Viktorin 1c2fddddae
Add feature macro PY_HAVE_THREAD_NATIVE_ID to the stable ABI definition (GH-32365) 2022-04-08 14:35:11 +02:00
Petr Viktorin d79f118d04
bpo-47115: Document which parts of structs are in limited API/stable ABI (GH-32196)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
2022-04-06 16:50:45 +02:00
Christian Heimes f66c857572
bpo-45459: Add Py_buffer to limited API (GH-29991)
- [x] ``Py_buffer`` struct
- [x] ``PyBuffer_*()`` API functions
- [x] ``PyBUF_*`` constants
- [x] ``Py_bf_getbuffer`` and ``Py_bf_releasebuffer`` type slots
- [x] ``PyMemoryView_FromBuffer()`` API
- [x] tests for limited API
- [x] ``make regen-limited-abi``
- [x] documentation update
- [ ] export ``PyPickleBuffer*()`` API ???
2022-02-02 07:03:10 -08:00
Gabriele N. Tornetta 50669083fe
bpo-43931: Export Python version as API data (GH-25577)
When Python is embedded in other applications, it is not easy to determine which version of Python is being used. This change exposes the Python version as part of the API data. Tools like Austin (https://github.com/P403n1x87/austin) can benefit from this data when targeting applications like uWSGI, as the Python version can then be inferred systematically by looking at the exported symbols rather than relying on unreliable pattern matching or other hacks (like remote code execution etc...).

Automerge-Triggered-By: GH:pablogsal
2021-12-09 17:52:05 -08:00
Irit Katriel f30ad65dbf
bpo-45292: [PEP 654] add the ExceptionGroup and BaseExceptionGroup classes (GH-28569) 2021-10-23 00:13:46 +01:00
Ken Jin 2cbf50e812
bpo-44220: Export PyStructSequence_UnnamedField in the limited API (GH-26331) 2021-10-21 10:46:48 +02:00
Yury Selivanov 2c3474a637
bpo-45123: PyAiter_Check and PyObject_GetAiter fix & rename. (GH-28194)
Fix PyAiter_Check to only check for the `__anext__` presense (not for
`__aiter__`). Rename `PyAiter_Check()` to `PyAIter_Check()`,
`PyObject_GetAiter()` -> `PyObject_GetAIter()`.

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2021-09-07 11:52:30 +01:00
Hai Shi 3e2c643ae0
bpo-42035: Add PyType_GetQualName() to get a type's qualified name. (GH-27551) 2021-08-17 15:39:34 +02:00
Inada Naoki ce5e1a6809
bpo-41103: Resurrect the old buffer protocol. (GH-27437)
Revert "bpo-41103: Remove old buffer protocol support (#21117)"

This reverts commit 6f8a6ee59c.
2021-07-29 12:46:47 +02:00
Hai Shi a390ebea17
bpo-42035: Add a PyType_GetName() to get type's short name. (GH-23903) 2021-07-29 09:57:02 +02:00
Petr Viktorin 29987f7265
bpo-40939: Remove documentation for PyParser_* & add porting notes (GH-26855)
I tried to be relatively thorough and give lots of links.
One reason is that this wasn't deprecated very long; also it seems people running into this tend to not be familiar with similar APIs.

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-06-24 14:57:28 +02:00
Ken Jin 6773c3eaa7
bpo-44392: Add Py_GenericAlias to C API docs (GH-26724)
Also fix stable ABI type definitions
2021-06-16 07:12:25 -07:00