Skip to content

Commit 2b0c57d

Browse files
committed
Tests passing
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
1 parent 99d2622 commit 2b0c57d

8 files changed

Lines changed: 214 additions & 219 deletions

File tree

Include/internal/pycore_object.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ static inline void _Py_SetImmutable(PyObject *op)
100100
}
101101
#define _Py_SetImmutable(op) _Py_SetImmutable(_PyObject_CAST(op))
102102

103+
/* _Py_ClearImmutable() should only be used during object deallocation and runtime finalization. */
104+
static inline void _Py_ClearImmutable(PyObject *op)
105+
{
106+
if (op) {
107+
op->ob_refcnt &= _Py_REFCNT_MASK;
108+
}
109+
}
110+
#define _Py_ClearImmutable(op) _Py_ClearImmutable(_PyObject_CAST(op))
111+
103112
#define Py_CHECKWRITE(op) ((op) && (!_Py_IsImmutable(op)))
104113
#define Py_REQUIREWRITE(op, msg) {if (Py_CHECKWRITE(op)) { _PyObject_ASSERT_FAILED_MSG(op, msg); }}
105114

Lib/test/test_freeze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def test_update(self):
149149

150150
class TestSet(BaseObjectTest):
151151
def __init__(self, *args, **kwargs):
152-
obj = {1: self.C(), "two": self.C()}
153-
BaseObjectTest.__init__(self, *args, obj={1, "two", None, True}, **kwargs)
152+
obj = {1, "two", None, True}
153+
BaseObjectTest.__init__(self, *args, obj=obj, **kwargs)
154154

155155
def test_add(self):
156156
with self.assertRaises(NotWriteableError):

Modules/gcmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
10261026
inquiry clear;
10271027
if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
10281028
Py_INCREF(op);
1029+
_Py_ClearImmutable(op);
10291030
(void) clear(op);
10301031
if (_PyErr_Occurred(tstate)) {
10311032
_PyErr_WriteUnraisableMsg("in tp_clear of",

Objects/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,7 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
26252625
void
26262626
_Py_Dealloc(PyObject *op)
26272627
{
2628+
_Py_ClearImmutable(op);
26282629
PyTypeObject *type = Py_TYPE(op);
26292630
destructor dealloc = type->tp_dealloc;
26302631
#ifdef Py_DEBUG

Objects/typeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5304,6 +5304,7 @@ type_clear(PyTypeObject *type)
53045304
PyType_Modified(type);
53055305
PyObject *dict = lookup_tp_dict(type);
53065306
if (dict) {
5307+
_Py_ClearImmutable(dict);
53075308
PyDict_Clear(dict);
53085309
}
53095310
Py_CLEAR(((PyHeapTypeObject *)type)->ht_module);

Python/bytecodes.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,6 @@ dummy_func(
19691969
if (!Py_CHECKWRITE(owner))
19701970
{
19711971
format_exc_notwriteable(tstate, frame->f_code, oparg);
1972-
Py_DECREF(owner);
19731972
goto error;
19741973
}
19751974
PyDictValues *values = _PyDictOrValues_GetValues(dorv);
@@ -1996,7 +1995,6 @@ dummy_func(
19961995
if (!Py_CHECKWRITE(owner))
19971996
{
19981997
format_exc_notwriteable(tstate, frame->f_code, oparg);
1999-
Py_DECREF(owner);
20001998
goto error;
20011999
}
20022000
assert(PyDict_CheckExact((PyObject *)dict));
@@ -2046,7 +2044,6 @@ dummy_func(
20462044
if (!Py_CHECKWRITE(owner))
20472045
{
20482046
format_exc_notwriteable(tstate, frame->f_code, oparg);
2049-
Py_DECREF(owner);
20502047
goto error;
20512048
}
20522049
char *addr = (char *)owner + index;

0 commit comments

Comments
 (0)