Commit fbff808d authored by Kevin Modzelewski's avatar Kevin Modzelewski

More refcounting fixes

parent 8bf40b6d
......@@ -1470,6 +1470,11 @@ extern "C" PyObject* PyThreadState_GetDict(void) noexcept {
return dict;
}
extern "C" void PyThreadState_Clear(PyThreadState* tstate) noexcept {
assert(tstate == NULL);
Py_CLEAR(cur_thread_state.dict);
}
extern "C" int _PyOS_URandom(void* buffer, Py_ssize_t size) noexcept {
if (size < 0) {
PyErr_Format(PyExc_ValueError, "negative argument not allowed");
......
......@@ -87,6 +87,7 @@ extern "C" Box* listRepr(BoxedList* self) {
assert(r->cls == str_cls);
BoxedString* s = static_cast<BoxedString*>(r);
chars.insert(chars.end(), s->s().begin(), s->s().end());
Py_DECREF(s);
}
chars.push_back(']');
} catch (ExcInfo e) {
......@@ -542,13 +543,21 @@ static inline void listSetitemSliceInt64(BoxedList* self, i64 start, i64 stop, i
int remaining_elts = self->size - stop;
self->ensure(delts);
// XXX should make a copy here in case a destructor gets run
for (int i = start; i < step; i++) {
Py_CLEAR(self->elts->elts[i]);
}
memmove(self->elts->elts + start + v_size, self->elts->elts + stop, remaining_elts * sizeof(Box*));
for (int i = 0; i < v_size; i++) {
Box* r = v_elts[i];
Py_INCREF(r);
self->elts->elts[start + i] = r;
}
self->size += delts;
Py_XDECREF(v_as_seq);
}
extern "C" Box* listSetitemSlice(BoxedList* self, BoxedSlice* slice, Box* v) {
......@@ -1246,12 +1255,15 @@ extern "C" int PyList_SetSlice(PyObject* a, Py_ssize_t ilow, Py_ssize_t ihigh, P
BoxedList* l = (BoxedList*)a;
ASSERT(PyList_Check(l), "%s", l->cls->tp_name);
// TODO should just call list_ass_slice
try {
BoxedSlice* slice = (BoxedSlice*)createSlice(boxInt(ilow), boxInt(ihigh), None);
BoxedSlice* slice = (BoxedSlice*)createSlice(autoDecref(boxInt(ilow)), autoDecref(boxInt(ihigh)), None);
if (v)
listSetitemSlice(l, slice, v);
autoDecref(listSetitemSlice(l, slice, v));
else
listDelitemSlice(l, slice);
autoDecref(listDelitemSlice(l, slice));
Py_DECREF(slice);
return 0;
} catch (ExcInfo e) {
setCAPIException(e);
......
......@@ -262,7 +262,7 @@ BoxedString* Box::reprICAsString() {
Box* r = this->reprIC();
if (isSubclass(r->cls, unicode_cls)) {
r = PyUnicode_AsASCIIString(r);
r = PyUnicode_AsASCIIString(autoDecref(r));
checkAndThrowCAPIException();
}
if (r->cls != str_cls) {
......@@ -4334,6 +4334,7 @@ extern "C" void Py_Finalize() noexcept {
PyOS_FiniInterrupts();
PyType_ClearCache();
_PyUnicode_Fini();
PyThreadState_Clear(NULL);
for (auto b : constants) {
Py_DECREF(b);
}
......
......@@ -983,6 +983,9 @@ class BoxedSlice : public Box {
public:
Box* start, *stop, *step;
BoxedSlice(Box* lower, Box* upper, Box* step) : start(lower), stop(upper), step(step) {
Py_INCREF(lower);
Py_INCREF(upper);
Py_INCREF(step);
ASSERT(lower->cls == none_cls || lower->cls == int_cls, "slice objects are not gc-aware (like in CPython)");
ASSERT(upper->cls == none_cls || upper->cls == int_cls, "slice objects are not gc-aware (like in CPython)");
ASSERT(step->cls == none_cls || step->cls == int_cls, "slice objects are not gc-aware (like in CPython)");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment