Skip to content

Commit

Permalink
Stackless issue python#134: use Py_TYPE, Py_SIZE and Py_REFCNT
Browse files Browse the repository at this point in the history
Replace the direct access to PyObject members with the preferred macros.
  • Loading branch information
Anselm Kruis committed Oct 29, 2017
1 parent f845a0f commit c7dcca5
Showing 4 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Stackless/core/stackless_impl.h
Original file line number Diff line number Diff line change
@@ -452,15 +452,15 @@ PyTaskletObject * slp_get_watchdog(PyThreadState *ts, int interrupt);

#define STACKLESS_PROMOTE(func) \
(stackless ? slp_try_stackless = \
(func)->ob_type->tp_flags & Py_TPFLAGS_HAVE_STACKLESS_CALL : 0)
Py_TYPE(func)->tp_flags & Py_TPFLAGS_HAVE_STACKLESS_CALL : 0)

#define STACKLESS_PROMOTE_FLAG(flag) \
(stackless ? slp_try_stackless = (flag) : 0)

#define STACKLESS_PROMOTE_METHOD(obj, meth) do { \
if ((obj->ob_type->tp_flags & Py_TPFLAGS_HAVE_STACKLESS_EXTENSION) && \
obj->ob_type->tp_as_mapping) \
slp_try_stackless = stackless & obj->ob_type->tp_as_mapping->slpflags.meth; \
if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_HAVE_STACKLESS_EXTENSION) && \
Py_TYPE(obj)->tp_as_mapping) \
slp_try_stackless = stackless & Py_TYPE(obj)->tp_as_mapping->slpflags.meth; \
} while (0)

#define STACKLESS_PROMOTE_WRAPPER(wp) \
2 changes: 1 addition & 1 deletion Stackless/module/channelobject.c
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ channel_dealloc(PyObject *ob)
}
if (ch->chan_weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)ch);
ob->ob_type->tp_free(ob);
Py_TYPE(ob)->tp_free(ob);
}

/* see if a tasklet is queued on a channel */
14 changes: 7 additions & 7 deletions Stackless/module/stacklessmodule.c
Original file line number Diff line number Diff line change
@@ -1375,8 +1375,8 @@ _peek(PyObject *self, PyObject *v)
/* this is plain heuristics, use for now */
if (CANNOT_READ_MEM(o, sizeof(PyObject))) goto noobject;
if (IS_ON_STACK(o)) goto noobject;
if (o->ob_refcnt < 1 || o->ob_refcnt > 10000) goto noobject;
t = o->ob_type;
if (Py_REFCNT(o) < 1 || Py_REFCNT(o) > 10000) goto noobject;
t = Py_TYPE(o);
for (i=0; i<100; i++) {
if (t == &PyType_Type)
break;
@@ -1423,15 +1423,15 @@ _get_refinfo(PyObject *self)
refchain = PyTuple_New(0)->_ob_next; /* None doesn't work in 2.2 */
Py_DECREF(refchain->_ob_prev);
/* find real refchain */
while (refchain->ob_type != NULL)
while (Py_TYPE(refchain) != NULL)
refchain = refchain->_ob_next;

for (op = refchain->_ob_next; op != refchain; op = op->_ob_next) {
if (op->ob_refcnt > max->ob_refcnt)
if (Py_REFCNT(op) > Py_REFCNT(max))
max = op;
computed_total += op->ob_refcnt;
computed_total += Py_REFCNT(op);
}
return Py_BuildValue("(Onnn)", max, max->ob_refcnt, ref_total,
return Py_BuildValue("(Onnn)", max, Py_REFCNT(max), ref_total,
computed_total);
}

@@ -1445,7 +1445,7 @@ _get_all_objects(PyObject *self)
lis = PyList_New(0);
if (lis) {
ob = lis->_ob_next;
while (ob != lis && ob->ob_type != NULL) {
while (ob != lis && Py_TYPE(ob) != NULL) {
if (PyList_Append(lis, ob))
return NULL;
ob = ob->_ob_next;
48 changes: 24 additions & 24 deletions Stackless/pickling/prickelpit.c
Original file line number Diff line number Diff line change
@@ -56,15 +56,15 @@ generic_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
assert(type->tp_base->tp_new != NULL);
inst = type->tp_base->tp_new(type->tp_base, args, kwds);
if (inst != NULL)
inst->ob_type = type;
Py_TYPE(inst) = type;
return inst;
}

int
generic_init(PyObject *ob, PyObject *args, PyObject *kwds)
{

initproc init = ob->ob_type->tp_base->tp_init;
initproc init = Py_TYPE(ob)->tp_base->tp_init;

if (init)
return init(ob, args, kwds);
@@ -74,8 +74,8 @@ generic_init(PyObject *ob, PyObject *args, PyObject *kwds)
static PyObject *
generic_setstate(PyObject *self, PyObject *args)
{
if (is_wrong_type(self->ob_type)) return NULL;
self->ob_type = self->ob_type->tp_base;
if (is_wrong_type(Py_TYPE(self))) return NULL;
Py_TYPE(self) = Py_TYPE(self)->tp_base;
Py_INCREF(self);
return self;
}
@@ -112,29 +112,29 @@ _new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
static void
_wrap_dealloc(PyObject *ob)
{
ob->ob_type = ob->ob_type->tp_base;
if (ob->ob_type->tp_dealloc != NULL)
ob->ob_type->tp_dealloc(ob);
Py_TYPE(ob) = Py_TYPE(ob)->tp_base;
if (Py_TYPE(ob)->tp_dealloc != NULL)
Py_TYPE(ob)->tp_dealloc(ob);
}

static int
_wrap_traverse(PyObject *ob, visitproc visit, void *arg)
{
PyTypeObject *type = ob->ob_type;
PyTypeObject *type = Py_TYPE(ob);
int ret = 0;
ob->ob_type = ob->ob_type->tp_base;
if (ob->ob_type->tp_traverse != NULL)
ret = ob->ob_type->tp_traverse(ob, visit, arg);
ob->ob_type = type;
Py_TYPE(ob) = type->tp_base;
if (Py_TYPE(ob)->tp_traverse != NULL)
ret = Py_TYPE(ob)->tp_traverse(ob, visit, arg);
Py_TYPE(ob) = type;
return ret;
}

static void
_wrap_clear(PyObject *ob)
{
ob->ob_type = ob->ob_type->tp_base;
if (ob->ob_type->tp_clear != NULL)
ob->ob_type->tp_clear(ob);
Py_TYPE(ob) = Py_TYPE(ob)->tp_base;
if (Py_TYPE(ob)->tp_clear != NULL)
Py_TYPE(ob)->tp_clear(ob);
}


@@ -699,7 +699,7 @@ cell_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
ob = PyCell_New(NULL);
if (ob != NULL)
ob->ob_type = type;
Py_TYPE(ob) = type;
return ob;
}

@@ -711,14 +711,14 @@ cell_setstate(PyObject *self, PyObject *args)
PyCellObject *cell = (PyCellObject *) self;
PyObject *ob = NULL;

if (is_wrong_type(self->ob_type)) return NULL;
if (is_wrong_type(Py_TYPE(self))) return NULL;
if (!PyArg_ParseTuple (args, "|O", &ob))
return NULL;
Py_XINCREF(ob);
Py_CLEAR(cell->ob_ref);
cell->ob_ref = ob;
Py_INCREF(self);
self->ob_type = self->ob_type->tp_base;
Py_TYPE(self) = Py_TYPE(self)->tp_base;
return self;
}

@@ -770,7 +770,7 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kewd)
if ((co = Py_CompileString("", "", Py_file_input)) != NULL)
if ((globals = PyDict_New()) != NULL)
if ((ob = PyFunction_New(co, globals)) != NULL)
ob->ob_type = type;
Py_TYPE(ob) = type;
Py_XDECREF(co);
Py_XDECREF(globals);
return ob;
@@ -785,13 +785,13 @@ func_setstate(PyObject *self, PyObject *args)
PyFunctionObject *fu;
PyObject *args2;

if (is_wrong_type(self->ob_type)) return NULL;
self->ob_type = self->ob_type->tp_base;
if (is_wrong_type(Py_TYPE(self))) return NULL;
Py_TYPE(self) = Py_TYPE(self)->tp_base;
args2 = PyTuple_GetSlice(args, 0, 5);
if (args2 == NULL)
return NULL;
fu = (PyFunctionObject *)
self->ob_type->tp_new(self->ob_type, args2, NULL);
Py_TYPE(self)->tp_new(Py_TYPE(self), args2, NULL);
Py_DECREF(args2);
if (fu != NULL) {
PyFunctionObject *target = (PyFunctionObject *) self;
@@ -1650,7 +1650,7 @@ methw_setstate(PyObject *self, PyObject *args)
PyObject *name, *inst;
PyObject *w;

if (is_wrong_type(self->ob_type)) return NULL;
if (is_wrong_type(Py_TYPE(self))) return NULL;
if (!PyArg_ParseTuple(args, "O!O:method-wrapper",
&PyUnicode_Type, &name,
&inst))
@@ -1675,7 +1675,7 @@ methw_setstate(PyObject *self, PyObject *args)
neww->self = oldw->self;
}
Py_DECREF(w);
self->ob_type = self->ob_type->tp_base;
Py_TYPE(self) = Py_TYPE(self)->tp_base;
Py_INCREF(self);
return self;
}

0 comments on commit c7dcca5

Please sign in to comment.