Commit 74256dc1 authored by Boxiang Sun's avatar Boxiang Sun

Original implementation.

parents
Changelog
=========
2.13.0 (2010-03-30)
-------------------
- Released as separate package.
Metadata-Version: 1.0
Name: Record
Version: 2.13.0
Summary: Special Record objects used in Zope2.
Home-page: http://pypi.python.org/pypi/Record
Author: Zope Corporation and Contributors
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: Overview
========
Record provides special objects used in some Zope2 internals like ZRDB.
Changelog
=========
2.13.0 (2010-03-30)
-------------------
- Released as separate package.
Platform: UNKNOWN
Overview
========
Record provides special objects used in some Zope2 internals like ZRDB.
##############################################################################
#
# Copyright (c) 2006 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Bootstrap a buildout-based project
Simply run this script in a directory containing a buildout.cfg.
The script accepts buildout command-line options, so you can
use the -c option to specify an alternate configuration file.
$Id: bootstrap.py 108946 2010-02-12 02:40:18Z yusei $
"""
import os, shutil, sys, tempfile, urllib2
from optparse import OptionParser
tmpeggs = tempfile.mkdtemp()
is_jython = sys.platform.startswith('java')
# parsing arguments
parser = OptionParser()
parser.add_option("-v", "--version", dest="version",
help="use a specific zc.buildout version")
parser.add_option("-d", "--distribute",
action="store_true", dest="distribute", default=False,
help="Use Distribute rather than Setuptools.")
parser.add_option("-c", None, action="store", dest="config_file",
help=("Specify the path to the buildout configuration "
"file to be used."))
options, args = parser.parse_args()
# if -c was provided, we push it back into args for buildout' main function
if options.config_file is not None:
args += ['-c', options.config_file]
if options.version is not None:
VERSION = '==%s' % options.version
else:
VERSION = ''
USE_DISTRIBUTE = options.distribute
args = args + ['bootstrap']
to_reload = False
try:
import pkg_resources
if not hasattr(pkg_resources, '_distribute'):
to_reload = True
raise ImportError
except ImportError:
ez = {}
if USE_DISTRIBUTE:
exec urllib2.urlopen('http://python-distribute.org/distribute_setup.py'
).read() in ez
ez['use_setuptools'](to_dir=tmpeggs, download_delay=0, no_fake=True)
else:
exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
).read() in ez
ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
if to_reload:
reload(pkg_resources)
else:
import pkg_resources
if sys.platform == 'win32':
def quote(c):
if ' ' in c:
return '"%s"' % c # work around spawn lamosity on windows
else:
return c
else:
def quote (c):
return c
cmd = 'from setuptools.command.easy_install import main; main()'
ws = pkg_resources.working_set
if USE_DISTRIBUTE:
requirement = 'distribute'
else:
requirement = 'setuptools'
if is_jython:
import subprocess
assert subprocess.Popen([sys.executable] + ['-c', quote(cmd), '-mqNxd',
quote(tmpeggs), 'zc.buildout' + VERSION],
env=dict(os.environ,
PYTHONPATH=
ws.find(pkg_resources.Requirement.parse(requirement)).location
),
).wait() == 0
else:
assert os.spawnle(
os.P_WAIT, sys.executable, quote (sys.executable),
'-c', quote (cmd), '-mqNxd', quote (tmpeggs), 'zc.buildout' + VERSION,
dict(os.environ,
PYTHONPATH=
ws.find(pkg_resources.Requirement.parse(requirement)).location
),
) == 0
ws.add_entry(tmpeggs)
ws.require('zc.buildout' + VERSION)
import zc.buildout.buildout
zc.buildout.buildout.main(args)
shutil.rmtree(tmpeggs)
[buildout]
develop = .
parts = interpreter test
[interpreter]
recipe = zc.recipe.egg
interpreter = python
eggs = Record
[test]
recipe = zc.recipe.testrunner
eggs = Record
/*****************************************************************************
Copyright (c) 1996-2002 Zope Corporation and Contributors.
All Rights Reserved.
This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE
****************************************************************************/
/*
$Id: ExtensionClass.h 40218 2005-11-18 14:39:19Z andreasjung $
Extension Class Definitions
Implementing base extension classes
A base extension class is implemented in much the same way that an
extension type is implemented, except:
- The include file, 'ExtensionClass.h', must be included.
- The type structure is declared to be of type
'PyExtensionClass', rather than of type 'PyTypeObject'.
- The type structure has an additional member that must be defined
after the documentation string. This extra member is a method chain
('PyMethodChain') containing a linked list of method definition
('PyMethodDef') lists. Method chains can be used to implement
method inheritance in C. Most extensions don't use method chains,
but simply define method lists, which are null-terminated arrays
of method definitions. A macro, 'METHOD_CHAIN' is defined in
'ExtensionClass.h' that converts a method list to a method chain.
(See the example below.)
- Module functions that create new instances must be replaced by an
'__init__' method that initializes, but does not create storage for
instances.
- The extension class must be initialized and exported to the module
with::
PyExtensionClass_Export(d,"name",type);
where 'name' is the module name and 'type' is the extension class
type object.
Attribute lookup
Attribute lookup is performed by calling the base extension class
'getattr' operation for the base extension class that includes C
data, or for the first base extension class, if none of the base
extension classes include C data. 'ExtensionClass.h' defines a
macro 'Py_FindAttrString' that can be used to find an object's
attributes that are stored in the object's instance dictionary or
in the object's class or base classes::
v = Py_FindAttrString(self,name);
In addition, a macro is provided that replaces 'Py_FindMethod'
calls with logic to perform the same sort of lookup that is
provided by 'Py_FindAttrString'.
Linking
The extension class mechanism was designed to be useful with
dynamically linked extension modules. Modules that implement
extension classes do not have to be linked against an extension
class library. The macro 'PyExtensionClass_Export' imports the
'ExtensionClass' module and uses objects imported from this module
to initialize an extension class with necessary behavior.
*/
#ifndef EXTENSIONCLASS_H
#define EXTENSIONCLASS_H
#include "Python.h"
#include "import.h"
/* Declarations for objects of type ExtensionClass */
#define EC PyTypeObject
#define PyExtensionClass PyTypeObject
#define EXTENSIONCLASS_BINDABLE_FLAG 1 << 2
#define EXTENSIONCLASS_NOINSTDICT_FLAG 1 << 5
typedef struct {
PyObject_HEAD
} _emptyobject;
static struct ExtensionClassCAPIstruct {
/*****************************************************************************
WARNING: THIS STRUCT IS PRIVATE TO THE EXTENSION CLASS INTERFACE
IMPLEMENTATION AND IS SUBJECT TO CHANGE !!!
*****************************************************************************/
PyObject *(*EC_findiattrs_)(PyObject *self, char *cname);
int (*PyExtensionClass_Export_)(PyObject *dict, char *name,
PyTypeObject *typ);
PyObject *(*PyECMethod_New_)(PyObject *callable, PyObject *inst);
PyExtensionClass *ECBaseType_;
PyExtensionClass *ECExtensionClassType_;
} *PyExtensionClassCAPI = NULL;
#define ECBaseType (PyExtensionClassCAPI->ECBaseType_)
#define ECExtensionClassType (PyExtensionClassCAPI->ECExtensionClassType_)
/* Following are macros that are needed or useful for defining extension
classes:
*/
/* This macro redefines Py_FindMethod to do attribute for an attribute
name given by a C string lookup using extension class meta-data.
This is used by older getattr implementations.
This macro is used in base class implementations of tp_getattr to
lookup methods or attributes that are not managed by the base type
directly. The macro is generally used to search for attributes
after other attribute searches have failed.
Note that in Python 1.4, a getattr operation may be provided that
uses an object argument. Classes that support this new operation
should use Py_FindAttr.
*/
#define EC_findiattrs (PyExtensionClassCAPI->EC_findiattrs_)
#define Py_FindMethod(M,SELF,NAME) (EC_findiattrs((SELF),(NAME)))
/* Do method or attribute lookup for an attribute name given by a C
string using extension class meta-data.
This macro is used in base class implementations of tp_getattro to
lookup methods or attributes that are not managed by the base type
directly. The macro is generally used to search for attributes
after other attribute searches have failed.
Note that in Python 1.4, a getattr operation may be provided that
uses an object argument. Classes that support this new operation
should use Py_FindAttr.
*/
#define Py_FindAttrString(SELF,NAME) (EC_findiattrs((SELF),(NAME)))
/* Do method or attribute lookup using extension class meta-data.
This macro is used in base class implementations of tp_getattr to
lookup methods or attributes that are not managed by the base type
directly. The macro is generally used to search for attributes
after other attribute searches have failed. */
#define Py_FindAttr (ECBaseType->tp_getattro)
/* Do attribute assignment for an attribute.
This macro is used in base class implementations of tp_setattro to
set attributes that are not managed by the base type directly. The
macro is generally used to assign attributes after other attribute
attempts to assign attributes have failed.
*/
#define PyEC_SetAttr(SELF,NAME,V) (ECBaseType->tp_setattro(SELF, NAME, V))
/* Convert a method list to a method chain. */
#define METHOD_CHAIN(DEF) (traverseproc)(DEF)
/* The following macro checks whether a type is an extension class: */
#define PyExtensionClass_Check(TYPE) \
PyObject_TypeCheck((PyObject*)(TYPE), ECExtensionClassType)
/* The following macro checks whether an instance is an extension instance: */
#define PyExtensionInstance_Check(INST) \
PyObject_TypeCheck(((PyObject*)(INST))->ob_type, ECExtensionClassType)
#define CHECK_FOR_ERRORS(MESS)
/* The following macro can be used to define an extension base class
that only provides method and that is used as a pure mix-in class. */
#define PURE_MIXIN_CLASS(NAME,DOC,METHODS) \
static PyExtensionClass NAME ## Type = { PyObject_HEAD_INIT(NULL) 0, # NAME, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0 , DOC, (traverseproc)METHODS, }
/* The following macros provide limited access to extension-class
method facilities. */
/* Test for an ExtensionClass method: */
#define PyECMethod_Check(O) PyMethod_Check((O))
/* Create a method object that wraps a callable object and an
instance. Note that if the callable object is an extension class
method, then the new method will wrap the callable object that is
wrapped by the extension class method. Also note that if the
callable object is an extension class method with a reference
count of 1, then the callable object will be rebound to the
instance and returned with an incremented reference count.
*/
#define PyECMethod_New(CALLABLE, INST) \
PyExtensionClassCAPI->PyECMethod_New_((CALLABLE),(INST))
/* Return the instance that is bound by an extension class method. */
#define PyECMethod_Self(M) \
(PyMethod_Check((M)) ? ((PyMethodObject*)(M))->im_self : NULL)
/* Check whether an object has an __of__ method for returning itself
in the context of it's container. */
#define has__of__(O) (PyObject_TypeCheck((O)->ob_type, ECExtensionClassType) \
&& (O)->ob_type->tp_descr_get != NULL)
/* The following macros are used to check whether an instance
or a class' instanses have instance dictionaries: */
#define HasInstDict(O) (_PyObject_GetDictPtr(O) != NULL)
#define ClassHasInstDict(C) ((C)->tp_dictoffset > 0))
/* Get an object's instance dictionary. Use with caution */
#define INSTANCE_DICT(inst) (_PyObject_GetDictPtr(O))
/* Test whether an ExtensionClass, S, is a subclass of ExtensionClass C. */
#define ExtensionClassSubclass_Check(S,C) PyType_IsSubtype((S), (C))
/* Test whether an ExtensionClass instance , I, is a subclass of
ExtensionClass C. */
#define ExtensionClassSubclassInstance_Check(I,C) PyObject_TypeCheck((I), (C))
/* Export an Extension Base class in a given module dictionary with a
given name and ExtensionClass structure.
*/
#define PyExtensionClass_Export(D,N,T) \
if (! ExtensionClassImported || \
PyExtensionClassCAPI->PyExtensionClass_Export_((D),(N),&(T)) < 0) return;
#define ExtensionClassImported \
((PyExtensionClassCAPI != NULL) || \
(PyExtensionClassCAPI = PyCObject_Import("ExtensionClass","CAPI2")))
/* These are being overridded to use tp_free when used with
new-style classes. This is to allow old extention-class code
to work.
*/
#undef PyMem_DEL
#undef PyObject_DEL
#define PyMem_DEL(O) \
if (((O)->ob_type->tp_flags & Py_TPFLAGS_HAVE_CLASS) \
&& ((O)->ob_type->tp_free != NULL)) \
(O)->ob_type->tp_free((PyObject*)(O)); \
else \
PyObject_FREE((O));
#define PyObject_DEL(O) PyMem_DEL(O)
#endif /* EXTENSIONCLASS_H */
/*
Copyright (c) 2003 Zope Corporation and Contributors.
All Rights Reserved.
This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE.
*/
static char _extensionclass_module_documentation[] =
"ExtensionClass\n"
"\n"
"$Id: _ExtensionClass.c 109054 2010-02-14 21:35:34Z hannosch $\n"
;
#include "ExtensionClass/ExtensionClass.h"
#define EC PyTypeObject
static PyObject *str__of__, *str__get__, *str__class_init__, *str__init__;
static PyObject *str__bases__, *str__mro__, *str__new__;
#define OBJECT(O) ((PyObject *)(O))
#define TYPE(O) ((PyTypeObject *)(O))
static PyTypeObject ExtensionClassType;
static PyTypeObject BaseType;
static PyObject *
of_get(PyObject *self, PyObject *inst, PyObject *cls)
{
/* Descriptor slot function that calls __of__ */
if (inst && PyExtensionInstance_Check(inst))
return PyObject_CallMethodObjArgs(self, str__of__, inst, NULL);
Py_INCREF(self);
return self;
}
PyObject *
Base_getattro(PyObject *obj, PyObject *name)
{
/* This is a modified copy of PyObject_GenericGetAttr.
See the change note below. */
PyTypeObject *tp = obj->ob_type;
PyObject *descr = NULL;
PyObject *res = NULL;
descrgetfunc f;
long dictoffset;
PyObject **dictptr;
if (!PyString_Check(name)){
#ifdef Py_USING_UNICODE
/* The Unicode to string conversion is done here because the
existing tp_setattro slots expect a string object as name
and we wouldn't want to break those. */
if (PyUnicode_Check(name)) {
name = PyUnicode_AsEncodedString(name, NULL, NULL);
if (name == NULL)
return NULL;
}
else
#endif
{
PyErr_SetString(PyExc_TypeError,
"attribute name must be string");
return NULL;
}
}
else
Py_INCREF(name);
if (tp->tp_dict == NULL) {
if (PyType_Ready(tp) < 0)
goto done;
}
#if !defined(Py_TPFLAGS_HAVE_VERSION_TAG)
/* Inline _PyType_Lookup */
/* this is not quite _PyType_Lookup anymore */
{
int i, n;
PyObject *mro, *base, *dict;
/* Look in tp_dict of types in MRO */
mro = tp->tp_mro;
assert(mro != NULL);
assert(PyTuple_Check(mro));
n = PyTuple_GET_SIZE(mro);
for (i = 0; i < n; i++) {
base = PyTuple_GET_ITEM(mro, i);
if (PyClass_Check(base))
dict = ((PyClassObject *)base)->cl_dict;
else {
assert(PyType_Check(base));
dict = ((PyTypeObject *)base)->tp_dict;
}
assert(dict && PyDict_Check(dict));
descr = PyDict_GetItem(dict, name);
if (descr != NULL)
break;
}
}
#else
descr = _PyType_Lookup(tp, name);
#endif
Py_XINCREF(descr);
f = NULL;
if (descr != NULL &&
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
f = descr->ob_type->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
res = f(descr, obj, (PyObject *)obj->ob_type);
Py_DECREF(descr);
goto done;
}
}
/* Inline _PyObject_GetDictPtr */
dictoffset = tp->tp_dictoffset;
if (dictoffset != 0) {
PyObject *dict;
if (dictoffset < 0) {
int tsize;
size_t size;
tsize = ((PyVarObject *)obj)->ob_size;
if (tsize < 0)
tsize = -tsize;
size = _PyObject_VAR_SIZE(tp, tsize);
dictoffset += (long)size;
assert(dictoffset > 0);
assert(dictoffset % SIZEOF_VOID_P == 0);
}
dictptr = (PyObject **) ((char *)obj + dictoffset);
dict = *dictptr;
if (dict != NULL) {
Py_INCREF(dict);
res = PyDict_GetItem(dict, name);
if (res != NULL) {
Py_INCREF(res);
Py_XDECREF(descr);
Py_DECREF(dict);
/* CHANGED!
If the tp_descr_get of res is of_get,
then call it. */
if (PyObject_TypeCheck(res->ob_type,
&ExtensionClassType)
&& res->ob_type->tp_descr_get != NULL) {
PyObject *tres;
tres = res->ob_type->tp_descr_get(
res, obj,
OBJECT(obj->ob_type));
Py_DECREF(res);
res = tres;
}
goto done;
}
Py_DECREF(dict);
}
}
if (f != NULL) {
res = f(descr, obj, (PyObject *)obj->ob_type);
Py_DECREF(descr);
goto done;
}
if (descr != NULL) {
res = descr;
/* descr was already increfed above */
goto done;
}
/* CHANGED: Just use the name. Don't format. */
PyErr_SetObject(PyExc_AttributeError, name);
done:
Py_DECREF(name);
return res;
}
#include "pickle/pickle.c"
static struct PyMethodDef Base_methods[] = {
PICKLE_METHODS
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
static EC BaseType = {
PyObject_HEAD_INIT(NULL)
/* ob_size */ 0,
/* tp_name */ "ExtensionClass."
"Base",
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* tp_getattro */ (getattrofunc)Base_getattro,
0, 0,
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
| Py_TPFLAGS_HAVE_VERSION_TAG
#endif
),
"Standard ExtensionClass base type",
0, 0, 0, 0, 0, 0,
Base_methods,
};
static EC NoInstanceDictionaryBaseType = {
PyObject_HEAD_INIT(NULL)
/* ob_size */ 0,
/* tp_name */ "ExtensionClass."
"NoInstanceDictionaryBase",
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
| Py_TPFLAGS_HAVE_VERSION_TAG
#endif
),
"Base types for subclasses without instance dictionaries",
};
static PyObject *
EC_new(PyTypeObject *self, PyObject *args, PyObject *kw)
{
PyObject *name, *bases=NULL, *dict=NULL;
PyObject *new_bases=NULL, *new_args, *result;
int have_base = 0, i;
if (kw && PyObject_IsTrue(kw))
{
PyErr_SetString(PyExc_TypeError,
"Keyword arguments are not supported");
return NULL;
}
if (!PyArg_ParseTuple(args, "O|O!O!", &name,
&PyTuple_Type, &bases, &PyDict_Type, &dict))
return NULL;
/* Make sure Base is in bases */
if (bases)
{
for (i = 0; i < PyTuple_GET_SIZE(bases); i++)
{
if (PyObject_TypeCheck(PyTuple_GET_ITEM(bases, i),
&ExtensionClassType))
{
have_base = 1;
break;
}
}
if (! have_base)
{
new_bases = PyTuple_New(PyTuple_GET_SIZE(bases) + 1);
if (new_bases == NULL)
return NULL;
for (i = 0; i < PyTuple_GET_SIZE(bases); i++)
{
Py_XINCREF(PyTuple_GET_ITEM(bases, i));
PyTuple_SET_ITEM(new_bases, i, PyTuple_GET_ITEM(bases, i));
}
Py_INCREF(OBJECT(&BaseType));
PyTuple_SET_ITEM(new_bases, PyTuple_GET_SIZE(bases),
OBJECT(&BaseType));
}
}
else
{
new_bases = Py_BuildValue("(O)", &BaseType);
if (new_bases == NULL)
return NULL;
}
if (new_bases)
{
if (dict)
new_args = Py_BuildValue("OOO", name, new_bases, dict);
else
new_args = Py_BuildValue("OO", name, new_bases);
Py_DECREF(new_bases);
if (new_args == NULL)
return NULL;
result = PyType_Type.tp_new(self, new_args, kw);
Py_DECREF(new_args);
}
else
{
result = PyType_Type.tp_new(self, args, kw);
/* We didn't have to add Base, so maybe NoInstanceDictionaryBase
is in the bases. We need to check if it was. If it was, we
need to suppress instance dictionary support. */
for (i = 0; i < PyTuple_GET_SIZE(bases); i++)
{
if (
PyObject_TypeCheck(PyTuple_GET_ITEM(bases, i),
&ExtensionClassType)
&&
PyType_IsSubtype(TYPE(PyTuple_GET_ITEM(bases, i)),
&NoInstanceDictionaryBaseType)
)
{
TYPE(result)->tp_dictoffset = 0;
break;
}
}
}
return result;
}
/* set up __get__, if necessary */
static int
EC_init_of(PyTypeObject *self)
{
PyObject *__of__;
__of__ = PyObject_GetAttr(OBJECT(self), str__of__);
if (__of__)
{
Py_DECREF(__of__);
if (self->tp_descr_get)
{
if (self->tp_descr_get != of_get)
{
PyErr_SetString(PyExc_TypeError,
"Can't mix __of__ and descriptors");
return -1;
}
}
else
self->tp_descr_get = of_get;
}
else
{
PyErr_Clear();
if (self->tp_descr_get == of_get)
self->tp_descr_get = NULL;
}
return 0;
}
static int
EC_init(PyTypeObject *self, PyObject *args, PyObject *kw)
{
PyObject *__class_init__, *r;
if (PyType_Type.tp_init(OBJECT(self), args, kw) < 0)
return -1;
if (self->tp_dict != NULL)
{
r = PyDict_GetItemString(self->tp_dict, "__doc__");
if ((r == Py_None) &&
(PyDict_DelItemString(self->tp_dict, "__doc__") < 0)
)
return -1;
}
if (EC_init_of(self) < 0)
return -1;
/* Call __class_init__ */
__class_init__ = PyObject_GetAttr(OBJECT(self), str__class_init__);
if (__class_init__ == NULL)
{
PyErr_Clear();
return 0;
}
if (! (PyMethod_Check(__class_init__)
&& PyMethod_GET_FUNCTION(__class_init__)
)
)
{
Py_DECREF(__class_init__);
PyErr_SetString(PyExc_TypeError, "Invalid type for __class_init__");
return -1;
}
r = PyObject_CallFunctionObjArgs(PyMethod_GET_FUNCTION(__class_init__),
OBJECT(self), NULL);
Py_DECREF(__class_init__);
if (! r)
return -1;
Py_DECREF(r);
return 0;
}
static int
EC_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
/* We want to allow setting attributes of builti-in types, because
EC did in the past and there's code that relies on it.
We can't really set slots though, but I don't think we need to.
There's no good way to spot slots. We could use a lame rule like
names that begin and end with __s and have just 4 _s smell too
much like slots.
*/
if (! (type->tp_flags & Py_TPFLAGS_HEAPTYPE))
{
char *cname;
int l;
cname = PyString_AsString(name);
if (cname == NULL)
return -1;
l = PyString_GET_SIZE(name);
if (l > 4
&& cname[0] == '_' && cname[1] == '_'
&& cname[l-1] == '_' && cname[l-2] == '_'
)
{
char *c;
c = strchr(cname+2, '_');
if (c != NULL && (c - cname) >= (l-2))
{
PyErr_Format
(PyExc_TypeError,
"can't set attributes of built-in/extension type '%s' if the "
"attribute name begins and ends with __ and contains only "
"4 _ characters",
type->tp_name
);
return -1;
}
}
if (PyObject_GenericSetAttr(OBJECT(type), name, value) < 0)
return -1;
}
else if (PyType_Type.tp_setattro(OBJECT(type), name, value) < 0)
return -1;
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
PyType_Modified(type);
#endif
return 0;
}
static PyObject *
inheritedAttribute(PyTypeObject *self, PyObject *name)
{
int i;
PyObject *d, *cls;
for (i = 1; i < PyTuple_GET_SIZE(self->tp_mro); i++)
{
cls = PyTuple_GET_ITEM(self->tp_mro, i);
if (PyType_Check(cls))
d = ((PyTypeObject *)cls)->tp_dict;
else if (PyClass_Check(cls))
d = ((PyClassObject *)cls)->cl_dict;
else
/* Unrecognized thing, punt */
d = NULL;
if ((d == NULL) || (PyDict_GetItem(d, name) == NULL))
continue;
return PyObject_GetAttr(cls, name);
}
PyErr_SetObject(PyExc_AttributeError, name);
return NULL;
}
static PyObject *
__basicnew__(PyObject *self)
{
return PyObject_CallMethodObjArgs(self, str__new__, self, NULL);
}
static int
append_new(PyObject *result, PyObject *v)
{
int contains;
if (v == OBJECT(&BaseType) || v == OBJECT(&PyBaseObject_Type))
return 0; /* Don't add these until end */
contains = PySequence_Contains(result, v);
if (contains != 0)
return contains;
return PyList_Append(result, v);
}
static int
copy_mro(PyObject *mro, PyObject *result)
{
PyObject *base;
int i, l;
l = PyTuple_Size(mro);
if (l < 0)
return -1;
for (i=0; i < l; i++)
{
base = PyTuple_GET_ITEM(mro, i);
if (append_new(result, base) < 0)
return -1;
}
return 0;
}
static int
copy_classic(PyObject *base, PyObject *result)
{
PyObject *bases, *basebase;
int i, l, err=-1;
if (append_new(result, base) < 0)
return -1;
bases = PyObject_GetAttr(base, str__bases__);
if (bases == NULL)
return -1;
l = PyTuple_Size(bases);
if (l < 0)
goto end;
for (i=0; i < l; i++)
{
basebase = PyTuple_GET_ITEM(bases, i);
if (copy_classic(basebase, result) < 0)
goto end;
}
err = 0;
end:
Py_DECREF(bases);
return err;
}
static PyObject *
mro(PyTypeObject *self)
{
/* Compute an MRO for a class */
PyObject *result, *base, *basemro, *mro=NULL;
int i, l, err;
result = PyList_New(0);
if (result == NULL)
return NULL;
if (PyList_Append(result, OBJECT(self)) < 0)
goto end;
l = PyTuple_Size(self->tp_bases);
if (l < 0)
goto end;
for (i=0; i < l; i++)
{
base = PyTuple_GET_ITEM(self->tp_bases, i);
if (base == NULL)
continue;
basemro = PyObject_GetAttr(base, str__mro__);
if (basemro != NULL)
{
/* Type */
err = copy_mro(basemro, result);
Py_DECREF(basemro);
if (err < 0)
goto end;
}
else
{
PyErr_Clear();
if (copy_classic(base, result) < 0)
goto end;
}
}
if (self != &BaseType && PyList_Append(result, OBJECT(&BaseType)) < 0)
goto end;
if (PyList_Append(result, OBJECT(&PyBaseObject_Type)) < 0)
goto end;
l = PyList_GET_SIZE(result);
mro = PyTuple_New(l);
if (mro == NULL)
goto end;
for (i=0; i < l; i++)
{
Py_INCREF(PyList_GET_ITEM(result, i));
PyTuple_SET_ITEM(mro, i, PyList_GET_ITEM(result, i));
}
end:
Py_DECREF(result);
return mro;
}
static struct PyMethodDef EC_methods[] = {
{"__basicnew__", (PyCFunction)__basicnew__, METH_NOARGS,
"Create a new empty object"},
{"inheritedAttribute", (PyCFunction)inheritedAttribute, METH_O,
"Look up an inherited attribute"},
{"mro", (PyCFunction)mro, METH_NOARGS,
"Compute an mro using the 'encalsulated base' scheme"},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
static PyTypeObject ExtensionClassType = {
PyObject_HEAD_INIT(NULL)
/* ob_size */ 0,
/* tp_name */ "ExtensionClass."
"ExtensionClass",
/* tp_basicsize */ 0,
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor)0,
/* tp_print */ (printfunc)0,
/* tp_getattr */ (getattrfunc)0,
/* tp_setattr */ (setattrfunc)0,
/* tp_compare */ (cmpfunc)0,
/* tp_repr */ (reprfunc)0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ (hashfunc)0,
/* tp_call */ (ternaryfunc)0,
/* tp_str */ (reprfunc)0,
/* tp_getattro */ (getattrofunc)0,
/* tp_setattro */ (setattrofunc)EC_setattro,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_BASETYPE
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
| Py_TPFLAGS_HAVE_VERSION_TAG
#endif
,
/* tp_doc */ "Meta-class for extension classes",
/* tp_traverse */ (traverseproc)0,
/* tp_clear */ (inquiry)0,
/* tp_richcompare */ (richcmpfunc)0,
/* tp_weaklistoffset */ (long)0,
/* tp_iter */ (getiterfunc)0,
/* tp_iternext */ (iternextfunc)0,
/* tp_methods */ EC_methods,
/* tp_members */ 0,
/* tp_getset */ 0,
/* tp_base */ 0,
/* tp_dict */ 0, /* internal use */
/* tp_descr_get */ (descrgetfunc)0,
/* tp_descr_set */ (descrsetfunc)0,
/* tp_dictoffset */ 0,
/* tp_init */ (initproc)EC_init,
/* tp_alloc */ (allocfunc)0,
/* tp_new */ (newfunc)EC_new,
/* tp_free */ 0, /* Low-level free-mem routine */
/* tp_is_gc */ (inquiry)0, /* For PyObject_IS_GC */
};
static PyObject *
debug(PyObject *self, PyObject *o)
{
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
pmc_init_of(PyObject *self, PyObject *args)
{
PyObject *o;
if (! PyArg_ParseTuple(args, "O!", (PyObject *)&ExtensionClassType, &o))
return NULL;
if (EC_init_of((PyTypeObject *)o) < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
/* List of methods defined in the module */
static struct PyMethodDef ec_methods[] = {
{"debug", (PyCFunction)debug, METH_O, ""},
{"pmc_init_of", (PyCFunction)pmc_init_of, METH_VARARGS,
"Initialize __get__ for classes that define __of__"},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
static PyObject *
EC_findiattrs_(PyObject *self, char *cname)
{
PyObject *name, *r;
name = PyString_FromString(cname);
if (name == NULL)
return NULL;
r = ECBaseType->tp_getattro(self, name);
Py_DECREF(name);
return r;
}
static PyObject *
ec_new_for_custom_dealloc(PyTypeObject *type, PyObject *args, PyObject *kw)
{
/* This is for EC's that have deallocs. For these, we need to
incref the type when we create an instance, because the deallocs
will decref the type.
*/
PyObject *r;
r = PyType_GenericNew(type, args, kw);
if (r)
{
Py_INCREF(type);
}
return r;
}
static int
ec_init(PyObject *self, PyObject *args, PyObject *kw)
{
PyObject *r, *__init__;
__init__ = PyObject_GetAttr(self, str__init__);
if (__init__ == NULL)
return -1;
r = PyObject_Call(__init__, args, kw);
Py_DECREF(__init__);
if (r == NULL)
return -1;
Py_DECREF(r);
return 0;
}
static int
PyExtensionClass_Export_(PyObject *dict, char *name, PyTypeObject *typ)
{
long ecflags = 0;
PyMethodDef *pure_methods = NULL, *mdef = NULL;
PyObject *m;
if (typ->tp_flags == 0)
{
/* Old-style EC */
if (typ->tp_traverse)
{
/* ExtensionClasses stick there methods in the tp_traverse slot */
mdef = (PyMethodDef *)typ->tp_traverse;
if (typ->tp_basicsize <= sizeof(_emptyobject))
/* Pure mixin. We want rebindable methods */
pure_methods = mdef;
else
typ->tp_methods = mdef;
typ->tp_traverse = NULL;
/* Look for __init__ method */
for (; mdef->ml_name; mdef++)
{
if (strcmp(mdef->ml_name, "__init__") == 0)
{
/* we have an old-style __init__, install a special slot */
typ->tp_init = ec_init;
break;
}
}
}
if (typ->tp_clear)
{
/* ExtensionClasses stick there flags in the tp_clear slot */
ecflags = (long)(typ->tp_clear);
/* Some old-style flags were set */
if ((ecflags & EXTENSIONCLASS_BINDABLE_FLAG)
&& typ->tp_descr_get == NULL)
/* We have __of__-style binding */
typ->tp_descr_get = of_get;
}
typ->tp_clear = NULL;
typ->tp_flags = Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE;
if (typ->tp_dealloc != NULL)
typ->tp_new = ec_new_for_custom_dealloc;
}
typ->ob_type = ECExtensionClassType;
if (ecflags & EXTENSIONCLASS_NOINSTDICT_FLAG)
typ->tp_base = &NoInstanceDictionaryBaseType;
else
typ->tp_base = &BaseType;
if (typ->tp_new == NULL)
typ->tp_new = PyType_GenericNew;
if (PyType_Ready(typ) < 0)
return -1;
if (pure_methods)
{
/* We had pure methods. We want to be able to rebind these, so
we'll make them ordinary method wrappers around method descrs
*/
for (; pure_methods->ml_name; pure_methods++)
{
m = PyDescr_NewMethod(ECBaseType, pure_methods);
if (! m)
return -1;
m = PyMethod_New((PyObject *)m, NULL, (PyObject *)ECBaseType);
if (! m)
return -1;
if (PyDict_SetItemString(typ->tp_dict, pure_methods->ml_name, m)
< 0)
return -1;
}
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
PyType_Modified(typ);
#endif
}
else if (mdef && mdef->ml_name)
{
/* Blast, we have to stick __init__ in the dict ourselves
because PyType_Ready probably stuck a wrapper for ec_init in
instead.
*/
m = PyDescr_NewMethod(typ, mdef);
if (! m)
return -1;
if (PyDict_SetItemString(typ->tp_dict, mdef->ml_name, m) < 0)
return -1;
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
PyType_Modified(typ);
#endif
}
if (PyMapping_SetItemString(dict, name, (PyObject*)typ) < 0)
return -1;
return 0;
}
PyObject *
PyECMethod_New_(PyObject *callable, PyObject *inst)
{
if (! PyExtensionInstance_Check(inst))
{
PyErr_SetString(PyExc_TypeError,
"Can't bind non-ExtensionClass instance.");
return NULL;
}
if (PyMethod_Check(callable))
{
if (callable->ob_refcnt == 1)
{
Py_XDECREF(((PyMethodObject*)callable)->im_self);
Py_INCREF(inst);
((PyMethodObject*)callable)->im_self = inst;
Py_INCREF(callable);
return callable;
}
else
return callable->ob_type->tp_descr_get(
callable, inst,
((PyMethodObject*)callable)->im_class);
}
else
return PyMethod_New(callable, inst, (PyObject*)(ECBaseType));
}
static struct ExtensionClassCAPIstruct
TrueExtensionClassCAPI = {
EC_findiattrs_,
PyExtensionClass_Export_,
PyECMethod_New_,
&BaseType,
&ExtensionClassType,
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
init_ExtensionClass(void)
{
PyObject *m, *s;
if (pickle_setup() < 0)
return;
#define DEFINE_STRING(S) \
if(! (str ## S = PyString_FromString(# S))) return
DEFINE_STRING(__of__);
DEFINE_STRING(__get__);
DEFINE_STRING(__class_init__);
DEFINE_STRING(__init__);
DEFINE_STRING(__bases__);
DEFINE_STRING(__mro__);
DEFINE_STRING(__new__);
#undef DEFINE_STRING
PyExtensionClassCAPI = &TrueExtensionClassCAPI;
ExtensionClassType.ob_type = &PyType_Type;
ExtensionClassType.tp_base = &PyType_Type;
ExtensionClassType.tp_traverse = PyType_Type.tp_traverse;
ExtensionClassType.tp_clear = PyType_Type.tp_clear;
/* Initialize types: */
if (PyType_Ready(&ExtensionClassType) < 0)
return;
BaseType.ob_type = &ExtensionClassType;
BaseType.tp_base = &PyBaseObject_Type;
BaseType.tp_new = PyType_GenericNew;
if (PyType_Ready(&BaseType) < 0)
return;
NoInstanceDictionaryBaseType.ob_type = &ExtensionClassType;
NoInstanceDictionaryBaseType.tp_base = &BaseType;
NoInstanceDictionaryBaseType.tp_new = PyType_GenericNew;
if (PyType_Ready(&NoInstanceDictionaryBaseType) < 0)
return;
/* Create the module and add the functions */
m = Py_InitModule3("_ExtensionClass", ec_methods,
_extensionclass_module_documentation);
if (m == NULL)
return;
s = PyCObject_FromVoidPtr(PyExtensionClassCAPI, NULL);
if (PyModule_AddObject(m, "CAPI2", s) < 0)
return;
/* Add types: */
if (PyModule_AddObject(m, "ExtensionClass",
(PyObject *)&ExtensionClassType) < 0)
return;
if (PyModule_AddObject(m, "Base", (PyObject *)&BaseType) < 0)
return;
if (PyModule_AddObject(m, "NoInstanceDictionaryBase",
(PyObject *)&NoInstanceDictionaryBaseType) < 0)
return;
}
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""ExtensionClass
Extension Class exists to support types derived from the old ExtensionType
meta-class that preceeded Python 2.2 and new-style classes.
As a meta-class, ExtensionClass provides the following features:
- Support for a class initialiser:
>>> from ExtensionClass import ExtensionClass, Base
>>> class C(Base):
... def __class_init__(self):
... print 'class init called'
... print self.__name__
... def bar(self):
... return 'bar called'
class init called
C
>>> c = C()
>>> int(c.__class__ is C)
1
>>> int(c.__class__ is type(c))
1
- Making sure that every instance of the meta-class has Base as a base class:
>>> class X:
... __metaclass__ = ExtensionClass
>>> Base in X.__mro__
1
- Provide an inheritedAttribute method for looking up attributes in
base classes:
>>> class C2(C):
... def bar(*a):
... return C2.inheritedAttribute('bar')(*a), 42
class init called
C2
>>> o = C2()
>>> o.bar()
('bar called', 42)
This is for compatability with old code. New code should use super
instead.
The base class, Base, exists mainly to support the __of__ protocol.
The __of__ protocol is similar to __get__ except that __of__ is called
when an implementor is retrieved from an instance as well as from a
class:
>>> class O(Base):
... def __of__(*a):
... return a
>>> o1 = O()
>>> o2 = O()
>>> C.o1 = o1
>>> c.o2 = o2
>>> c.o1 == (o1, c)
1
>>> C.o1 == o1
1
>>> int(c.o2 == (o2, c))
1
We accomplish this by making a class that implements __of__ a
descriptor and treating all descriptor ExtensionClasses this way. That
is, if an extension class is a descriptor, it's __get__ method will be
called even when it is retrieved from an instance.
>>> class O(Base):
... def __get__(*a):
... return a
...
>>> o1 = O()
>>> o2 = O()
>>> C.o1 = o1
>>> c.o2 = o2
>>> int(c.o1 == (o1, c, type(c)))
1
>>> int(C.o1 == (o1, None, type(c)))
1
>>> int(c.o2 == (o2, c, type(c)))
1
$Id: __init__.py 40218 2005-11-18 14:39:19Z andreasjung $
"""
from _ExtensionClass import *
/*
Copyright (c) 2003 Zope Corporation and Contributors.
All Rights Reserved.
This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE.
*/
/*
Reusable pickle support code
This is "includeware", meant to be used through a C include
*/
/* It's a dang shame we can't inherit __get/setstate__ from object :( */
static PyObject *str__slotnames__, *copy_reg_slotnames, *__newobj__;
static PyObject *str__getnewargs__, *str__getstate__;
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
static int
pickle_setup(void)
{
PyObject *copy_reg;
int r = -1;
#define DEFINE_STRING(S) \
if(! (str ## S = PyString_FromString(# S))) return -1
DEFINE_STRING(__slotnames__);
DEFINE_STRING(__getnewargs__);
DEFINE_STRING(__getstate__);
#undef DEFINE_STRING
copy_reg = PyImport_ImportModule("copy_reg");
if (copy_reg == NULL)
return -1;
copy_reg_slotnames = PyObject_GetAttrString(copy_reg, "_slotnames");
if (copy_reg_slotnames == NULL)
goto end;
__newobj__ = PyObject_GetAttrString(copy_reg, "__newobj__");
if (__newobj__ == NULL)
goto end;
r = 0;
end:
Py_DECREF(copy_reg);
return r;
}
static PyObject *
pickle_slotnames(PyTypeObject *cls)
{
PyObject *slotnames;
slotnames = PyDict_GetItem(cls->tp_dict, str__slotnames__);
if (slotnames != NULL)
{
Py_INCREF(slotnames);
return slotnames;
}
slotnames = PyObject_CallFunctionObjArgs(copy_reg_slotnames, (PyObject*)cls,
NULL);
if (slotnames != NULL &&
slotnames != Py_None &&
! PyList_Check(slotnames))
{
PyErr_SetString(PyExc_TypeError,
"copy_reg._slotnames didn't return a list or None");
Py_DECREF(slotnames);
slotnames = NULL;
}
return slotnames;
}
static PyObject *
pickle_copy_dict(PyObject *state)
{
PyObject *copy, *key, *value;
char *ckey;
Py_ssize_t pos = 0;
Py_ssize_t nr;
copy = PyDict_New();
if (copy == NULL)
return NULL;
if (state == NULL)
return copy;
while ((nr = PyDict_Next(state, &pos, &key, &value)))
{
if (nr < 0)
goto err;
if (key && PyString_Check(key))
{
ckey = PyString_AS_STRING(key);
if (*ckey == '_' &&
(ckey[1] == 'v' || ckey[1] == 'p') &&
ckey[2] == '_')
/* skip volatile and persistent */
continue;
}
if (key != NULL && value != NULL &&
(PyObject_SetItem(copy, key, value) < 0)
)
goto err;
}
return copy;
err:
Py_DECREF(copy);
return NULL;
}
static char pickle___getstate__doc[] =
"Get the object serialization state\n"
"\n"
"If the object has no assigned slots and has no instance dictionary, then \n"
"None is returned.\n"
"\n"
"If the object has no assigned slots and has an instance dictionary, then \n"
"the a copy of the instance dictionary is returned. The copy has any items \n"
"with names starting with '_v_' or '_p_' ommitted.\n"
"\n"
"If the object has assigned slots, then a two-element tuple is returned. \n"
"The first element is either None or a copy of the instance dictionary, \n"
"as described above. The second element is a dictionary with items \n"
"for each of the assigned slots.\n"
;
static PyObject *
pickle___getstate__(PyObject *self)
{
PyObject *slotnames=NULL, *slots=NULL, *state=NULL;
PyObject **dictp;
int n=0;
slotnames = pickle_slotnames(self->ob_type);
if (slotnames == NULL)
return NULL;
dictp = _PyObject_GetDictPtr(self);
if (dictp)
state = pickle_copy_dict(*dictp);
else
{
state = Py_None;
Py_INCREF(state);
}
if (slotnames != Py_None)
{
int i;
slots = PyDict_New();
if (slots == NULL)
goto end;
for (i = 0; i < PyList_GET_SIZE(slotnames); i++)
{
PyObject *name, *value;
char *cname;
name = PyList_GET_ITEM(slotnames, i);
if (PyString_Check(name))
{
cname = PyString_AS_STRING(name);
if (*cname == '_' &&
(cname[1] == 'v' || cname[1] == 'p') &&
cname[2] == '_')
/* skip volatile and persistent */
continue;
}
value = PyObject_GetAttr(self, name);
if (value == NULL)
PyErr_Clear();
else
{
int err = PyDict_SetItem(slots, name, value);
Py_DECREF(value);
if (err)
goto end;
n++;
}
}
}
if (n)
state = Py_BuildValue("(NO)", state, slots);
end:
Py_XDECREF(slotnames);
Py_XDECREF(slots);
return state;
}
static int
pickle_setattrs_from_dict(PyObject *self, PyObject *dict)
{
PyObject *key, *value;
Py_ssize_t pos = 0;
if (! PyDict_Check(dict))
{
PyErr_SetString(PyExc_TypeError, "Expected dictionary");
return -1;
}
while (PyDict_Next(dict, &pos, &key, &value))
{
if (key != NULL && value != NULL &&
(PyObject_SetAttr(self, key, value) < 0)
)
return -1;
}
return 0;
}
static char pickle___setstate__doc[] =
"Set the object serialization state\n"
"\n"
"The state should be in one of 3 forms:\n"
"\n"
"- None\n"
"\n"
" Ignored\n"
"\n"
"- A dictionary\n"
"\n"
" In this case, the object's instance dictionary will be cleared and \n"
" updated with the new state.\n"
"\n"
"- A two-tuple with a string as the first element. \n"
"\n"
" In this case, the method named by the string in the first element will be\n"
" called with the second element.\n"
"\n"
" This form supports migration of data formats.\n"
"\n"
"- A two-tuple with None or a Dictionary as the first element and\n"
" with a dictionary as the second element.\n"
"\n"
" If the first element is not None, then the object's instance dictionary \n"
" will be cleared and updated with the value.\n"
"\n"
" The items in the second element will be assigned as attributes.\n"
;
static PyObject *
pickle___setstate__(PyObject *self, PyObject *state)
{
PyObject *slots=NULL;
if (PyTuple_Check(state))
{
if (! PyArg_ParseTuple(state, "OO", &state, &slots))
return NULL;
}
if (state != Py_None)
{
PyObject **dict;
dict = _PyObject_GetDictPtr(self);
if (dict)
{
if (*dict == NULL)
{
*dict = PyDict_New();
if (*dict == NULL)
return NULL;
}
}
if (*dict != NULL)
{
PyDict_Clear(*dict);
if (PyDict_Update(*dict, state) < 0)
return NULL;
}
else if (pickle_setattrs_from_dict(self, state) < 0)
return NULL;
}
if (slots != NULL && pickle_setattrs_from_dict(self, slots) < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static char pickle___getnewargs__doc[] =
"Get arguments to be passed to __new__\n"
;
static PyObject *
pickle___getnewargs__(PyObject *self)
{
return PyTuple_New(0);
}
static char pickle___reduce__doc[] =
"Reduce an object to contituent parts for serialization\n"
;
static PyObject *
pickle___reduce__(PyObject *self)
{
PyObject *args=NULL, *bargs=0, *state=NULL;
int l, i;
/* we no longer require '__getnewargs__' to be defined but use it if it is */
PyObject *getnewargs=NULL;
getnewargs = PyObject_GetAttr(self, str__getnewargs__);
if (getnewargs)
bargs = PyEval_CallObject(getnewargs, (PyObject *)NULL);
else {
PyErr_Clear();
bargs = PyTuple_New(0);
}
l = PyTuple_Size(bargs);
if (l < 0)
goto end;
args = PyTuple_New(l+1);
if (args == NULL)
goto end;
Py_INCREF(self->ob_type);
PyTuple_SET_ITEM(args, 0, (PyObject*)(self->ob_type));
for (i = 0; i < l; i++)
{
Py_INCREF(PyTuple_GET_ITEM(bargs, i));
PyTuple_SET_ITEM(args, i+1, PyTuple_GET_ITEM(bargs, i));
}
state = PyObject_CallMethodObjArgs(self, str__getstate__, NULL);
if (state == NULL)
goto end;
state = Py_BuildValue("(OON)", __newobj__, args, state);
end:
Py_XDECREF(bargs);
Py_XDECREF(args);
Py_XDECREF(getnewargs);
return state;
}
#define PICKLE_GETSTATE_DEF \
{"__getstate__", (PyCFunction)pickle___getstate__, METH_NOARGS, \
pickle___getstate__doc},
#define PICKLE_SETSTATE_DEF \
{"__setstate__", (PyCFunction)pickle___setstate__, METH_O, \
pickle___setstate__doc},
#define PICKLE_GETNEWARGS_DEF
#define PICKLE_REDUCE_DEF \
{"__reduce__", (PyCFunction)pickle___reduce__, METH_NOARGS, \
pickle___reduce__doc},
#define PICKLE_METHODS PICKLE_GETSTATE_DEF PICKLE_SETSTATE_DEF \
PICKLE_GETNEWARGS_DEF PICKLE_REDUCE_DEF
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: tests.py 109298 2010-02-22 17:30:41Z hannosch $
"""
from ExtensionClass import *
import pickle
def print_dict(d):
d = d.items()
d.sort()
print '{%s}' % (', '.join(
[('%r: %r' % (k, v)) for (k, v) in d]
))
def test_mixing():
"""Test working with a classic class
>>> class Classic:
... def x(self):
... return 42
>>> class O(Base):
... def __of__(*a):
... return a
>>> class O2(Classic, O):
... def __of__(*a):
... return (O2.inheritedAttribute('__of__')(*a),
... O2.inheritedAttribute('x')(a[0]))
>>> class C(Base):
... def __class_init__(self):
... print 'class init called'
... print self.__name__
... def bar(self):
... return 'bar called'
class init called
C
>>> c = C()
>>> o2 = O2()
>>> c.o2 = o2
>>> int(c.o2 == ((o2, c), 42))
1
Test working with a new style
>>> class Modern(object):
... def x(self):
... return 42
>>> class O2(Modern, O):
... def __of__(*a):
... return (O2.inheritedAttribute('__of__')(*a),
... O2.inheritedAttribute('x')(a[0]))
>>> o2 = O2()
>>> c.o2 = o2
>>> int(c.o2 == ((o2, c), 42))
1
"""
def test_class_creation_under_stress():
"""
>>> for i in range(100):
... class B(Base):
... print i,
... if i and i%20 == 0:
... print
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
>>> import gc
>>> x = gc.collect()
"""
def old_test_add():
"""test_add.py from old EC
>>> class foo(Base):
... def __add__(self,other): print 'add called'
>>> foo()+foo()
add called
"""
def proper_error_on_deleattr():
"""
Florent Guillaume wrote:
...
Excellent.
Will it also fix this particularity of ExtensionClass:
>>> class A(Base):
... def foo(self):
... self.gee
... def bar(self):
... del self.gee
>>> a=A()
>>> a.foo()
Traceback (most recent call last):
...
AttributeError: gee
>>> a.bar()
Traceback (most recent call last):
...
AttributeError: 'A' object has no attribute 'gee'
I.e., the fact that KeyError is raised whereas a normal class would
raise AttributeError.
"""
def test_NoInstanceDictionaryBase():
"""
>>> class B(NoInstanceDictionaryBase): pass
...
>>> B().__dict__
Traceback (most recent call last):
...
AttributeError: This object has no __dict__
>>> class B(NoInstanceDictionaryBase):
... __slots__ = ('a', 'b')
...
>>> class BB(B): pass
...
>>> b = BB()
>>> b.__dict__
Traceback (most recent call last):
...
AttributeError: This object has no __dict__
>>> b.a = 1
>>> b.b = 2
>>> b.a
1
>>> b.b
2
"""
def test__basicnew__():
"""
>>> x = Simple.__basicnew__()
>>> x.__dict__
{}
"""
def cmpattrs(self, other, *attrs):
for attr in attrs:
if attr[:3] in ('_v_', '_p_'):
continue
c = cmp(getattr(self, attr, None), getattr(other, attr, None))
if c:
return c
return 0
class Simple(Base):
def __init__(self, name, **kw):
self.__name__ = name
self.__dict__.update(kw)
self._v_favorite_color = 'blue'
self._p_foo = 'bar'
def __cmp__(self, other):
return cmpattrs(self, other, '__class__', *(self.__dict__.keys()))
def test_basic_pickling():
"""
>>> x = Simple('x', aaa=1, bbb='foo')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> print_dict(x.__getstate__())
{'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}
>>> f, (c,), state = x.__reduce__()
>>> f.__name__
'__newobj__'
>>> f.__module__
'copy_reg'
>>> c.__name__
'Simple'
>>> print_dict(state)
{'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.__setstate__({'z': 1})
>>> x.__dict__
{'z': 1}
"""
class Custom(Simple):
def __new__(cls, x, y):
r = Base.__new__(cls)
r.x, r.y = x, y
return r
def __init__(self, x, y):
self.a = 42
def __getnewargs__(self):
return self.x, self.y
def __getstate__(self):
return self.a
def __setstate__(self, a):
self.a = a
def test_pickling_w_overrides():
"""
>>> x = Custom('x', 'y')
>>> x.a = 99
>>> (f, (c, ax, ay), a) = x.__reduce__()
>>> f.__name__
'__newobj__'
>>> f.__module__
'copy_reg'
>>> c.__name__
'Custom'
>>> ax, ay, a
('x', 'y', 99)
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
class Slotted(Base):
__slots__ = 's1', 's2', '_p_splat', '_v_eek'
def __init__(self, s1, s2):
self.s1, self.s2 = s1, s2
self._v_eek = 1
self._p_splat = 2
class SubSlotted(Slotted):
__slots__ = 's3', 's4'
def __init__(self, s1, s2, s3):
Slotted.__init__(self, s1, s2)
self.s3 = s3
def __cmp__(self, other):
return cmpattrs(self, other, '__class__', 's1', 's2', 's3', 's4')
def test_pickling_w_slots_only():
"""
>>> x = SubSlotted('x', 'y', 'z')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> d
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> d
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
class SubSubSlotted(SubSlotted):
def __init__(self, s1, s2, s3, **kw):
SubSlotted.__init__(self, s1, s2, s3)
self.__dict__.update(kw)
self._v_favorite_color = 'blue'
self._p_foo = 'bar'
def __cmp__(self, other):
return cmpattrs(self, other,
'__class__', 's1', 's2', 's3', 's4',
*(self.__dict__.keys()))
def test_pickling_w_slots():
"""
>>> x = SubSubSlotted('x', 'y', 'z', aaa=1, bbb='foo')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> print_dict(d)
{'aaa': 1, 'bbb': 'foo'}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> print_dict(d)
{'aaa': 1, 'bbb': 'foo'}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
def test_pickling_w_slots_w_empty_dict():
"""
>>> x = SubSubSlotted('x', 'y', 'z')
>>> x.__getnewargs__()
Traceback (most recent call last):
...
AttributeError: __getnewargs__
>>> d, s = x.__getstate__()
>>> print_dict(d)
{}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
>>> x.s4 = 'spam'
>>> d, s = x.__getstate__()
>>> print_dict(d)
{}
>>> print_dict(s)
{'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}
>>> pickle.loads(pickle.dumps(x)) == x
1
>>> pickle.loads(pickle.dumps(x, 0)) == x
1
>>> pickle.loads(pickle.dumps(x, 1)) == x
1
>>> pickle.loads(pickle.dumps(x, 2)) == x
1
"""
def test_setattr_on_extension_type():
"""
>>> for name in 'x', '_x', 'x_', '__x_y__', '___x__', '__x___', '_x_':
... setattr(Base, name, 1)
... print getattr(Base, name)
... delattr(Base, name)
... print getattr(Base, name, 0)
1
0
1
0
1
0
1
0
1
0
1
0
1
0
>>> Base.__foo__ = 1
Traceback (most recent call last):
...
TypeError: can't set attributes of built-in/extension type """ \
"""'ExtensionClass.Base' if the attribute name begins """ \
"""and ends with __ and contains only 4 _ characters
>>> Base.__foo__
Traceback (most recent call last):
...
AttributeError: type object 'ExtensionClass.Base' """ \
"""has no attribute '__foo__'
>>> del Base.__foo__
Traceback (most recent call last):
...
TypeError: can't set attributes of built-in/extension type """ \
"""'ExtensionClass.Base' if the attribute name begins """ \
"""and ends with __ and contains only 4 _ characters
"""
def test_mro():
"""ExtensionClass method-resolution order
The EC MRO is chosen to maximize backward compatibility and
provide a model that is easy to reason about. The basic idea is:
I'll call this the "encapsulated base" scheme.
Consider:
>>> class X(Base):
... pass
>>> class Y(Base):
... pass
>>> class Z(Base):
... pass
>>> class C(X, Y, Z):
... def foo(self):
... return 42
When we look up an attribute, we do the following:
- Look in C's dictionary first.
- Look up the attribute in X. We don't care how we get the
attribute from X. If X is a new-style-class, we use the new
algorithm. If X is a classic class, we use left-to-right
depth-first. If X is an nsEC, use the "encapsulated base"
algorithm.
If we don't find the attribute in X, look in Y and then in Z,
using the same approach.
This algorithm will produce backward compatible results, providing
the equivalent of left-to-right depth-first for nsECs and classic
classes.
We'll actually do something less abstract. We'll use a simple
algorthm to merge the __mro__ of the base classes, computing an
__mro__ for classic classes using the left-to-right depth-first
algorithm. We'll basically lay the mros end-to-end left-to-right
and remove repeats, keeping the first occurence of each class.
>>> [c.__name__ for c in C.__mro__]
['C', 'X', 'Y', 'Z', 'Base', 'object']
For backward-compatability's sake, we actually depart from the
above description a bit. We always put Base and object last in the
mro, as shown in the example above. The primary reason for this is
that object provides a do-nothing __init__ method. It is common
practice to mix a C-implemented base class that implements a few
methods with a Python class that implements those methods and
others. The idea is that the C implementation overrides selected
methods in C, so the C subclass is listed first. Unfortunately,
because all extension classes are required to subclass Base, and
thus, object, the C subclass brings along the __init__ object
from objects, which would hide any __init__ method provided by the
Python mix-in.
Base and object are special in that they are implied by their meta
classes. For example, a new-style class always has object as an
ancestor, even if it isn't listed as a base:
>>> class O:
... __metaclass__ = type
>>> [c.__name__ for c in O.__bases__]
['object']
>>> [c.__name__ for c in O.__mro__]
['O', 'object']
Similarly, Base is always an ancestor of an extension class:
>>> class E:
... __metaclass__ = ExtensionClass
>>> [c.__name__ for c in E.__bases__]
['Base']
>>> [c.__name__ for c in E.__mro__]
['E', 'Base', 'object']
Base and object are generally added soley to get a particular meta
class. They aren't used to provide application functionality and
really shouldn't be considered when reasoning about where
attributes come from. They do provide some useful default
functionality and should be included at the end of the mro.
Here are more examples:
>>> from ExtensionClass import Base
>>> class NA(object):
... pass
>>> class NB(NA):
... pass
>>> class NC(NA):
... pass
>>> class ND(NB, NC):
... pass
>>> [c.__name__ for c in ND.__mro__]
['ND', 'NB', 'NC', 'NA', 'object']
>>> class EA(Base):
... pass
>>> class EB(EA):
... pass
>>> class EC(EA):
... pass
>>> class ED(EB, EC):
... pass
>>> [c.__name__ for c in ED.__mro__]
['ED', 'EB', 'EA', 'EC', 'Base', 'object']
>>> class EE(ED, ND):
... pass
>>> [c.__name__ for c in EE.__mro__]
['EE', 'ED', 'EB', 'EA', 'EC', 'ND', 'NB', 'NC', 'NA', 'Base', 'object']
>>> class EF(ND, ED):
... pass
>>> [c.__name__ for c in EF.__mro__]
['EF', 'ND', 'NB', 'NC', 'NA', 'ED', 'EB', 'EA', 'EC', 'Base', 'object']
>>> class CA:
... pass
>>> class CB(CA):
... pass
>>> class CC(CA):
... pass
>>> class CD(CB, CC):
... pass
>>> class ECD(Base, CD):
... pass
>>> [c.__name__ for c in ECD.__mro__]
['ECD', 'CD', 'CB', 'CA', 'CC', 'Base', 'object']
>>> class CDE(CD, Base):
... pass
>>> [c.__name__ for c in CDE.__mro__]
['CDE', 'CD', 'CB', 'CA', 'CC', 'Base', 'object']
>>> class CEND(CD, ED, ND):
... pass
>>> [c.__name__ for c in CEND.__mro__]
['CEND', 'CD', 'CB', 'CA', 'CC', """ \
"""'ED', 'EB', 'EA', 'EC', 'ND', 'NB', 'NC', 'NA', 'Base', 'object']
"""
def test_avoiding___init__decoy_w_inheritedAttribute():
"""
>>> class Decoy(Base):
... pass
>>> class B(Base):
... def __init__(self, a, b):
... print '__init__', a, b
>>> class C(Decoy, B):
... def __init__(self):
... print 'C init'
... C.inheritedAttribute('__init__')(self, 1, 2)
>>> x = C()
C init
__init__ 1 2
"""
def test_of_not_called_when_not_accessed_through_EC_instance():
"""
>>> class Eek(Base):
... def __of__(self, parent):
... return self, parent
If I define an EC instance as an attr of an ordinary class:
>>> class O(object):
... eek = Eek()
>>> class C:
... eek = Eek()
I get the instance, without calling __of__, when I get it from
either tha class:
>>> O.eek is O.__dict__['eek']
True
>>> C.eek is C.__dict__['eek']
True
or an instance of the class:
>>> O().eek is O.__dict__['eek']
True
>>> C().eek is C.__dict__['eek']
True
If I define an EC instance as an attr of an extension class:
>>> class E(Base):
... eek = Eek()
I get the instance, without calling __of__, when I get it from
tha class:
>>> E.eek is E.__dict__['eek']
True
But __of__ is called if I go through the instance:
>>> e = E()
>>> e.eek == (E.__dict__['eek'], e)
True
"""
def test_inheriting___doc__():
"""Old-style ExtensionClass inherited __doc__ from base classes.
>>> class E(Base):
... "eek"
>>> class EE(E):
... pass
>>> EE.__doc__
'eek'
>>> EE().__doc__
'eek'
"""
def test___of___w_metaclass_instance():
"""When looking for extension class instances, need to handle meta classes
>>> class C(Base):
... pass
>>> class O(Base):
... def __of__(self, parent):
... print '__of__ called on an O'
>>> class M(ExtensionClass):
... pass
>>> class X:
... __metaclass__ = M
...
>>> class S(X, O):
... pass
>>> c = C()
>>> c.s = S()
>>> c.s
__of__ called on an O
"""
def test___of__set_after_creation():
"""We may need to set __of__ after a class is created.
Normally, in a class's __init__, the initialization code checks for
an __of__ method and, if it isn't already set, sets __get__.
If a class is persistent and loaded from the database, we want
this to happen in __setstate__. The pmc_init_of function allws us
to do that.
We'll create an extension class without a __of__. We'll also give
it a special meta class, just to make sure that this works with
funny metaclasses too:
>>> import ExtensionClass
>>> class M(ExtensionClass.ExtensionClass):
... "A meta class"
>>> class B(ExtensionClass.Base):
... __metaclass__ = M
... def __init__(self, name):
... self.name = name
... def __repr__(self):
... return self.name
>>> B.__class__ is M
True
>>> x = B('x')
>>> x.y = B('y')
>>> x.y
y
We define a __of__ method for B after the fact:
>>> def __of__(self, other):
... print '__of__(%r, %r)' % (self, other)
... return self
>>> B.__of__ = __of__
We see that this has no effect:
>>> x.y
y
Until we use pmc_init_of:
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
__of__(y, x)
y
Note that there is no harm in calling pmc_init_of multiple times:
>>> ExtensionClass.pmc_init_of(B)
>>> ExtensionClass.pmc_init_of(B)
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
__of__(y, x)
y
If we remove __of__, we'll go back to the behavior we had before:
>>> del B.__of__
>>> ExtensionClass.pmc_init_of(B)
>>> x.y
y
"""
def test_Basic_gc():
"""Test to make sure that EC instances participate in GC
>>> from ExtensionClass import Base
>>> import gc
>>> class C1(Base):
... pass
...
>>> class C2(Base):
... def __del__(self):
... print 'removed'
...
>>> a=C1()
>>> a.b = C1()
>>> a.b.a = a
>>> a.b.c = C2()
>>> thresholds = gc.get_threshold()
>>> gc.set_threshold(0)
>>> ignore = gc.collect()
>>> del a
>>> ignored = gc.collect()
removed
>>> ignored > 0
True
>>> gc.set_threshold(*thresholds)
"""
from zope.testing.doctest import DocTestSuite
import unittest
def test_suite():
return unittest.TestSuite((
DocTestSuite('ExtensionClass'),
DocTestSuite(),
))
if __name__ == '__main__': unittest.main()
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
##############################################################################
#
# Copyright (c) 2010 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from os.path import join
from setuptools import setup, find_packages, Extension
setup(name='Record',
version = '2.13.0',
url='http://pypi.python.org/pypi/Record',
license='ZPL 2.1',
description="Special Record objects used in Zope2.",
author='Zope Corporation and Contributors',
author_email='zope-dev@zope.org',
long_description=open('README.txt').read() + '\n' +
open('CHANGES.txt').read(),
packages=find_packages('src'),
package_dir={'': 'src'},
ext_modules=[Extension(
name='Record._Record',
include_dirs=['include', 'src'],
sources=[join('src', 'Record', '_Record.c')],
depends=[join('include', 'ExtensionClass', 'ExtensionClass.h')]),
],
install_requires=['ExtensionClass'],
include_package_data=True,
zip_safe=False,
)
Metadata-Version: 1.0
Name: Record
Version: 2.13.0
Summary: Special Record objects used in Zope2.
Home-page: http://pypi.python.org/pypi/Record
Author: Zope Corporation and Contributors
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: Overview
========
Record provides special objects used in some Zope2 internals like ZRDB.
Changelog
=========
2.13.0 (2010-03-30)
-------------------
- Released as separate package.
Platform: UNKNOWN
CHANGES.txt
README.txt
bootstrap.py
buildout.cfg
setup.py
include/ExtensionClass/ExtensionClass.h
include/ExtensionClass/_ExtensionClass.c
include/ExtensionClass/__init__.py
include/ExtensionClass/tests.py
include/ExtensionClass/pickle/pickle.c
src/Record/_Record.c
src/Record/__init__.py
src/Record/tests.py
src/Record.egg-info/PKG-INFO
src/Record.egg-info/SOURCES.txt
src/Record.egg-info/dependency_links.txt
src/Record.egg-info/not-zip-safe
src/Record.egg-info/requires.txt
src/Record.egg-info/top_level.txt
\ No newline at end of file
ExtensionClass
\ No newline at end of file
/*****************************************************************************
Copyright (c) 1996-2002 Zope Corporation and Contributors.
All Rights Reserved.
This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE
****************************************************************************/
static char Record_module_documentation[] =
""
"\n$Id: _Record.c,v 1.2 2003/11/28 16:46:36 jim Exp $"
;
#include "ExtensionClass/ExtensionClass.h"
/* ----------------------------------------------------- */
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
typedef Py_ssize_t (*lenfunc)(PyObject *);
typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
static void PyVar_Assign(PyObject **v, PyObject *e) { Py_XDECREF(*v); *v=e;}
#define ASSIGN(V,E) PyVar_Assign(&(V),(E))
#define UNLESS(E) if(!(E))
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E); UNLESS(V)
#define OBJECT(O) ((PyObject*)(O))
static PyObject *py___record_schema__;
/* Declarations for objects of type Record */
typedef struct {
PyObject_HEAD
PyObject **data;
PyObject *schema;
} Record;
staticforward PyExtensionClass RecordType;
/* ---------------------------------------------------------------- */
static Py_ssize_t
Record_init(Record *self)
{
Py_ssize_t l;
UNLESS(self->schema)
UNLESS(self->schema=PyObject_GetAttr(OBJECT(self->ob_type),
py___record_schema__)) return -1;
if((l=PyObject_Length(self->schema)) < 0) return -1;
UNLESS(self->data)
{
UNLESS(self->data=malloc(sizeof(PyObject*)*(l+1)))
{
PyErr_NoMemory();
return -1;
}
memset(self->data, 0, sizeof(PyObject*)*(l+1));
}
return l;
}
static PyObject *
Record___setstate__(Record *self, PyObject *args)
{
PyObject *state=0, *parent, **d;
Py_ssize_t l, ls, i;
if((l=Record_init(self)) < 0) return NULL;
UNLESS(PyArg_ParseTuple(args, "|OO", &state, &parent)) return NULL;
if(state) {
if(PyDict_Check(state))
{
PyObject *k, *v;
for(i=0; PyDict_Next(state, &i, &k, &v);)
if(k && v && PyObject_SetAttr(OBJECT(self),k,v) < 0)
PyErr_Clear();
}
else
{
if((ls=PyObject_Length(state)) < 0) return NULL;
for(i=0, d=self->data; i < l && i < ls; i++, d++)
{
ASSIGN(*d, PySequence_GetItem(state, i));
UNLESS(*d) return NULL;
}
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
Record___getstate__( Record *self, PyObject *args)
{
PyObject *r, **d, *v;
Py_ssize_t i, l;
UNLESS(self->data) return PyTuple_New(0);
if((l=Record_init(self)) < 0) return NULL;
UNLESS(r=PyTuple_New(l)) return NULL;
for(d=self->data, i=0; i < l; i++, d++)
{
v= *d;
if(!v) v=Py_None;
Py_INCREF(v);
PyTuple_SET_ITEM(r,i,v);
}
return r;
}
static void
Record_deal(Record *self)
{
int l;
PyObject **d;
if(self->schema)
{
l=PyObject_Length(self->schema);
for(d=self->data; --l >= 0; d++)
{
Py_XDECREF(*d);
}
Py_DECREF(self->schema);
free(self->data);
}
}
static struct PyMethodDef Record_methods[] = {
{"__getstate__", (PyCFunction)Record___getstate__, METH_VARARGS,
"__getstate__() -- Get the record's data"
},
{"__setstate__", (PyCFunction)Record___setstate__, METH_VARARGS,
"__setstate__(v) -- Set the record's data"
},
{"__init__", (PyCFunction)Record___setstate__, METH_VARARGS,
"__init__([v]) -- Initialize a record, possibly with state"
},
{NULL, NULL} /* sentinel */
};
/* ---------- */
static void
Record_dealloc(Record *self)
{
Record_deal(self);
Py_DECREF(self->ob_type);
PyObject_DEL(self);
}
static PyObject *
Record_getattr(Record *self, PyObject *name)
{
Py_ssize_t l, i;
PyObject *io;
if((l=Record_init(self)) < 0) return NULL;
if ((io=Py_FindAttr((PyObject *)self, name))) return io;
PyErr_Clear();
if((io=PyObject_GetItem(self->schema, name)))
{
UNLESS(PyInt_Check(io))
{
PyErr_SetString(PyExc_TypeError, "invalid record schema");
return NULL;
}
i=PyInt_AsLong(io);
if(i >= 0 && i < l)
{
ASSIGN(io, self->data[i]);
UNLESS(io) io=Py_None;
}
else ASSIGN(io, Py_None);
Py_INCREF(io);
return io;
}
PyErr_SetObject(PyExc_AttributeError, name);
return NULL;
}
static int
Record_setattr(Record *self, PyObject *name, PyObject *v)
{
Py_ssize_t l, i;
PyObject *io;
if((l=Record_init(self)) < 0) return -1;
if((io=PyObject_GetItem(self->schema, name)))
{
UNLESS(PyInt_Check(io))
{
PyErr_SetString(PyExc_TypeError, "invalid record schema");
return -1;
}
i=PyInt_AsLong(io);
Py_DECREF(io);
if(i >= 0 && i < l)
{
Py_XINCREF(v);
ASSIGN(self->data[i],v);
return 0;
}
}
PyErr_SetObject(PyExc_AttributeError, name);
return -1;
}
static int
Record_compare(Record *v, Record *w)
{
Py_ssize_t lv, lw, i;
int c;
PyObject **dv, **dw;
if((lv=Record_init(v)) < 0) return -1;
if((lw=Record_init(w)) < 0) return -1;
if(lw < lv) lv=lw;
for(i=0, dv=v->data, dw=w->data; i < lv; i++, dv++, dw++)
{
if(*dv)
if(*dw)
{
if((c=PyObject_Compare(*dv,*dw))) return c;
}
else return 1;
else if(*dw) return -1;
}
if(*dv) return 1;
if(*dw) return -1;
return 0;
}
/* Code to handle accessing Record objects as sequence objects */
static PyObject *
Record_concat(Record *self, PyObject *bb)
{
PyErr_SetString(PyExc_TypeError,
"Record objects do not support concatenation");
return NULL;
}
static PyObject *
Record_repeat(Record *self, Py_ssize_t n)
{
PyErr_SetString(PyExc_TypeError,
"Record objects do not support repetition");
return NULL;
}
static PyObject *
IndexError(int i)
{
PyObject *v;
if((v=PyInt_FromLong(i)))
{
PyErr_SetObject(PyExc_IndexError, v);
Py_DECREF(v);
}
return NULL;
}
static PyObject *
Record_item(Record *self, Py_ssize_t i)
{
PyObject *o;
Py_ssize_t l;
if((l=Record_init(self)) < 0) return NULL;
if(i < 0 || i >= l) return IndexError(i);
o=self->data[i];
UNLESS(o) o=Py_None;
Py_INCREF(o);
return o;
}
static PyObject *
Record_slice(Record *self, Py_ssize_t ilow, Py_ssize_t ihigh)
{
PyErr_SetString(PyExc_TypeError,
"Record objects do not support slicing");
return NULL;
}
static int
Record_ass_item(Record *self, Py_ssize_t i, PyObject *v)
{
Py_ssize_t l;
if((l=Record_init(self)) < 0) return -1;
if(i < 0 || i >= l)
{
IndexError(i);
return -1;
}
UNLESS(v)
{
PyErr_SetString(PyExc_TypeError,"cannot delete record items");
return -1;
}
Py_INCREF(v);
ASSIGN(self->data[i], v);
return 0;
}
static int
Record_ass_slice(Record *self, int ilow, int ihigh, PyObject *v)
{
PyErr_SetString(PyExc_TypeError,
"Record objects do not support slice assignment");
return -1;
}
static PySequenceMethods Record_as_sequence = {
(lenfunc)Record_init, /*sq_length*/
(binaryfunc)Record_concat, /*sq_concat*/
(ssizeargfunc)Record_repeat, /*sq_repeat*/
(ssizeargfunc)Record_item, /*sq_item*/
(ssizessizeargfunc)Record_slice, /*sq_slice*/
(ssizeobjargproc)Record_ass_item, /*sq_ass_item*/
(ssizessizeobjargproc)Record_ass_slice, /*sq_ass_slice*/
};
/* -------------------------------------------------------------- */
static PyObject *
Record_subscript(Record *self, PyObject *key)
{
Py_ssize_t i, l;
PyObject *io;
if((l=Record_init(self)) < 0) return NULL;
if(PyInt_Check(key))
{
i=PyInt_AsLong(key);
if(i<0) i+=l;
return Record_item(self, i);
}
if((io=PyObject_GetItem(self->schema, key)))
{
UNLESS(PyInt_Check(io))
{
PyErr_SetString(PyExc_TypeError, "invalid record schema");
return NULL;
}
i=PyInt_AsLong(io);
if(i >= 0 && i < l)
{
ASSIGN(io, self->data[i]);
UNLESS(io) io=Py_None;
}
else ASSIGN(io, Py_None);
Py_INCREF(io);
return io;
}
PyErr_Clear();
if ((io=PyObject_GetAttr(OBJECT(self), key))) return io;
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}
static int
Record_ass_sub(Record *self, PyObject *key, PyObject *v)
{
Py_ssize_t i, l;
PyObject *io;
if((l=Record_init(self)) < 0) return -1;
if(PyInt_Check(key))
{
i=PyInt_AsLong(key);
if(i<0) i+=l;
return Record_ass_item(self, i, v);
}
if((io=PyObject_GetItem(self->schema, key)))
{
UNLESS(PyInt_Check(io))
{
PyErr_SetString(PyExc_TypeError, "invalid record schema");
return -1;
}
i=PyInt_AsLong(io);
Py_DECREF(io);
if(i >= 0 && i < l)
{
Py_XINCREF(v);
ASSIGN(self->data[i],v);
return 0;
}
}
return -1;
}
static PyMappingMethods Record_as_mapping = {
(lenfunc)Record_init, /*mp_length*/
(binaryfunc)Record_subscript, /*mp_subscript*/
(objobjargproc)Record_ass_sub, /*mp_ass_subscript*/
};
/* -------------------------------------------------------- */
static PyExtensionClass RecordType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"Record", /*tp_name*/
sizeof(Record), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)Record_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)0, /*obsolete tp_getattr*/
(setattrfunc)0, /*obsolete tp_setattr*/
(cmpfunc)Record_compare, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/
&Record_as_sequence, /*tp_as_sequence*/
&Record_as_mapping, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
(getattrofunc)Record_getattr, /*tp_getattro*/
(setattrofunc)Record_setattr, /*tp_setattro*/
/* Space for future expansion */
0L,0L,
"Simple Record Types", /* Documentation string */
METHOD_CHAIN(Record_methods),
(void*)(EXTENSIONCLASS_NOINSTDICT_FLAG),
};
/* End of code for Record objects */
/* -------------------------------------------------------- */
/* List of methods defined in the module */
static struct PyMethodDef Module_Level__methods[] = {
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initRecord) */
void
init_Record(void)
{
PyObject *m, *d;
UNLESS(py___record_schema__=PyString_FromString("__record_schema__")) return;
UNLESS(ExtensionClassImported) return;
/* Create the module and add the functions */
m = Py_InitModule4("_Record", Module_Level__methods,
Record_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
PyExtensionClass_Export(d,"Record",RecordType);
}
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Records
Records are used to provide compact storage for database query result rows.
They don't use instance dictionaries. Rather, they store they data in
a compact array internally. They use a record schema to map names to
positions within the array.
>>> class R(Record):
... __record_schema__ = {'a': 0, 'b': 1, 'c': 2}
>>> r = R()
>>> r.__dict__
Traceback (most recent call last):
...
AttributeError: __dict__
>>> r.a
>>> r.a = 1
>>> r.a
1
>>> r.b
>>> r.c
Records can be used as mapping objects:
>>> "%(a)s %(b)s %(c)s" % r
'1 None None'
>>> r['a']
1
>>> r['b'] = 42
And like sequences:
>>> r[0]
1
>>> r[1]
42
>>> list(r)
[1, 42, None]
$Id: __init__.py,v 1.2 2003/11/28 16:46:36 jim Exp $
"""
from _Record import Record
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Record tests
$Id: tests.py,v 1.2 2003/11/28 16:46:36 jim Exp $
"""
from Record import Record
import pickle
class P(Record):
__record_schema__ = {'a': 0, 'b': 1, 'c': 2}
def test_RecordPickling():
"""
We can create records from sequences:
>>> r = P(('x', 42, 1.23))
We can pickle them:
>>> r2 = pickle.loads(pickle.dumps(r))
>>> list(r) == list(r2)
1
>>> r.__record_schema__ == r2.__record_schema__
1
"""
import unittest
from doctest import DocTestSuite
def test_suite():
return unittest.TestSuite((
DocTestSuite('Record'),
DocTestSuite(),
))
if __name__ == '__main__': unittest.main()
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