GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add PyUnstable_SetImmortal by kumaraditya303 · Pull Request #164 · python/pythoncapi-compat

Add PyUnstable_SetImmortal #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
vstinner merged 8 commits into python:main from kumaraditya303:main
Feb 12, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add PyUnstable_SetImmortal #164

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
8 commits
Select commit Hold shift + click to select a range
a57ea4e
Add PyUnstable_SetImmortal
kumaraditya303 Feb 12, 2026
36c4507
restrict to 3.14
kumaraditya303 Feb 12, 2026
c201d29
add docs
kumaraditya303 Feb 12, 2026
e2bac98
Update pythoncapi_compat.h
kumaraditya303 Feb 12, 2026
9103321
add availability
kumaraditya303 Feb 12, 2026
3ccded6
add support for 3.13
kumaraditya303 Feb 12, 2026
0280559
fix 3.14
kumaraditya303 Feb 12, 2026
3eae7cb
fmt
kumaraditya303 Feb 12, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

5 changes: 5 additions & 0 deletions 5 docs/api.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Python 3.15

On PyPy, always returns ``-1``.

.. c:function:: int PyUnstable_SetImmortal(PyObject *op)

See `PyUnstable_SetImmortal() documentation <https://docs.python.org/dev/c-api/object.html#c.PyUnstable_SetImmortal>`__.

Comment thread
kumaraditya303 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

Availability: Python 3.13 and newer, not available on PyPy.

Python 3.14
-----------
Expand Down
4 changes: 4 additions & 0 deletions 4 docs/changelog.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

* 2026-02-12: Add functions:

* ``PyUnstable_SetImmortal()``

* 2025-10-14: Add functions:

* ``PyTuple_FromArray()``
Expand Down
31 changes: 31 additions & 0 deletions 31 pythoncapi_compat.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,37 @@ PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)
}
#endif

#if 0x030D0000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030F00A7 && !defined(PYPY_VERSION)
Comment thread
kumaraditya303 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

// Immortal objects were implemented in Python 3.12, however there is no easy API
// to make objects immortal until 3.14 which has _Py_SetImmortal(). Since
// immortal objects are primarily needed for free-threading, this API is implemented
// for 3.14 using _Py_SetImmortal() and uses private macros on 3.13.
static inline int
PyUnstable_SetImmortal(PyObject *op)
{
assert(op != NULL);
if (!PyUnstable_Object_IsUniquelyReferenced(op) || PyUnicode_Check(op)) {
return 0;
}
#if 0x030E0000 <= PY_VERSION_HEX
PyAPI_FUNC(void) _Py_SetImmortal(PyObject *op);
_Py_SetImmortal(op);
#else
// Python 3.13 doesn't export _Py_SetImmortal() function
if (PyObject_GC_IsTracked(op)) {
PyObject_GC_UnTrack(op);
}
#ifdef Py_GIL_DISABLED
op->ob_tid = _Py_UNOWNED_TID;
op->ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL;
op->ob_ref_shared = 0;
#else
op->ob_refcnt = _Py_IMMORTAL_REFCNT;
#endif
#endif
return 1;
}
#endif

#ifdef __cplusplus
}
Expand Down
36 changes: 36 additions & 0 deletions 36 tests/test_pythoncapi_compat_cext.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,39 @@ test_try_incref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}

#if 0x030D0000 <= PY_VERSION_HEX && !defined(PYPY_VERSION)
static PyObject *
test_set_immortal(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject object;
memset(&object, 0, sizeof(PyObject));
#ifdef Py_GIL_DISABLED
object.ob_tid = _Py_ThreadId();
object.ob_gc_bits = 0;
object.ob_ref_local = 1;
object.ob_ref_shared = 0;
#else
object.ob_refcnt = 1;
#endif
object.ob_type = &PyBaseObject_Type;

int rc = PyUnstable_SetImmortal(&object);
assert(rc == 1);
Py_DECREF(&object); // should not dealloc

// Check already immortal object
rc = PyUnstable_SetImmortal(&object);
assert(rc == 0);

// Check unicode objects
PyObject *unicode = PyUnicode_FromString("test");
rc = PyUnstable_SetImmortal(unicode);
assert(rc == 0);
Py_DECREF(unicode);
Py_RETURN_NONE;
}
#endif


static struct PyMethodDef methods[] = {
{"test_object", test_object, METH_NOARGS, _Py_NULL},
Expand Down Expand Up @@ -2546,6 +2579,9 @@ static struct PyMethodDef methods[] = {
{"test_byteswriter", test_byteswriter, METH_NOARGS, _Py_NULL},
{"test_tuple", test_tuple, METH_NOARGS, _Py_NULL},
{"test_try_incref", test_try_incref, METH_NOARGS, _Py_NULL},
#if 0x030D0000 <= PY_VERSION_HEX && !defined(PYPY_VERSION)
{"test_set_immortal", test_set_immortal, METH_NOARGS, _Py_NULL},
#endif
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.