diff --git a/docs/changelog.rst b/docs/changelog.rst index ec263a3..3500bd5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,32 @@ Changelog ========= +* 2024-12-16: Add ``structmember.h`` constants: + + * ``Py_T_BOOL`` + * ``Py_T_BYTE`` + * ``Py_T_CHAR`` + * ``Py_T_DOUBLE`` + * ``Py_T_FLOAT`` + * ``Py_T_INT`` + * ``Py_T_LONGLONG`` + * ``Py_T_LONG`` + * ``Py_T_OBJECT_EX`` + * ``Py_T_PYSSIZET`` + * ``Py_T_SHORT`` + * ``Py_T_STRING_INPLACE`` + * ``Py_T_STRING`` + * ``Py_T_UBYTE`` + * ``Py_T_UINT`` + * ``Py_T_ULONGLONG`` + * ``Py_T_ULONG`` + * ``Py_T_USHORT`` + * ``_Py_T_NONE`` + * ``_Py_T_OBJECT`` + * ``Py_AUDIT_READ`` + * ``Py_READONLY`` + * ``_Py_WRITE_RESTRICTED`` + * 2024-12-13: Add functions and structs: * ``PyLongLayout`` diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index 5e22e7d..cee282d 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -24,6 +24,9 @@ extern "C" { #if PY_VERSION_HEX < 0x030b00B4 && !defined(PYPY_VERSION) # include "frameobject.h" // PyFrameObject, PyFrame_GetBack() #endif +#if PY_VERSION_HEX < 0x030C00A3 +# include // T_SHORT, READONLY +#endif #ifndef _Py_CAST @@ -1899,6 +1902,37 @@ PyLongWriter_Finish(PyLongWriter *writer) #endif +#if PY_VERSION_HEX < 0x030C00A3 +# define Py_T_SHORT T_SHORT +# define Py_T_INT T_INT +# define Py_T_LONG T_LONG +# define Py_T_FLOAT T_FLOAT +# define Py_T_DOUBLE T_DOUBLE +# define Py_T_STRING T_STRING +# define _Py_T_OBJECT T_OBJECT +# define Py_T_CHAR T_CHAR +# define Py_T_BYTE T_BYTE +# define Py_T_UBYTE T_UBYTE +# define Py_T_USHORT T_USHORT +# define Py_T_UINT T_UINT +# define Py_T_ULONG T_ULONG +# define Py_T_STRING_INPLACE T_STRING_INPLACE +# define Py_T_BOOL T_BOOL +# define Py_T_OBJECT_EX T_OBJECT_EX +# define Py_T_LONGLONG T_LONGLONG +# define Py_T_ULONGLONG T_ULONGLONG +# define Py_T_PYSSIZET T_PYSSIZET + +# if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION) +# define _Py_T_NONE T_NONE +# endif + +# define Py_READONLY READONLY +# define Py_AUDIT_READ READ_RESTRICTED +# define _Py_WRITE_RESTRICTED PY_WRITE_RESTRICTED +#endif + + #ifdef __cplusplus } #endif diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 28663d2..aff19e0 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -2063,6 +2063,40 @@ test_long_stdint(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) } +static PyObject * +test_structmember(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) +{ + assert(Py_T_SHORT >= 0); + assert(Py_T_INT >= 0); + assert(Py_T_LONG >= 0); + assert(Py_T_FLOAT >= 0); + assert(Py_T_DOUBLE >= 0); + assert(Py_T_STRING >= 0); + assert(_Py_T_OBJECT >= 0); + assert(Py_T_CHAR >= 0); + assert(Py_T_BYTE >= 0); + assert(Py_T_UBYTE >= 0); + assert(Py_T_USHORT >= 0); + assert(Py_T_UINT >= 0); + assert(Py_T_ULONG >= 0); + assert(Py_T_STRING_INPLACE >= 0); + assert(Py_T_BOOL >= 0); + assert(Py_T_OBJECT_EX >= 0); + assert(Py_T_LONGLONG >= 0); + assert(Py_T_ULONGLONG >= 0); + assert(Py_T_PYSSIZET >= 0); +#if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION) + assert(_Py_T_NONE >= 0); +#endif + + assert(Py_READONLY >= 0); + assert(Py_AUDIT_READ >= 0); + assert(_Py_WRITE_RESTRICTED >= 0); + + Py_RETURN_NONE; +} + + static struct PyMethodDef methods[] = { {"test_object", test_object, METH_NOARGS, _Py_NULL}, {"test_py_is", test_py_is, METH_NOARGS, _Py_NULL}, @@ -2109,6 +2143,7 @@ static struct PyMethodDef methods[] = { {"test_bytes", test_bytes, METH_NOARGS, _Py_NULL}, {"test_iter", test_iter, METH_NOARGS, _Py_NULL}, {"test_long_stdint", test_long_stdint, METH_NOARGS, _Py_NULL}, + {"test_structmember", test_structmember, METH_NOARGS, _Py_NULL}, {_Py_NULL, _Py_NULL, 0, _Py_NULL} };