Commit 4131ccdf authored by Jeremy Hylton's avatar Jeremy Hylton

Remove decoy version of cPickle.

I couldn't find any references to cPickle in any of the setup/build
tools, but I couldn't actually verify that the Windows build was
unaffected.
parent acc6b48e
/*
PyErr_Format -- Raise an exception from C using format strings
Arguments:
ErrType -- The type of error to be raised
string_format -- A Python format-string-style format used to
create a string return value. This argument may be NULL, in
which case the error value will be a value built with
Py_BuildValue using the build-format argument.
build_format -- A PyBuild_Value format string for building a
Python values from C objects. If this argument is NULL, and
string_format is not NULL, then PyErr_Format is equivalent to
PyErr_SetString, except that a NULL value is returned.
If both build_format and string_format are ommitted, then
PyErr_Format uses None as the error return value.
The remaining arguments will be passed to PyBuildValue.
Return
This function always returns a NULL pointer. This allows the
function to be used in simple return statements. For example, in
a function that returns a PyObject pointer, an error may be
raised like this::
if(some_error_condition)
return PyErr_Format("Something bad happened with %s at index %s",
"si", some_c_string, some_int)
*/
PyObject *
PyErr_Format(PyObject *ErrType, char *string_format, char *build_format, ...);
/*
PyErr_Format -- Raise an exception from C using a C format string
Arguments:
ErrType -- The type of error to be raised
format -- A C format string
The remaining arguments will be used with the C format string.
Limitation
The size of the formatted string must not exceed 500 characters.
Return
This function always returns a NULL pointer. This allows the
function to be used in simple return statements. For example, in
a function that returns a PyObject pointer, an error may be
raised like this::
if(some_error_condition)
return PyErr_CFormat("Something bad happened with %s at index %d",
some_c_string, some_int)
*/
PyObject *
PyErr_CFormat(PyObject *ErrType, char *format, ...);
*shared*
cStringIO cStringIO.c
cPickle cPickle.c -DFORMAT_1_3
# install copy_reg.py
This source diff could not be displayed because it is too large. You can view the blob instead.
cPickle, A C Pickle Implementation
"cPickle" is a C implementation of the Python module, pickle.py, and is
fully compatible with pickle: all types picklable by pickle can be
pickled using cPickle [1], and pickles generated using pickle may be
unpickled using cPickle and vice versa.
The basic interface for cPickle is nearly the same as pickle's, the
only difference being that an optional value may be specified as the
final argument to Pickler(), dump, and dumps(), specifying that
binary mode should be used. For instance, to pickle an object, ob,
to file, f, which has been opened for binary writing::
p = cPickle.Pickler(f, 1)
p.dump(ob)
alternately::
cPickle.dump(ob, f, 1)
Using binary format produces smaller pickles and increases the speed of
pickling and unpickling. Note, however, old versions of pickle
cannot read binary pickles and files containing binary pickles
should be opened in binary mode.
Customization
For this release of cPickle, a small amount of customization is
possible for the Pickler and Unpickler. Specifically, the
persistent_id attribute of the Pickler and the persistent_load
attribute of the Unpickler may be set to user-defined functions by
setting attributes. For instance::
def peristent_id(object):
... return appropriate persistent id string ...
p = cPickle.Pickler(f)
p.persistent_id = persistent_id
def persistent_load(pers_id):
... return appropriate object, given a persistent id ...
u = cPickle.Unpickler(f)
u.persistent_load = persistent_load
Note that this mechanism works with the current pickle module too.
The cPickle module does not support customization through inheritance
as pickle.py does.
Pickling Extension Types
When the pickler encounters an object it doesn't know how to pickle,
it checks to see if the object has a __reduce__ method. This should
return a tuple consisting of an callable object, a tuple of arguments, and
an optional object state value. If a state value is supplied, it will be
passed to the newly created object's __setstate__ method. If the object
has no __setstate__ method, it will be added to the object's __dict__
(note that the state value must be a dictionary in this case).
The contents of this tuple will be saved, and, on unpickling, the callable
object will be called with the arguments to create the instance.
The callable object must either be:
- a Class,
- registered in the set of safe constructors (see below),
- or have an attribute, __safe_for_unpickling__, with a true value.
Note that the __reduce__ method may also return a string value. In this
case, the value of the global variable with the name given by the string
value is used.
The module copy_reg.py provides an interface for registering pickling
functions::
def pickle(ob_type, pickle_function, constructor = None):
'''Register the function, 'pickle_function' to be called
to pickle objects of type 'ob_type'.
The pickle function must behave in the same manner as the __reduce__
method described above. If the argument 'constructor' is specified,
then it will be registered as an object that is safe for unpickling
objects.
copy_reg.py also provides an interface for updating a registry of safe
constructors to be used when unpickling::
def constructor(object):
'Register the object as safe for unpicking objects'
The following example shows how one might add support for pickling
complex numbers::
import copy_reg
def pickle_complex(z):
return (complex, (z.real, z.imag))
copy_reg.pickle(type(complex(0,0)), pickle_complex, complex)
This release also includes the latest release of the cStringIO
module. The cStringIO module is a C implementation of the Python
StringIO module; it is used internally by cPickle. It may also
be used outside of cPickle as a much faster StringIO implementation.
A new version of pickle.py is also provided. This version adds support
for writing and reading binary pickles.
Status
The current release of cPickle is "1.0b2". This release requires
Python version 1.4 or higher.
This version introduces a special binary mode format for floats.
Since this could not be implemented in the python version, it is
disabled by default in cPickle. To enable the new float format,
see the instructions in the Makefile.
Installation
The cPickle distribution now uses the "Universal Unix Makefile for
Python extensions", 'Makefile.pre.in', which was introduced as
part of Python1.4. A copy of this make file is included with this
release. See the instructions in the make file, itself.
Note that cPickle requires that both copy_reg and cStringIO be importable.
Files
Makefile.pre.in -- The Universal Unix Makefile for Python extensions
Setup -- a configuration file used by the Universal
Unix Makefile for Python extensions
cPickle.stx -- This file in structured text format
cPickle.html -- This file in HTML format
cPickle.c -- The cPickle source
cStringIO.c -- The cStringIO source
cStringIO.h -- The cStringIO C-API definition
pickle.py -- The Python implementation of pickle with cPickle
Extensions
copy_reg.py -- An auxilary module used to keep Pickle registration
information used by cPickle
Release Notes
0.1 -- Initial (Alpha) Release
0.2 -- Second release incorporating many fixes and improvements over
initial release. This is the first Beta release.
0.2.1 -- Bug fix release and slight format enhancement
Bugs fixed:
- Objects placed in memo when pickling were not
actually retained, causing weird behavior when
additional objects were created with the same ids
(i.e. addresses).
- Objects saved via the '__reduce__' mechanism were not
placed in memo when pickling.
In addition, binary floating point format was added.
This uses code from the new struct module.
Unfortunately, this cannot be supported in the current
Python version. Support of this format is disabled by
default. To enable this feature, edit the make file
'CFLAGS' variable as described in the make file,
'Makefile'.
0.2.2 -- Bug fix release
Bugs fixed:
- Incomplete initialization of Pickler and Unpickler objects
caused core dump if an error occured while creating either
of the objects.
- Empty tuples saved in non-binary mode when using pickle.py
produced an extra MARK if there was already an empty tuple
in the memo.
- Too few POPs written for recursive tuples.
- Lists and dictionaries were not being placed into the memo
before their items were saved
- Only placing objects with a reference count > 1 in the memo
was not always safe.
- PyMapping_HasKey() leaked memory
0.3 -- Bug fixes and minor new features
New features:
- In cStringIO, Guido added an unused softspace attribute for
compatibility with file objects.
- In cStringIO, the getvalue() method now accepts an
optional argument. If the argument is true, then the
string returned is the contents of the StringIO up to
the current file position. Otherwise, the entire
contents are returned.
- In cPickle, made some minor optimizations in
pickling.
- In cPickle, added clear_memo method to Pickler
objects. This is useful when reusing Picklers.
Bug fixes
- In cStringIO, getvalue() returned it's contents
only up to the file position, rather than the entire
contents.
- In cPickle, added logic from pickle.py to avoid
picking '__main__' as a class' module when pickling
classes.
- Fixed bug (pointed out by Mark Lake) in saving
tuples that participate in circular references.
This release includes a number of changes made for 1.5,
and changes to make 'gcc -Wall -pedantic' happy.
0.3.1 -- Bug Fix
- Fixed a reference counting bug in the code that
unpickles global objects. (I incorrectly assumed that
PyEval_GetGlobals incremented the reference count of the
object it returned.) This bug caused really bizarre
things to happen after unpickling a pickle with lots of
global references.
1.0b1 -- Official release (almost)
This is the release that I expect to be included in Python
1.5 final.
Old new features
Some important features were added in earlier releases, but
are not mentioned above.
- Flexible module import. Now cPickle calles __import__
so it works with ni and other specialized import
mechanisms.
- New unpickler methods and features to support persistent
object systems:
- The persistent_load attribute can be set to a list instead
of a function. In this case, persistent ids encountered
during unpickling are added to the list.
- Unpickler 'noload' method is like 'load' except that
it doesn't create any objects. This is useful in
finding persistent ids in a pickle::
f=StringIO(some_pickle)
u=Unpickler(f)
ids=[]
u.persistent_load=ids
u.noload()
# at this point, ids contains any persistent ids
# found in the pickle.
Note that this technique works even if instances are
encountered for classes defined in modules that cannot
be imported, since no attempt is made to actually
instantiate the instances.
New feature
- In cPickle, cPickle checks to see if a class has a __module__
attribute and, if present, uses that attribute to pickle the
class.
- A new instance pickling/unpickling protocol is used. If
a class defines no __getinitargs__ method, then __init__
method is not called when an instance is unpickled.
- Support for restricted execution environments was finished
in cPickle.
Bug Fix
- Changed the way __version__ is created to avoid seg fault when
different C version strings are used.
Cleanup
- A number of cleanups done by Guido were incorporated.
1.0b2 -- I forgot a few things in 1.0b1
New features
- Loading non-binary string pickles checks for insecure
strings. This is needed because cPickle (still) uses
a restricted eval to parse non-binary string pickles.
This change is needed to prevent untrusted pickles like::
"S'hello world'*2000000\012p0\012."
from hosing an application.
- User-defined types can now support unpickling without
executing a constructor.
The second value returned from '__reduce__' can now be None,
rather than an argument tuple. On unpickling, if the
second value returned from '__reduce__' during pickling was
None, then rather than calling the first value returned from
'__reduce__', directly, the '__basicnew__' method of the
first value returned from '__reduce__' is called without
arguments.
Cleanup
- Incorporated Guido's changes to cStringIO.h and copy_reg.py
- Removed pickle.py from cPickle distribution.
To do
- Support for pickling humongous arrays. Konrad Hinsen summed up
the issue:
"With pickle.py, NumPy derives special pickler/unpickler
classes that know how to deal with arrays. With cPickle, I'd
have to use the method described under "pickling extension
types". Unless I have missed something essential, that means I
have to find a representation of the extension type in terms
of built-in Python types. For large arrays, this will
certainly eat up all speed advantage that cPickle can offer,
and in addition it creates memory problems.
Is there any specific reason for not simply allowing an extension
type to pickle und unpickle itself with special methods?"
cPickle and it's associated version of pickle.py use a new
"reduce" protocol to handle new types. Unfortunately, the
intermediate objects created in this protocol are imprectical
when working with millions of numbers. Konrad and I have
discussed a number of options, but I'm not happy enough with any
of them to have implemented them.
.. [1] An earlier version of the pickle module provided for saving
extension type instances that had __class__ attributes. Using
cPickle, extension types can only be be pickled using the
mechanism described in the section "Pickling Extension Types"
above.
/*
* cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp
*
* Copyright (c) 1996-1998, Zope Corporation, Fredericksburg, VA, USA.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the disclaimer that follows.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions, and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* o All advertising materials mentioning features or use of this
* software must display the following acknowledgement:
*
* This product includes software developed by Zope Corporation
* and its contributors.
*
* o Neither the name of Zope Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY ZOPE CORPORATION AND CONTRIBUTORS *AS
* IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ZOPE CORPORATION
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
#
# If you have questions regarding this software, contact:
#
# Zope Corporation
# info@zope.com
#
# (540) 371-6909
*/
static char cStringIO_module_documentation[] =
"A simple fast partial StringIO replacement.\n"
"\n"
"This module provides a simple useful replacement for\n"
"the StringIO module that is written in C. It does not provide the\n"
"full generality of StringIO, but it provides enough for most\n"
"applications and is especially useful in conjunction with the\n"
"pickle module.\n"
"\n"
"Usage:\n"
"\n"
" from cStringIO import StringIO\n"
"\n"
" an_output_stream=StringIO()\n"
" an_output_stream.write(some_stuff)\n"
" ...\n"
" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
"\n"
" an_input_stream=StringIO(a_string)\n"
" spam=an_input_stream.readline()\n"
" spam=an_input_stream.read(5)\n"
" an_input_stream.seek(0) # OK, start over\n"
" spam=an_input_stream.read() # and read it all\n"
" \n"
"If someone else wants to provide a more complete implementation,\n"
"go for it. :-) \n"
"\n"
"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"
;
#include "Python.h"
#include "import.h"
#include "cStringIO.h"
#define UNLESS(E) if (!(E))
/* Declaration for file-like objects that manage data as strings
The IOobject type should be though of as a common base type for
Iobjects, which provide input (read-only) StringIO objects and
Oobjects, which provide read-write objects. Most of the methods
depend only on common data.
*/
typedef struct {
PyObject_HEAD
char *buf;
int pos, string_size;
} IOobject;
#define IOOOBJECT(O) ((IOobject*)(O))
/* Declarations for objects of type StringO */
typedef struct { /* Subtype of IOobject */
PyObject_HEAD
char *buf;
int pos, string_size;
int buf_size, softspace;
} Oobject;
/* Declarations for objects of type StringI */
typedef struct { /* Subtype of IOobject */
PyObject_HEAD
char *buf;
int pos, string_size;
PyObject *pbuf;
} Iobject;
/* IOobject (common) methods */
static char IO_flush__doc__[] = "flush(): does nothing.";
static int
IO__opencheck(IOobject *self) {
UNLESS (self->buf) {
PyErr_SetString(PyExc_ValueError,
"I/O operation on closed file");
return 0;
}
return 1;
}
static PyObject *
IO_flush(IOobject *self, PyObject *args) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static char IO_getval__doc__[] =
"getvalue([use_pos]) -- Get the string value."
"\n"
"If use_pos is specified and is a true value, then the string returned\n"
"will include only the text up to the current file position.\n"
;
static PyObject *
IO_cgetval(PyObject *self) {
UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
return PyString_FromStringAndSize(((IOobject*)self)->buf,
((IOobject*)self)->pos);
}
static PyObject *
IO_getval(IOobject *self, PyObject *args) {
PyObject *use_pos=Py_None;
int s;
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
if (PyObject_IsTrue(use_pos)) {
s=self->pos;
if (s > self->string_size) s=self->string_size;
}
else
s=self->string_size;
return PyString_FromStringAndSize(self->buf, s);
}
static char IO_isatty__doc__[] = "isatty(): always returns 0";
static PyObject *
IO_isatty(IOobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL;
return PyInt_FromLong(0);
}
static char IO_read__doc__[] =
"read([s]) -- Read s characters, or the rest of the string"
;
static int
IO_cread(PyObject *self, char **output, int n) {
int l;
UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
if (n < 0 || n > l) {
n = l;
if (n < 0) n=0;
}
*output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
((IOobject*)self)->pos += n;
return n;
}
static PyObject *
IO_read(IOobject *self, PyObject *args) {
int n = -1;
char *output;
UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
return PyString_FromStringAndSize(output, n);
}
static char IO_readline__doc__[] =
"readline() -- Read one line"
;
static int
IO_creadline(PyObject *self, char **output) {
char *n, *s;
int l;
UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
n < s && *n != '\n'; n++);
if (n < s) n++;
*output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
((IOobject*)self)->pos += l;
return l;
}
static PyObject *
IO_readline(IOobject *self, PyObject *args) {
int n, m=-1;
char *output;
UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
if (m >= 0 && m < n) {
m = n - m;
n -= m;
self->pos -= m;
}
return PyString_FromStringAndSize(output, n);
}
static char IO_readlines__doc__[] =
"readlines() -- Read all lines"
;
static PyObject *
IO_readlines(IOobject *self, PyObject *args) {
int n;
char *output;
PyObject *result, *line;
int hint = 0, length = 0;
UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
result = PyList_New(0);
if (!result)
return NULL;
while (1){
if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
goto err;
if (n == 0)
break;
line = PyString_FromStringAndSize (output, n);
if (!line)
goto err;
PyList_Append (result, line);
Py_DECREF (line);
length += n;
if (hint > 0 && length >= hint)
break;
}
return result;
err:
Py_DECREF(result);
return NULL;
}
static char IO_reset__doc__[] =
"reset() -- Reset the file position to the beginning"
;
static PyObject *
IO_reset(IOobject *self, PyObject *args) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL;
self->pos = 0;
Py_INCREF(Py_None);
return Py_None;
}
static char IO_tell__doc__[] =
"tell() -- get the current position.";
static PyObject *
IO_tell(IOobject *self, PyObject *args) {
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL;
return PyInt_FromLong(self->pos);
}
static char IO_truncate__doc__[] =
"truncate(): truncate the file at the current position.";
static PyObject *
IO_truncate(IOobject *self, PyObject *args) {
int pos = -1;
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
if (pos < 0) pos = self->pos;
if (self->string_size > pos) self->string_size = pos;
Py_INCREF(Py_None);
return Py_None;
}
/* Read-write object methods */
static char O_seek__doc__[] =
"seek(position) -- set the current position\n"
"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
static PyObject *
O_seek(Oobject *self, PyObject *args) {
int position, mode = 0;
UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
return NULL;
if (mode == 2) {
position += self->string_size;
}
else if (mode == 1) {
position += self->pos;
}
if (position > self->buf_size) {
self->buf_size*=2;
if (self->buf_size <= position) self->buf_size=position+1;
UNLESS (self->buf=(char*)
realloc(self->buf,self->buf_size*sizeof(char))) {
self->buf_size=self->pos=0;
return PyErr_NoMemory();
}
}
else if (position < 0) position=0;
self->pos=position;
while (--position >= self->string_size) self->buf[position]=0;
Py_INCREF(Py_None);
return Py_None;
}
static char O_write__doc__[] =
"write(s) -- Write a string to the file"
"\n\nNote (hack:) writing None resets the buffer"
;
static int
O_cwrite(PyObject *self, char *c, int l) {
int newl;
UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
newl=((Oobject*)self)->pos+l;
if (newl >= ((Oobject*)self)->buf_size) {
((Oobject*)self)->buf_size*=2;
if (((Oobject*)self)->buf_size <= newl)
((Oobject*)self)->buf_size=newl+1;
UNLESS (((Oobject*)self)->buf=
(char*)realloc(
((Oobject*)self)->buf,
(((Oobject*)self)->buf_size) *sizeof(char))) {
PyErr_SetString(PyExc_MemoryError,"out of memory");
((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
return -1;
}
}
memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
((Oobject*)self)->pos += l;
if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
((Oobject*)self)->string_size = ((Oobject*)self)->pos;
}
return l;
}
static PyObject *
O_write(Oobject *self, PyObject *args) {
PyObject *s;
char *c;
int l;
UNLESS (PyArg_ParseTuple(args, "O:write", &s)) return NULL;
UNLESS (-1 != (l=PyString_Size(s))) return NULL;
UNLESS (c=PyString_AsString(s)) return NULL;
if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static char O_close__doc__[] = "close(): explicitly release resources held.";
static PyObject *
O_close(Oobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
if (self->buf != NULL) free(self->buf);
self->buf = NULL;
self->pos = self->string_size = self->buf_size = 0;
Py_INCREF(Py_None);
return Py_None;
}
static char O_writelines__doc__[] =
"writelines(sequence_of_strings): write each string";
static PyObject *
O_writelines(Oobject *self, PyObject *args) {
PyObject *tmp = 0;
static PyObject *string_joinfields = 0;
UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
if (!string_joinfields) {
UNLESS (tmp = PyImport_ImportModule("string")) return NULL;
string_joinfields=PyObject_GetAttrString(tmp, "joinfields");
Py_DECREF(tmp);
UNLESS (string_joinfields) return NULL;
}
if (PyObject_Length(args) < 0) return NULL;
tmp = PyObject_CallFunction(string_joinfields, "Os", args, "");
UNLESS (tmp) return NULL;
args = Py_BuildValue("(O)", tmp);
Py_DECREF(tmp);
UNLESS (args) return NULL;
tmp = O_write(self, args);
Py_DECREF(args);
return tmp;
}
static struct PyMethodDef O_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-write StringIO specific methods: */
{"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
{"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
{"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
{NULL, NULL} /* sentinel */
};
static void
O_dealloc(Oobject *self) {
if (self->buf != NULL)
free(self->buf);
PyMem_DEL(self);
}
static PyObject *
O_getattr(Oobject *self, char *name) {
if (strcmp(name, "softspace") == 0) {
return PyInt_FromLong(self->softspace);
}
return Py_FindMethod(O_methods, (PyObject *)self, name);
}
static int
O_setattr(Oobject *self, char *name, PyObject *value) {
long x;
if (strcmp(name, "softspace") != 0) {
PyErr_SetString(PyExc_AttributeError, name);
return -1;
}
x = PyInt_AsLong(value);
if (x < 0 && PyErr_Occurred())
return -1;
self->softspace = x;
return 0;
}
static char Otype__doc__[] =
"Simple type for output to strings."
;
static PyTypeObject Otype = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"StringO", /*tp_name*/
sizeof(Oobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)O_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)O_getattr, /*tp_getattr*/
(setattrfunc)O_setattr, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Otype__doc__ /* Documentation string */
};
static PyObject *
newOobject(int size) {
Oobject *self;
self = PyObject_NEW(Oobject, &Otype);
if (self == NULL)
return NULL;
self->pos=0;
self->string_size = 0;
self->softspace = 0;
UNLESS (self->buf=malloc(size*sizeof(char))) {
PyErr_SetString(PyExc_MemoryError,"out of memory");
self->buf_size = 0;
return NULL;
}
self->buf_size=size;
return (PyObject*)self;
}
/* End of code for StringO objects */
/* -------------------------------------------------------- */
static PyObject *
I_close(Iobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Py_XDECREF(self->pbuf);
self->pbuf = NULL;
self->buf = NULL;
self->pos = self->string_size = 0;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
I_seek(Iobject *self, PyObject *args) {
int position, mode = 0;
UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
return NULL;
if (mode == 2) position += self->string_size;
else if (mode == 1) position += self->pos;
if (position < 0) position=0;
self->pos=position;
Py_INCREF(Py_None);
return Py_None;
}
static struct PyMethodDef I_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-only StringIO specific methods: */
{"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
{"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
{NULL, NULL}
};
static void
I_dealloc(Iobject *self) {
Py_XDECREF(self->pbuf);
PyMem_DEL(self);
}
static PyObject *
I_getattr(Iobject *self, char *name) {
return Py_FindMethod(I_methods, (PyObject *)self, name);
}
static char Itype__doc__[] =
"Simple type for treating strings as input file streams"
;
static PyTypeObject Itype = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"StringI", /*tp_name*/
sizeof(Iobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)I_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)I_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Itype__doc__ /* Documentation string */
};
static PyObject *
newIobject(PyObject *s) {
Iobject *self;
char *buf;
int size;
if (!PyString_Check(s)) {
PyErr_Format(PyExc_TypeError, "expected string, %.200s found",
s->ob_type->tp_name);
return NULL;
}
buf = PyString_AS_STRING(s);
size = PyString_GET_SIZE(s);
UNLESS (self = PyObject_NEW(Iobject, &Itype)) return NULL;
Py_INCREF(s);
self->buf=buf;
self->string_size=size;
self->pbuf=s;
self->pos=0;
return (PyObject*)self;
}
/* End of code for StringI objects */
/* -------------------------------------------------------- */
static char IO_StringIO__doc__[] =
"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
;
static PyObject *
IO_StringIO(PyObject *self, PyObject *args) {
PyObject *s=0;
if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
if (s) return newIobject(s);
return newOobject(128);
}
/* List of methods defined in the module */
static struct PyMethodDef IO_methods[] = {
{"StringIO", (PyCFunction)IO_StringIO,
METH_VARARGS, IO_StringIO__doc__},
{NULL, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initcStringIO) */
static struct PycStringIO_CAPI CAPI = {
IO_cread,
IO_creadline,
O_cwrite,
IO_cgetval,
newOobject,
newIobject,
&Itype,
&Otype,
};
#ifndef DL_EXPORT /* declarations for DLL import/export */
#define DL_EXPORT(RTYPE) RTYPE
#endif
DL_EXPORT(void)
initcStringIO(void) {
PyObject *m, *d, *v;
/* Create the module and add the functions */
m = Py_InitModule4("cStringIO", IO_methods,
cStringIO_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
/* Export C API */
Itype.ob_type=&PyType_Type;
Otype.ob_type=&PyType_Type;
PyDict_SetItemString(d,"cStringIO_CAPI",
v = PyCObject_FromVoidPtr(&CAPI,NULL));
Py_XDECREF(v);
/* Export Types */
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
/* Maybe make certain warnings go away */
if (0) PycString_IMPORT;
}
#ifndef CSTRINGIO_INCLUDED
#define CSTRINGIO_INCLUDED
/*
$Id: cStringIO.h,v 1.5 2002/08/26 13:40:29 Brian Exp $
cStringIO C API
Copyright
Copyright 1996 Zope Corporation, Fredericksburg, Virginia 22401
U.S.A. All
rights reserved. Copyright in this software is owned by ZC,
unless otherwise indicated. Permission to use, copy and
distribute this software is hereby granted, provided that the
above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear. Note that
any product, process or technology described in this software
may be the subject of other Intellectual Property rights
reserved by Zope Corporation and are not licensed
hereunder.
Trademarks
Zope Corporation is a trademark of Zope Corporation.
All other trademarks are owned by their respective companies.
No Warranty
The software is provided "as is" without warranty of any kind,
either express or implied, including, but not limited to, the
implied warranties of merchantability, fitness for a particular
purpose, or non-infringement. This software could include
technical inaccuracies or typographical errors. Changes are
periodically made to the software; these changes will be
incorporated in new editions of the software. Zope Corporation
may make improvements and/or changes in this software at any time
without notice.
Limitation Of Liability
In no event will Zope Corporation be liable for direct, indirect,
special,
incidental, economic, cover, or consequential damages arising
out of the use of or inability to use this software even if
advised of the possibility of such damages. Some states do not
allow the exclusion or limitation of implied warranties or
limitation of liability for incidental or consequential
damages, so the above limitation or exclusion may not apply to
you.
If you have questions regarding this software,
contact:
Zope Corporation
info@zope.com
(540) 371-6909
This header provides access to cStringIO objects from C.
Functions are provided for calling cStringIO objects and
macros are provided for testing whether you have cStringIO
objects.
Before calling any of the functions or macros, you must initialize
the routines with:
PycStringIO_IMPORT
This would typically be done in your init function.
*/
/* Basic fuctions to manipulate cStringIO objects from C */
static struct PycStringIO_CAPI {
/* Read a string. If the last argument is -1, the remainder will be read. */
int(*cread) Py_FPROTO((PyObject *, char **, int));
/* Read a line */
int(*creadline) Py_FPROTO((PyObject *, char **));
/* Write a string */
int(*cwrite) Py_FPROTO((PyObject *, char *, int));
/* Get the cStringIO object as a Python string */
PyObject *(*cgetvalue) Py_FPROTO((PyObject *));
/* Create a new output object */
PyObject *(*NewOutput) Py_FPROTO((int));
/* Create an input object from a Python string */
PyObject *(*NewInput) Py_FPROTO((PyObject *));
/* The Python types for cStringIO input and output objects.
Note that you can do input on an output object.
*/
PyTypeObject *InputType, *OutputType;
} * PycStringIO = NULL;
/* These can be used to test if you have one */
#define PycStringIO_InputCheck(O) \
((O)->ob_type==PycStringIO->InputType)
#define PycStringIO_OutputCheck(O) \
((O)->ob_type==PycStringIO->OutputType)
static void *
xxxPyCObject_Import(module_name, name)
char *module_name;
char *name;
{
PyObject *m, *c;
void *r=NULL;
if((m=PyImport_ImportModule(module_name)))
{
if((c=PyObject_GetAttrString(m,name)))
{
r=PyCObject_AsVoidPtr(c);
Py_DECREF(c);
}
Py_DECREF(m);
}
return r;
}
#define PycString_IMPORT \
PycStringIO=xxxPyCObject_Import("cStringIO", "cStringIO_CAPI")
#endif /* CSTRINGIO_INCLUDED */
# Helper to provide extensibility for pickle/cPickle.
dispatch_table = {}
safe_constructors = {}
def pickle(ob_type, pickle_function, constructor_ob = None):
dispatch_table[ob_type] = pickle_function
if constructor_ob is not None:
constructor(constructor_ob)
def constructor(object):
safe_constructors[object] = 1
# Example: provide pickling support for complex numbers.
def pickle_complex(c):
return complex, (c.real, c.imag)
pickle(type(1j), pickle_complex, complex)
Setup
Makefile.pre.in
cPickle.stx
cPickle.html
cPickle.c
cStringIO.c
cStringIO.h
copy_reg.py
#!/bin/sh
R=$1
M=cPickle
StructuredText < $M.stx > $M.html
rm -rf "$M-$R"
mkdir "$M-$R"
tar -c -R release.fl -f - | (cd "$M-$R"; tar xvf -)
tar cvf - "$M-$R" | gzip > "$M-$R.tar.gz"
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