gh-95991: Add some infrastructure for testing Limited API in _testcapi (GH-95992)

- Limited API needs to be enabled per source file
- Some builds don't support Limited API, so Limited API tests must be skipped on those builds
  (currently this is `Py_TRACE_REFS`, but that may change.)
- `Py_LIMITED_API` must be defined before `<Python.h>` is included.

This puts the hoop-jumping in `testcapi/parts.h`, so individual
test files can be relatively simple. (Currently that's only
`vectorcall_limited.c`, imagine more.)
This commit is contained in:
Petr Viktorin 2022-08-17 13:48:43 +02:00 committed by GitHub
parent 7276ca25f5
commit 0f2b469ce1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 24 deletions

View file

@ -1,9 +1,36 @@
#include "Python.h"
#ifndef Py_TESTCAPI_PARTS_H
#define Py_TESTCAPI_PARTS_H
/* Always enable assertions */
#include "pyconfig.h" // for Py_TRACE_REFS
// Figure out if Limited API is available for this build. If it isn't we won't
// build tests for it.
// Currently, only Py_TRACE_REFS disables Limited API.
#ifdef Py_TRACE_REFS
#undef LIMITED_API_AVAILABLE
#else
#define LIMITED_API_AVAILABLE 1
#endif
// Always enable assertions
#undef NDEBUG
#if !defined(LIMITED_API_AVAILABLE) && defined(Py_LIMITED_API)
// Limited API being unavailable means that with Py_LIMITED_API defined
// we can't even include Python.h.
// Do nothing; the .c file that defined Py_LIMITED_API should also do nothing.
#else
#include "Python.h"
int _PyTestCapi_Init_Vectorcall(PyObject *module);
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
int _PyTestCapi_Init_Heaptype(PyObject *module);
int _PyTestCapi_Init_Unicode(PyObject *module);
#ifdef LIMITED_API_AVAILABLE
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
#endif // LIMITED_API_AVAILABLE
#endif
#endif // Py_TESTCAPI_PARTS_H

View file

@ -1,18 +1,8 @@
#include "pyconfig.h" // Py_TRACE_REFS
#ifdef Py_TRACE_REFS
// Py_TRACE_REFS is incompatible with Limited API
#include "parts.h"
int
_PyTestCapi_Init_VectorcallLimited(PyObject *m) {
return 0;
}
#else
#define Py_LIMITED_API 0x030c0000 // 3.12
#include "parts.h"
#ifdef LIMITED_API_AVAILABLE
#include "structmember.h" // PyMemberDef
/* Test Vectorcall in the limited API */
@ -89,4 +79,4 @@ _PyTestCapi_Init_VectorcallLimited(PyObject *m) {
return 0;
}
#endif // Py_TRACE_REFS
#endif // LIMITED_API_AVAILABLE

View file

@ -6544,9 +6544,6 @@ PyInit__testcapi(void)
if (_PyTestCapi_Init_Vectorcall(m) < 0) {
return NULL;
}
if (_PyTestCapi_Init_VectorcallLimited(m) < 0) {
return NULL;
}
if (_PyTestCapi_Init_Heaptype(m) < 0) {
return NULL;
}
@ -6554,6 +6551,15 @@ PyInit__testcapi(void)
return NULL;
}
#ifndef LIMITED_API_AVAILABLE
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);
#else
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_True);
if (_PyTestCapi_Init_VectorcallLimited(m) < 0) {
return NULL;
}
#endif
PyState_AddModule(m, &_testcapimodule);
return m;
}