Skip to content

Commit 30ff20e

Browse files
Remove guards from memo related code.
1 parent 73de0ba commit 30ff20e

File tree

2 files changed

+21
-48
lines changed

2 files changed

+21
-48
lines changed

Lib/test/test_pickle.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,6 @@ def __reduce__(slf):
429429
pickler.dump(X()) # should not crash
430430
self.assertEqual(pickle.loads(f.getvalue()), [])
431431

432-
def test_concurrent_pickler_dump_and_clear_memo(self):
433-
for clear_memo in [lambda: pickler.clear_memo(), lambda: pickler.memo.clear()]:
434-
f = io.BytesIO()
435-
pickler = self.pickler_class(f)
436-
class X:
437-
def __reduce__(slf):
438-
self.assertRaises(RuntimeError, clear_memo)
439-
return list, (('inner',),), None, iter((slf,))
440-
pickler.dump(['outer', X()])
441-
unpickled = pickle.loads(f.getvalue())
442-
self.assertIs(unpickled[1][1], unpickled[1])
443-
444432
def test_concurrent_pickler_dump_and_init(self):
445433
f = io.BytesIO()
446434
pickler = self.pickler_class(f)

Modules/_pickle.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,11 +4763,9 @@ static PyObject *
47634763
_pickle_Pickler_clear_memo_impl(PicklerObject *self)
47644764
/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
47654765
{
4766-
BEGIN_USING_PICKLER(self, NULL);
47674766
if (self->memo)
47684767
PyMemoTable_Clear(self->memo);
47694768

4770-
END_USING_PICKLER(self);
47714769
Py_RETURN_NONE;
47724770
}
47734771

@@ -5025,10 +5023,8 @@ static PyObject *
50255023
_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
50265024
/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
50275025
{
5028-
BEGIN_USING_PICKLER(self->pickler, NULL);
50295026
if (self->pickler->memo)
50305027
PyMemoTable_Clear(self->pickler->memo);
5031-
END_USING_PICKLER(self->pickler);
50325028
Py_RETURN_NONE;
50335029
}
50345030

@@ -5042,13 +5038,12 @@ static PyObject *
50425038
_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
50435039
/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
50445040
{
5045-
PyObject *new_memo = NULL;
5046-
BEGIN_USING_PICKLER(self->pickler, NULL);
5047-
new_memo = PyDict_New();
5048-
if (new_memo == NULL) {
5049-
goto error;
5050-
}
5051-
PyMemoTable *memo = self->pickler->memo;
5041+
PyMemoTable *memo;
5042+
PyObject *new_memo = PyDict_New();
5043+
if (new_memo == NULL)
5044+
return NULL;
5045+
5046+
memo = self->pickler->memo;
50525047
for (size_t i = 0; i < memo->mt_allocated; ++i) {
50535048
PyMemoEntry entry = memo->mt_table[i];
50545049
if (entry.me_key != NULL) {
@@ -5071,12 +5066,10 @@ _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
50715066
goto error;
50725067
}
50735068
}
5074-
END_USING_PICKLER(self->pickler);
50755069
return new_memo;
50765070

50775071
error:
50785072
Py_XDECREF(new_memo);
5079-
END_USING_PICKLER(self->pickler);
50805073
return NULL;
50815074
}
50825075

@@ -5195,28 +5188,25 @@ Pickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
51955188
if (obj == NULL) {
51965189
PyErr_SetString(PyExc_TypeError,
51975190
"attribute deletion is not supported");
5198-
goto error;
5191+
return -1;
51995192
}
5200-
BEGIN_USING_PICKLER(self, -1);
52015193

52025194
PickleState *st = _Pickle_FindStateByType(Py_TYPE(self));
52035195
if (Py_IS_TYPE(obj, st->PicklerMemoProxyType)) {
52045196
PicklerObject *pickler = /* safe fast cast for 'obj' */
52055197
((PicklerMemoProxyObject *)obj)->pickler;
52065198

52075199
new_memo = PyMemoTable_Copy(pickler->memo);
5208-
if (new_memo == NULL) {
5209-
goto error;
5210-
}
5200+
if (new_memo == NULL)
5201+
return -1;
52115202
}
52125203
else if (PyDict_Check(obj)) {
52135204
Py_ssize_t i = 0;
52145205
PyObject *key, *value;
52155206

52165207
new_memo = PyMemoTable_New();
5217-
if (new_memo == NULL) {
5218-
goto error;
5219-
}
5208+
if (new_memo == NULL)
5209+
return -1;
52205210

52215211
while (PyDict_Next(obj, &i, &key, &value)) {
52225212
Py_ssize_t memo_id;
@@ -5239,19 +5229,17 @@ Pickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
52395229
PyErr_Format(PyExc_TypeError,
52405230
"'memo' attribute must be a PicklerMemoProxy object "
52415231
"or dict, not %.200s", Py_TYPE(obj)->tp_name);
5242-
goto error;
5232+
return -1;
52435233
}
52445234

52455235
PyMemoTable_Del(self->memo);
52465236
self->memo = new_memo;
52475237

5248-
END_USING_PICKLER(self);
52495238
return 0;
52505239

52515240
error:
52525241
if (new_memo)
52535242
PyMemoTable_Del(new_memo);
5254-
END_USING_PICKLER(self);
52555243
return -1;
52565244
}
52575245

@@ -7710,7 +7698,7 @@ Unpickler_get_memo(PyObject *op, void *Py_UNUSED(closure))
77107698
static int
77117699
Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
77127700
{
7713-
PyObject **new_memo = NULL;
7701+
PyObject **new_memo;
77147702
UnpicklerObject *self = UnpicklerObject_CAST(op);
77157703
size_t new_memo_size = 0;
77167704

@@ -7719,7 +7707,6 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
77197707
"attribute deletion is not supported");
77207708
return -1;
77217709
}
7722-
BEGIN_USING_UNPICKLER(self, -1);
77237710

77247711
PickleState *state = _Pickle_FindStateByType(Py_TYPE(self));
77257712
if (Py_IS_TYPE(obj, state->UnpicklerMemoProxyType)) {
@@ -7728,9 +7715,9 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
77287715

77297716
new_memo_size = unpickler->memo_size;
77307717
new_memo = _Unpickler_NewMemo(new_memo_size);
7731-
if (new_memo == NULL) {
7732-
goto error;
7733-
}
7718+
if (new_memo == NULL)
7719+
return -1;
7720+
77347721
for (size_t i = 0; i < new_memo_size; i++) {
77357722
new_memo[i] = Py_XNewRef(unpickler->memo[i]);
77367723
}
@@ -7741,9 +7728,9 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
77417728

77427729
new_memo_size = PyDict_GET_SIZE(obj);
77437730
new_memo = _Unpickler_NewMemo(new_memo_size);
7744-
if (new_memo == NULL) {
7745-
goto error;
7746-
}
7731+
if (new_memo == NULL)
7732+
return -1;
7733+
77477734
while (PyDict_Next(obj, &i, &key, &value)) {
77487735
Py_ssize_t idx;
77497736
if (!PyLong_Check(key)) {
@@ -7767,24 +7754,22 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
77677754
PyErr_Format(PyExc_TypeError,
77687755
"'memo' attribute must be an UnpicklerMemoProxy object "
77697756
"or dict, not %.200s", Py_TYPE(obj)->tp_name);
7770-
goto error;
7757+
return -1;
77717758
}
77727759

77737760
_Unpickler_MemoCleanup(self);
77747761
self->memo_size = new_memo_size;
77757762
self->memo = new_memo;
77767763

7777-
END_USING_UNPICKLER(self);
77787764
return 0;
77797765

77807766
error:
7781-
if (new_memo) {
7767+
if (new_memo_size) {
77827768
for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
77837769
Py_XDECREF(new_memo[i]);
77847770
}
77857771
PyMem_Free(new_memo);
77867772
}
7787-
END_USING_UNPICKLER(self);
77887773
return -1;
77897774
}
77907775

0 commit comments

Comments
 (0)