Commit Graph

8767 Commits

Author SHA1 Message Date
Shantanu 0a9ae5ed48
gh-130932: Fix incorrect exception handling in _PyModule_IsPossiblyShadowing (#130934)
I chose to not raise an exception here because I think it would be
confusing for module attribute access to start raising something other
than AttributeError if e.g. the cwd goes away

Without the change in moduleobject.c
```
./python.exe -m unittest test.test_import.ImportTests.test_script_shadowing_stdlib_cwd_failure
...
Assertion failed: (PyErr_Occurred()), function _PyObject_SetAttributeErrorContext, file object.c, line 1253.
```
2025-03-06 23:41:28 -08:00
mpage d7bb7c7817
gh-118331: Fix a couple of issues when list allocation fails (#130811)
* Fix use after free in list objects

Set the items pointer in the list object to NULL after the items array
is freed during list deallocation. Otherwise, we can end up with a list
object added to the free list that contains a pointer to an already-freed
items array.

* Mark `_PyList_FromStackRefStealOnSuccess` as escaping

I think technically it's not escaping, because the only object that
can be decrefed if allocation fails is an exact list, which cannot
execute arbitrary code when it is destroyed. However, this seems less
intrusive than trying to special cases objects in the assert in `_Py_Dealloc`
that checks for non-null stackpointers and shouldn't matter for performance.
2025-03-05 10:42:09 -08:00
Sam Gross 2905690a91
gh-130851: Don't crash when deduping unusual code constants (#130853)
The bytecode compiler only generates a few different types of constants,
like str, int, tuple, slices, etc. Users can construct code objects with
various unusual constants, including ones that are not hashable or not
even constant.

The free threaded build previously crashed with a fatal error when
confronted with these constants. Instead, treat distinct objects of
otherwise unhandled types as not equal for the purposes of deduplication.
2025-03-05 15:04:49 +01:00
Mark Shannon 78d50e91ff
GH-127705: better double free message. (GH-130785)
* Add location information when accessing already closed stackref

* Add #def option to track closed stackrefs to provide precise information for use after free and double frees.
2025-03-05 14:00:42 +00:00
Sam Gross 2f6e0e9f70
gh-130794: Process interpreter QSBR queue in _PyMem_AbandonDelayed. (gh-130808)
This avoids a case where the interpreter's queue of memory to be freed
could grow rapidly if there are many short lived threads.
2025-03-04 18:04:04 -05:00
Jelle Zijlstra dc6d66f44c
gh-105499: Merge typing.Union and types.UnionType (#105511)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Ken Jin <kenjin@python.org>
Co-authored-by: Carl Meyer <carl@oddbird.net>
2025-03-04 11:44:19 -08:00
Tomas R. e091520fdb
gh-126085: Add `tp_iter` to TypeAliasType to allow star unpacking (#127981) 2025-03-04 11:34:59 -08:00
Donghee Na 80e6d3ec49
gh-130547: Fix race between dict_dealloc and split_keys_entry_added (gh-130778) 2025-03-04 22:50:33 +09:00
Victor Stinner 0b6e98c841
gh-111178: Fix function signatures in structseq.c (#130683) 2025-03-04 10:33:45 +01:00
Victor Stinner ed8675c571
gh-111178: Fix function signatures of unicodeiter (#130684) 2025-03-04 10:33:09 +01:00
Neil Schemenauer 813bc5694b
gh-130599: use static constants str-to-int conversion (gh-130714)
Avoid a data race in free-threaded builds due to mutating global arrays at
runtime.  Instead, compute the constants with an external Python script and
then define them as static global constant arrays.  These constants are
used by `long_from_non_binary_base()`.
2025-03-03 19:00:50 -08:00
Tomas R. bbf197913c
gh-124445: Allow specializing generic ParamSpec aliases (#124512)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-03-03 14:58:33 -08:00
Sergey Miryanov 3a7f17c7e2
gh-130790: Remove references about unicode's readiness from comments (#130801) 2025-03-03 19:18:09 +00:00
Bénédikt Tran ddc27f9c38
gh-128974: Fix `UnicodeError.__str__` when custom attributes have side-effects (#128975)
Fix some crashes when (custom) attributes of `UnicodeError` objects implement `object.__str__` with side-effects.
2025-03-01 12:01:20 +01:00
Hugo Beauzée-Luyssen 830f04b505
Postpone <stdbool.h> inclusion after Python.h (#130641)
Remove inclusions prior to Python.h.

<stdbool.h> will cause <features.h> to be included before Python.h can
define some macros to enable some additional features, causing multiple
types not to be defined down the line.
2025-02-28 10:09:27 +01:00
Tian Gao 6140b0896e
gh-127271: Remove the PyCell_Get usage for framelocalsproxy (#130383) 2025-02-27 18:12:04 -05:00
Tomasz Pytel e85f81f430
gh-129107: fix thread safety of `bytearray` where two critical sections are needed (#130227) 2025-02-27 20:29:58 +05:30
Victor Stinner daeb0efaf4
gh-111178: Fix function signatures in sliceobject.c (#130575)
Rename slicehash() to slice_hash() for consistency.
2025-02-26 21:37:22 +01:00
Victor Stinner d5d4cbbd8f
gh-111178: Fix function signatures in namespaceobject.c (#130590) 2025-02-26 21:35:56 +01:00
Sam Gross 45bc120d45
gh-130519: Fix crash in QSBR when destructor reenters QSBR (gh-130553)
The `free_work_item()` function in QSBR may call arbitrary code via
Python object destructors, which may reenter the QSBR code. Reorder
the processing of work items to be robust to reentrancy.

Also fix the TODO for the out of memory situation.
2025-02-26 14:55:15 -05:00
Neil Schemenauer baae9cb159
gh-117657: Use an atomic store to set type flags. (gh-127588)
The `PyType_HasFeature()` function reads the flags with a relaxed atomic
load and without holding the type lock.  To avoid data races, use atomic
stores if `PyType_Ready()` has already been called.
2025-02-25 21:24:20 -08:00
Serhiy Storchaka 0ef4ffeefd
gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
2025-02-25 23:04:27 +02:00
Sam Gross f963239ff1
gh-130202: Fix bug in `_PyObject_ResurrectEnd` in free threaded build (gh-130281)
This fixes a fairly subtle bug involving finalizers and resurrection in
debug free threaded builds: if `_PyObject_ResurrectEnd` returns `1`
(i.e., the object was resurrected by a finalizer), it's not safe to
access the object because it might still be deallocated. For example:

 * The finalizer may have exposed the object to another thread. That
   thread may hold the last reference and concurrently deallocate it any
   time after `_PyObject_ResurrectEnd()` returns `1`.
 * `_PyObject_ResurrectEnd()` may call `_Py_brc_queue_object()`, which
   may internally deallocate the object immediately if the owning thread
   is dead.

Therefore, it's important not to access the object after it's
resurrected. We only violate this in two cases, and only in debug
builds:

 * We assert that the object is tracked appropriately. This is now moved
   up betewen the finalizer and the `_PyObject_ResurrectEnd()` call.

 * The `--with-trace-refs` builds may need to remember the object if
   it's resurrected. This is now handled by `_PyObject_ResurrectStart()`
   and `_PyObject_ResurrectEnd()`.

Note that `--with-trace-refs` is currently disabled in `--disable-gil`
builds because the refchain hash table isn't thread-safe, but this
refactoring avoids an additional thread-safety issue.
2025-02-25 12:03:28 -05:00
Sergey B Kirpichev f39a07be47
gh-87790: support thousands separators for formatting fractional part of floats (#125304)
```pycon
>>> f"{123_456.123_456:_._f}"  # Whole and fractional
'123_456.123_456'
>>> f"{123_456.123_456:_f}"    # Integer component only
'123_456.123456'
>>> f"{123_456.123_456:._f}"   # Fractional component only
'123456.123_456'
>>> f"{123_456.123_456:.4_f}"  # with precision
'123456.1_235'
```
2025-02-25 16:27:07 +01:00
Bénédikt Tran ecde940025
gh-111178: fix UBSan failures in `Objects/typevarobject.c` (GH-129800)
Fix UBSan failures for `typealiasobject`, `paramspecobject`, `typevarobject`, `typevartupleobject`, `paramspecattrobject`

Use _PyCFunction_CAST macros

Use macro for `constevaluatorobject` casts
2025-02-25 13:12:47 +01:00
Bénédikt Tran 3d40317ed2
gh-111178: fix UBSan failures in `Objects/typeobject.c` (#129799)
Fix UBSan failures for `PyTypeObject`.
Introduce a macro cast for `superobject` and remove redundant casts.
Rename the unused parameter in getter/setter methods  to `closure`
for semantic purposes.
2025-02-25 10:44:59 +00:00
Mark Shannon 014223649c
GH-130396: Use computed stack limits on linux (GH-130398)
* Implement C recursion protection with limit pointers for Linux, MacOS and Windows

* Remove calls to PyOS_CheckStack

* Add stack protection to parser

* Make tests more robust to low stacks

* Improve error messages for stack overflow
2025-02-25 09:24:48 +00:00
Petr Viktorin ef29104f7d
GH-91079: Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now (GH130413)
Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now

Unfortunatlely, the change broke some buildbots.

This reverts commit 2498c22fa0.
2025-02-24 11:16:08 +01:00
Dino Viehland 6c450f44c2
gh-130313: Avoid locking when clearing objects (#130126)
Avoid locking when clearing objects in the free-threaded build
2025-02-20 13:32:57 -08:00
Sam Gross ca22147547
gh-111924: Fix data races when swapping allocators (gh-130287)
CPython current temporarily changes `PYMEM_DOMAIN_RAW` to the default
allocator during initialization and shutdown. The motivation is to
ensure that core runtime structures are allocated and freed using the
same allocator. However, modifying the current allocator changes global
state and is not thread-safe even with the GIL. Other threads may be
allocating or freeing objects use PYMEM_DOMAIN_RAW; they are not
required to hold the GIL to call PyMem_RawMalloc/PyMem_RawFree.

This adds new internal-only functions like `_PyMem_DefaultRawMalloc`
that aren't affected by calls to `PyMem_SetAllocator()`, so they're
appropriate for Python runtime initialization and finalization. Use
these calls in places where we previously swapped to the default raw
allocator.
2025-02-20 11:31:15 -05:00
Tian Gao ccf17323c2
gh-128396: Fix a crash when inline comprehension has the same local variable as the outside scope (#130235) 2025-02-19 12:11:17 -05:00
Mark Shannon 2498c22fa0
GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)
* Implement C recursion protection with limit pointers

* Remove calls to PyOS_CheckStack

* Add stack protection to parser

* Make tests more robust to low stacks

* Improve error messages for stack overflow
2025-02-19 11:44:57 +00:00
Tomasz Pytel 1b6bef8086
gh-129107: make `bytearray` iterator thread safe (#130096)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-02-19 15:42:45 +05:30
T. Wouters 388e1ca9f0
gh-115999: Make list and tuple iteration more thread-safe. (#128637)
Make tuple iteration more thread-safe, and actually test concurrent iteration of tuple, range and list. (This is prep work for enabling specialization of FOR_ITER in free-threaded builds.) The basic premise is:

Iterating over a shared iterable (list, tuple or range) should be safe, not involve data races, and behave like iteration normally does.

Using a shared iterator should not crash or involve data races, and should only produce items regular iteration would produce. It is not guaranteed to produce all items, or produce each item only once. (This is not the case for range iteration even after this PR.)

Providing stronger guarantees is possible for some of these iterators, but it's not always straight-forward and can significantly hamper the common case. Since iterators in general aren't shared between threads, and it's simply impossible to concurrently use many iterators (like generators), better to make sharing iterators without explicit synchronization clearly wrong.

Specific issues fixed in order to make the tests pass:

 - List iteration could occasionally fail an assertion when a shared list was shrunk and an item past the new end was retrieved concurrently. There's still some unsafety when deleting/inserting multiple items through for example slice assignment, which uses memmove/memcpy.

 - Tuple iteration could occasionally crash when the iterator's reference to the tuple was cleared on exhaustion. Like with list iteration, in free-threaded builds we can't safely and efficiently clear the iterator's reference to the iterable (doing it safely would mean extra, slow refcount operations), so just keep the iterable reference around.
2025-02-18 16:52:46 -08:00
Sam Gross b9d2ee687c
gh-129701: Fix a data race in `intern_common` in the free threaded build (GH-130089)
* gh-129701: Fix a data race in `intern_common` in the free threaded build

* Use a mutex to avoid potentially returning a non-immortalized string,
  because immortalization happens after the insertion into the interned
  dict.

* Use `Py_DECREF()` calls instead of `Py_SET_REFCNT(s, Py_REFCNT(s) - 2)`
  for thread-safety. This code path isn't performance sensistive, so
  just use `Py_DECREF()` unconditionally for simplicity.
2025-02-17 14:15:40 +01:00
Bénédikt Tran 7ea8927ec7
gh-111178: fix UBSan failures in `Objects/exceptions.c` (GH-128154)
Fix UBSan failures for `PyBaseExceptionObject`, `PyStopIterationObject`, `PySystemExitObject`, `PyImportErrorObject`, `PyOSErrorObject`,  `PyNameErrorObject`,  `PyAttributeErrorObject`, `PySyntaxErrorObject`,  `KeyError`,  `UnicodeError*`, `PyBaseExceptionGroupObject`

Remove redundant casts

Align the naming convention `Py[...]_CAST(op)` where only an assert-only type check is performed.
2025-02-17 11:03:16 +01:00
Tomasz Pytel a05433f24a
gh-129107: make `bytearray` thread safe (#129108)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-02-15 07:19:42 +00:00
Dino Viehland 5a586c3e81
gh-130123: Make __new__ wrapper be deferred (#130124)
Make __new__ wrapper be deferred
2025-02-14 12:50:54 -08:00
Stan Ulbrych 3402e133ef
gh-82045: Correct and deduplicate "isprintable" docs; add test. (GH-130118)
We had the definition of what makes a character "printable" documented in three places, giving two different definitions.
The definition in the comment on `_PyUnicode_IsPrintable` was inverted; correct that.

With that correction, the two definitions turn out to be equivalent -- but to confirm that, you have to go look up, or happen to know, that those are the only five "Other" categories and only three "Separator" categories in the Unicode character database.  That makes it hard for the reader to tell whether they really are the same, or if there's some subtle difference in the intended semantics.

Fix that by cutting the C API docs' and the C comment's copies of the subtle details, in favor of referring to the Python-level docs. That ensures it's explicit that these are all meant to agree, and also lets us concentrate improvements to the wording in one place.

Speaking of which, borrow some ideas from the C comment, along with other tweaks, to hopefully add a bit more clarity to that one newly-centralized copy in the docs.

Also add a thorough test that the implementation agrees with this definition.

Author:    Greg Price <gnprice@gmail.com>

Co-authored-by: Greg Price <gnprice@gmail.com>
2025-02-14 18:16:47 +01:00
Dino Viehland 28f5e3de57
gh-129984: Mark immortal objects as deferred (#129985)
Mark immortal objects as deferred
2025-02-13 09:01:43 -08:00
Sam Gross 0559339ccd
gh-130019: Fix data race in _PyType_AllocNoTrack (gh-130058)
The reference count fields, such as `ob_tid` and `ob_ref_shared`, may be
accessed concurrently in the free threading build by a `_Py_TryXGetRef`
or similar operation. The PyObject header fields will be initialized by
`_PyObject_Init`, so only call `memset()` to zero-initialize the remainder
of the allocation.
2025-02-13 11:50:45 -05:00
Sam Gross e09442089e
gh-130030: Fix crash on 32-bit Linux with free threading (gh-130043)
The `gc_get_refs` assertion needs to be after we check the alive and
unreachable bits. Otherwise, `ob_tid` may store the actual thread id
instead of the computed `gc_refs`, which may trigger the assertion if
the `ob_tid` looks like a negative value.

Also fix a few type warnings on 32-bit systems.
2025-02-12 18:09:15 -05:00
Mark Shannon 72f56654d0
GH-128682: Account for escapes in `DECREF_INPUTS` (GH-129953)
* Handle escapes in DECREF_INPUTS

* Mark a few more functions as escaping

* Replace DECREF_INPUTS with PyStackRef_CLOSE where possible
2025-02-12 17:44:59 +00:00
Sam Gross 57f45ee2d8
gh-128759: Fix accesses to `tp_version_tag`. (GH-129750)
We should use a relaxed atomic load in the free threading build in
`PyType_Modified()` because that's called without the type lock held.
It's not necessary to use atomics in `type_modified_unlocked()`.

We should also use `FT_ATOMIC_STORE_UINT_RELAXED()` instead of the
`UINT32` variant because `tp_version_tag` is declared as `unsigned int`.
2025-02-12 09:34:40 -05:00
Sam Gross a7427f2db9
gh-129967: Fix race condition in `repr(set)` (gh-129978)
The call to `PySequence_List()` could temporarily unlock and relock the
set, allowing the items to be cleared and return the incorrect
notation `{}` for a empty set (it should be `set()`).

Co-authored-by: T. Wouters <thomas@python.org>
2025-02-11 17:29:27 -05:00
Sam Gross f151d27159
gh-117657: Enable test_opcache under TSAN (GH-129831)
Fix a few thread-safety bugs to enable test_opcache when run with TSAN:

 * Use relaxed atomics when clearing `ht->_spec_cache.getitem`
   (gh-115999)
 * Add temporary suppression for type slot modifications (gh-127266)
 * Use atomic load when reading `*dictptr`
2025-02-11 16:53:08 -05:00
Bénédikt Tran d046421f4e
gh-111178: fix UBSan failures in `Objects/frameobject.c` (GH-129777)
fix UBSan failures for `PyFrameObject`, `PyFrameLocalsProxyObject`
2025-02-08 14:54:34 +01:00
Bénédikt Tran 12e1d3011b
gh-111178: fix UBSan failures in `Objects/floatobject.c` (GH-129776)
fix UBSan failures for `PyFloatObject`
2025-02-08 14:47:19 +01:00
Sam Gross 34379d0a59
gh-117657: Fix data race in `dict_dict_merge` (gh-129755)
Found while running `test_load_attr_module` from `test_opcache` under TSan.
2025-02-07 09:44:24 -05:00
Bogdan Romanyuk 365cf5fc23
gh-117657: Fix data race in `new_reference` for free threaded build (gh-129665) 2025-02-06 15:35:37 -05:00
Sam Gross 51b4edb1a4
gh-129668: Fix thread-safety of MemoryError freelist in free threaded build (gh-129704)
The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
2025-02-06 12:38:12 -05:00
Xuanteng Huang 55f17b77c3
gh-128714: Fix function object races in `__annotate__`, `__annotations__` and `__type_params__` in free-threading build (#129016) 2025-02-06 20:10:50 +05:30
sobolevn 63f0406d5a
gh-129643: Fix `PyList_Insert` in free-threading builds (#129680) 2025-02-06 15:54:40 +03:00
Cody Maloney 5fb019fc29
gh-129559: Add `bytearray.resize()` (GH-129560)
Add bytearray.resize() which wraps PyByteArray_Resize.

Make negative size passed to resize exception/error rather than crash in optimized builds.
2025-02-05 11:33:17 -08:00
Victor Stinner a25042e6d2
gh-129354: Use PyErr_FormatUnraisable() function (#129523)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update tests:

* test_coroutines
* test_exceptions
* test_generators
* test_struct
2025-02-05 10:31:59 +00:00
Kumar Aditya fb5d1c9236
gh-129643: fix thread safety of `PyList_SetItem` (#129644) 2025-02-05 13:08:02 +05:30
Yan Yanchii e6c76b947b
GH-128872: Remove unused argument from _PyCode_Quicken (GH-128873)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
2025-02-02 15:09:30 -08: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
Victor Stinner 95504f429e
gh-129354: Fix grammar in PyErr_FormatUnraisable() (#129475)
Replace "on verb+ing" with "while verb+ing".
2025-01-31 09:45:35 +01:00
Victor Stinner 4e47e05045
gh-129354: Use PyErr_FormatUnraisable() function (#129435)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-30 16:09:38 +01:00
Victor Stinner a810cb89f1
gh-89188: Implement PyUnicode_KIND() as a function (#129412)
Implement PyUnicode_KIND() and PyUnicode_DATA() as function, in
addition to the macros with the same names. The macros rely on C bit
fields which have compiler-specific layout.
2025-01-30 11:27:27 +00:00
Victor Stinner 3bebe46d34
gh-128911: Add PyImport_ImportModuleAttr() function (#128912)
Add PyImport_ImportModuleAttr() and
PyImport_ImportModuleAttrString() functions.

* Add unit tests.
* Replace _PyImport_GetModuleAttr()
  with PyImport_ImportModuleAttr().
* Replace _PyImport_GetModuleAttrString()
  with PyImport_ImportModuleAttrString().
* Remove "pycore_import.h" includes, no longer needed.
2025-01-30 11:17:29 +00:00
Pieter Eendebak a29221675e
gh-127119: Faster check for small ints in long_dealloc (GH-127620) 2025-01-29 15:22:18 +00:00
Pieter Eendebak 1a80214f11
gh-126703: Add freelists for list and tuple iterators (GH-128592) 2025-01-29 09:15:24 +00:00
Brandt Bucher 828b27680f
GH-126599: Remove the PyOptimizer API (GH-129194) 2025-01-28 16:10:51 -08:00
Pieter Eendebak 64c417dee5
gh-112075: Remove critical section in dict.get (gh-129336)
The `dict.get` implementation uses `_Py_dict_lookup_threadsafe`, which is
thread-safe, so we remove the critical section from the argument clinic.

Add a test for concurrent dict get and set operations.
2025-01-28 21:55:45 +00:00
Sam Gross d23f5701ad
gh-128844: Make `_Py_TryIncref` public as an unstable API. (#128926)
This exposes `_Py_TryIncref` as `PyUnstable_TryIncref()` and the helper
function `_PyObject_SetMaybeWeakref` as `PyUnstable_EnableTryIncRef`.

These are helpers for dealing with unowned references in a safe way,
particularly in the free threading build.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
2025-01-28 19:32:27 +00:00
Peter Bierma 3fb5f6eb9b
gh-128509: Add `PyUnstable_IsImmortal` for finding immortal objects (GH-129182)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2025-01-27 14:36:33 +01:00
Victor Stinner 7ec17429d4
gh-102471: Change PyLongWriter_Discard(NULL) to do nothing (#129339)
It's convenient to be able to call PyLongWriter_Discard(NULL) in
error handling code.
2025-01-27 10:51:16 +00:00
Kumar Aditya 3f2cfd0462
gh-128421: make exception group methods thread safe (#129298) 2025-01-25 23:50:09 +05:30
Sergey B Kirpichev 233fd00f0a
gh-128863: Deprecate _PyLong_FromDigits() function (#127939) 2025-01-24 13:17:10 +01:00
Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్ రెడ్డి) ab353b31bb
gh-129149: Add fast path in PYLONG_FROM_UINT macro for compact integers (#129168)
Add fast path in PyLong_From*() functions for compact integers.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Yan Yanchii <yyanchiy@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2025-01-23 14:36:52 +00:00
Sergey B Kirpichev 5c9a63f62c
gh-128863: Deprecate _PyLong_New() function (#129212) 2025-01-23 13:50:34 +01:00
Sam Gross a10f99375e
Revert "GH-128914: Remove conditional stack effects from `bytecodes.c` and the code generators (GH-128918)" (GH-129202)
The commit introduced a ~2.5-3% regression in the free threading build.

This reverts commit ab61d3f430.
2025-01-23 09:26:25 +00:00
Victor Stinner 1d485db953
gh-128863: Deprecate _PyLong_Sign() function (#129176)
Replace _PyLong_Sign() with PyLong_GetSign().
2025-01-23 03:11:53 +01:00
Yury Selivanov 188598851d
GH-91048: Add utils for capturing async call stack for asyncio programs and enable profiling (#124640)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
Co-authored-by: Jacob Coffee <jacob@z7x.org>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2025-01-22 17:25:29 +01:00
Mikhail Efimov 8e20e42cc6
gh-125723: Fix crash with f_locals when generator frame outlive their generator (#126956)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
2025-01-22 15:50:01 +03:00
Victor Stinner 9012fa741d
gh-128863: Deprecate private C API functions (#128864)
Deprecate private C API functions:

* _PyBytes_Join()
* _PyDict_GetItemStringWithError()
* _PyDict_Pop()
* _PyThreadState_UncheckedGet()
* _PyUnicode_AsString()
* _Py_HashPointer()
* _Py_fopen_obj()

Replace _Py_HashPointer() with Py_HashPointer().

Remove references to deprecated functions.
2025-01-22 11:04:19 +00:00
Mark Shannon 470a0a68eb
GH-128682: Change a couple of functions to only steal references on success. (GH-129132)
Change PyTuple_FromStackRefSteal and PyList_FromStackRefSteal to only steal on success to avoid escaping
2025-01-22 10:51:37 +00:00
Bénédikt Tran 36f341ca3e
gh-127787: allow retrieving the clipped slice length in `_PyUnicodeError_GetParams` (GH-128980) 2025-01-21 11:45:53 +01:00
Mark Shannon f5b6356a11
GH-128563: Add new frame owner type for interpreter entry frames (GH-129078)
Add new frame owner type for interpreter entry frames
2025-01-21 10:15:02 +00:00
Mark Shannon 7239da7559
GH-127953: Make line number lookup O(1) regardless of the size of the code object (GH-128350) 2025-01-21 09:33:23 +00:00
Mark Shannon ab61d3f430
GH-128914: Remove conditional stack effects from `bytecodes.c` and the code generators (GH-128918) 2025-01-20 17:09:23 +00:00
Erlend E. Aasland da0f47ceab
gh-111178: Regen clinic and fix exceptions.c post gh-128447 (#129060) 2025-01-20 12:46:30 +00:00
Erlend E. Aasland 537296cdcd
gh-111178: Generate correct signature for most self converters (#128447) 2025-01-20 12:40:18 +01:00
Peter Bierma 4d0a6595a0
gh-128360: Add `_Py_AssertHoldsTstate` as assertion for holding a thread state (#128361)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-01-20 17:04:35 +05:30
Sam Gross d66c08aa75
gh-128923: Use zero to indicate unassigned unique id (#128925)
In the free threading build, the per thread reference counting uses a
unique id for some objects to index into the local reference count
table. Use 0 instead of -1 to indicate that the id is not assigned. This
avoids bugs where zero-initialized heap type objects look like they have
a unique id assigned.
2025-01-17 16:42:27 +01:00
Xuanteng Huang b44ff6d0df
GH-126599: Remove the "counter" optimizer/executor (GH-126853) 2025-01-16 15:57:04 -08:00
Umar Butler 8d8b854824
gh-128016: Improved invalid escape sequence warning message (#128020) 2025-01-15 18:00:54 +01:00
mpage b5ee0258bf
gh-115999: Specialize `LOAD_ATTR` for instance and class receivers in free-threaded builds (#128164)
Finish specialization for LOAD_ATTR in the free-threaded build by adding support for class and instance receivers.
2025-01-14 11:56:11 -08:00
Bénédikt Tran 402b91da87
gh-128078: Use `PyErr_SetRaisedException` in `_PyGen_SetStopIterationValue` (#128287)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-01-13 21:24:13 +05:30
Kumar Aditya 75214f87f1
gh-128421: make getters and setters of `BaseException` thread safe (#128728) 2025-01-13 20:08:33 +05:30
Bénédikt Tran 4533036e50
gh-111178: fix UBSan failures in `Objects/codeobject.c` (GH-128240) 2025-01-13 14:25:04 +01:00
Petr Viktorin aa6579cb60
gh-127773: Disable attribute cache on incompatible MRO entries (GH-127924) 2025-01-13 14:10:41 +01:00
Bénédikt Tran 76ffaef729
gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue` (#128780)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-01-13 18:25:09 +05:30
sobolevn 6e1e780540
gh-128759: fix data race in `type_modified_unlocked` (#128764) 2025-01-13 07:40:52 +00:00
Pieter Eendebak ff39e3ff7b
gh-126703: Add freelist for `PyMethodObject` (#128594) 2025-01-12 18:31:49 +05:30
Bénédikt Tran 613240bf03
gh-111178: fix UBSan failures in `Objects/bytearrayobject.c` (GH-128236)
* fix UBSan failures for `bytesiterobject`
* fix UBSan failures for `PyByteArrayObject`
2025-01-10 11:50:02 +01:00
Bénédikt Tran 295776c7f3
gh-111178: fix UBSan failures in `Objects/bytesobject.c` (GH-128237)
* remove redundant casts for `bytesobject`
* fix UBSan failures for `striterobject`
2025-01-10 11:48:06 +01:00
Bénédikt Tran 6cf31750e0
gh-111178: fix UBSan failures in `Objects/complexobject.c` (GH-128241)
fix UBSan failures for `PyComplexObject`
2025-01-10 10:46:36 +00:00
Bénédikt Tran 2fcdc8488c
gh-126862: Use `Py_ssize_t` instead of `int` when processing the number of super-classes (#127523) 2025-01-10 03:32:53 +00:00
Bénédikt Tran 845d924efb
gh-111178: fix UBSan failures in `Objects/capsule.c` (GH-128239)
fix UBSan failures for `PyCapsule`
2025-01-08 14:55:04 +01:00
Bénédikt Tran 1ef6bf4e29
gh-111178: fix UBSan failures in `Objects/descrobject.c` (GH-128245)
fix UBSan failures for `propertyobject`
2025-01-06 12:50:01 +01:00
Donghee Na ae23a012e6
gh-128137: Update PyASCIIObject to handle interned field with the atomic operation (gh-128196) 2025-01-05 18:17:06 +09:00
Alexander Shadchin 46cb6340d7
gh-127903: Fix a crash on debug builds when calling `Objects/unicodeobject::_copy_characters`` (#127876) 2025-01-03 18:47:58 +00:00
Bénédikt Tran 5643032053
gh-111178: fix UBSan failures in `Objects/tupleobject.c` (GH-128251)
fix UBSan failures for `_PyTupleIterObject`
2025-01-03 15:35:05 +01:00
Bénédikt Tran aad5ba4b6a
gh-111178: fix UBSan failures in `Objects/enumobject.c` (GH-128246)
* fix UBSan failures for `enumobject`
* fix UBSan failures for `reversedobject`
2025-01-03 15:29:41 +01:00
Bénédikt Tran fa985bee61
gh-127787: refactor helpers for `PyUnicodeErrorObject` internal interface (GH-127789)
- Unify `get_unicode` and `get_string` in a single function.

- Allow to retrieve the underlying `object` attribute, its
  size, and the adjusted 'start' and 'end', all at once.
  Add a new `_PyUnicodeError_GetParams` internal function for this.
  (In `exceptions.c`, it's somewhat common to not need all the attributes,
  but the compiler has opportunity to inline the function and optimize
  unneeded work away. Outside that file, we'll usually need all or
  most of them at once.)

- Use a common implementation for the following functions:

  - `PyUnicode{Decode,Encode}Error_GetEncoding`
  - `PyUnicode{Decode,Encode,Translate}Error_GetObject`
  - `PyUnicode{Decode,Encode,Translate}Error_{Get,Set}Reason`
  - `PyUnicode{Decode,Encode,Translate}Error_{Get,Set}{Start,End}`
2025-01-03 13:37:02 +01:00
Abhijeet 0706bab1c0
gh-128133: use relaxed atomics for hash of bytes (#128412) 2025-01-03 13:50:56 +05:30
Sam Gross 8eebe4e6d0
gh-128212: Fix race in `_PyUnicode_CheckConsistency` (GH-128367)
There was a data race on the utf8 field between `PyUnicode_SET_UTF8` and
`_PyUnicode_CheckConsistency`. Use the `_PyUnicode_UTF8()` accessor,
which uses an atomic load internally, to avoid the data race.
2025-01-02 14:02:54 -05:00
Bogdan Romanyuk 47d2cb8eb7
gh-128100: Use atomic dictionary load in `_PyObject_GenericGetAttrWithDict` (GH-128297) 2024-12-30 20:38:49 +00:00
Pieter Eendebak 3bd7730bbd
gh-126868: Add freelist for compact ints to `_PyLong_New` (#128181)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2024-12-26 15:17:22 +00:00
da-woods 42f7a00ae8
Clean up redundant ifdef in list getitem (#128257)
It's already inside a `Py_GIL_DISABLED` block so the `#else` clause is always unused.
2024-12-26 14:40:48 +00:00
Yan Yanchii 5c814c83cd
gh-128198: Add missing error checks for usages of PyIter_Next() (GH-128199) 2024-12-25 19:42:04 +02:00
Mark Shannon 128cc47fbd
GH-127705: Add debug mode for `_PyStackRef`s inspired by HPy debug mode (GH-128121) 2024-12-20 16:52:20 +00:00
mpage 255762c09f
gh-127274: Defer nested methods (#128012)
Methods (functions defined in class scope) are likely to be cleaned
up by the GC anyway.

Add a new code flag, `CO_METHOD`, that is set for functions defined
in a class scope. Use that when deciding to defer functions.
2024-12-19 13:03:14 -08:00
Neil Schemenauer 1b15c89a17
gh-115999: Specialize `STORE_ATTR` in free-threaded builds. (gh-127838)
* Add `_PyDictKeys_StringLookupSplit` which does locking on dict keys and
  use in place of `_PyDictKeys_StringLookup`.

* Change `_PyObject_TryGetInstanceAttribute` to use that function
  in the case of split keys.

* Add `unicodekeys_lookup_split` helper which allows code sharing
  between `_Py_dict_lookup` and `_PyDictKeys_StringLookupSplit`.

* Fix locking for `STORE_ATTR_INSTANCE_VALUE`.  Create
  `_GUARD_TYPE_VERSION_AND_LOCK` uop so that object stays locked and
  `tp_version_tag` cannot change.

* Pass `tp_version_tag` to `specialize_dict_access()`, ensuring
  the version we store on the cache is the correct one (in case of
  it changing during the specalize analysis).

* Split `analyze_descriptor` into `analyze_descriptor_load` and
  `analyze_descriptor_store` since those don't share much logic.
  Add `descriptor_is_class` helper function.

* In `specialize_dict_access`, double check `_PyObject_GetManagedDict()`
  in case we race and dict was materialized before the lock.

* Avoid borrowed references in `_Py_Specialize_StoreAttr()`.

* Use `specialize()` and `unspecialize()` helpers.

* Add unit tests to ensure specializing happens as expected in FT builds.

* Add unit tests to attempt to trigger data races (useful for running under TSAN).

* Add `has_split_table` function to `_testinternalcapi`.
2024-12-19 10:21:17 -08:00
Mark Shannon d2f1d917e8
GH-122548: Implement branch taken and not taken events for sys.monitoring (GH-122564) 2024-12-19 16:59:51 +00:00
Sam Gross 7b811d0562
gh-128008: Add `PyWeakref_IsDead()` (GH-128009)
The `PyWeakref_IsDead()` function tests if a weak reference is dead
without any side effects. Although you can also detect if a weak
reference is dead using `PyWeakref_GetRef()`, that function returns a
strong reference that must be `Py_DECREF()`'d, which can introduce side
effects if the last reference is concurrently dropped (at least in the
free threading build).
2024-12-19 16:17:15 +01:00
Kumar Aditya 3c168f7f79
gh-128013: fix data race in `PyUnicode_AsUTF8AndSize` on free-threading (#128021) 2024-12-19 17:08:32 +05:30
Donghee Na 48c70b8f7d
gh-115999: Enable BINARY_SUBSCR_GETITEM for free-threaded build (gh-127737) 2024-12-19 11:08:17 +09:00
Victor Stinner f802c8bf87
gh-128013: Convert unicodeobject.c macros to functions (#128061)
Convert unicodeobject.c macros to static inline functions.

* Add _PyUnicode_SET_UTF8() and _PyUnicode_SET_UTF8_LENGTH() macros.
* Add PyUnicode_HASH() and PyUnicode_SET_HASH() macros.
* Remove unused _PyUnicode_KIND() and _PyUnicode_GET_LENGTH() macros.
2024-12-18 16:34:31 +01:00
Edward Xu 4937ba54c0
gh-127085: fix some data races in memoryview in free-threading (#127412) 2024-12-17 00:42:19 +05:30
Inada Naoki 5dd775bed0
gh-126024: unicodeobject: optimize find_first_nonascii (GH-127790)
Remove 1 branch.
2024-12-13 17:21:46 +01:00
Bénédikt Tran 8bc18182a7
gh-127691: add type checks when using `PyUnicodeError` objects (GH-127694) 2024-12-13 17:16:22 +01:00
Victor Stinner 6446408d42
gh-102471, PEP 757: Add PyLong import and export API (#121339)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Steve Dower <steve.dower@microsoft.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-12-13 14:24:48 +01:00
Pieter Eendebak 5fc6bb2754
gh-126868: Add freelist for compact int objects (GH-126865) 2024-12-13 10:06:26 +00:00
Mark Shannon bc262de06b
GH-125174: Mark objects as statically allocated. (#127797)
* Set a bit in the unused part of the refcount on 64 bit machines and the free-threaded build.

* Use the top of the refcount range on 32 bit machines
2024-12-11 17:37:38 +00:00
Mark Shannon 5a23994a3d
GH-127058: Make `PySequence_Tuple` safer and probably faster. (#127758)
* Use a small buffer, then list when constructing a tuple from an arbitrary sequence.
2024-12-11 14:02:59 +00:00
Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్ రెడ్డి) db9bea0386
gh-127740: For odd-length input to bytes.fromhex(...) change the error message to ValueError: fromhex() arg must be of even length (#127756) 2024-12-11 08:35:17 +01:00
Bénédikt Tran 9af96f4406
gh-127563: use `dk_log2_index_bytes=3` in empty dicts (GH-127568)
This fixes a UBSan failure (unaligned zero-size memcpy) in `dictobject.c`.
2024-12-10 16:58:17 +01:00
Petr Viktorin 690fe077f6
gh-126491: Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)" (GH-127770)
Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)"

This reverts commit 023b7d2141, which introduced
a refleak.
2024-12-10 11:53:56 +01:00
Bénédikt Tran 4331832db0
gh-125420: implement `Sequence.count` API on `memoryview` objects (#125443) 2024-12-10 10:12:33 +00:00
Bénédikt Tran 58c753827a
gh-125420: implement `Sequence.index` API on `memoryview` objects (#125446) 2024-12-09 18:48:38 -08:00
Bénédikt Tran 8fa5ecec01
gh-123378: fix post-merge typos in comments and NEWS (#127739) 2024-12-08 13:47:22 +00:00
Peter Bierma 12680ec5bd
gh-127314: Don't mention the GIL when calling without a thread state on the free-threaded build (#127315)
Co-authored-by: Victor Stinner <vstinner@python.org>
2024-12-06 16:58:19 +01:00
Bénédikt Tran 36c6178d37
gh-126024: fix UBSan failure in `unicodeobject.c:find_first_nonascii` (GH-127566) 2024-12-06 09:31:30 -05:00
Mark Shannon 023b7d2141
GH-126491: Lower heap size limit with faster marking (GH-127519)
* Faster marking of reachable objects

* Changes calculation of work to do and work done.

* Merges transitive closure calculations
2024-12-06 10:46:59 +00:00
Sergey B Kirpichev 8b7c194c7b
gh-120010: Fix invalid (nan+nanj) results in _Py_c_prod() (GH-120287)
In some cases, previously computed as (nan+nanj), we could recover
meaningful component values in the result, see e.g. the C11, Annex
G.5.1, routine _Cmultd():

>>> z = 1e300+1j
>>> z*(nan+infj)  # was (nan+nanj)
(-inf+infj)

That also fix some complex powers for small integer exponents, computed
with optimized algorithm (by squaring):

>>> z**5  # was (nan+nanj)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    z**5
    ~^^~
OverflowError: complex exponentiation
2024-12-06 12:28:32 +02:00
Sam Gross f4f530804b
gh-127582: Make object resurrection thread-safe for free threading. (GH-127612)
Objects may be temporarily "resurrected" in destructors when calling
finalizers or watcher callbacks. We previously undid the resurrection
by decrementing the reference count using `Py_SET_REFCNT`. This was not
thread-safe because other threads might be accessing the object
(modifying its reference count) if it was exposed by the finalizer,
watcher callback, or temporarily accessed by a racy dictionary or list
access.

This adds internal-only thread-safe functions for temporary object
resurrection during destructors.
2024-12-05 16:07:31 -05:00
Sam Gross e51da64ac3
gh-127536: Add missing locks in listobject.c (GH-127580)
We were missing locks around some list operations in the free threading
build.
2024-12-04 14:12:15 -05:00
Bénédikt Tran bc0f2e9459
gh-123378: Ensure results of `PyUnicode*Error_Get{Start,End}` are clamped (GH-123380)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
2024-12-04 14:13:52 +01:00
mpage dabcecfd6d
gh-115999: Enable specialization of `CALL` instructions in free-threaded builds (#127123)
The CALL family of instructions were mostly thread-safe already and only required a small number of changes, which are documented below.

A few changes were needed to make CALL_ALLOC_AND_ENTER_INIT thread-safe:

Added _PyType_LookupRefAndVersion, which returns the type version corresponding to the returned ref.

Added _PyType_CacheInitForSpecialization, which takes an init method and the corresponding type version and only populates the specialization cache if the current type version matches the supplied version. This prevents potentially caching a stale value in free-threaded builds if we race with an update to __init__.

Only cache __init__ functions that are deferred in free-threaded builds. This ensures that the reference to __init__ that is stored in the specialization cache is valid if the type version guard in _CHECK_AND_ALLOCATE_OBJECT passes.
Fix a bug in _CREATE_INIT_FRAME where the frame is pushed to the stack on failure.

A few other miscellaneous changes were also needed:

Use {LOCK,UNLOCK}_OBJECT in LIST_APPEND. This ensures that the list's per-object lock is held while we are appending to it.

Add missing co_tlbc for _Py_InitCleanup.

Stop/start the world around setting the eval frame hook. This allows us to read interp->eval_frame non-atomically and preserves the behavior of _CHECK_PEP_523 documented below.
2024-12-03 11:20:20 -08:00
Neil Schemenauer fc5a0dc224
gh-127271: Replace use of PyCell_GET/SET (gh-127272)
* Replace uses of `PyCell_GET` and `PyCell_SET`.  These macros are not
  safe to use in the free-threaded build.  Use `PyCell_GetRef()` and
  `PyCell_SetTakeRef()` instead. 

* Since `PyCell_GetRef()` returns a strong rather than borrowed ref, some
  code restructuring was required, e.g. `frame_get_var()` returns a strong
  ref now.

* Add critical sections to `PyCell_GET` and `PyCell_SET`.

* Move critical_section.h earlier in the Python.h file.

* Add `PyCell_GET` to the free-threading howto table of APIs that return
  borrowed refs.

* Add additional unit tests for free-threading.
2024-12-03 10:33:06 -08:00
Neil Schemenauer 0cb5222079
gh-115999: Specialize `LOAD_SUPER_ATTR` in free-threaded builds (gh-127128)
Use existing helpers to atomically modify the bytecode.  Add unit tests
to ensure specializing is happening as expected.  Add test_specialize.py
that can be used with ThreadSanitizer to detect data races.  
Fix thread safety issue with cell_set_contents().
2024-12-03 09:32:26 -08:00
Sam Gross c7dec02de2
gh-127521: Mark list as "shared" before resizing if necessary (#127524)
In the free threading build, if a non-owning thread resizes a list,
it must use QSBR to free the old list array because there may be a
concurrent access (without a lock) from the owning thread.

To match the pattern in dictobject.c, we just mark the list as "shared"
before resizing if it's from a non-owning thread and not already marked
as shared.
2024-12-02 14:38:26 -05:00
Victor Stinner bf21e2160d
Fix Unicode encode_wstr_utf8() (#127420)
Raise RuntimeError instead of RuntimeWarning.
2024-12-02 11:14:47 +01:00
Donghee Na e2713409cf
gh-115999: Add partial free-thread specialization for BINARY_SUBSCR (gh-127227) 2024-12-02 10:38:17 +09:00
Inada Naoki 7043bbd1ca
gh-127417: fix UTF-8 decoder optimization on AIX (#127433) 2024-11-30 21:52:37 +09:00
Kumar Aditya 45c5cba318
gh-127316: fix incorrect assertion in setting `__class__` in free-threading (#127399) 2024-11-29 21:44:20 +05:30