-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3335,6 +3335,49 @@ def run(self): | |
self.assertEqual(len(set(py_thread_ids)), len(py_thread_ids), | ||
py_thread_ids) | ||
|
||
class TestVersions(unittest.TestCase): | ||
full_cases = ( | ||
(3, 4, 1, 0xA, 2, 0x030401a2), | ||
(3, 10, 0, 0xF, 0, 0x030a00f0), | ||
(0x103, 0x10B, 0xFF00, -1, 0xF0, 0x030b00f0), # test masking | ||
) | ||
xy_cases = ( | ||
(3, 4, 0x03040000), | ||
(3, 10, 0x030a0000), | ||
(0x103, 0x10B, 0x030b0000), # test masking | ||
) | ||
|
||
def test_pack_full_version(self): | ||
for *args, expected in self.full_cases: | ||
with self.subTest(hexversion=hex(expected)): | ||
result = _testlimitedcapi.pack_full_version(*args) | ||
self.assertEqual(result, expected) | ||
|
||
def test_pack_version(self): | ||
for *args, expected in self.xy_cases: | ||
with self.subTest(hexversion=hex(expected)): | ||
result = _testlimitedcapi.pack_version(*args) | ||
self.assertEqual(result, expected) | ||
|
||
def test_pack_full_version_ctypes(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It tests the exported function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The #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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The C tests do something similar, with Also, I don't particularly want to avoid ctypes :) |
||
ctypes = import_helper.import_module('ctypes') | ||
ctypes_func = ctypes.pythonapi.Py_PACK_FULL_VERSION | ||
ctypes_func.restype = ctypes.c_uint32 | ||
ctypes_func.argtypes = [ctypes.c_int] * 5 | ||
for *args, expected in self.full_cases: | ||
with self.subTest(hexversion=hex(expected)): | ||
result = ctypes_func(*args) | ||
self.assertEqual(result, expected) | ||
|
||
def test_pack_version_ctypes(self): | ||
ctypes = import_helper.import_module('ctypes') | ||
ctypes_func = ctypes.pythonapi.Py_PACK_VERSION | ||
ctypes_func.restype = ctypes.c_uint32 | ||
ctypes_func.argtypes = [ctypes.c_int] * 2 | ||
for *args, expected in self.xy_cases: | ||
with self.subTest(hexversion=hex(expected)): | ||
result = ctypes_func(*args) | ||
self.assertEqual(result, expected) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add macros :c:func:`Py_PACK_VERSION` and :c:func:`Py_PACK_FULL_VERSION` for | ||
bit-packing Python version numbers. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.