[Python-checkins] python/dist/src/Modules cPickle.c,2.105,2.106
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Sat, 01 Feb 2003 08:45:10 -0800
Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv16979/Modules
Modified Files:
cPickle.c
Log Message:
The C pickle now knows how to deal with a proto= argument. Assorted
code cleanups, and purged more references to text-vs-binary modes.
Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.105
retrieving revision 2.106
diff -C2 -d -r2.105 -r2.106
*** cPickle.c 1 Feb 2003 06:30:12 -0000 2.105
--- cPickle.c 1 Feb 2003 16:45:06 -0000 2.106
***************
*** 15,18 ****
--- 15,21 ----
#define WRITE_BUF_SIZE 256
+ /* Bump this when new opcodes are added to the pickle protocol. */
+ #define CURRENT_PROTOCOL_NUMBER 2
+
/*
* Pickle opcodes. These must be kept in synch with pickle.py. Extensive
***************
*** 2317,2327 ****
static Picklerobject *
! newPicklerobject(PyObject *file, int bin)
{
Picklerobject *self;
! if (!( self = PyObject_New(Picklerobject, &Picklertype)))
return NULL;
self->fp = NULL;
self->write = NULL;
--- 2320,2341 ----
static Picklerobject *
! newPicklerobject(PyObject *file, int proto)
{
Picklerobject *self;
! if (proto < 0)
! proto = CURRENT_PROTOCOL_NUMBER;
! if (proto > CURRENT_PROTOCOL_NUMBER) {
! PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
! "the highest available protocol is %d",
! proto, CURRENT_PROTOCOL_NUMBER);
return NULL;
+ }
+ self = PyObject_New(Picklerobject, &Picklertype);
+ if (self == NULL)
+ return NULL;
+ self->proto = proto;
+ self->bin = proto > 0;
self->fp = NULL;
self->write = NULL;
***************
*** 2331,2335 ****
self->inst_pers_func = NULL;
self->write_buf = NULL;
- self->bin = bin;
self->fast = 0;
self->nesting = 0;
--- 2345,2348 ----
***************
*** 2339,2349 ****
self->dispatch_table = NULL;
if (file)
Py_INCREF(file);
! else
! file=Pdata_New();
!
! if (!( self->file = file ))
! goto err;
if (!( self->memo = PyDict_New()))
--- 2352,2364 ----
self->dispatch_table = NULL;
+ self->file = NULL;
if (file)
Py_INCREF(file);
! else {
! file = Pdata_New();
! if (file == NULL)
! goto err;
! }
! self->file = file;
if (!( self->memo = PyDict_New()))
***************
*** 2353,2357 ****
self->fp = PyFile_AsFile(file);
if (self->fp == NULL) {
! PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
goto err;
}
--- 2368,2373 ----
self->fp = PyFile_AsFile(file);
if (self->fp == NULL) {
! PyErr_SetString(PyExc_ValueError,
! "I/O operation on closed file");
goto err;
}
***************
*** 2378,2383 ****
}
! if (!( self->write_buf =
! (char *)malloc(WRITE_BUF_SIZE * sizeof(char)))) {
PyErr_NoMemory();
goto err;
--- 2394,2399 ----
}
! self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
! if (self->write_buf == NULL) {
PyErr_NoMemory();
goto err;
***************
*** 2402,2406 ****
err:
! Py_DECREF((PyObject *)self);
return NULL;
}
--- 2418,2422 ----
err:
! Py_DECREF(self);
return NULL;
}
***************
*** 2411,2423 ****
{
PyObject *file = NULL;
! int bin = 1;
! if (!PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
PyErr_Clear();
! bin = 0;
! if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
return NULL;
}
! return (PyObject *)newPicklerobject(file, bin);
}
--- 2427,2444 ----
{
PyObject *file = NULL;
! int proto = 0;
! /* XXX What is this doing? The documented signature is
! * XXX Pickler(file, proto=0), but this accepts Pickler() and
! * XXX Pickler(integer) too. The meaning then is clear as mud.
! * XXX Bug? Feature?
! */
! if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
PyErr_Clear();
! proto = 0;
! if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
return NULL;
}
! return (PyObject *)newPicklerobject(file, proto);
}
***************
*** 2434,2442 ****
Py_XDECREF(self->inst_pers_func);
Py_XDECREF(self->dispatch_table);
!
! if (self->write_buf) {
! free(self->write_buf);
! }
!
PyObject_Del(self);
}
--- 2455,2459 ----
Py_XDECREF(self->inst_pers_func);
Py_XDECREF(self->dispatch_table);
! PyMem_Free(self->write_buf);
PyObject_Del(self);
}
***************
*** 4488,4492 ****
--- 4505,4513 ----
}
+ /* ---------------------------------------------------------------------------
+ * Module-level functions.
+ */
+ /* dump(obj, file, proto=0). */
static PyObject *
cpm_dump(PyObject *self, PyObject *args)
***************
*** 4494,4503 ****
PyObject *ob, *file, *res = NULL;
Picklerobject *pickler = 0;
! int bin = 0;
! if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin)))
goto finally;
! if (!( pickler = newPicklerobject(file, bin)))
goto finally;
--- 4515,4524 ----
PyObject *ob, *file, *res = NULL;
Picklerobject *pickler = 0;
! int proto = 0;
! if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
goto finally;
! if (!( pickler = newPicklerobject(file, proto)))
goto finally;
***************
*** 4515,4518 ****
--- 4536,4540 ----
+ /* dumps(obj, proto=0). */
static PyObject *
cpm_dumps(PyObject *self, PyObject *args)
***************
*** 4520,4526 ****
PyObject *ob, *file = 0, *res = NULL;
Picklerobject *pickler = 0;
! int bin = 0;
! if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin)))
goto finally;
--- 4542,4548 ----
PyObject *ob, *file = 0, *res = NULL;
Picklerobject *pickler = 0;
! int proto = 0;
! if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
goto finally;
***************
*** 4528,4532 ****
goto finally;
! if (!( pickler = newPicklerobject(file, bin)))
goto finally;
--- 4550,4554 ----
goto finally;
! if (!( pickler = newPicklerobject(file, proto)))
goto finally;
***************
*** 4544,4547 ****
--- 4566,4570 ----
+ /* load(fileobj). */
static PyObject *
cpm_load(PyObject *self, PyObject *args)
***************
*** 4565,4568 ****
--- 4588,4592 ----
+ /* loads(string) */
static PyObject *
cpm_loads(PyObject *self, PyObject *args)
***************
*** 4620,4651 ****
static struct PyMethodDef cPickle_methods[] = {
{"dump", (PyCFunction)cpm_dump, METH_VARARGS,
! PyDoc_STR("dump(object, file, [binary]) --"
! "Write an object in pickle format to the given file\n"
"\n"
! "If the optional argument, binary, is provided and is true, then the\n"
! "pickle will be written in binary format, which is more space and\n"
! "computationally efficient. \n")
},
{"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
! PyDoc_STR("dumps(object, [binary]) --"
! "Return a string containing an object in pickle format\n"
"\n"
! "If the optional argument, binary, is provided and is true, then the\n"
! "pickle will be written in binary format, which is more space and\n"
! "computationally efficient. \n")
},
{"load", (PyCFunction)cpm_load, METH_VARARGS,
PyDoc_STR("load(file) -- Load a pickle from the given file")},
{"loads", (PyCFunction)cpm_loads, METH_VARARGS,
PyDoc_STR("loads(string) -- Load a pickle from the given string")},
{"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
! PyDoc_STR("Pickler(file, [binary]) -- Create a pickler\n"
"\n"
! "If the optional argument, binary, is provided and is true, then\n"
! "pickles will be written in binary format, which is more space and\n"
! "computationally efficient. \n")
},
{"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
! PyDoc_STR("Unpickler(file) -- Create an unpickler")},
{ NULL, NULL }
};
--- 4644,4694 ----
static struct PyMethodDef cPickle_methods[] = {
{"dump", (PyCFunction)cpm_dump, METH_VARARGS,
! PyDoc_STR("dump(object, file, proto=0) -- "
! "Write an object in pickle format to the given file.\n"
"\n"
! "See the Pickler docstring for the meaning of optional argument proto.")
},
+
{"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
! PyDoc_STR("dumps(object, proto=0) -- "
! "Return a string containing an object in pickle format.\n"
"\n"
! "See the Pickler docstring for the meaning of optional argument proto.")
},
+
{"load", (PyCFunction)cpm_load, METH_VARARGS,
PyDoc_STR("load(file) -- Load a pickle from the given file")},
+
{"loads", (PyCFunction)cpm_loads, METH_VARARGS,
PyDoc_STR("loads(string) -- Load a pickle from the given string")},
+
{"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
! PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
"\n"
! "This takes a file-like object for writing a pickle data stream.\n"
! "The optional proto argument tells the pickler to use the given\n"
! "protocol; supported protocols are 0, 1, 2. The default\n"
! "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
! "only protocol that can be written to a file opened in text\n"
! "mode and read back successfully. When using a protocol higher\n"
! "than 0, make sure the file is opened in binary mode, both when\n"
! "pickling and unpickling.)\n"
! "\n"
! "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
! "more efficient than protocol 1.\n"
! "\n"
! "Specifying a negative protocol version selects the highest\n"
! "protocol version supported. The higher the protocol used, the\n"
! "more recent the version of Python needed to read the pickle\n"
! "produced.\n"
! "\n"
! "The file parameter must have a write() method that accepts a single\n"
! "string argument. It can thus be an open file object, a StringIO\n"
! "object, or any other custom object that meets this interface.\n")
},
+
{"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
! PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
!
{ NULL, NULL }
};
***************
*** 4684,4689 ****
Py_DECREF(copy_reg);
-
- /* Down to here ********************************** */
if (!( empty_tuple = PyTuple_New(0)))
--- 4727,4730 ----