100 Commits
Author SHA1 Message Date
Victor Stinner 7ee9ddda05 gh-133651: Increase multiprocessing test_interrupt() timeout (#160281)
Increase timeout from 10 to at least 30 seconds (SHORT_TIMEOUT).
2026-10-22 19:04:14 +00:00
Victor Stinner 9d42aee573 gh-160206: Close connection explicitly in test_ssl (#160222)
test_https_client_non_tls_response_ignored() now uses a context
manager for the server and always closes the connection to avoid
ResourceWarning.

Also add a timeout to SingleConnectionTestServerThread.__exit__() to
not hang forever if something goes wrong.
2026-10-22 12:43:27 -05:00
Victor Stinner 105e65bc70 gh-160208: Fix typo in test_cext tests name (#160216)
Replace "Interal" with "Internal".
2026-10-22 08:29:35 +00:00
Victor Stinner 226ca06ce8 On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (#160210)
fd_count() can now use /dev/fd/ fdescfs file system if it's mounted,
which is not the case by default. Otherwise, call os.dup(fd) in a
loop which is slow since os.sysconf("SC_OPEN_MAX") is larger than
200,000 by default.
2026-10-22 01:05:04 -07:00
Victor Stinner 6fd15f0ea3 gh-159534: Fix race condition in test_sampling_profiler (#160028)
Fix test_is_python_process_non_python_subprocess(). Wait until the
forked Python process calls exec() before calling is_python_process().
Previously, is_python_build() returned True if exec() was not called
yet in the forked process.
2026-10-21 12:56:35 -05:00
Victor Stinner 88d922ad98 gh-159914: Ignore gdb stderr in test_gdb.test_pretty_print (#159916)
test_strings() gets gdb.host_charset(), but fails if gdb writes
anything to stderr. For example, it fails if gdb logs the warning:

    "/usr/bin/gdb: warning: Couldn't determine a path for the index
    cache directory."

Even if stdout contains the expected encoding ("UTF-8").

Change test_gdb to only fail if stdout is empty, ignore stderr
otherwise to ignore gdb warnings.
2026-10-20 13:48:29 -05:00
Victor Stinner 14b43d4d3c gh-159603: Call pthread_attr_init() in hardware_stack_limits() (#159857)
Always call pthread_attr_init() in hardware_stack_limits(), not only
on FreeBSD (if pthread_attr_get_np() is available).

Emscripten: add pthread_attr_init() workaround.
2026-10-20 13:15:57 -05:00
Victor Stinner 71ba39dc05 gh-159603: Use pthread_attr_get_np() on FreeBSD (#159604)
Use pthread_attr_get_np() on FreeBSD to get the stack size (and guard
size) of the current thread.

Check if pthread_attr_get_np() function and <pthread_np.h> header are
available in configure.

Update test_json.test_recursion. On FreeBSD 16 with a default stack size of
512 MB, the json C implementation no longer raises RecursionError in
test_highly_nested_objects_encoding(). Reduce also max_depth from 5,000 to
2,000 so test_highly_nested_objects_encoding() of the Python implementation
only takes 1.6 seconds, instead of 12 seconds.
2026-10-19 20:54:27 -07:00
Victor Stinner 97a713566b gh-157899: Factorize support._requires_unix_version() code (GH-159523)
Reuse the same code for _requires_unix_version() and
_requires_apple_os_version().

Check sys.platform instead of platform.system().
2026-10-18 09:56:05 +02:00
Victor Stinner a2c999bd8b gh-157710: There is no need to export _PyUnicode_IsModifiable() (#159601)
_PyUnicodeWriter_CanWrite() doesn't call _PyUnicode_IsModifiable(),
but reimplement the function.
2026-10-17 00:36:48 +00:00
Victor Stinner d3ada4509e gh-157242: Use support.inject_memory_error_cm() in test_dict (#159543) 2026-10-16 17:43:30 +02:00
Victor Stinner 35a000974a Revert "gh-158445: Allocate memory in the heap in Py_GetVersion() (#158608)" (#159519)
This reverts commit f5e913c7f7.

Truncating the static buffer to 20 bytes caused a regression in debuggers
and crash reporters checking `version` static buffer, see:
https://github.com/python/cpython/pull/158608#issuecomment-6027641907
2026-10-16 10:49:53 +00:00
Victor Stinner e96d943d7d Set default arguments in _testlimitedcapi/unicode.c (#159380)
Some optional arguments had no default value leading to undefined
behavior if they were omitted.
2026-10-14 14:52:44 +02:00
Victor Stinner 2514548b1e Reuse assert_python_ok() in test_cmd_line (#159377)
support.script_helper no longer strips refcount from -X showrefcount
stderr.
2026-10-14 14:46:49 +02:00
Victor Stinner 2ec47444ae gh-157899: Define HAVE_FREADLINK_RUNTIME on FreeBSD 16 (GH-159397)
The commit 4b0e334843 added support for
freadlink() on macOS. The function is also available on FreeBSD 16.
Define HAVE_FREADLINK_RUNTIME on FreeBSD.
2026-10-14 11:18:53 +02:00
Victor Stinner cab7ace9f4 gh-157649: Use more warmups in test_cext to check for refleaks (#159378)
Use 3 warmups instead of 1 before checking for reference leaks. Also,
always call gc.collect() before reading sys.gettotalrefcount().
2026-10-13 22:43:39 +00:00
Victor Stinner 8992704916 gh-144175: Remove internal _PyArg_ParseStack() function (#159150)
Replace internal _PyArg_ParseStack() with public PyArg_ParseArray()
in Argument Clinic.
2026-10-12 19:56:53 +02:00
Victor Stinner 8bf6305bc7 gh-158451: Remove _PyUnicodeWriter_WriteASCIIString() read-only optim (#159141)
The optimization is not worth it, _PyUnicode_FromASCII() doesn't make
anything special.
2026-10-10 20:09:32 +00:00
Victor Stinner 5eefa589f1 gh-158585: Add 3 examples to PyBytesWriter documentation (#159129) 2026-10-10 21:46:56 +02:00
Victor Stinner 565f76fee1 Fix @reap_threads usage in test_free_threading.test_json (#158937)
Use the decorator on test methods, not on the whole class.

Previously, test_json ran no tests:

    $ ./python -m test -v test_free_threading.test_json -v
    ...
    Ran 0 tests in 0.000s

    NO TESTS RAN

Add also a check in @reap_threads to detect misuse (on a class).
2026-10-10 14:14:54 +02:00
Victor Stinner e2c3c7e4dd gh-158942: Use more unique variable names in Py_MIN/MAX/ABS() (#158969)
If Py_MIN/MAX/ABS() macros are called on an existing "_x" or "_y"
variable name, the external variable is used instead of macro local
variable. So use more unique names in the macros to avoid
the issue.

Add test_min_max_abs_macros() to _testlimitedcapi.
2026-10-08 17:48:43 +00:00
Victor StinnerandPetr Viktorin 20cb7fc707 gh-158886: Add INCREF/DECREF macros to stable_abi.toml (GH-158941)
* Add 7 INCREF/DECREF macros
* distinguish between 'const' and 'macro' in stable_abi.dat
  The file now has the 'kind' from the toml, not the ReST role.
* Fix kind of PY_VECTORCALL_ARGUMENTS_OFFSET
* Make it explicit that macros aren't part of the ABI
* Add is_static_inline_function to the TOML file (these are treated as macros in the docs)


Co-authored-by: Petr Viktorin <encukou@gmail.com>
2026-10-08 14:12:57 +02:00
Victor Stinner b22a8c9fd4 gh-149044: Build test_cext with -Wall -Wextra (#158867) 2026-10-08 03:26:19 +02:00
Victor Stinner 15dd7356f3 gh-158893: Add internal _Py_strerror() function (#158982)
Add a new internal _Py_strerror() function to Python/fileutils.c. It
uses strerror_r() if available, or use strerror() otherwise.

Replace all strerror(code) calls with _Py_strerror(code).
2026-10-07 20:31:27 +00:00
Victor Stinner 16108074b1 gh-158897: Optimize io.BufferedReader.readline() by calling memchr() (#158944)
Optimize io.BufferedReader.readline() when lines are close to the
buffer size (128 kB by default) or longer than the buffer size.
Replace the C loop searching for the newline byte in the buffer with
a memchr() call which is more efficient. In the good case,
it makes readline() up to 4x faster!
2026-10-07 13:18:10 +00:00
Victor Stinner ba9d96ff8d gh-149044: Avoid C++ warnings in PySlot_END macro (GH-158866)
Initialize all fields in the macro. Otherwise, g++ -Wall -Wextra
complains that some fields are not initialized
[-Werror=missing-field-initializers]:

  error: missing initializer for member ‘PySlot::sl_flags’
  error: missing initializer for member ‘PySlot::<anonymous>’
  error: missing initializer for member ‘PySlot::<anonymous>’
2026-10-07 14:12:32 +02:00
Victor Stinner 1818fba7e7 Use _testlimitedcapi in test_free_threading.test_capi (#158935)
Replace ctypes with _testlimitedcapi. ctypes is not always available,
and _testlimitedcapi is a thin wrapper to the C API.

Skip the test if _testlimitedcapi is missing.
2026-10-06 23:03:46 +02:00
Victor Stinner 6318730aee gh-158893: Make os.strerror() thread-safe (#158927)
Make os.strerror() thread-safe: use the reentrant strerror_r()
function if available.

* The configure script now checks if strerror_r() is supported.
* Add a stress test to test_free_threading.test_os (new module).
* Add a comment on decode_current_locale() assertion which fails if
  the input string is mutated.
* Add an assertion to _Py_DecodeLocale() to detect if the input
  string was mutated during the function call.
2026-10-06 23:02:13 +02:00
Victor Stinner 978a8befa5 gh-157695: Parse also _Py_HAVE_xxx variables in sysconfig (#158926)
Add parse_config_h() tests to test_sysconfig.
2026-10-06 19:15:14 +00:00
Victor Stinner ec14f81e74 gh-158842: Add Py_SETREF() and Py_XSETREF() to the limited C API (#158843)
Test these macros with the limited C API in _testlimitedcapi and
test_cext.

Move also other tests (refcount, Py_CLEAR, etc.) to _testlimitedcapi.
2026-10-06 21:13:49 +02:00
Victor Stinner 85c03d47a2 gh-158810: Use _Py_TYPEOF in Py_ARRAY_LENGTH() (#158838)
Fix the compiler warning "extension used" when building test_cext
with clang.

* test_cext no longer overrides the CC environment variable if it's
  already defined.
* test_cext now also logs __STRICT_ANSI__ macro.
2026-10-06 00:06:09 +00:00
Victor Stinner 047157b891 Use PyDict_GetItemRef() in _elementtree.c (#158809)
Replace PyDict_GetItemWithError() with PyDict_GetItemRef().

expat_default_handler() now holds a strong reference to value.
2026-10-06 01:56:03 +02:00
Victor Stinner fc3cd8e3f5 gh-158585: Optimize bytes.join() for non-empty separator (#158689)
Remove the conditional branch from the loop.
2026-10-05 13:51:53 +02:00
Victor Stinner b2bfbce9a2 gh-158810: Fix Py_ARRAY_LENGTH() macro on C++ (#158812) 2026-10-05 13:30:16 +02:00
Victor StinnerandPeter Bierma 84b0669f81 gh-157710: Soft deprecate C API modifying str objects (#157711)
Soft deprecate PyUnicode_New(), PyUnicode_CopyCharacters(),
PyUnicode_Fill(), PyUnicode_Resize(), PyUnicode_WRITE() and
PyUnicode_WriteChar() functions. Use the PyUnicodeWriter API instead.

* Add tests on PyUnicode_New() and PyUnicode_Resize(). Check that the
  result is either a mutable string or the empty string singleton.
* Check that PyUnicode_Fill(), PyUnicode_CopyCharacters() and
  PyUnicode_WriteChar() fail to modify a string with 2 references.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
2026-10-05 07:16:37 +00:00
Victor Stinner 182f323154 gh-158585: Adjust _PyBytes_FormatEx() allocation (#158823)
Count properly the number of preallocated bytes when growing the
buffer.
2026-10-05 03:09:25 +00:00
Victor Stinner 30ce3d462d gh-158585: Simplify PyBytesWriter_Finish() (#158822)
PyBytesWriter_Finish() doesn't need to check the size. It's known to
be valid.

PyBytesWriter_Create() now only calls byteswriter_resize() if the
size is larger than the small buffer.

Remove byteswriter_data() function: call static inline
_PyBytesWriter_GetData() function directly.
2026-10-05 04:47:04 +02:00
Victor Stinner d95b7ae6dc Test MemoryError on locale encoding C functions (#158808)
Fix also two typos.
2026-10-05 04:41:29 +02:00
Victor Stinner 587b7a5b31 gh-158585: Fix refleak in _testcapi.bytes_resize() (#158806) 2026-10-04 22:07:31 +00:00
Victor Stinner d19febb00c Rework locale encoding C functions (#158680)
Add output_length to _Py_EncodeLocale(), _Py_EncodeUTF8(),
encode_current_locale() and encode_ascii(). So unicode_encode_locale() and
wstr_to_utf8() can now use the output_length, instead of having to compute
strlen().

* Rename functions:

  * _Py_DecodeUTF8Ex() => _Py_DecodeUTF8()
  * _Py_EncodeUTF8Ex() => _Py_EncodeUTF8()
  * _Py_EncodeLocaleEx() => _Py_EncodeLocale()
  * _Py_DecodeLocaleEx() => _Py_DecodeLocale()

* Add tests on Py_DecodeLocale() and Py_EncodeLocale() in test_codecs.
  Clarify also their documentation.
* _Py_EncodeLocale() and _Py_DecodeLocale() now sets all output parameters on
  error. Add more tests on these output parameters.
* Add encode_current_locale_impl() to simplify encode_current_locale().
* Remove reason parameter of _Py_EncodeLocale() and _Py_DecodeLocale() to
  simplify the code. Only the UTF-8 decoder used specific reasons.
* _Py_EncodeUTF8() now uses size_t instead of Py_ssize_t to iterate
  on the input string.
* On error, encode_current_locale_impl() now returns ENCODE_ERROR (new
  constant) instead of DECODE_ERROR.
* Mark _Py_EncodeLocaleRaw() as static (it's not used outside fileutils.c).
2026-10-04 23:22:18 +02:00
Victor Stinner 8dba039708 gh-158585: Optimize bytes object creation (#158664)
Add bytes_alloc() helper function. bytes_alloc() and
_PyBytes_FromStringAndSize() have less conditional branches than
previous code, and so should be a little bit faster.

* Rename existing bytes_alloc() to bytes_type_alloc().
* Replace _PyBytes_FromSize(calloc=1) with _PyBytes_FromSizeZero().
* Add set_ob_shash_unsafe(): similar to set_ob_shash() but don't use
  an atomic operation on Free Threading. Use this new function
  on newly allocated bytes objects and in bytes_resize_inplace().
* Replace PyBytes_FromStringAndSize(NULL, size) with
  bytes_alloc(size).
* Use bytes_alloc() in PyBytes_FromString() and _PyBytes_Repeat().
* Add _PyBytes_FromStringAndSize(): similar to
  PyBytes_FromStringAndSize(), but str must not be NULL.
* Replace PyBytes_FromStringAndSize() with
  _PyBytes_FromStringAndSize().
* Test that PyBytes_FromString() and PyBytes_FromStringAndSize()
  return singletons for 0 or 1 bytes.
* Fix test_tracemalloc.test_set_traceback_limit().
2026-10-04 21:01:31 +00:00
Victor Stinner 9d22a5334b gh-158583: Fix uninitialized memory read in bytes.fromhex() (#158584) 2026-10-03 23:51:37 +00:00
Victor Stinner 880696af1a gh-158585: Use PyBytesWriter in io.BufferedReader.readline() (#158615)
Replace a list of bytes object with PyBytesWriter.
2026-10-03 22:15:45 +00:00
Victor Stinner 6e7738bb5e gh-158585: Fix _PyBytes_Resize() if hash value is already computed (#158589)
_PyBytes_Resize() now creates a new bytes object if the hash value
was already computed.

bytes_resize_inplace() no longer sets the hash value to -1, since
this function must not be called if the hash value was already
computed.

Add tests on PyUnicode_Resize().
2026-10-03 23:35:46 +02:00
Victor Stinner b42fdf6b44 Fix _PyOS_GetOpt() error message for non-ASCII characters (#158626)
Replace %c with %lc.
2026-10-03 15:37:58 +02:00
Victor Stinner f5e913c7f7 gh-158445: Allocate memory in the heap in Py_GetVersion() (#158608)
Py_GetVersion() now allocates memory on the heap, instead of using a
static buffer, to no longer truncate the version if it's longer than
299 bytes.

Update Py_GetCompiler() and Py_GetBuildInfo() tests: they are now
always a part of sys.version.
2026-10-03 15:15:08 +02:00
Victor Stinner 1a85213940 gh-158585: Adjust allocation in bytes_fromformat() (#158621)
Take in account preallocated bytes when calling
PyBytesWriter_GrowAndUpdatePointer().

Use sprintf() return value instead of calling strlen().
2026-10-03 00:34:38 +02:00
Victor Stinner b5aa195427 gh-155742: Use PyMem_Malloc() in memoryview.hex() (#158582)
Replace PyBytesWriter with a simple PyMem_Malloc(), since no bytes
object is needed. PyMem_Malloc() should be more efficient in this
case.
2026-10-02 17:51:47 +02:00
Victor Stinner 35bdbcad63 gh-158445: Skip invalid UCS-4 tests if Python is built with assertions (#158604)
If Python is built in release mode with assertions,
PyUnicode_FromKindAndData() and PyUnicodeWriter_Finish() fail with an
assertion error on invalid characters.
2026-10-02 15:22:13 +00:00
Victor Stinner a831abba14 gh-158445: Add C API tests on build information functions (#158603)
Add tests on functions:

* Py_GetBuildInfo()
* Py_GetCompiler()
* Py_GetCopyright()
* Py_GetPlatform()
* Py_GetVersion()

Changes:

* Add Modules/_testlimitedcapi.c and
  Lib/test/test_capi/test_build.py files.
* Add internal _Py_GetBuiltWithAssert() function.
* Reimplemenet support.built_with_c_assertions() using
  _Py_GetBuiltWithAssert() to cover more cases like buildbots using
  CFLAGS=-UNDEBUG in release mode.
2026-10-02 14:43:44 +00:00
Victor Stinner ee782e6143 gh-158585: Optimize PyBytesWriter_FinishWithSize() (#158586)
Add specialized bytes_resize_inplace() and inline
PyBytesWriter_Discard() in PyBytesWriter_FinishWithSize().

No longer copy the buffer if _PyObject_IsUniquelyReferenced() is false.

Add also bytes_get_char() function.
2026-10-02 03:06:58 +00:00
Victor Stinner c7a79bd2f4 gh-158567: Workaround GCC crash in PyFloat_Pack2() (#158568)
Avoid __builtin_bswap16() with _Float16 on s390x.
2026-10-01 16:19:26 +00:00
Victor Stinner 82fcaf881c Cleanup Python/getopt.c: add _PyOS_GetOpt structure (#158362)
Replace global variables with a new _PyOS_GetOpt structure:

* _PyOS_opterr => getopt->error
* _PyOS_optind => getopt->index
* _PyOS_optarg => getopt->arg

Changes:

* Add tests to test_cmd_line.
* Rename _PyOS_ResetGetOpt() to _PyOS_GetOpt_Init(). Add argc and
  argv parameters.
* Don't expose longindex in the structure, it's only used internally.
* Move _PyOS_LongOption structure from the internal C API to
  getopt.c.
2026-10-01 14:58:55 +00:00
Victor Stinner eb30e3d9d4 Optimize PyFloat_Pack*() functions (#158483)
Use _Py_bswap16/32/64() functions to swap bytes. PyFloat_Pack4() and
PyFloat_Pack8() avoid temporary buffer copy in the native byte order.
2026-10-01 12:25:38 +00:00
Victor Stinner a14a4a6bc9 gh-158445: Detect invalid UCS4 characters in debug mode (#158502)
In debug mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND) and
PyUnicodeWriter_WriteUCS4() now raise a SystemError if a character is
not in range [U+0000; U+10ffff], instead of creating an invalid str
object.

In release mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND)
raises a SystemError if the size is 1.

* In debug mode, unicode_char() no longer fails with an assertion
  error if the character is invalid. Instead, raise SystemError.
* Add tests on invalid UCS-4 characters.
* Add _testinternalcapi._Py_MAX_UNICODE.
* Add unicode_invalid_character() helper function.
2026-09-30 17:19:51 +02:00
Victor Stinner 7eada7c2c6 gh-158451: Fix python-gdb.py for invalid Unicode strings (#158475)
python-gdb.py is now able to format an invalid Unicode string:
render invalid characters as "\Uhhhhhhhh" instead of raising an
exception.

The PyUnicodeWriter API fills a UCS-4 buffer with 0xff byte pattern
to detect usage of uninitialized characters. It produces invalid
characters '\Uffffffff'.

PyBytesObjectPtr now iterates on bytes (list of integers), instead of
creating a temporary Unicode string. Add more bytes tests in test_gdb
pretty printer.

Remove the unused 'ch2' variable.
2026-09-30 14:12:59 +02:00
Victor Stinner 77c0675157 gh-158451: Add _PyUnicodeWriter_FinishWithSize() test (#158481) 2026-09-30 07:26:32 +00:00
Victor Stinner 70e6f3cfe1 gh-158451: Convert _PyUnicodeWriter_Prepare() to static inline function (#158466)
Add assertion to _PyUnicodeWriter_Prepare() to check that length is
not negative.

Add some assertions to check that _PyUnicodeWriter_Prepare() is
called with len >= 1.
2026-09-30 05:31:36 +02:00
Victor Stinner 82a89b38aa gh-158451: Reuse PyUnicodeWriter_WriteStr() in PyUnicode_Format() (#158467)
Remove special case for int in PyUnicode_Format(). Instead, reuse
PyUnicodeWriter_WriteStr() and PyUnicodeWriter_WriteRepr() in
PyUnicode_Format().

Add PyUnicode_Format() tests on integer formats.
2026-09-30 05:30:22 +02:00
Victor Stinner 26d3f5dd6a gh-158451: Add _PyUnicodeWriter_WriteLatin1String() test (#158464)
Fix _PyUnicodeWriter_WriteLatin1String() when the writer buffer kind
is UCS-2 or UCS-4.
2026-09-30 03:53:38 +02:00
Victor Stinner 00307b0e69 gh-158451: Add _PyUnicodeWriter_SetBuffer() helper function (#158452)
Replace _PyUnicodeWriter_Update() with _PyUnicodeWriter_SetBuffer().

Add _PyUnicodeWriter_SetReadOnly() helper function.

Add _PyUnicodeWriter_FinishWithSize() for PyUnicode_DecodeUTF7Stateful().
2026-09-30 02:47:46 +02:00
Victor Stinner 39e5ed7fea gh-157710: Avoid copy for large int in PyUnicodeWriter_WriteStr() (#158448)
Enable the PyUnicodeWriter read-only optimization for large integer
(more than 1000 digits) in PyUnicodeWriter_WriteStr() and
PyUnicodeWriter_WriteRepr().
2026-09-29 22:15:41 +02:00
Victor Stinner a0491c8939 gh-158439: Add _PyUnicodeWriter.recheck_maxchar (#158442)
If PyUnicodeWriter_WriteUTF8() or PyUnicodeWriter_Format() fails,
PyUnicodeWriter_Finish() now checks if the string uses the most
efficient storage. Previously, PyUnicodeWriter_Finish() could return
a string with an inefficient storage.

* Add _PyUnicodeWriter.recheck_maxchar. PyUnicodeWriter_WriteUTF8()
  and PyUnicodeWriter_Format() now set recheck_maxchar to 1 on error.
* Move error handling from caller to _PyUnicode_DecodeUTF8Writer().
* PyUnicodeWriter_DecodeUTF8Stateful(): rename length parameter to
  size and rename string parameter to str.
2026-09-29 22:03:16 +02:00
Victor StinnerandClaude Opus 4.6 24ea85a4c2 gh-156939: Fix two bugs in PyBytesWriter (#158404)
Fix PyBytesWriter_Format() error handling: restore the writer to its
previous state on error. Add an unit test injecting MemoryError.

Fix PyBytesWriter_FinishWithSize() to return an empty bytearray when
use_bytearray is set and size is 0, instead of always returning an
empty bytes object.

Fix test_capi.test_bytes: always check writer finish result type, to
make sure that _PyBytesWriter_CreateByteArray() always create
bytearray objects.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-09-29 14:50:06 +02:00
Victor StinnerandClaude Opus 4.6 90d40027f7 gh-156939: Fix Modules/_testcapi/bytes.c: remove double Py_DECREF (#158400)
Remove a spurious Py_DECREF(writer_type) in _PyTestCapi_Init_Bytes()
that over-decremented the reference after it was already released.
Also fix writer_discard method flags (METH_VARARGS -> METH_NOARGS),
a typo, and PEP 7 spacing in for-loop initializers.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-09-29 13:56:02 +02:00
Victor StinnerandVyron Vasileiadis b4be3a5804 gh-158334: Fix bytes.translate() when the input is returned unchanged (#158344)
Do no read output_start pointer after PyBytesWriter_Finish() since
the pointer became a dangling pointer.

Co-authored-by: Vyron Vasileiadis <hi@fedonman.com>
2026-09-29 12:17:17 +02:00
Victor StinnerandCody Maloney 969af80daf gh-158219: Fix bytearray constructor to not use hashed object (#158329)
* Add _PyBytes_GET_CACHED_HASH() static inline function.
* _PyBytes_IsMutable() makes sure that the hash value is not cached
  yet.
* bytearray_reinit_from_bytes() checks that the bytes object is
  mutable.

Co-authored-by: Cody Maloney <cmaloney@theoreticalchaos.com>
2026-09-29 11:31:54 +02:00
Victor Stinner 333071231d gh-157649: Fix Py_CLEAR()/Py_SETREF() on C++ (#158070)
On C++, do not use decltype() in _Py_TYPEOF since it produces invalid
code in Py_CLEAR() and Py_SETREF(). Instead, implement Py_CLEAR(),
Py_SETREF() and Py_XSETREF() using "auto" on C++11 and newer.

Add Py_CLEAR(), Py_SETREF() and Py_XSETREF() tests on an array.
2026-09-28 19:18:28 +00:00
Victor Stinner 795271306b gh-157649: Check for reference leak in test_cext (#158227)
test_cext now discovers and calls all "test" functions. On a debug
build, it also checks for reference leaks.

Fix a reference leak in test_virtual_object(): use a dealloc function
which calls Py_DECREF() on the type, and trigger an explicit garbage
collection.
2026-09-26 16:22:14 +02:00
Victor Stinner a000129df3 gh-158001: Fix test_ctypes.test_version_var() on s390x (#158193)
Py_Version type is "unsigned long", not "int". The bug is visible on
big endian machines such as s390x.
2026-09-26 01:02:29 +02:00
Victor Stinner b71989d657 gh-157649: Merge test_cppext into test_cext (#158176)
* test_cext now uses a single virtual environment for all tests.
  It makes the test way faster since creating a virtual environment
  is quite slow.
* Add Py_TARGET_ABI3T test on C++.
* Move macro tests from module exec function to test_macros().
* Call Py_BEGIN_CRITICAL_SECTION() on a fresh dictionary object,
  rather than on the module.
* Rename some environment variables to always use "CPYTHON_TEST_"
  prefix.
* Replace support.MS_WINDOWS with MSVC to be more explicit.
* Move macro tests to test_macros().
* On macOS, use C++11 by default.
2026-09-25 22:31:44 +00:00
Victor Stinner fe7717c611 gh-158080: Py_InitializeFromConfig() no longer exits Python (#158085)
If a command line option wants to exit Python, move this option
processing from PyConfig_Read() to Py_Main(). So
Py_InitializeFromInitConfig() and Py_InitializeFromConfig() can no
longer return an exit code.

Deprecate PyInitConfig_GetExitCode(), PyStatus_Exit(), and PyStatus_IsExit()
functions, and PyStatus.exitcode member.

Renumber getopt.c long options to start at 1, instead of 0.
2026-09-25 11:34:05 +00:00
Victor Stinner 0b72907cb5 gh-154757: No longer deprecate PyModule_GetFilename() (#158076) 2026-09-24 16:47:40 +02:00
Victor Stinner 7242fea93c gh-158001: Remove global configuration variables from C API (#158020)
Keep 4 variables in the stable ABI:

* Py_FileSystemDefaultEncodeErrors
* Py_FileSystemDefaultEncoding
* Py_HasFileSystemDefaultEncoding
* Py_UTF8Mode

Mark the 4 variables as ABI only in Misc/stable_abi.toml.
2026-09-24 16:42:02 +02:00
Victor Stinner a5a4659548 gh-158001: Remove Py_SetProgramName() function (#158024)
Remove Py_SetProgramName(), Py_SetPythonHome(), PySys_SetArgv() and
PySys_SetArgvEx() functions, deprecated since Python 3.11.

Mark the 4 functions as abi_only in stable_abi.toml.
2026-09-24 12:52:54 +02:00
Victor Stinner 82936545cc gh-154757: Remove deprecated PyEval_InitThreads() function (#158037)
Remove it from the C API, but keep it in the stable ABI.
2026-09-24 10:47:59 +02:00
Victor Stinner 2f5791df35 gh-158001: No longer read global config vars in PyConfig_Read() (#158014)
PyConfig_Read() and _PyPreConfig_Read() no longer read global config
variables (such as Py_BytesWarningFlag). Instead, PyConfig_Read() now
copies PyPreConfig members (isolated, use_environment and dev_mode).

_PyPreConfig_Read() still reads the last global configuration
variable: Py_UTF8Mode.
2026-09-23 21:21:38 +02:00
Victor Stinner becfd65c38 gh-158001: Avoid deprecated variable in Py_GETENV() (#158003)
No longer use deprecated Py_IgnoreEnvironmentFlag in Py_GETENV().
Instead, use PyConfig.use_environment when Python is initialized.

Modify also PySys_SetArgv() to use PyConfig.isolated when Python is
initialized.
2026-09-23 17:46:59 +02:00
Victor Stinner 5bde996950 gh-148233: Group stable_abi.toml entries per Python version (GH-157990) 2026-09-23 13:07:11 +00:00
Victor Stinner 71f6c005dc gh-157710: Defer allocation in PyUnicodeWriter_Create() (#157969)
PyUnicodeWriter_Create(length) no longer allocates 'length'
characters immediately. The allocation of the buffer is now done
lazily at the first write, except if the read-only optimization is
used. So the read-only optimization can also be used even if length
is greater than 0.

No longer overallocate the first buffer (at the first write).
Only overallocate when the buffer is resized (at the second write).
It avoids the need to truncate in PyUnicodeWriter_Finish() when
PyUnicodeWriter_Create(length) used the exact output length.

Add get_buffer() method to writer tests.
2026-09-23 11:20:30 +02:00
Victor Stinner 7cfb6552ff gh-148233: Add Py_HashBuffer() to the limited C API (#157966) 2026-09-23 11:17:39 +02:00
Victor Stinner 6f64920479 gh-157695: Fix acospi() detection in configure.ac (#157948)
Include <math.h> to check for acospi() and similar functions.
2026-09-22 22:17:16 +02:00
Victor Stinner a32139044a gh-157695: Rename HAVE_ACOSPI to _Py_HAVE_ACOSPI (#157858)
configure.ac: Add PY_CHECK_FUNC_PRIVATE() function, similar to
PY_CHECK_FUNC() but defines macro _Py_HAVE_xxx instead of HAVE_xxx.

Use PY_CHECK_FUNC_PRIVATE() for functions: acospi, asinpi, atanpi,
atan2pi, cospi, sinpi and tanpi. Update check_capi_macros_ignored.txt.
2026-09-22 06:00:28 +00:00
Victor Stinner 315ed1669a gh-157695: Rename HAVE_ICONV to _Py_HAVE_ICONV (#157821)
Update check_capi_macros_ignored.txt.
2026-09-22 07:28:48 +02:00
643013f58f gh-157695: Check that C API macros start with "Py" (#157696)
Add Tools/build/check_capi_macros.py. Ignore existing macros which
don't start with "Py", only prevent adding new names which don't
respect this convention.

Run check_capi_macros.py in pre-commit.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2026-09-21 16:23:12 +00:00
Victor Stinner 21223c9ff5 gh-157710: Enable read-only optimization in PyUnicodeWriter (#157861)
If the first and only write to a PyUnicodeWriter is a Python str
object, PyUnicodeWriter_Finish() returns the object unchanged.

PyUnicodeWriter_WriteChar() uses a singleton if no buffer was
allocated yet.

Move test_unicode_equal() to the correct test case (CAPITest).
2026-09-21 10:32:18 +02:00
Victor Stinner a64b7ecba4 gh-157710: Avoid resize in PyUnicodeWriter_Finish() for singleton (#157862)
PyBytesWriter_FinishWithSize() and PyUnicodeWriter_Finish() now
discard the output string instead of resizing it if the single
byte/character singleton is used.
2026-09-20 16:25:20 +02:00
Victor Stinner 6b97452836 gh-157710: Add MemoryError tests to PyUnicodeWriter (#157853)
Add a test changing the buffer kind multiple times.

Add assertions to _PyUnicodeWriter_InitWithBuffer().
2026-09-20 14:01:40 +02:00
Victor Stinner a92e0f9b9f gh-157242: Use support.inject_memory_error_cm() in tests (#157811)
Rename support.memory_error_cm() to support.inject_memory_error_cm().
2026-09-19 17:53:35 +00:00
Victor Stinner e8f644868b gh-156939: Add tests on the PyUnicode C API (#157798)
Add tests on functions:

* PyUnicode_CHECK_INTERNED()
* PyUnicode_Check()
* PyUnicode_CheckExact()
* PyUnicode_Equal()
* PyUnicode_GET_LENGTH()
* PyUnicode_IS_ASCII()
* PyUnicode_IS_COMPACT()
* PyUnicode_IS_COMPACT_ASCII()
* PyUnicode_KIND()
* PyUnicode_MAX_CHAR_VALUE()
* PyUnicode_READ()
* PyUnicode_READ_CHAR()
* PyUnicode_WRITE()

Changes:

* Add test on PyUnicode_FromOrdinal(0).
* Require _testcapi module in test_capi.test_unicode. Since _testcapi
  is specific to CPython, remove @support.cpython_only.
2026-09-19 16:08:58 +00:00
Victor Stinner 4d3b405df8 gh-156939: Document that PyUnicodeObject ends with null character (#157708)
Document also that PyUnicode_READ() and PyUnicode_READ_CHAR()
accept reading the trailing null character, and that PyUnicode_GetLength()
doesn't count the trailing null character.
2026-09-19 13:56:08 +00:00
Victor Stinner 1d0f1ad0aa gh-157695: Rename HAVE_FLOAT16 macro to _Py_HAVE_FLOAT16 (#157752) 2026-09-19 10:34:26 +02:00
Victor Stinner c7dbdb5f6b gh-155742: Use PyBytesWriter in PySSL_RAND() (#157731)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.

Remove 'pseudo' parameter of PySSL_RAND(): it's no longer needed since
ssl.RAND_pseudo_bytes() has been removed (in 2022,
commit d435a18c53).

Add a test on ssl.RAND_bytes(0).
2026-09-18 15:10:42 +02:00
Victor Stinner 86c93a5b84 gh-155742: Use PyBytesWriter in _Py_strhex_impl() (#157734)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.

Replace also PyBytes_FromStringAndSize(NULL, 0) with
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) in other functions.

Replace also PyBytes_FromStringAndSize(NULL, 0) with
PyBytes_FromStringAndSize("", 0) in _Py_GetConstant_Init().
2026-09-18 14:09:09 +02:00
Victor Stinner e2ff498cc0 gh-157242: Add byteswriter_check_consistency() (#157723)
Add byteswriter_check_consistency() to check that the bytes/bytearray
object can be mutated. It is called by all PyBytesWriter functions.

* Add test_thread to PyBytesWriter tests.
* _PyBytes_IsMutable() now checks _PyObject_IsUniquelyReferenced().
* Clarify thread safety in PyBytesWriter documentation.
2026-09-18 02:43:38 +00:00
Victor Stinner 23852543ed gh-157710: Detect overflow in PyUnicodeWriter_Finish() (#157715)
Check if the trailing null character has been modified to detect
buffer overflow in C extensions.

_PyUnicode_CheckConsistency() now always check if the trailing null
character has been overridden to detect buffer overflow. Previously,
it was only been checked if check_content parameter was non-zero.
2026-09-18 02:12:56 +00:00
Victor Stinner 854295809a gh-157710: Add _PyUnicodeWriter_CanWrite() function (#157712)
Make sure that a writer can be modified before writing into it.

* Document that PyUnicodeWriter is not thread safe.
* Add singletons tests to test_capi.test_unicode.
* Check PyUnicode_CheckExact() earlier in _PyUnicode_IsModifiable().
2026-09-18 00:32:50 +00:00
Victor StinnerandNathan Goldbaum d1878ccb44 gh-157415: Fix data race in pthread wrapper (#157430)
Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid
a data race if another thread calls PyMem_SetAllocator() in parallel.

Reenable @nomemtest tests on TSAN.

Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
2026-09-17 16:34:50 +02:00
Victor Stinner 86c17926b7 gh-154137, regrtest: Check for Windows handle leaks (#154140) 2026-09-17 16:32:27 +02:00
Victor StinnerandPetr Viktorin 8bcbcf8cfc gh-155742: Check singletons consistency at Python exit (#157572)
In debug mode at Python exit, check if immutable singleton objects
have been modified by mistake to detect bugs in C extensions.

Add tests corrupting bytes, str, bool and int singleton objects.

Add pycore_global_objects_fini.h internal header file.
pycore_global_objects_fini_generated.h is now fully generated by the
script.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
2026-09-17 16:03:45 +02:00