Skip to content

Commit

Permalink
Initial Python 3.11 support
Browse files Browse the repository at this point in the history
Just to fic compilation. It doesn't really work yet.

For #41
  • Loading branch information
zhuyifei1999 committed Oct 3, 2022
1 parent 4cb9fcb commit 7ecdd2a
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ There will be several more tests. Some tests may take a while.
## License

Copyright (C) 2005-2013 Sverker Nilsson, S. Nilsson Computer System AB
Copyright (C) 2019-2021 YiFei Zhu
Copyright (C) 2019-2021 YiFei Zhu
Copyright (C) 2021-2022 YiFei Zhu, Google LLC

The right is granted to copy, use, modify and redistribute this code
Expand Down
4 changes: 2 additions & 2 deletions src/heapy/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ static void
cli_dealloc(NyObjectClassifierObject *op)
{
PyObject_GC_UnTrack(op);
Py_TRASHCAN_SAFE_BEGIN(op)
Py_TRASHCAN_BEGIN(op, cli_dealloc)
Py_XDECREF(op->self);
PyObject_GC_Del(op);
Py_TRASHCAN_SAFE_END(op)
Py_TRASHCAN_END
}

static int
Expand Down
4 changes: 2 additions & 2 deletions src/heapy/hv.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,10 @@ static void
hv_dealloc(PyObject *v)
{
PyObject_GC_UnTrack(v);
Py_TRASHCAN_SAFE_BEGIN(v)
Py_TRASHCAN_BEGIN(v, hv_dealloc)
hv_gc_clear((NyHeapViewObject *)v);
Py_TYPE(v)->tp_free(v);
Py_TRASHCAN_SAFE_END(v)
Py_TRASHCAN_END
}

PyDoc_STRVAR(hv_delete_extra_type_doc,
Expand Down
4 changes: 2 additions & 2 deletions src/heapy/hv_cli_rel.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ static void
rel_dealloc(NyRelationObject *op)
{
PyObject_GC_UnTrack(op);
Py_TRASHCAN_SAFE_BEGIN(op)
Py_TRASHCAN_BEGIN(op, rel_dealloc)
Py_XDECREF(op->relator);
Py_TYPE(op)->tp_free(op);
Py_TRASHCAN_SAFE_END(op)
Py_TRASHCAN_END
}

PyObject *
Expand Down
5 changes: 2 additions & 3 deletions src/heapy/nodegraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,16 @@ ng_dealloc(PyObject *v)
{
NyNodeGraphObject *ng = (void *)v;
Py_ssize_t i;
Py_TRASHCAN_SAFE_BEGIN(v)
PyObject_GC_UnTrack(v);
Py_TRASHCAN_BEGIN(v, ng_dealloc)
ng_gc_clear(ng);
for (i = 0; i < ng->used_size; i++) {
Py_DECREF(ng->edges[i].src);
Py_DECREF(ng->edges[i].tgt);
}
PyMem_FREE(ng->edges);
Py_TYPE(ng)->tp_free(v);
Py_TRASHCAN_SAFE_END(v)

Py_TRASHCAN_END
}

static int
Expand Down
74 changes: 63 additions & 11 deletions src/heapy/rootstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ static struct PyMemberDef is_members[] = {
#define RENAMEMEMBER(name, newname) {#newname, T_OBJECT, offsetof(PyThreadState, name)}

static struct PyMemberDef ts_members[] = {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION < 11
MEMBER(frame),
#endif

MEMBER(c_profileobj),
MEMBER(c_traceobj),

MEMBER(curexc_type),
MEMBER(curexc_value),
MEMBER(curexc_traceback),

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
RENAMEMEMBER(exc_state.exc_value, exc_value),
#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
RENAMEMEMBER(exc_state.exc_type, exc_type),
RENAMEMEMBER(exc_state.exc_value, exc_value),
RENAMEMEMBER(exc_state.exc_traceback, exc_traceback),
Expand Down Expand Up @@ -303,22 +308,31 @@ rootstate_relate(NyHeapRelate *r)
ISATTR(audit_hooks);
#endif

for (ts = is->tstate_head; ts; ts = ts->next) {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ts = is->threads.head;
#else
ts = is->tstate_head;
#endif
for (; ts; ts = ts->next) {
if ((ts == bts && r->tgt == hv->limitframe) ||
(!hv->limitframe && isframe)) {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
continue; // TODO
#else
int frameno = -1;
int numframes = 0;
PyFrameObject *frame;
for (frame = (PyFrameObject *)ts->frame; frame; frame = frame->f_back) {
numframes++;
if (r->tgt == (PyObject *)frame)
if ((PyObject *)frame == r->tgt)
frameno = numframes;
}
if (frameno != -1) {
frameno = numframes - frameno;
if (r->visit(NYHR_ATTRIBUTE, PyUnicode_FromFormat("i%d_t%lu_f%d", isno, THREAD_ID(ts), frameno), r))
return 1;
}
#endif
}
TSATTR(c_profileobj);
TSATTR(c_traceobj);
Expand All @@ -327,7 +341,9 @@ rootstate_relate(NyHeapRelate *r)
TSATTR(curexc_value);
TSATTR(curexc_traceback);

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
RENAMETSATTR(exc_state.exc_value, exc_value);
#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
RENAMETSATTR(exc_state.exc_type, exc_type);
RENAMETSATTR(exc_state.exc_value, exc_value);
RENAMETSATTR(exc_state.exc_traceback, exc_traceback);
Expand Down Expand Up @@ -406,20 +422,31 @@ rootstate_traverse(NyHeapTraverse *ta)
Py_VISIT(is->audit_hooks);
#endif

for (ts = is->tstate_head; ts; ts = ts->next) {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ts = is->threads.head;
#else
ts = is->tstate_head;
#endif
for (; ts; ts = ts->next) {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
// TODO
#else
if (ts == bts && hv->limitframe) {
Py_VISIT(hv->limitframe);
} else if (!hv->limitframe) {
Py_VISIT(ts->frame);
}
#endif
Py_VISIT(ts->c_profileobj);
Py_VISIT(ts->c_traceobj);

Py_VISIT(ts->curexc_type);
Py_VISIT(ts->curexc_value);
Py_VISIT(ts->curexc_traceback);

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
Py_VISIT(ts->exc_state.exc_value);
#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
Py_VISIT(ts->exc_state.exc_type);
Py_VISIT(ts->exc_state.exc_value);
Py_VISIT(ts->exc_state.exc_traceback);
Expand Down Expand Up @@ -468,6 +495,7 @@ rootstate_getattr(PyObject *obj, PyObject *name)
{
const char *s = PyUnicode_AsUTF8(name);
PyInterpreterState *is;
PyThreadState *ts;
int n = 0;
int ino;
unsigned long tno;
Expand All @@ -492,11 +520,19 @@ rootstate_getattr(PyObject *obj, PyObject *name)
if (isno == ino) {
if (sscanf(s, "t%lu_%n", &tno, &n) == 1) {
s += n;
PyThreadState *ts;
for (ts = is->tstate_head; ts; ts = ts->next) {

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ts = is->threads.head;
#else
ts = is->tstate_head;
#endif
for (; ts; ts = ts->next) {
if (THREAD_ID(ts) == tno) {
int frameno = 0;
if (sscanf(s, "f%d%n", &frameno, &n) == 1 && s[n] == '\0') {
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
// TODO
#else
PyFrameObject *frame;
int numframes = 0;
for (frame = ts->frame; frame; frame = frame->f_back) {
Expand All @@ -510,6 +546,7 @@ rootstate_getattr(PyObject *obj, PyObject *name)
return (PyObject *)frame;
}
}
#endif
PyErr_Format(PyExc_AttributeError,
"thread state has no frame numbered %d from bottom",
frameno);
Expand Down Expand Up @@ -559,8 +596,13 @@ rootstate_getattr(PyObject *obj, PyObject *name)
continue;
#endif
int isno = numis - countis - 1;
PyThreadState *ts;
for (ts = is->tstate_head; ts; ts = ts->next) {

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ts = is->threads.head;
#else
ts = is->tstate_head;
#endif
for (; ts; ts = ts->next) {
if (THREAD_ID(ts) == tno) {
PyObject *fullname = PyUnicode_FromFormat("i%d_%U", isno, name);
if (!fullname) {
Expand Down Expand Up @@ -651,8 +693,17 @@ rootstate_dir(PyObject *self, PyObject *args)
ISATTR_DIR(audit_hooks);
#endif

for (ts = is->tstate_head; ts; ts = ts->next) {

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ts = is->threads.head;
#else
ts = is->tstate_head;
#endif
for (; ts; ts = ts->next) {
int numframes = 0;
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
// TODO
#else
PyFrameObject *frame;
for (frame = (PyFrameObject *)ts->frame; frame; frame = frame->f_back) {
numframes++;
Expand All @@ -668,6 +719,7 @@ rootstate_dir(PyObject *self, PyObject *args)
Py_DECREF(attr);
}
}
#endif
TSATTR_DIR(c_profileobj);
TSATTR_DIR(c_traceobj);

Expand Down
Loading

0 comments on commit 7ecdd2a

Please sign in to comment.