Skip to content

Conversation

@ngoldbaum
Copy link
Member

Per PEP 703, objects that are automatically immortal in the nogil build include:

Some objects, such as interned strings, small integers, statically allocated PyTypeObjects, and the True, False, and None objects stay alive for the lifetime of the program.

That means np.dtype('f4') is immortal, because all the legacy descriptors are declared statically:

static _PyArray_LegacyDescr @from@_Descr = {
PyObject_HEAD_INIT(&PyArrayDescr_Type)
.typeobj = &Py@NAME@ArrType_Type,
.kind = NPY_@from@LTR,
.type = NPY_@from@LTR,
.byteorder = '@endian@',
.flags = @flags@,
.type_num = NPY_@from@,
.elsize = 0,
.alignment = NPY_ALIGNOF(@align@),
.hash = -1,
};

As far as I can tell, this is the only test in the test suite that depends on INCREF or DECREF for a built-in dtype not being a no-op.

The spelling I've used to detect the nogil build is also what CPython uses internally: https://github.com/python/cpython/blob/7ecd55d604a8fa287c1d131cac14d10260be826b/Lib/test/support/__init__.py#L839

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummm, interesting that it manages that for statically allocated instances, but I guess the PyObject_HEAD code does so. We could also use sys.getrefcount(dt) > rc2_dt or immortal(dt)?

But unless that seems like a good idea to you immediately, this looks fine to me.

@ngoldbaum
Copy link
Member Author

Thanks! I think the comment is sufficient to explain what's going on for now.

@ngoldbaum ngoldbaum merged commit 815c53e into numpy:main Apr 8, 2024
@rgommers rgommers added this to the 2.1.0 release milestone Apr 9, 2024
@rgommers rgommers added the 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

05 - Testing 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants