Commit graph

772 commits

Author SHA1 Message Date
Peter Bierma
082f370cdd
gh-137514: Add a free-threading wrapper for mutexes (GH-137515)
Add `FT_MUTEX_LOCK`/`FT_MUTEX_UNLOCK`, which call `PyMutex_Lock` and `PyMutex_Unlock` on the free-threaded build, and no-op otherwise.
2025-08-07 11:24:50 -04:00
Serhiy Storchaka
3a89dfe32b
Revert "gh-112068: C API: Add support of nullable arguments in PyArg_Parse (GH-121303)" (#136991) 2025-07-22 16:39:50 +03:00
Nathan Goldbaum
89c220b93c
gh-133296: Publicly expose critical section API that accepts PyMutex (gh-135899)
This makes the following APIs public:

* `Py_BEGIN_CRITICAL_SECTION_MUTEX(mutex),`
* `Py_BEGIN_CRITICAL_SECTION2_MUTEX(mutex1, mutex2)`
* `void PyCriticalSection_BeginMutex(PyCriticalSection *c, PyMutex *mutex)`
* `void PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *mutex1, PyMutex *mutex2)`

The macros are identical to the corresponding `Py_BEGIN_CRITICAL_SECTION` and
`Py_BEGIN_CRITICAL_SECTION2` macros (e.g., they include braces), but they
accept a `PyMutex` instead of an object.

The new macros are still paired with the existing END macros
(`Py_END_CRITICAL_SECTION`, `Py_END_CRITICAL_SECTION2`).
2025-07-21 17:25:43 -04:00
Roman
09ac8e042f
Fix typo: "occured" =>"occurred" (#134928)
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
2025-07-19 11:48:04 +03:00
Petr Viktorin
9193efdeab
gh-125206: Make _Py_FFI_SUPPORT_C_COMPLEX private (GH-135932) 2025-06-26 11:48:37 +02:00
Kumar Aditya
3c0525126e
gh-134637: Fix performance regression in calling ctypes function pointer in free threading. (#134702)
Fix performance regression in calling `ctypes` function pointer in `free threading`.
2025-05-26 18:26:40 +00:00
Collin Funk
b8f55266bf
gh-134486: Fix missing alloca() symbol in _ctypes on NetBSD (#134487)
Previously the module would fail to load because the `alloca()` symbol
was undefined. Now we check for GCC/Clang builtins for systems who do
not define `alloca()` in headers.
2025-05-23 13:11:04 +02:00
Max Bachmann
74c4e35ff1
Let PyUnicode_FromWideChar calculate the input length (GH-134045) 2025-05-15 11:56:50 +00:00
Kumar Aditya
9b9cdb6440
gh-100926: use explicit stginfo lock for pointer cache (#133867) 2025-05-11 08:24:20 +00:00
Kumar Aditya
70f9b3de36
gh-100926: fix thread safety of ctypes __pointer_type__ (#133843) 2025-05-10 17:38:06 +00:00
Xuehai Pan
f0f93ba5fa
gh-131942: Use the Python-specific Py_DEBUG macro rather than _DEBUG in Windows-related C code (GH-131944) 2025-05-08 15:01:25 +00:00
Sergey B Kirpichev
1bc16504ef
gh-61103: drop unused Py_HAVE_C_COMPLEX define (GH-133435)
Py_HAVE_C_COMPLEX was added in 3.14 so the removal doesn't need a deprecation
period even under a strict reading of PEP 387.

The Py_FFI_SUPPORT_C_COMPLEX check configure check implies support for
complex types in ctypes.
2025-05-05 15:50:57 +02:00
Sergey B Kirpichev
8d0e07eb89
gh-61103: don't use C _Complex types to implement F/D/G in ctypes (GH-133237)
According to the C standard, the memory representation of _Complex types
is equivalent to 2-element arrays. Unlike _Complex, arrays are always available.

- drop _complex.h header
- use appropriate real arrays to replace complex types

Co-authored-by: Lisandro Dalcin <dalcinl@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
2025-05-05 11:23:30 +02:00
Sergey Miryanov
a0bc0c462f
gh-100926: Move ctype's pointers cache from _pointer_type_cache to StgInfo (GH-131282)
Deprecate _pointer_type_cache and calling POINTER on a string.

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2025-05-02 19:06:37 +02:00
Petr Viktorin
2590774c9b
gh-133290: Use PyObject_SetAttr to set _type_ (GH-133292) 2025-05-02 14:47:07 +02:00
Sergey B Kirpichev
580888927c
gh-121249: fix naming of struct tagPyCArgObject members (#132863)
It seems, no code actually uses these names, only sizes of the unnamed
union members are important.  Though, I think it's good to be here
consistent wrt type codes ('g' for long double, etc).

This amends 85f89cb3e6.
2025-04-24 11:50:47 +02:00
Sergey B Kirpichev
85f89cb3e6
gh-121249: adjust formatting codes for complex types in struct/ctypes (#132827)
* F - for float _Complex
* D - for double _Complex
* G - for long double _Complex (not supported by the struct module)
2025-04-23 15:38:24 +02:00
dura0ok
3b4b56f46d
gh-132470: Prevent crash in ctypes.CField when byte_size is incorrect (#132475)
Fix: Prevent crash in ctypes.CField when byte_size does not match type size  (gh-132470)

When creating a ctypes.CField with an incorrect byte_size (e.g., using `byte_size=2` for `ctypes.c_byte`), the code would previously abort due to the failed assertion `byte_size == info->size`.

This commit replaces the assertion with a proper error handling mechanism that raises a `ValueError` when `byte_size` does not match the expected type size. This prevents the crash and provides a more informative error message to the us

Co-authored-by: sobolevn <mail@sobolevn.me>
2025-04-22 16:13:00 +03:00
Kumar Aditya
5b06d2ceca
gh-127945: add lock held assertions in ctypes arrays (#132720) 2025-04-19 18:27:10 +05:30
Kumar Aditya
cf59bc3ae7
gh-127945: fix critical sections around ctypes array (#132646) 2025-04-17 18:44:14 +00:00
Kumar Aditya
2925462411
gh-127945: change _ctypes_test.c static globals to thread local (#132575) 2025-04-16 23:11:27 +05:30
Kumar Aditya
2ff5eb8582
gh-127945: move initialization of field desc to module exec in ctypes (#132552) 2025-04-15 19:30:33 +05:30
Kumar Aditya
be763e550e
gh-127945: fix thread safety and add lock held assertions to paramfunc in ctypes (#132473) 2025-04-14 14:05:06 +05:30
Gregory P. Smith
7a29c9883f
GH-115322: fix ctypes call_function audit hook on 32-bit platforms (GH-132496)
* GH-115322: fix ctypes call_function audit hook on 32-bit platforms.

It was using a signed conversion to communicate the function id (pointer) value.
2025-04-14 06:22:29 +00:00
Robin Jadoul
2666a06d33
GH-115322: Add missing audit hooks (GH-115624)
Add extra audit hooks to catch C function calling from ctypes,
reading/writing files through readline and executing external
programs through _posixsubprocess.

* Make audit-tests for open pass when readline.append_history_file is unavailable
* Less direct testing of _posixsubprocess for audit hooks
* Also remove the audit hook from call_cdeclfunction now that _ctypes_callproc does it instead.
* reword the NEWS entry.
* mention readline in NEWS
* add versionchanged markers
* fix audit_events.rst versionadded
* doc lint

---------

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2025-04-13 21:46:20 +00:00
Nicolas Trangez
2aab2db146
GH-132417: ctypes: Fix potential Py_DECREF(NULL) when handling functions returning PyObject * (#132418)
Some functions (such as `PyErr_Occurred`) with a `restype` set to `ctypes.py_object` may return NULL without setting an exception.
2025-04-12 07:40:34 +00:00
Kumar Aditya
d47584aae6
gh-131336: fix thread safety for ctypes functions (#132232) 2025-04-09 18:18:40 +00:00
Serhiy Storchaka
487cf3c170
gh-132305: Make Argument Clinic code compatible with Python 3.10 (#132306) 2025-04-09 19:10:02 +01:00
Serhiy Storchaka
f5f1ac84b3
gh-112068: C API: Add support of nullable arguments in PyArg_Parse (GH-121303) 2025-04-08 22:08:00 +03:00
Peter Bierma
8e260b384a
gh-128182: switch ctypes locking to critical sections (#132133)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-04-07 22:00:31 +05:30
Kumar Aditya
0dba59e689
gh-128182: add critical sections to ctypes arrays getters and setters (#132152) 2025-04-06 16:21:44 +05:30
Kumar Aditya
b9d8d99563
gh-128182: add critical section to _ctypes.Simple getters and setters (#132081) 2025-04-05 10:27:41 +00:00
Kumar Aditya
f7a8bc50db
gh-128182: add critical section to _ctypes.PyCData methods (#132082) 2025-04-05 15:24:58 +05:30
Peter Bierma
6e91d1f9aa
gh-131974: Fix usages of locked_deref in ctypes (#131975) 2025-04-02 17:04:25 +02:00
Kumar Aditya
46ada1e5e0
gh-127945: acquire critical section around PyCFuncPtr_call (#131898) 2025-03-30 11:01:12 +00:00
Kumar Aditya
28e476f6a2
gh-127945: make initialization of error_object_name thread safe in ctypes (#131896) 2025-03-30 10:50:35 +00:00
Kumar Aditya
bc5a028c13
gh-127945: fix thread safety of creating instances of ctypes structures (#131716) 2025-03-30 15:22:30 +05:30
Michael Droettboom
8614f86b71
gh-131525: Cache the result of tuple_hash (#131529)
* gh-131525: Cache the result of tuple_hash

* Fix debug builds

* Add blurb

* Fix formatting

* Pre-compute empty tuple singleton

* Mostly set the cache within tuple_alloc

* Fixes for TSAN

* Pre-compute empty tuple singleton

* Fix for 32-bit platforms

* Assert that op != NULL in _PyTuple_RESET_HASH_CACHE

* Use FT_ATOMIC_STORE_SSIZE_RELAXED macro

* Update Include/internal/pycore_tuple.h

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Fix alignment

* atomic load

* Update Objects/tupleobject.c

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
2025-03-27 09:57:06 -04:00
Kumar Aditya
f1967e7249
gh-127945: fix thread safety of ctypes state (#131710)
This fixes thread safety of `array_cache` and `swapped_suffix` by initializing them in module exec to make it thread safety.
2025-03-25 17:03:05 +05:30
Kumar Aditya
96ef4c511f
gh-127945: add locking to malloc closure in free-threading (#131662)
The freelist is not thread safe in free-threading so this adds lock around it make it thread safe in free-threading.
2025-03-25 16:48:46 +05:30
Victor Stinner
abcd9d4f7d
gh-111178: Fix function signatures for test_ctypes (#131660) 2025-03-24 14:30:13 +01:00
Petr Viktorin
0e53038ea8
gh-128715: Expose ctypes.CField, with info attributes (GH-128950)
- Restore max field size to sys.maxsize, as in Python 3.13 & below
- PyCField: Split out bit/byte sizes/offsets.
- Expose CField's size/offset data to Python code
- Add generic checks for all the test structs/unions, using the newly exposed attrs
2025-03-24 14:18:34 +01:00
Sergey Miryanov
62fb15d866
gh-131311: Extract _replace_array_elements from PyCStructUnionType_update_stginfo (GH-131504) 2025-03-24 13:55:09 +01:00
Kumar Aditya
04d4aacaac
gh-128485: ensure that dlmalloc initializes itself at import time in ctypes (#131633) 2025-03-24 18:14:25 +05:30
Sergey Miryanov
9c4fb92e12
gh-131311: Fix additional memory leaks in ctypes (GH-131429)
* Visit keep in StructParam_traverse
* Decref swapped in PyCSimpleType_init
* Decref ob in make_funcptrtype_dict
* Check Pointer_item result while constructing result list in Pointer_subscript

* Fix align and size naming in PyCStructUnionType_update_stginfo
  - as discussed in previous PR
2025-03-24 13:42:10 +01:00
Victor Stinner
20c5f969dd
gh-131238: Remove more includes from pycore_interp.h (#131480) 2025-03-19 23:01:32 +01:00
Sergey Miryanov
812074e291
gh-131311: Consolidate reference handling in PyCStructUnionType_update_stginfo to fix memory leak (GH-131312) 2025-03-18 11:50:22 +01:00
Victor Stinner
dc804ffb2f
gh-128911: Use PyImport_ImportModuleAttr() function (#129657)
* Replace PyImport_ImportModule() + PyObject_GetAttr() with
  PyImport_ImportModuleAttr().
* Replace PyImport_ImportModule() + PyObject_GetAttrString() with
  PyImport_ImportModuleAttrString().
2025-02-05 11:03:58 +01:00
Serhiy Storchaka
9d63ae5fe5
gh-129502: Fix handling errors in ctypes callbacks (GH-129504)
Unlikely errors in preparing arguments for ctypes callback are now
handled in the same way as errors raised in the callback of in converting
the result of the callback -- using sys.unraisablehook() instead of
sys.excepthook() and not setting sys.last_exc and other variables.
2025-01-31 15:30:19 +02:00
Victor Stinner
0373926260
gh-129354: Use PyErr_FormatUnraisable() function (#129511)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-31 13:16:08 +01:00