mirror of
https://github.com/python/cpython
synced 2026-11-06 09:02:40 +03:00
gh-161185: Fix test_cext: replace PyType_FromSlots() with PyType_FromSpec() Replace PyType_FromSlots() with PyType_FromSpec(), and replace PySlot with PyType_Spec. Python 3.14 doesn't support PyType_FromSlots(), it has been added to Python 3.15.
551 lines
14 KiB
C
551 lines
14 KiB
C
// gh-116869: C/C++ extension module used to check that the Python C API does
|
|
// not emit compiler warnings.
|
|
//
|
|
// Test also the internal C API if the TEST_INTERNAL_C_API macro is defined.
|
|
//
|
|
// Declare variables at the top of each function to support building with
|
|
// -Werror=declaration-after-statement.
|
|
|
|
// Always enable assertions
|
|
#undef NDEBUG
|
|
|
|
#ifdef TEST_INTERNAL_C_API
|
|
# define Py_BUILD_CORE_MODULE 1
|
|
#endif
|
|
|
|
#include "Python.h"
|
|
#include "datetime.h"
|
|
|
|
#ifdef __cplusplus
|
|
# ifdef TEST_INTERNAL_C_API
|
|
// gh-135906: Check for compiler warnings in the internal C API
|
|
// - Cython uses pycore_critical_section.h, pycore_frame.h and
|
|
// pycore_template.h.
|
|
// - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and
|
|
// pycore_interpframe.h.
|
|
# include "internal/pycore_frame.h"
|
|
# include "internal/pycore_interpframe_structs.h"
|
|
# include "internal/pycore_template.h"
|
|
|
|
// mimalloc emits compiler warnings on Windows.
|
|
# if !defined(MS_WINDOWS)
|
|
# include "internal/pycore_backoff.h"
|
|
# include "internal/pycore_cell.h"
|
|
# include "internal/pycore_critical_section.h"
|
|
# include "internal/pycore_interpframe.h"
|
|
# endif
|
|
# endif
|
|
#else
|
|
# ifdef TEST_INTERNAL_C_API
|
|
// gh-135906: Check for compiler warnings in the internal C API.
|
|
// - Cython uses pycore_critical_section.h, pycore_frame.h and
|
|
// pycore_template.h.
|
|
// - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and
|
|
// pycore_interpframe.h.
|
|
# include "internal/pycore_critical_section.h"
|
|
# include "internal/pycore_frame.h"
|
|
# include "internal/pycore_gc.h"
|
|
# include "internal/pycore_interp.h"
|
|
# include "internal/pycore_interpframe.h"
|
|
# include "internal/pycore_interpframe_structs.h"
|
|
# include "internal/pycore_object.h"
|
|
# include "internal/pycore_pystate.h"
|
|
# include "internal/pycore_template.h"
|
|
# endif
|
|
#endif
|
|
|
|
#ifndef MODULE_NAME
|
|
# error "MODULE_NAME macro must be defined"
|
|
#endif
|
|
|
|
#define _STR(NAME) #NAME
|
|
#define STR(NAME) _STR(NAME)
|
|
|
|
PyDoc_STRVAR(test_add_doc,
|
|
"add(x, y)\n"
|
|
"\n"
|
|
"Return the sum of two integers: x + y.");
|
|
|
|
static PyObject *
|
|
test_add(PyObject *Py_UNUSED(module), PyObject *args)
|
|
{
|
|
long i, j, res;
|
|
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
|
|
return NULL;
|
|
}
|
|
res = i + j;
|
|
return PyLong_FromLong(res);
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
test_macros(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
PyObject *obj;
|
|
#ifndef Py_LIMITED_API
|
|
PyObject *dict;
|
|
#endif
|
|
PyObject *slots[1];
|
|
int small_array[] = {2, 5, 7};
|
|
|
|
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
|
|
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
|
|
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
|
|
|
|
// Test Py_MIN(), Py_MAX(), Py_ABS()
|
|
assert(Py_MIN(5, 11) == 5);
|
|
assert(Py_MAX(5, 11) == 11);
|
|
assert(Py_ABS(-5) == 5);
|
|
|
|
// Test Py_CLEAR(): use typeof()/__typeof__() if available, or memcpy()
|
|
obj = Py_None;
|
|
Py_CLEAR(obj);
|
|
assert(obj == _Py_NULL);
|
|
|
|
// gh-157649: Test Py_CLEAR() on an array
|
|
slots[0] = Py_None;
|
|
Py_CLEAR(slots[0]);
|
|
assert(slots[0] == _Py_NULL);
|
|
|
|
#ifndef Py_LIMITED_API
|
|
// Test Py_SETREF(): use typeof()/__typeof__() if available, or memcpy()
|
|
obj = Py_None;
|
|
Py_SETREF(obj, _Py_NULL);
|
|
assert(obj == _Py_NULL);
|
|
|
|
// gh-157649: Test Py_SETREF() on an array
|
|
slots[0] = Py_None;
|
|
Py_SETREF(slots[0], _Py_NULL);
|
|
assert(slots[0] == _Py_NULL);
|
|
|
|
// Test Py_XSETREF(): use typeof()/__typeof__() if available, or memcpy()
|
|
obj = Py_None;
|
|
Py_XSETREF(obj, _Py_NULL);
|
|
assert(obj == _Py_NULL);
|
|
|
|
// gh-157649: Test Py_XSETREF() on an array
|
|
slots[0] = Py_None;
|
|
Py_XSETREF(slots[0], _Py_NULL);
|
|
assert(slots[0] == _Py_NULL);
|
|
|
|
// Test that Py_BEGIN_CRITICAL_SECTION is available
|
|
dict = PyDict_New();
|
|
if (dict == NULL) {
|
|
return NULL;
|
|
}
|
|
Py_BEGIN_CRITICAL_SECTION(dict);
|
|
Py_END_CRITICAL_SECTION();
|
|
Py_DECREF(dict);
|
|
#endif // !Py_LIMITED_API
|
|
|
|
// Test Py_ARRAY_LENGTH()
|
|
assert(Py_ARRAY_LENGTH(small_array) == 3);
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
|
|
// Py_SETREF/XSETREF() macros are excluded from the limited C API
|
|
#ifndef Py_LIMITED_API
|
|
Py_NO_INLINE static void
|
|
pyclear_strict_aliasing(PyTypeObject **a, PyTypeObject **b)
|
|
{
|
|
Py_CLEAR(*a);
|
|
*b = NULL;
|
|
}
|
|
|
|
|
|
Py_NO_INLINE static void
|
|
setref_strict_aliasing(PyTypeObject **a, PyTypeObject **b,
|
|
PyTypeObject *first, PyTypeObject *second)
|
|
{
|
|
*a = first;
|
|
Py_SETREF(*b, second);
|
|
}
|
|
|
|
|
|
Py_NO_INLINE static void
|
|
xsetref_strict_aliasing(PyTypeObject **a, PyTypeObject **b,
|
|
PyTypeObject *first, PyTypeObject *second)
|
|
{
|
|
*a = first;
|
|
Py_XSETREF(*b, second);
|
|
}
|
|
|
|
static PyType_Slot empty_type_slots[] = {
|
|
{0, 0},
|
|
};
|
|
|
|
static PyType_Spec setref_type_spec = {
|
|
STR(MODULE_NAME) ".setref_type", // .name
|
|
0, // .basicsize
|
|
0, // .itemsize
|
|
Py_TPFLAGS_DEFAULT, // .flags
|
|
empty_type_slots, // .slots
|
|
};
|
|
|
|
// gh-161185: Test that Py_SETREF(), Py_XSETREF() and Py_CLEAR() macros
|
|
// have no strict aliasing issue
|
|
static PyObject *
|
|
test_setref_strict_aliasing(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
PyObject *first, *second;
|
|
Py_ssize_t first_refcnt, second_refcnt;
|
|
PyTypeObject *slot = NULL;
|
|
|
|
first = PyType_FromSpec(&setref_type_spec);
|
|
if (first == NULL) {
|
|
return NULL;
|
|
}
|
|
second = PyType_FromSpec(&setref_type_spec);
|
|
if (second == NULL) {
|
|
Py_DECREF(first);
|
|
return NULL;
|
|
}
|
|
first_refcnt = Py_REFCNT(first);
|
|
second_refcnt = Py_REFCNT(second);
|
|
|
|
// Both pointer arguments alias. The assignment makes first the old value
|
|
// that Py_SETREF() must release before storing second.
|
|
setref_strict_aliasing(&slot, &slot,
|
|
(PyTypeObject*)Py_NewRef(first),
|
|
(PyTypeObject*)Py_NewRef(second));
|
|
assert((PyObject*)slot == second);
|
|
assert(Py_REFCNT(first) == first_refcnt);
|
|
assert(Py_REFCNT(second) == (second_refcnt + 1));
|
|
|
|
pyclear_strict_aliasing(&slot, &slot);
|
|
assert(slot == NULL);
|
|
assert(Py_REFCNT(first) == first_refcnt);
|
|
assert(Py_REFCNT(second) == second_refcnt);
|
|
|
|
xsetref_strict_aliasing(&slot, &slot,
|
|
(PyTypeObject*)Py_NewRef(first),
|
|
(PyTypeObject*)Py_NewRef(second));
|
|
assert((PyObject*)slot == second);
|
|
assert(Py_REFCNT(first) == first_refcnt);
|
|
assert(Py_REFCNT(second) == (second_refcnt + 1));
|
|
|
|
Py_DECREF(first);
|
|
Py_DECREF(second);
|
|
Py_DECREF((PyObject*)slot);
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
#endif // !Py_LIMITED_API
|
|
|
|
|
|
static PyObject *
|
|
test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
// datetime.h is excluded from the limited C API
|
|
#ifndef Py_LIMITED_API
|
|
if (PyDateTimeAPI == NULL) {
|
|
PyDateTime_IMPORT;
|
|
if (PyErr_Occurred()) {
|
|
return NULL;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
PyObject *str;
|
|
#ifndef Py_LIMITED_API
|
|
const void* data;
|
|
int kind;
|
|
const void* const_data;
|
|
unsigned int ukind;
|
|
#endif
|
|
|
|
str = PyUnicode_FromString("abc");
|
|
if (str == _Py_NULL) {
|
|
return _Py_NULL;
|
|
}
|
|
|
|
assert(PyUnicode_Check(str));
|
|
|
|
assert(PyUnicode_GetLength(str) == 3);
|
|
assert(PyUnicode_ReadChar(str, 0) == 'a');
|
|
assert(PyUnicode_ReadChar(str, 1) == 'b');
|
|
|
|
#ifndef Py_LIMITED_API
|
|
assert(PyUnicode_GET_LENGTH(str) == 3);
|
|
|
|
// gh-92800: test PyUnicode_READ()
|
|
data = PyUnicode_DATA(str);
|
|
assert(data != _Py_NULL);
|
|
kind = PyUnicode_KIND(str);
|
|
assert(kind == PyUnicode_1BYTE_KIND);
|
|
assert(PyUnicode_READ(kind, data, 0) == 'a');
|
|
|
|
// gh-92800: test PyUnicode_READ() casts
|
|
const_data = PyUnicode_DATA(str);
|
|
#ifdef __cplusplus
|
|
ukind = static_cast<unsigned int>(kind);
|
|
#else
|
|
ukind = (unsigned int)kind;
|
|
#endif
|
|
assert(PyUnicode_READ(ukind, const_data, 2) == 'c');
|
|
|
|
assert(PyUnicode_READ_CHAR(str, 1) == 'b');
|
|
#endif
|
|
|
|
Py_DECREF(str);
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
|
|
#ifdef __cplusplus
|
|
// Class to test operator casting an object to PyObject*
|
|
class StrongRef
|
|
{
|
|
public:
|
|
StrongRef(PyObject *obj) : m_obj(obj) {
|
|
Py_INCREF(this->m_obj);
|
|
}
|
|
|
|
~StrongRef() {
|
|
Py_DECREF(this->m_obj);
|
|
}
|
|
|
|
// Cast to PyObject*: get a borrowed reference
|
|
inline operator PyObject*() const { return this->m_obj; }
|
|
|
|
private:
|
|
PyObject *m_obj; // Strong reference
|
|
};
|
|
|
|
|
|
static PyObject *
|
|
test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
PyObject *obj = Py_BuildValue("(ii)", 1, 2);
|
|
if (obj == _Py_NULL) {
|
|
return _Py_NULL;
|
|
}
|
|
Py_ssize_t refcnt = Py_REFCNT(obj);
|
|
assert(refcnt >= 1);
|
|
|
|
#ifndef Py_LIMITED_API
|
|
// gh-92138: For backward compatibility, functions of Python C API accepts
|
|
// "const PyObject*". Check that using it does not emit C++ compiler
|
|
// warnings.
|
|
const PyObject *const_obj = obj;
|
|
Py_INCREF(const_obj);
|
|
Py_DECREF(const_obj);
|
|
PyTypeObject *type = Py_TYPE(const_obj);
|
|
assert(Py_REFCNT(const_obj) == refcnt);
|
|
assert(type == &PyTuple_Type);
|
|
assert(PyTuple_GET_SIZE(const_obj) == 2);
|
|
PyObject *one = PyTuple_GET_ITEM(const_obj, 0);
|
|
assert(PyLong_AsLong(one) == 1);
|
|
#endif
|
|
|
|
// gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
|
|
// cast to PyObject*.
|
|
StrongRef strong_ref(obj);
|
|
assert(Py_TYPE(strong_ref) == &PyTuple_Type);
|
|
assert(Py_REFCNT(strong_ref) == (refcnt + 1));
|
|
Py_INCREF(strong_ref);
|
|
Py_DECREF(strong_ref);
|
|
|
|
// gh-93442: Pass 0 as NULL for PyObject*
|
|
Py_XINCREF(0);
|
|
Py_XDECREF(0);
|
|
#if __cplusplus >= 201103
|
|
// Test nullptr passed as PyObject*
|
|
Py_XINCREF(nullptr);
|
|
Py_XDECREF(nullptr);
|
|
#endif
|
|
|
|
Py_DECREF(obj);
|
|
Py_RETURN_NONE;
|
|
}
|
|
#endif // __cplusplus
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
/* Test a `new`-allocated object with a virtual method.
|
|
* (https://github.com/python/cpython/issues/94731) */
|
|
|
|
class VirtualPyObject : public PyObject {
|
|
public:
|
|
VirtualPyObject();
|
|
virtual ~VirtualPyObject() {
|
|
PyTypeObject *type = Py_TYPE(this);
|
|
delete [] internal_data;
|
|
--instance_count;
|
|
// Do not call type->tp_free(this), C++ manages the memory
|
|
Py_DECREF(type);
|
|
}
|
|
virtual void set_internal_data() {
|
|
internal_data[0] = 1;
|
|
}
|
|
static void dealloc(PyObject* o) {
|
|
delete static_cast<VirtualPyObject*>(o);
|
|
}
|
|
|
|
// Number of "living" instances
|
|
static int instance_count;
|
|
private:
|
|
// buffer that can get corrupted
|
|
int* internal_data;
|
|
};
|
|
|
|
int VirtualPyObject::instance_count = 0;
|
|
|
|
// Converting from function pointer to void* has undefined behavior, but
|
|
// works on all known platforms, and CPython's module and type slots currently
|
|
// need it.
|
|
// (GCC doesn't have a narrower category for this than -Wpedantic.)
|
|
_Py_COMP_DIAG_PUSH
|
|
#if defined(__GNUC__)
|
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
|
#elif defined(__clang__)
|
|
#pragma clang diagnostic ignored "-Wpedantic"
|
|
#endif
|
|
|
|
PyType_Slot VirtualPyObject_Slots[] = {
|
|
{Py_tp_dealloc, (void*)VirtualPyObject::dealloc},
|
|
{0, _Py_NULL},
|
|
};
|
|
|
|
_Py_COMP_DIAG_POP
|
|
|
|
PyType_Spec VirtualPyObject_Spec = {
|
|
/* .name */ STR(MODULE_NAME) ".VirtualPyObject",
|
|
/* .basicsize */ sizeof(VirtualPyObject),
|
|
/* .itemsize */ 0,
|
|
/* .flags */ Py_TPFLAGS_DEFAULT,
|
|
/* .slots */ VirtualPyObject_Slots,
|
|
};
|
|
|
|
VirtualPyObject::VirtualPyObject() {
|
|
// Create a temporary type (just so we don't need to store it)
|
|
PyObject *type = PyType_FromSpec(&VirtualPyObject_Spec);
|
|
// no good way to signal failure from a C++ constructor, so use assert
|
|
// for error handling
|
|
assert(type);
|
|
assert(PyObject_Init(this, (PyTypeObject *)type));
|
|
Py_DECREF(type);
|
|
internal_data = new int[50];
|
|
++instance_count;
|
|
}
|
|
|
|
static PyObject *
|
|
test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
|
{
|
|
VirtualPyObject* obj = new VirtualPyObject();
|
|
obj->set_internal_data();
|
|
Py_DECREF(obj);
|
|
if (VirtualPyObject::instance_count != 0) {
|
|
return PyErr_Format(
|
|
PyExc_AssertionError,
|
|
"instance_count should be 0, got %d",
|
|
VirtualPyObject::instance_count);
|
|
}
|
|
|
|
// Force a garbage collection to delete the temporary heap type
|
|
// used by this test
|
|
PyGC_Collect();
|
|
Py_RETURN_NONE;
|
|
}
|
|
#endif // __cplusplus && !Py_TARGET_ABI3T
|
|
|
|
|
|
static PyMethodDef module_methods[] = {
|
|
{"add", test_add, METH_VARARGS, test_add_doc},
|
|
{"test_macros", test_macros, METH_NOARGS, _Py_NULL},
|
|
#ifndef Py_LIMITED_API
|
|
{"test_setref_strict_aliasing", test_setref_strict_aliasing, METH_NOARGS, _Py_NULL},
|
|
#endif
|
|
{"test_datetime", test_datetime, METH_NOARGS, _Py_NULL},
|
|
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
|
|
#ifdef __cplusplus
|
|
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
|
|
#endif
|
|
#if defined(__cplusplus)
|
|
{"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
|
|
#endif
|
|
{_Py_NULL, _Py_NULL, 0, _Py_NULL}, // sentinel
|
|
};
|
|
|
|
|
|
static int
|
|
module_exec(PyObject *module)
|
|
{
|
|
#ifdef __STDC_VERSION__
|
|
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
|
|
return -1;
|
|
}
|
|
#endif
|
|
#ifdef __STRICT_ANSI__
|
|
if (PyModule_AddIntConstant(module, "__STRICT_ANSI__", 1) < 0) {
|
|
return -1;
|
|
}
|
|
#endif
|
|
#ifdef __cplusplus
|
|
if (PyModule_AddIntMacro(module, __cplusplus) < 0) {
|
|
return -1;
|
|
}
|
|
#endif
|
|
#ifdef _MSVC_LANG
|
|
if (PyModule_AddIntMacro(module, _MSVC_LANG) < 0) {
|
|
return -1;
|
|
}
|
|
#endif
|
|
// Ignore "unused argument" warning when none of these macros is defined
|
|
(void)module;
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Need to ignore "-Wpedantic" warnings; see VirtualPyObject_Slots above
|
|
_Py_COMP_DIAG_PUSH
|
|
#if defined(__GNUC__)
|
|
# pragma GCC diagnostic ignored "-Wpedantic"
|
|
#elif defined(__clang__)
|
|
# pragma clang diagnostic ignored "-Wpedantic"
|
|
#endif
|
|
|
|
static PyModuleDef_Slot module_slots[] = {
|
|
{Py_mod_exec, (void*)module_exec},
|
|
{0, NULL}
|
|
};
|
|
|
|
_Py_COMP_DIAG_POP
|
|
|
|
|
|
#ifdef __cplusplus
|
|
PyDoc_STRVAR(module_doc, "C++ test extension.");
|
|
#else
|
|
PyDoc_STRVAR(module_doc, "C test extension.");
|
|
#endif
|
|
|
|
static struct PyModuleDef _testcext_module = {
|
|
PyModuleDef_HEAD_INIT, // m_base
|
|
STR(MODULE_NAME), // m_name
|
|
module_doc, // m_doc
|
|
0, // m_size
|
|
module_methods, // m_methods
|
|
module_slots, // m_slots
|
|
NULL, // m_traverse
|
|
NULL, // m_clear
|
|
NULL, // m_free
|
|
};
|
|
|
|
|
|
#define _FUNC_NAME(NAME) PyInit_ ## NAME
|
|
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
|
|
|
|
PyMODINIT_FUNC
|
|
FUNC_NAME(MODULE_NAME)(void)
|
|
{
|
|
return PyModuleDef_Init(&_testcext_module);
|
|
}
|