Skip to content

Commit 1f1b2d2

Browse files
committed
Removed all uses of the out-of-favor __safe_for_unpickling__ magic
attr, and copy_reg.safe_constructors.
1 parent 371935f commit 1f1b2d2

5 files changed

Lines changed: 5 additions & 79 deletions

File tree

Lib/copy_reg.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"add_extension", "remove_extension", "clear_extension_cache"]
1111

1212
dispatch_table = {}
13-
safe_constructors = {}
1413

1514
def pickle(ob_type, pickle_function, constructor_ob=None):
1615
if type(ob_type) is _ClassType:
@@ -26,7 +25,6 @@ def pickle(ob_type, pickle_function, constructor_ob=None):
2625
def constructor(object):
2726
if not callable(object):
2827
raise TypeError("constructors must be callable")
29-
safe_constructors[object] = 1
3028

3129
# Example: provide pickling support for complex numbers.
3230

@@ -41,7 +39,6 @@ def _reconstructor(cls, base, state):
4139
obj = base.__new__(cls, state)
4240
base.__init__(obj, state)
4341
return obj
44-
_reconstructor.__safe_for_unpickling__ = 1
4542

4643
_HEAPTYPE = 1<<9
4744

Lib/test/pickletester.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def __init__(self, x):
1515

1616
class initarg(C):
1717

18-
__safe_for_unpickling__ = 1
19-
2018
def __init__(self, a, b):
2119
self.a = a
2220
self.b = b

Modules/cPickle.c

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ static PyObject *BadPickleGet;
9292

9393

9494
static PyObject *dispatch_table;
95-
static PyObject *safe_constructors;
9695
static PyObject *empty_tuple;
9796

9897
static PyObject *__class___str, *__getinitargs___str, *__dict___str,
9998
*__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
100-
*write_str, *__safe_for_unpickling___str, *append_str,
99+
*write_str, *append_str,
101100
*read_str, *readline_str, *__main___str, *__basicnew___str,
102-
*copy_reg_str, *dispatch_table_str, *safe_constructors_str;
101+
*copy_reg_str, *dispatch_table_str;
103102

104103
/*************************************************************************
105104
Internal Data type for pickle data. */
@@ -306,7 +305,6 @@ typedef struct Unpicklerobject {
306305
int (*readline_func)(struct Unpicklerobject *, char **);
307306
int buf_size;
308307
char *buf;
309-
PyObject *safe_constructors;
310308
PyObject *find_class;
311309
} Unpicklerobject;
312310

@@ -3078,8 +3076,7 @@ load_dict(Unpicklerobject *self)
30783076
static PyObject *
30793077
Instance_New(PyObject *cls, PyObject *args)
30803078
{
3081-
int has_key;
3082-
PyObject *safe=0, *r=0;
3079+
PyObject *r = 0;
30833080

30843081
if (PyClass_Check(cls)) {
30853082
int l;
@@ -3107,21 +3104,6 @@ Instance_New(PyObject *cls, PyObject *args)
31073104
else goto err;
31083105
}
31093106

3110-
/* Is safe_constructors always a dict? */
3111-
has_key = cPickle_PyMapping_HasKey(safe_constructors, cls);
3112-
if (!has_key) {
3113-
safe = PyObject_GetAttr(cls, __safe_for_unpickling___str);
3114-
if (!safe ||
3115-
!PyObject_IsTrue(safe)) {
3116-
cPickle_ErrFormat(UnpicklingError,
3117-
"%s is not safe for unpickling",
3118-
"O", cls);
3119-
Py_XDECREF(safe);
3120-
return NULL;
3121-
}
3122-
Py_DECREF(safe);
3123-
}
3124-
31253107
if (args==Py_None) {
31263108
/* Special case, call cls.__basicnew__() */
31273109
PyObject *basicnew;
@@ -4332,7 +4314,6 @@ newUnpicklerobject(PyObject *f)
43324314
self->buf_size = 0;
43334315
self->read = NULL;
43344316
self->readline = NULL;
4335-
self->safe_constructors = NULL;
43364317
self->find_class = NULL;
43374318

43384319
if (!( self->memo = PyDict_New()))
@@ -4373,21 +4354,6 @@ newUnpicklerobject(PyObject *f)
43734354
}
43744355
}
43754356

4376-
if (PyEval_GetRestricted()) {
4377-
/* Restricted execution, get private tables */
4378-
PyObject *m;
4379-
4380-
if (!( m=PyImport_Import(copy_reg_str))) goto err;
4381-
self->safe_constructors=PyObject_GetAttr(m,
4382-
safe_constructors_str);
4383-
Py_DECREF(m);
4384-
if (!( self->safe_constructors )) goto err;
4385-
}
4386-
else {
4387-
self->safe_constructors=safe_constructors;
4388-
Py_INCREF(safe_constructors);
4389-
}
4390-
43914357
return self;
43924358

43934359
err:
@@ -4418,7 +4384,6 @@ Unpickler_dealloc(Unpicklerobject *self)
44184384
Py_XDECREF(self->pers_func);
44194385
Py_XDECREF(self->arg);
44204386
Py_XDECREF(self->last_string);
4421-
Py_XDECREF(self->safe_constructors);
44224387

44234388
if (self->marks) {
44244389
free(self->marks);
@@ -4693,28 +4658,22 @@ init_stuff(PyObject *module_dict)
46934658
INIT_STR(__main__);
46944659
INIT_STR(__reduce__);
46954660
INIT_STR(write);
4696-
INIT_STR(__safe_for_unpickling__);
46974661
INIT_STR(append);
46984662
INIT_STR(read);
46994663
INIT_STR(readline);
47004664
INIT_STR(copy_reg);
47014665
INIT_STR(dispatch_table);
4702-
INIT_STR(safe_constructors);
47034666
INIT_STR(__basicnew__);
47044667

47054668
if (!( copy_reg = PyImport_ImportModule("copy_reg")))
47064669
return -1;
47074670

4708-
/* These next few are special because we want to use different
4709-
ones in restricted mode. */
4671+
/* This is special because we want to use a different
4672+
one in restricted mode. */
47104673
dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
47114674
if (!dispatch_table)
47124675
return -1;
47134676

4714-
if (!( safe_constructors = PyObject_GetAttr(copy_reg,
4715-
safe_constructors_str)))
4716-
return -1;
4717-
47184677
Py_DECREF(copy_reg);
47194678

47204679
/* Down to here ********************************** */

Modules/datetimemodule.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,13 +4516,6 @@ initdatetime(void)
45164516
PyObject *d; /* its dict */
45174517
PyObject *x;
45184518

4519-
/* Types that use __reduce__ for pickling need to set the following
4520-
* magical attr in the type dict, with a true value.
4521-
*/
4522-
PyObject *safepickle = PyString_FromString("__safe_for_unpickling__");
4523-
if (safepickle == NULL)
4524-
return;
4525-
45264519
m = Py_InitModule3("datetime", module_methods,
45274520
"Fast implementation of the datetime type.");
45284521

@@ -4577,18 +4570,9 @@ initdatetime(void)
45774570
}
45784571
}
45794572

4580-
/* tzinfo values */
4581-
d = PyDateTime_TZInfoType.tp_dict;
4582-
4583-
if (PyDict_SetItem(d, safepickle, Py_True) < 0)
4584-
return;
4585-
45864573
/* timedelta values */
45874574
d = PyDateTime_DeltaType.tp_dict;
45884575

4589-
if (PyDict_SetItem(d, safepickle, Py_True) < 0)
4590-
return;
4591-
45924576
x = new_delta(0, 0, 1, 0);
45934577
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
45944578
return;
@@ -4607,9 +4591,6 @@ initdatetime(void)
46074591
/* date values */
46084592
d = PyDateTime_DateType.tp_dict;
46094593

4610-
if (PyDict_SetItem(d, safepickle, Py_True) < 0)
4611-
return;
4612-
46134594
x = new_date(1, 1, 1);
46144595
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
46154596
return;
@@ -4628,9 +4609,6 @@ initdatetime(void)
46284609
/* time values */
46294610
d = PyDateTime_TimeType.tp_dict;
46304611

4631-
if (PyDict_SetItem(d, safepickle, Py_True) < 0)
4632-
return;
4633-
46344612
x = new_time(0, 0, 0, 0, Py_None);
46354613
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
46364614
return;
@@ -4649,9 +4627,6 @@ initdatetime(void)
46494627
/* datetime values */
46504628
d = PyDateTime_DateTimeType.tp_dict;
46514629

4652-
if (PyDict_SetItem(d, safepickle, Py_True) < 0)
4653-
return;
4654-
46554630
x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None);
46564631
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
46574632
return;
@@ -4667,8 +4642,6 @@ initdatetime(void)
46674642
return;
46684643
Py_DECREF(x);
46694644

4670-
Py_DECREF(safepickle);
4671-
46724645
/* module initialization */
46734646
PyModule_AddIntConstant(m, "MINYEAR", MINYEAR);
46744647
PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR);

Objects/structseq.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,5 +392,4 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
392392
PyInt_FromLong((long) n_members));
393393
PyDict_SetItemString(dict, unnamed_fields_key,
394394
PyInt_FromLong((long) n_unnamed_members));
395-
PyDict_SetItemString(dict, "__safe_for_unpickling__", Py_True);
396395
}

0 commit comments

Comments
 (0)