Files
typthon/Modules/_testcapi/tuple.c
copilot-swe-agent[bot] 42e2356c88 Fix incorrect replacements in Py_ to Ty_ renaming
Fixed several macros and constants that should not have been renamed:
- _Py_CAST, _Py_NULL, _Py_RVALUE (internal utility macros)
- Py_UNUSED (unused parameter macro)
- Py_EQ, Py_NE, Py_LT, Py_LE, Py_GT, Py_GE (comparison constants)
- Py_RETURN_* macros (NONE, TRUE, FALSE, NOTIMPLEMENTED, RICHCOMPARE)
- Py_READONLY, Py_ULL, Py_CONTEXT_SWITCHED
- TyGC_Head in generated clinic files

Build is still in progress with some remaining issues to resolve.

Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
2025-12-29 17:44:05 +00:00

125 lines
2.8 KiB
C

#include "parts.h"
#include "util.h"
static TyObject *
tuple_get_size(TyObject *Py_UNUSED(module), TyObject *obj)
{
NULLABLE(obj);
RETURN_SIZE(TyTuple_GET_SIZE(obj));
}
static TyObject *
tuple_get_item(TyObject *Py_UNUSED(module), TyObject *args)
{
TyObject *obj;
Ty_ssize_t i;
if (!TyArg_ParseTuple(args, "On", &obj, &i)) {
return NULL;
}
NULLABLE(obj);
return Ty_XNewRef(TyTuple_GET_ITEM(obj, i));
}
static TyObject *
tuple_copy(TyObject *tuple)
{
Ty_ssize_t size = TyTuple_GET_SIZE(tuple);
TyObject *newtuple = TyTuple_New(size);
if (!newtuple) {
return NULL;
}
for (Ty_ssize_t n = 0; n < size; n++) {
TyTuple_SET_ITEM(newtuple, n, Ty_XNewRef(TyTuple_GET_ITEM(tuple, n)));
}
return newtuple;
}
static TyObject *
tuple_set_item(TyObject *Py_UNUSED(module), TyObject *args)
{
TyObject *obj, *value, *newtuple;
Ty_ssize_t i;
if (!TyArg_ParseTuple(args, "OnO", &obj, &i, &value)) {
return NULL;
}
NULLABLE(value);
if (TyTuple_CheckExact(obj)) {
newtuple = tuple_copy(obj);
if (!newtuple) {
return NULL;
}
TyObject *val = TyTuple_GET_ITEM(newtuple, i);
TyTuple_SET_ITEM(newtuple, i, Ty_XNewRef(value));
Ty_DECREF(val);
return newtuple;
}
else {
NULLABLE(obj);
TyObject *val = TyTuple_GET_ITEM(obj, i);
TyTuple_SET_ITEM(obj, i, Ty_XNewRef(value));
Ty_DECREF(val);
return Ty_XNewRef(obj);
}
}
static TyObject *
_tuple_resize(TyObject *Py_UNUSED(module), TyObject *args)
{
TyObject *tup;
Ty_ssize_t newsize;
int new = 1;
if (!TyArg_ParseTuple(args, "On|p", &tup, &newsize, &new)) {
return NULL;
}
if (new) {
tup = tuple_copy(tup);
if (!tup) {
return NULL;
}
}
else {
NULLABLE(tup);
Ty_XINCREF(tup);
}
int r = _TyTuple_Resize(&tup, newsize);
if (r == -1) {
assert(tup == NULL);
return NULL;
}
return tup;
}
static TyObject *
_check_tuple_item_is_NULL(TyObject *Py_UNUSED(module), TyObject *args)
{
TyObject *obj;
Ty_ssize_t i;
if (!TyArg_ParseTuple(args, "On", &obj, &i)) {
return NULL;
}
return TyLong_FromLong(TyTuple_GET_ITEM(obj, i) == NULL);
}
static TyMethodDef test_methods[] = {
{"tuple_get_size", tuple_get_size, METH_O},
{"tuple_get_item", tuple_get_item, METH_VARARGS},
{"tuple_set_item", tuple_set_item, METH_VARARGS},
{"_tuple_resize", _tuple_resize, METH_VARARGS},
{"_check_tuple_item_is_NULL", _check_tuple_item_is_NULL, METH_VARARGS},
{NULL},
};
int
_PyTestCapi_Init_Tuple(TyObject *m)
{
if (TyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}