Commit 0a89c829 authored by Jim Fulton's avatar Jim Fulton

Added new Unpickler attribute, find_global. If set to None, then

global and instance pickles are disabled.  Otherwise, it should be set to
a callable object that takes two arguments, a module name and an
object name, and returns an object.  If the attribute is unset, then
the default mechanism is used.

This feature provides an additional mechanism for controlling which
classes can be used for unpickling.
parent 5392722d
/* /*
* $Id: cPickle.c,v 1.65 1999/04/13 16:45:41 jim Exp $ * $Id: cPickle.c,v 1.66 1999/04/13 17:23:48 jim Exp $
* *
* Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA. * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
* All rights reserved. * All rights reserved.
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
static char cPickle_module_documentation[] = static char cPickle_module_documentation[] =
"C implementation and optimization of the Python pickle module\n" "C implementation and optimization of the Python pickle module\n"
"\n" "\n"
"$Id: cPickle.c,v 1.65 1999/04/13 16:45:41 jim Exp $\n" "$Id: cPickle.c,v 1.66 1999/04/13 17:23:48 jim Exp $\n"
; ;
#include "Python.h" #include "Python.h"
...@@ -345,6 +345,7 @@ typedef struct { ...@@ -345,6 +345,7 @@ typedef struct {
int buf_size; int buf_size;
char *buf; char *buf;
PyObject *safe_constructors; PyObject *safe_constructors;
PyObject *find_class;
} Unpicklerobject; } Unpicklerobject;
staticforward PyTypeObject Unpicklertype; staticforward PyTypeObject Unpicklertype;
...@@ -2289,9 +2290,18 @@ static PyTypeObject Picklertype = { ...@@ -2289,9 +2290,18 @@ static PyTypeObject Picklertype = {
}; };
static PyObject * static PyObject *
find_class(PyObject *py_module_name, PyObject *py_global_name) { find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
PyObject *global = 0, *module; PyObject *global = 0, *module;
if (fc) {
if (fc==Py_None) {
PyErr_SetString(UnpicklingError,
"Global and instance pickles are not supported.");
return NULL;
}
return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
}
module = PySys_GetObject("modules"); module = PySys_GetObject("modules");
if (module == NULL) if (module == NULL)
return NULL; return NULL;
...@@ -2842,7 +2852,7 @@ load_inst(Unpicklerobject *self) { ...@@ -2842,7 +2852,7 @@ load_inst(Unpicklerobject *self) {
if ((len = (*self->readline_func)(self, &s)) >= 0) { if ((len = (*self->readline_func)(self, &s)) >= 0) {
if (len < 2) return bad_readline(); if (len < 2) return bad_readline();
if (class_name = PyString_FromStringAndSize(s, len - 1)) { if (class_name = PyString_FromStringAndSize(s, len - 1)) {
class = find_class(module_name, class_name); class = find_class(module_name, class_name, self->find_class);
Py_DECREF(class_name); Py_DECREF(class_name);
} }
} }
...@@ -2876,7 +2886,7 @@ load_global(Unpicklerobject *self) { ...@@ -2876,7 +2886,7 @@ load_global(Unpicklerobject *self) {
if ((len = (*self->readline_func)(self, &s)) >= 0) { if ((len = (*self->readline_func)(self, &s)) >= 0) {
if (len < 2) return bad_readline(); if (len < 2) return bad_readline();
if (class_name = PyString_FromStringAndSize(s, len - 1)) { if (class_name = PyString_FromStringAndSize(s, len - 1)) {
class = find_class(module_name, class_name); class = find_class(module_name, class_name, self->find_class);
Py_DECREF(class_name); Py_DECREF(class_name);
} }
} }
...@@ -3889,6 +3899,7 @@ newUnpicklerobject(PyObject *f) { ...@@ -3889,6 +3899,7 @@ newUnpicklerobject(PyObject *f) {
self->read = NULL; self->read = NULL;
self->readline = NULL; self->readline = NULL;
self->safe_constructors = NULL; self->safe_constructors = NULL;
self->find_class = NULL;
UNLESS (self->memo = PyDict_New()) { UNLESS (self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self); Py_XDECREF((PyObject *)self);
...@@ -3996,6 +4007,16 @@ Unpickler_getattr(Unpicklerobject *self, char *name) { ...@@ -3996,6 +4007,16 @@ Unpickler_getattr(Unpicklerobject *self, char *name) {
return self->pers_func; return self->pers_func;
} }
if (!strcmp(name, "find_global")) {
if (!self->find_class) {
PyErr_SetString(PyExc_AttributeError, name);
return NULL;
}
Py_INCREF(self->find_class);
return self->find_class;
}
if (!strcmp(name, "memo")) { if (!strcmp(name, "memo")) {
if (!self->memo) { if (!self->memo) {
PyErr_SetString(PyExc_AttributeError, name); PyErr_SetString(PyExc_AttributeError, name);
...@@ -4018,19 +4039,26 @@ Unpickler_getattr(Unpicklerobject *self, char *name) { ...@@ -4018,19 +4039,26 @@ Unpickler_getattr(Unpicklerobject *self, char *name) {
static int static int
Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) { Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
if (! value) {
PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported");
return -1;
}
if (!strcmp(name, "persistent_load")) { if (!strcmp(name, "persistent_load")) {
Py_XDECREF(self->pers_func); Py_XDECREF(self->pers_func);
self->pers_func = value; self->pers_func = value;
Py_INCREF(value); Py_XINCREF(value);
return 0;
}
if (!strcmp(name, "find_global")) {
Py_XDECREF(self->find_class);
self->find_class = value;
Py_XINCREF(value);
return 0; return 0;
} }
if (! value) {
PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported");
return -1;
}
if (strcmp(name, "memo") == 0) { if (strcmp(name, "memo") == 0) {
if (! PyDict_Check(value)) { if (! PyDict_Check(value)) {
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
...@@ -4305,7 +4333,7 @@ init_stuff(PyObject *module, PyObject *module_dict) { ...@@ -4305,7 +4333,7 @@ init_stuff(PyObject *module, PyObject *module_dict) {
DL_EXPORT(void) DL_EXPORT(void)
initcPickle() { initcPickle() {
PyObject *m, *d, *v; PyObject *m, *d, *v;
char *rev="$Revision: 1.65 $"; char *rev="$Revision: 1.66 $";
PyObject *format_version; PyObject *format_version;
PyObject *compatible_formats; PyObject *compatible_formats;
......
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