Commit Graph

8275 Commits

Author SHA1 Message Date
Eric Snow 105f22ea46
gh-117398: Use Per-Interpreter State for the _datetime Static Types (gh-119929)
We make use of the same mechanism that we use for the static builtin types.  This required a few tweaks.

The relevant code could use some cleanup but I opted to avoid the significant churn in this change.  I'll tackle that separately.

This change is the final piece needed to make _datetime support multiple interpreters.  I've updated the module slot accordingly.
2024-06-03 17:09:18 -06:00
Sam Gross 47fb4327b5
gh-117657: Fix race involving immortalizing objects (#119927)
The free-threaded build currently immortalizes objects that use deferred
reference counting (see gh-117783). This typically happens once the
first non-main thread is created, but the behavior can be suppressed for
tests, in subinterpreters, or during a compile() call.

This fixes a race condition involving the tracking of whether the
behavior is suppressed.
2024-06-03 20:58:41 +00:00
Nikita Sobolev 1d4c2e4a87
gh-119057: Use better error messages for zero division (#119066) 2024-06-03 19:03:56 +03:00
Sergey B Kirpichev 61d3ab32da
gh-116560: Add PyLong_GetSign() public function (#116561)
Co-authored-by: Victor Stinner <vstinner@python.org>
2024-06-03 14:06:31 +02:00
Irit Katriel 367adc91fb
gh-119786: move exception handling doc to InternalDocs (#119815) 2024-06-03 09:36:20 +00:00
Victor Stinner 3ea9b92086
gh-119396: Optimize unicode_decode_utf8_writer() (#119957)
Optimize unicode_decode_utf8_writer()

Take the ascii_decode() fast-path even if dest is not aligned on
size_t bytes.
2024-06-03 08:45:20 +02:00
Donghee Na 0594a27e5f
gh-117657: Fix data races report by TSAN unicode-hash (gh-119907) 2024-06-03 12:22:41 +09:00
Mark Dickinson f79ffc879b
gh-119740: Remove deprecated trunc delegation (#119743)
Remove the delegation of `int` to the `__trunc__` special method: `int` will now only delegate to `__int__` and `__index__` (in that order). `__trunc__` continues to exist, but its sole purpose is to support `math.trunc`.

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-06-02 10:16:49 +01:00
Nikita Sobolev 4aed319a8e
gh-119775: Remove ability to create immutable types with mutable bases (#119776) 2024-06-02 07:27:20 +00:00
Alyssa Coghlan 3859e09e3d
gh-74929: PEP 667 C API documentation (gh-119379)
* Add docs for new APIs
* Add soft-deprecation notices
* Add What's New porting entries
* Update comments referencing `PyFrame_LocalsToFast()` to mention the proxy instead
* Other related cleanups found when looking for refs to the deprecated APIs
2024-06-01 13:59:35 +10:00
Jelle Zijlstra d28afd3fa0
gh-119180: Lazily wrap annotations on classmethod and staticmethod (#119864) 2024-05-31 14:05:51 -07:00
Serhiy Storchaka ef01e95ae3
gh-109218: Deprecate weird cases in the complex() constructor (GH-119620)
* Passing a string as the "real" keyword argument is now an error;
  it should only be passed as a single positional argument.
* Passing a complex number as the "real" or "imag" argument is now deprecated;
  it should only be passed as a single positional argument.
2024-05-30 23:30:57 +03:00
Serhiy Storchaka ec1ba26460
gh-109218: Improve documentation for the complex() constructor (GH-119687)
* Remove the equivalence with real+imag*1j which can be incorrect in corner
  cases (non-finite numbers, the sign of zeroes).
* Separately document the three roles of the constructor: parsing a string,
  converting a number, and constructing a complex from components.
* Document positional-only parameters of complex(), float(), int() and bool()
  as positional-only.
* Add examples for complex() and int().
* Specify the grammar of the string for complex().
* Improve the grammar of the string for float().
* Describe more explicitly the behavior when real and/or imag arguments are
  complex numbers. (This will be deprecated in future.)
2024-05-30 20:20:07 +00:00
Sam Gross c22323cd1c
gh-119525: Fix deadlock with `_PyType_Lookup` and the GIL (#119527)
The deadlock only affected the free-threaded build and only occurred
when the GIL was enabled at runtime. The `Py_DECREF(old_name)` call
might temporarily release the GIL while holding the type seqlock.
Another thread may spin trying to acquire the seqlock while holding the
GIL.

The deadlock occurred roughly 1 in ~1,000 runs of `pool_in_threads.py`
from `test_multiprocessing_pool_circular_import`.
2024-05-29 15:26:04 -04:00
Sergey B Kirpichev cd11ff12ac
gh-119613: Use C99+ functions instead of Py_IS_NAN/INFINITY/FINITE (#119619) 2024-05-29 09:51:19 +02:00
Nikita Sobolev 6b240c2308
gh-119011: `type.__type_params__` now return an empty tuple (#119296) 2024-05-28 18:12:58 +00:00
Victor Stinner 0518edc170
gh-119396: Optimize unicode_repr() (#119617)
Use stringlib to specialize unicode_repr() for each string kind
(UCS1, UCS2, UCS4).

Benchmark:

+-------------------------------------+---------+----------------------+
| Benchmark                           | ref     | change2              |
+=====================================+=========+======================+
| repr('abc')                         | 100 ns  | 103 ns: 1.02x slower |
+-------------------------------------+---------+----------------------+
| repr('a' * 100)                     | 369 ns  | 369 ns: 1.00x slower |
+-------------------------------------+---------+----------------------+
| repr(('a' + squote) * 100)          | 1.21 us | 946 ns: 1.27x faster |
+-------------------------------------+---------+----------------------+
| repr(('a' + nl) * 100)              | 1.23 us | 907 ns: 1.36x faster |
+-------------------------------------+---------+----------------------+
| repr(dquote + ('a' + squote) * 100) | 1.08 us | 858 ns: 1.25x faster |
+-------------------------------------+---------+----------------------+
| Geometric mean                      | (ref)   | 1.16x faster         |
+-------------------------------------+---------+----------------------+
2024-05-28 18:05:20 +02:00
Serhiy Storchaka b313cc68d5
gh-117557: Improve error messages when a string, bytes or bytearray of length 1 are expected (GH-117631) 2024-05-28 12:01:37 +03:00
Serhiy Storchaka 08e65430aa
gh-111999: Fix the signature of str.format_map() (#119540) 2024-05-25 06:21:11 -07:00
Germán Méndez Bravo f0ed1863bd
gh-112075: Fix dict thread safety issues (#119288)
Fix dict thread safety issues
2024-05-24 10:39:06 -07:00
Mark Shannon 406ffb5293
GH-117195: Avoid assertion error in `object.__sizeof__` (GH-117220) 2024-05-23 11:06:10 +01:00
Ayato Hayashi 43807fec8d
Tiny fix: Update link for HAC algorithm (gh-118546) 2024-05-23 02:11:11 +00:00
Victor Stinner 9b422fc6af
gh-119396: Optimize PyUnicode_FromFormat() UTF-8 decoder (#119398)
Add unicode_decode_utf8_writer() to write directly characters into a
_PyUnicodeWriter writer: avoid the creation of a temporary string.
Optimize PyUnicode_FromFormat() by using the new
unicode_decode_utf8_writer().

Rename unicode_fromformat_write_cstr() to
unicode_fromformat_write_utf8().

Microbenchmark on the code:

    return PyUnicode_FromFormat(
        "%s %s %s %s %s.",
        "format", "multiple", "utf8", "short", "strings");

Result: 620 ns +- 8 ns -> 382 ns +- 2 ns: 1.62x faster.
2024-05-22 21:05:26 +00:00
Josh {*()} Rosenberg baf347d916
gh-119247: Add macros to use PySequence_Fast safely in free-threaded build (#119315)
Add `Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST` and
`Py_END_CRITICAL_SECTION_SEQUENCE_FAST` macros and update `str.join` to use
them. Also add a regression test that would crash reliably without this
patch.
2024-05-22 17:45:34 +00:00
Dino Viehland 2b3fb767be
gh-117657: Fix missing atomic in dict_resize (#119312)
Fix missing atomic in dict_resize
2024-05-22 10:38:56 -07:00
Geoffrey Thomas ef172521a9
Remove almost all unpaired backticks in docstrings (#119231)
As reported in #117847 and #115366, an unpaired backtick in a docstring
tends to confuse e.g. Sphinx running on subclasses of standard library
objects, and the typographic style of using a backtick as an opening
quote is no longer in favor. Convert almost all uses of the form

    The variable `foo' should do xyz

to

    The variable 'foo' should do xyz

and also fix up miscellaneous other unpaired backticks (extraneous /
missing characters).

No functional change is intended here other than in human-readable
docstrings.
2024-05-22 12:35:18 -04:00
Jelle Zijlstra e9875ecb5d
gh-119180: PEP 649: Add __annotate__ attributes (#119209) 2024-05-22 04:38:12 +02:00
Jelle Zijlstra 98e855fcc1
gh-119180: Add LOAD_COMMON_CONSTANT opcode (#119321)
The PEP 649 implementation will require a way to load NotImplementedError
from the bytecode. @markshannon suggested implementing this by converting
LOAD_ASSERTION_ERROR into a more general mechanism for loading constants.

This PR adds this new opcode. I will work on the rest of the implementation
of the PEP separately.

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2024-05-22 00:46:39 +00:00
Victor Stinner f6da790122
gh-111389: Add PyHASH_MULTIPLIER constant (#119214) 2024-05-21 19:51:51 +02:00
Irit Katriel 77ff28bb67
gh-109176: replace _PyFrame_OpAlreadyRan by an assertion that the frame is complete. (#119234) 2024-05-21 17:08:51 +00:00
Donghee Na ab4263a82a
gh-119053: Implement the fast path for list.__getitem__ (gh-119112) 2024-05-21 09:49:18 -04:00
Tian Gao 35c436186b
gh-118921: Add `copy()` method for `FrameLocalsProxy` (#118923) 2024-05-10 15:53:10 -07:00
Victor Stinner aa36f83c16
gh-118702: Implement vectorcall for BaseException (#118703)
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().
2024-05-10 21:08:24 +02:00
Nikita Sobolev 004db2170e
Rename `notimplemented_methods` into `nodefault_methods` (#118896) 2024-05-10 12:46:20 +00:00
Steve Dower 7e6fcab200
Fix some missing null checks. (GH-118721) 2024-05-10 10:31:55 +01:00
Sam Gross 2402715e10
gh-118561: Fix crash involving list.extend in free-threaded build (#118723)
The `list_preallocate_exact` function did not zero initialize array
contents. In the free-threaded build, this could expose uninitialized
memory to concurrent readers between the call to
`list_preallocate_exact` and the filling of the array contents with
items.
2024-05-09 18:52:27 +00:00
Nikita Sobolev 82abe75e77
gh-118849: Fix "code will never be executed" warning in `dictobject.c` (#118850) 2024-05-09 20:15:14 +03:00
mpage cb6f75a32c
gh-117657: Fix data races when writing / reading `ob_gc_bits` (#118292)
Use relaxed atomics when reading / writing to the field. There are still a
few places in the GC where we do not use atomics. Those should be safe as
the world is stopped.
2024-05-08 16:03:39 -04:00
Jelle Zijlstra 3c079a0203
gh-118767: Make bool(NotImplemented) raise TypeError (#118775) 2024-05-08 11:12:00 -07:00
Tian Gao e7aec8713f
gh-118746: Fix crash in frame_getlocals and _PyFrame_GetLocals (#118748)
We don't know how to create an unoptimized frame with f_locals == NULL,
but they are seen in the wild, and this fixes the crash.
2024-05-07 17:48:05 -07:00
James Hilton-Balfe e7dafdc224
gh-110209: Add __class_getitem__ for generator and coroutine (#110212) 2024-05-06 18:41:32 -07:00
Sam Gross 723d4d2fe8
gh-118527: Intern code consts in free-threaded build (#118667)
We already intern and immortalize most string constants. In the
free-threaded build, other constants can be a source of reference count
contention because they are shared by all threads running the same code
objects.
2024-05-06 20:12:39 -04:00
Dino Viehland 636b8d94c9
gh-112075: Fix race in constructing dict for instance (#118499) 2024-05-06 23:31:09 +00:00
Sam Gross 2ba2c142a6
gh-118527: Intern code name and filename on default build (#118576)
Interned and non-interned strings are treated differently by `marshal`,
so be consistent between the default and free-threaded build.
2024-05-06 17:24:14 -04:00
Mark Shannon 616b745b89
GH-115709: Invalidate executors when a local variable is changed via frame.f_locals (#118639)
Also fix unrelated assert in debug Tier2/JIT builds.
2024-05-06 21:21:06 +00:00
Dino Viehland 5a1618a2c8
gh-118362: Fix thread safety around lookups from the type cache in the face of concurrent mutators (#118454)
Add _PyType_LookupRef and use incref before setting attribute on type
Makes setting an attribute on a class and signaling type modified atomic
Avoid adding re-entrancy exposing the type cache in an inconsistent state by decrefing after type is updated
2024-05-06 10:50:35 -07:00
Thomas Grainger e5c699280d
GH-117714: implement athrow().close() and asend().close() using throw (GH-117906)
* GH-117714: replace athrow().close() and asend().close() stubs with implimentations

* test athrow().close() and asend().close() raises RuntimeError

* 📜🤖 Added by blurb_it.

* Update Objects/genobject.c

Co-authored-by: Petr Viktorin <encukou@gmail.com>

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2024-05-06 17:13:15 +00:00
Petr Viktorin 417dd3aca7
gh-116322: Rename PyModule_ExperimentalSetGIL to PyUnstable_Module_SetGIL (GH-118645) 2024-05-06 18:59:36 +02:00
Tian Gao 7528b84e94
gh-118609: Add proper error check for framelocalsproxy (#118615) 2024-05-06 08:46:15 -07:00
Tian Gao afbe5bf9c8
gh-74929: Make containment checks more efficient in `FrameLocalsProxy` (#118624)
Properly implement the `sq_contains` slot for frame locals proxy containment checks.
2024-05-06 20:16:48 +10:00