Skip to content

Commit

Permalink
pythongh-111178: Fix function signatures in codeobject.c
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Oct 9, 2024
1 parent c203955 commit cba8ce6
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "clinic/codeobject.c.h"

#define _PyCode_CAST(op) \
(assert(PyCode_Check(op)), _Py_CAST(PyCodeObject*, (op)))

static const char *
code_event_name(PyCodeEvent event) {
switch (event) {
Expand Down Expand Up @@ -1278,8 +1281,9 @@ typedef struct {


static void
lineiter_dealloc(lineiterator *li)
lineiter_dealloc(PyObject *self)
{
lineiterator *li = (lineiterator*)self;
Py_DECREF(li->li_code);
Py_TYPE(li)->tp_free(li);
}
Expand All @@ -1293,8 +1297,9 @@ _source_offset_converter(int *value) {
}

static PyObject *
lineiter_next(lineiterator *li)
lineiter_next(PyObject *self)
{
lineiterator *li = (lineiterator*)self;
PyCodeAddressRange *bounds = &li->li_line;
if (!_PyLineTable_NextAddressRange(bounds)) {
return NULL;
Expand All @@ -1318,7 +1323,7 @@ PyTypeObject _PyLineIterator = {
sizeof(lineiterator), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)lineiter_dealloc, /* tp_dealloc */
lineiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand All @@ -1340,7 +1345,7 @@ PyTypeObject _PyLineIterator = {
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)lineiter_next, /* tp_iternext */
lineiter_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
Expand Down Expand Up @@ -1379,15 +1384,17 @@ typedef struct {
} positionsiterator;

static void
positionsiter_dealloc(positionsiterator* pi)
positionsiter_dealloc(PyObject *self)
{
positionsiterator *pi = (positionsiterator*)self;
Py_DECREF(pi->pi_code);
Py_TYPE(pi)->tp_free(pi);
}

static PyObject*
positionsiter_next(positionsiterator* pi)
positionsiter_next(PyObject *self)
{
positionsiterator *pi = (positionsiterator*)self;
if (pi->pi_offset >= pi->pi_range.ar_end) {
assert(pi->pi_offset == pi->pi_range.ar_end);
if (at_end(&pi->pi_range)) {
Expand All @@ -1409,7 +1416,7 @@ PyTypeObject _PyPositionsIterator = {
sizeof(positionsiterator), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)positionsiter_dealloc, /* tp_dealloc */
positionsiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand All @@ -1431,7 +1438,7 @@ PyTypeObject _PyPositionsIterator = {
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)positionsiter_next, /* tp_iternext */
positionsiter_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
Expand All @@ -1447,8 +1454,9 @@ PyTypeObject _PyPositionsIterator = {
};

static PyObject*
code_positionsiterator(PyCodeObject* code, PyObject* Py_UNUSED(args))
code_positionsiterator(PyObject *self, PyObject* Py_UNUSED(args))
{
PyCodeObject *code = _PyCode_CAST(self);
positionsiterator* pi = (positionsiterator*)PyType_GenericAlloc(&_PyPositionsIterator, 0);
if (pi == NULL) {
return NULL;
Expand Down Expand Up @@ -1875,16 +1883,18 @@ code_dealloc(PyCodeObject *co)

#ifdef Py_GIL_DISABLED
static int
code_traverse(PyCodeObject *co, visitproc visit, void *arg)
code_traverse(PyObject *self, visitproc visit, void *arg)
{
PyCodeObject *co = _PyCode_CAST(self);
Py_VISIT(co->co_consts);
return 0;
}
#endif

static PyObject *
code_repr(PyCodeObject *co)
code_repr(PyObject *self)
{
PyCodeObject *co = _PyCode_CAST(self);
int lineno;
if (co->co_firstlineno != 0)
lineno = co->co_firstlineno;
Expand Down Expand Up @@ -1991,8 +2001,9 @@ code_richcompare(PyObject *self, PyObject *other, int op)
}

static Py_hash_t
code_hash(PyCodeObject *co)
code_hash(PyObject *self)
{
PyCodeObject *co = _PyCode_CAST(self);
Py_uhash_t uhash = 20221211;
#define SCRAMBLE_IN(H) do { \
uhash ^= (Py_uhash_t)(H); \
Expand Down Expand Up @@ -2053,8 +2064,9 @@ static PyMemberDef code_memberlist[] = {


static PyObject *
code_getlnotab(PyCodeObject *code, void *closure)
code_getlnotab(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"co_lnotab is deprecated, use co_lines instead.",
1) < 0) {
Expand All @@ -2064,51 +2076,57 @@ code_getlnotab(PyCodeObject *code, void *closure)
}

static PyObject *
code_getvarnames(PyCodeObject *code, void *closure)
code_getvarnames(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
return _PyCode_GetVarnames(code);
}

static PyObject *
code_getcellvars(PyCodeObject *code, void *closure)
code_getcellvars(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
return _PyCode_GetCellvars(code);
}

static PyObject *
code_getfreevars(PyCodeObject *code, void *closure)
code_getfreevars(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
return _PyCode_GetFreevars(code);
}

static PyObject *
code_getcodeadaptive(PyCodeObject *code, void *closure)
code_getcodeadaptive(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
return PyBytes_FromStringAndSize(code->co_code_adaptive,
_PyCode_NBYTES(code));
}

static PyObject *
code_getcode(PyCodeObject *code, void *closure)
code_getcode(PyObject *self, void *closure)
{
PyCodeObject *code = _PyCode_CAST(self);
return _PyCode_GetCode(code);
}

static PyGetSetDef code_getsetlist[] = {
{"co_lnotab", (getter)code_getlnotab, NULL, NULL},
{"_co_code_adaptive", (getter)code_getcodeadaptive, NULL, NULL},
{"co_lnotab", code_getlnotab, NULL, NULL},
{"_co_code_adaptive", code_getcodeadaptive, NULL, NULL},
// The following old names are kept for backward compatibility.
{"co_varnames", (getter)code_getvarnames, NULL, NULL},
{"co_cellvars", (getter)code_getcellvars, NULL, NULL},
{"co_freevars", (getter)code_getfreevars, NULL, NULL},
{"co_code", (getter)code_getcode, NULL, NULL},
{"co_varnames", code_getvarnames, NULL, NULL},
{"co_cellvars", code_getcellvars, NULL, NULL},
{"co_freevars", code_getfreevars, NULL, NULL},
{"co_code", code_getcode, NULL, NULL},
{0}
};


static PyObject *
code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
code_sizeof(PyObject *self, PyObject *Py_UNUSED(args))
{
PyCodeObject *co = _PyCode_CAST(self);
size_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
if (co_extra != NULL) {
Expand All @@ -2119,8 +2137,9 @@ code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
}

static PyObject *
code_linesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args))
code_linesiterator(PyObject *self, PyObject *Py_UNUSED(args))
{
PyCodeObject *code = _PyCode_CAST(self);
return (PyObject *)new_linesiterator(code);
}

Expand Down Expand Up @@ -2262,9 +2281,9 @@ code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
/* XXX code objects need to participate in GC? */

static struct PyMethodDef code_methods[] = {
{"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
{"co_lines", (PyCFunction)code_linesiterator, METH_NOARGS},
{"co_positions", (PyCFunction)code_positionsiterator, METH_NOARGS},
{"__sizeof__", code_sizeof, METH_NOARGS},
{"co_lines", code_linesiterator, METH_NOARGS},
{"co_positions", code_positionsiterator, METH_NOARGS},
CODE_REPLACE_METHODDEF
CODE__VARNAME_FROM_OPARG_METHODDEF
{"__replace__", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS,
Expand All @@ -2283,11 +2302,11 @@ PyTypeObject PyCode_Type = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)code_repr, /* tp_repr */
code_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)code_hash, /* tp_hash */
code_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
Expand All @@ -2300,7 +2319,7 @@ PyTypeObject PyCode_Type = {
#endif
code_new__doc__, /* tp_doc */
#ifdef Py_GIL_DISABLED
(traverseproc)code_traverse, /* tp_traverse */
code_traverse, /* tp_traverse */
#else
0, /* tp_traverse */
#endif
Expand Down

0 comments on commit cba8ce6

Please sign in to comment.