Skip to content
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.

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

gh-128629: Add Py_PACK_VERSION and Py_PACK_FULL_VERSION #128630

Merged
merged 2 commits into from
Jan 9, 2025

Conversation

encukou
Copy link
Member

@encukou encukou commented Jan 8, 2025

@@ -0,0 +1,74 @@
/* Test version macros in the limited API */

#define Py_LIMITED_API 0x030E0000 // Added in 3.14
Copy link
Member

Choose a reason for hiding this comment

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

You need more complicated code for Free Threading, something like:

#include "pyconfig.h"  // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
#  define Py_LIMITED_API 0x030e0000  // Added in 3.14
#endif

result = _testlimitedcapi.pack_version(*args)
self.assertEqual(result, expected)

def test_pack_full_version_ctypes(self):
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of testing the API through ctypes? It should be the same as testing _testlimitedcapi, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

It tests the exported function.
(Unlike most similar API, these are macros even in limited API. This tests the case of not using the C headers at all.)

Copy link
Member

Choose a reason for hiding this comment

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

The _testcapi module tests Py_INCREF() macro and function this way:

#define TEST_REFCOUNT() \
    do { \
        PyObject *obj = PyList_New(0); \
        if (obj == NULL) { \
            return NULL; \
        } \
        assert(Py_REFCNT(obj) == 1); \
        \
        /* test Py_NewRef() */ \
        PyObject *ref = Py_NewRef(obj); \
        assert(ref == obj); \
        assert(Py_REFCNT(obj) == 2); \
        Py_DECREF(ref); \
        \
        /* test Py_XNewRef() */ \
        PyObject *xref = Py_XNewRef(obj); \
        assert(xref == obj); \
        assert(Py_REFCNT(obj) == 2); \
        Py_DECREF(xref); \
        \
        assert(Py_XNewRef(NULL) == NULL); \
        \
        Py_DECREF(obj); \
        Py_RETURN_NONE; \
    } while (0)


// Test Py_NewRef() and Py_XNewRef() macros
static PyObject*
test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
{
    TEST_REFCOUNT();
}

#undef Py_NewRef
#undef Py_XNewRef

// Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
static PyObject*
test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
{
    TEST_REFCOUNT();
}

Maybe you can do something similar to avoid ctypes.

Copy link
Member Author

Choose a reason for hiding this comment

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

The C tests do something similar, with #undef Py_PACK_FULL_VERSION.
(But if I had misspelled that #undef, the test would quietly be ineffective.)

Also, I don't particularly want to avoid ctypes :)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -1,4 +1,5 @@

#ifndef _Py_PATCHLEVEL_H
Copy link
Member

Choose a reason for hiding this comment

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

Why not just Py_PATCHLEVEL_H, as done in other header files?

Copy link
Member Author

@encukou encukou Jan 9, 2025

Choose a reason for hiding this comment

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

Because I don't want to add this define to public API. It's a private detail, users don't need this; we should be able to, for example, merge this code into another header and remove patchlevel.h.

@encukou
Copy link
Member Author

encukou commented Jan 9, 2025

Thank you for the reviews!

@encukou encukou merged commit 1439b81 into python:main Jan 9, 2025
45 checks passed
@encukou encukou deleted the pack_version_wg branch January 9, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants