Commit 2d813aae authored by Boxiang Sun's avatar Boxiang Sun

Original implementation

parents
Changelog
=========
2.13.2 (2010-06-16)
-------------------
- LP #587760: Handle tp_basicsize correctly.
2.13.1 (2010-04-03)
-------------------
- Removed undeclared testing dependency on zope.testing.
- Removed cruft in ``pickle/pickle.c`` related to removed ``__getnewargs__``.
2.13.0 (2010-02-22)
-------------------
- Avoid defining ``__getnewargs__`` as not to defeat the ZODB persistent
reference optimization. Refs https://bugs.launchpad.net/zope2/+bug/143657.
In order to take advantage of this optimization, you need to re-save your
objects.
2.12.0 (2010-02-14)
-------------------
- Removed old build artifacts and some metadata cleanup.
- Added support for method cache in ExtensionClass. Patch contributed by
Yoshinori K. Okuji. See https://bugs.launchpad.net/zope2/+bug/486182.
2.11.3 (2009-08-02)
-------------------
- Further 64-bit fixes (Python 2.4 compatibility).
2.11.2 (2009-08-02)
-------------------
- Fixed 64-bit compatibility issues for Python 2.5.x / 2.6.x. See
http://www.python.org/dev/peps/pep-0353/ for details.
2.11.1 (2009-02-19)
-------------------
- Initial egg release.
Zope Foundation and Contributors
\ No newline at end of file
Zope Public License (ZPL) Version 2.1
A copyright notice accompanies this license document that identifies the
copyright holders.
This license has been certified as open source. It has also been designated as
GPL compatible by the Free Software Foundation (FSF).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions in source code must retain the accompanying copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the accompanying copyright
notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Names of the copyright holders must not be used to endorse or promote
products derived from this software without prior written permission from the
copyright holders.
4. The right to distribute this software or to use it for any purpose does not
give you the right to use Servicemarks (sm) or Trademarks (tm) of the
copyright
holders. Use of them is covered by separate agreement with the copyright
holders.
5. If any files are modified, you must cause the modified files to carry
prominent notices stating that you changed the files and the date of any
change.
Disclaimer
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
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 THE COPYRIGHT HOLDERS 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.
Metadata-Version: 1.0
Name: ExtensionClass
Version: 2.13.2
Summary: Metaclass for subclassable extension types
Home-page: http://pypi.python.org/pypi/ExtensionClass
Author: Zope Foundation and Contributors
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: ExtensionClass and ExtensionClass-related packages
==================================================
ExtensionClass
--------------
This package provides a metaclass that allows classes implemented in
extension modules to be subclassed in Python. Unless you need
ExtensionClasses for legacy applications (e.g. Zope 2), you probably
want to use Python's new-style classes (available since Python 2.2).
ComputedAttribute
-----------------
This package provides a way to attach attributes to an
``ExtensionClass`` or instance that are computed by calling a
callable. This works very much like ``property`` known from new-style
classes, except that a ``ComputedAttribute`` can also be attached to
an instance and that it honours ExtensionClass semantics (which is
useful for retaining Acquisition wrappers, for example).
MethodObject
------------
This package lets you attach additional "methods" to ExtensionClasses.
These "methods" are actually implemented by subclassing the
``MethodObject.Method`` class and implementing the ``__call__`` method
there. Instances of those classes will be bound to the instances
they're attached to and will receive that instance object as a first
parameter (after ``self``).
Changelog
=========
2.13.2 (2010-06-16)
-------------------
- LP #587760: Handle tp_basicsize correctly.
2.13.1 (2010-04-03)
-------------------
- Removed undeclared testing dependency on zope.testing.
- Removed cruft in ``pickle/pickle.c`` related to removed ``__getnewargs__``.
2.13.0 (2010-02-22)
-------------------
- Avoid defining ``__getnewargs__`` as not to defeat the ZODB persistent
reference optimization. Refs https://bugs.launchpad.net/zope2/+bug/143657.
In order to take advantage of this optimization, you need to re-save your
objects.
2.12.0 (2010-02-14)
-------------------
- Removed old build artifacts and some metadata cleanup.
- Added support for method cache in ExtensionClass. Patch contributed by
Yoshinori K. Okuji. See https://bugs.launchpad.net/zope2/+bug/486182.
2.11.3 (2009-08-02)
-------------------
- Further 64-bit fixes (Python 2.4 compatibility).
2.11.2 (2009-08-02)
-------------------
- Fixed 64-bit compatibility issues for Python 2.5.x / 2.6.x. See
http://www.python.org/dev/peps/pep-0353/ for details.
2.11.1 (2009-02-19)
-------------------
- Initial egg release.
Platform: UNKNOWN
ExtensionClass and ExtensionClass-related packages
==================================================
ExtensionClass
--------------
This package provides a metaclass that allows classes implemented in
extension modules to be subclassed in Python. Unless you need
ExtensionClasses for legacy applications (e.g. Zope 2), you probably
want to use Python's new-style classes (available since Python 2.2).
ComputedAttribute
-----------------
This package provides a way to attach attributes to an
``ExtensionClass`` or instance that are computed by calling a
callable. This works very much like ``property`` known from new-style
classes, except that a ``ComputedAttribute`` can also be attached to
an instance and that it honours ExtensionClass semantics (which is
useful for retaining Acquisition wrappers, for example).
MethodObject
------------
This package lets you attach additional "methods" to ExtensionClasses.
These "methods" are actually implemented by subclassing the
``MethodObject.Method`` class and implementing the ``__call__`` method
there. Instances of those classes will be bound to the instances
they're attached to and will receive that instance object as a first
parameter (after ``self``).
##############################################################################
#
# Copyright (c) 2006 Zope Foundation 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$
"""
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 Disribute 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 = test
versions = versions
[test]
recipe = zc.recipe.testrunner
eggs = ExtensionClass
[versions]
zc.buildout = 1.4.3
zc.recipe.egg = 1.2.2
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
##############################################################################
#
# Copyright (c) 2007 Zope Foundation 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.
#
##############################################################################
"""Setup for the ExtensionClass distribution
"""
import os
from setuptools import setup, find_packages, Extension
README = open('README.txt').read()
CHANGES = open('CHANGES.txt').read()
setup(name='ExtensionClass',
version = '2.13.2',
url='http://pypi.python.org/pypi/ExtensionClass',
license='ZPL 2.1',
description='Metaclass for subclassable extension types',
author='Zope Foundation and Contributors',
author_email='zope-dev@zope.org',
long_description='\n\n'.join([README, CHANGES]),
packages=find_packages('src'),
package_dir={'': 'src'},
ext_modules=[Extension("ExtensionClass._ExtensionClass",
[os.path.join('src', 'ExtensionClass',
'_ExtensionClass.c')],
include_dirs=['src']),
Extension("ComputedAttribute._ComputedAttribute",
[os.path.join('src', 'ComputedAttribute',
'_ComputedAttribute.c')],
include_dirs=['src']),
Extension("MethodObject._MethodObject",
[os.path.join('src', 'MethodObject',
'_MethodObject.c')],
include_dirs=['src']),
],
include_package_data=True,
zip_safe=False,
)
/*****************************************************************************
Copyright (c) 1996-2003 Zope Foundation 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
****************************************************************************/
#include "ExtensionClass/ExtensionClass.h"
#define UNLESS(E) if(!(E))
#define OBJECT(O) ((PyObject*)(O))
typedef struct {
PyObject_HEAD
PyObject *callable;
int level;
} CA;
static PyObject *
CA__init__(CA *self, PyObject *args)
{
PyObject *callable;
int level=0;
UNLESS(PyArg_ParseTuple(args,"O|i",&callable, &level)) return NULL;
if (level > 0)
{
callable=PyObject_CallFunction(OBJECT(self->ob_type), "Oi",
callable, level-1);
UNLESS (callable) return NULL;
self->level=level;
}
else
{
Py_INCREF(callable);
self->level=0;
}
self->callable=callable;
Py_INCREF(Py_None);
return Py_None;
}
static void
CA_dealloc(CA *self)
{
Py_DECREF(self->callable);
Py_DECREF(self->ob_type);
PyObject_DEL(self);
}
static PyObject *
CA_of(CA *self, PyObject *args)
{
if (self->level > 0)
{
Py_INCREF(self->callable);
return self->callable;
}
if (PyString_Check(self->callable))
{
/* Special case string as simple alias. */
PyObject *o;
UNLESS (PyArg_ParseTuple(args,"O", &o)) return NULL;
return PyObject_GetAttr(o, self->callable);
}
return PyObject_CallObject(self->callable, args);
}
static struct PyMethodDef CA_methods[] = {
{"__init__",(PyCFunction)CA__init__, METH_VARARGS, ""},
{"__of__", (PyCFunction)CA_of, METH_VARARGS, ""},
{NULL, NULL} /* sentinel */
};
static PyExtensionClass ComputedAttributeType = {
PyObject_HEAD_INIT(NULL) 0,
"ComputedAttribute", sizeof(CA),
0,
(destructor)CA_dealloc,
0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,
"ComputedAttribute(callable) -- Create a computed attribute",
METHOD_CHAIN(CA_methods),
(void*)(EXTENSIONCLASS_BINDABLE_FLAG)
};
static struct PyMethodDef methods[] = {
{NULL, NULL}
};
void
init_ComputedAttribute(void)
{
PyObject *m, *d;
UNLESS(ExtensionClassImported) return;
/* Create the module and add the functions */
m = Py_InitModule4("_ComputedAttribute", methods,
"Provide Computed Attributes\n\n"
"$Id: _ComputedAttribute.c 110581 2010-04-07 15:04:20Z tseaver $\n",
OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m);
PyExtensionClass_Export(d,"ComputedAttribute",ComputedAttributeType);
}
from _ComputedAttribute import *
##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
#
##############################################################################
"""Computed Attributes
Computed attributes work much like properties:
>>> import math
>>> from ComputedAttribute import ComputedAttribute
>>> from ExtensionClass import Base
>>> class Point(Base):
... def __init__(self, x, y):
... self.x, self.y = x, y
... length = ComputedAttribute(lambda self: math.sqrt(self.x**2+self.y**2))
>>> p = Point(3, 4)
>>> "%.1f" % p.length
'5.0'
Except that you can also use computed attributes with instances:
>>> p.angle = ComputedAttribute(lambda self: math.atan(self.y*1.0/self.x))
>>> "%.2f" % p.angle
'0.93'
$Id: tests.py 111623 2010-04-30 13:55:00Z hannosch $
"""
def test_wrapper_support():
"""Wrapper support
To support acquisition wrappers, computed attributes have a level.
The computation is only done when the level is zero. Retrieving a
computed attribute with a level > 0 returns a computed attribute
with a decremented level.
>>> from ExtensionClass import Base
>>> class X(Base):
... pass
>>> x = X()
>>> x.n = 1
>>> from ComputedAttribute import ComputedAttribute
>>> x.n2 = ComputedAttribute(lambda self: self.n*2)
>>> x.n2
2
>>> x.n2.__class__.__name__
'int'
>>> x.n2 = ComputedAttribute(lambda self: self.n*2, 2)
>>> x.n2.__class__.__name__
'ComputedAttribute'
>>> x.n2 = x.n2
>>> x.n2.__class__.__name__
'ComputedAttribute'
>>> x.n2 = x.n2
>>> x.n2.__class__.__name__
'int'
"""
import unittest
from doctest import DocTestSuite
def test_suite():
return unittest.TestSuite((DocTestSuite(),))
if __name__ == '__main__': unittest.main()
Metadata-Version: 1.0
Name: ExtensionClass
Version: 2.13.2
Summary: Metaclass for subclassable extension types
Home-page: http://pypi.python.org/pypi/ExtensionClass
Author: Zope Foundation and Contributors
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: ExtensionClass and ExtensionClass-related packages
==================================================
ExtensionClass
--------------
This package provides a metaclass that allows classes implemented in
extension modules to be subclassed in Python. Unless you need
ExtensionClasses for legacy applications (e.g. Zope 2), you probably
want to use Python's new-style classes (available since Python 2.2).
ComputedAttribute
-----------------
This package provides a way to attach attributes to an
``ExtensionClass`` or instance that are computed by calling a
callable. This works very much like ``property`` known from new-style
classes, except that a ``ComputedAttribute`` can also be attached to
an instance and that it honours ExtensionClass semantics (which is
useful for retaining Acquisition wrappers, for example).
MethodObject
------------
This package lets you attach additional "methods" to ExtensionClasses.
These "methods" are actually implemented by subclassing the
``MethodObject.Method`` class and implementing the ``__call__`` method
there. Instances of those classes will be bound to the instances
they're attached to and will receive that instance object as a first
parameter (after ``self``).
Changelog
=========
2.13.2 (2010-06-16)
-------------------
- LP #587760: Handle tp_basicsize correctly.
2.13.1 (2010-04-03)
-------------------
- Removed undeclared testing dependency on zope.testing.
- Removed cruft in ``pickle/pickle.c`` related to removed ``__getnewargs__``.
2.13.0 (2010-02-22)
-------------------
- Avoid defining ``__getnewargs__`` as not to defeat the ZODB persistent
reference optimization. Refs https://bugs.launchpad.net/zope2/+bug/143657.
In order to take advantage of this optimization, you need to re-save your
objects.
2.12.0 (2010-02-14)
-------------------
- Removed old build artifacts and some metadata cleanup.
- Added support for method cache in ExtensionClass. Patch contributed by
Yoshinori K. Okuji. See https://bugs.launchpad.net/zope2/+bug/486182.
2.11.3 (2009-08-02)
-------------------
- Further 64-bit fixes (Python 2.4 compatibility).
2.11.2 (2009-08-02)
-------------------
- Fixed 64-bit compatibility issues for Python 2.5.x / 2.6.x. See
http://www.python.org/dev/peps/pep-0353/ for details.
2.11.1 (2009-02-19)
-------------------
- Initial egg release.
Platform: UNKNOWN
CHANGES.txt
COPYRIGHT.txt
LICENSE.txt
README.txt
bootstrap.py
buildout.cfg
setup.py
src/ComputedAttribute/_ComputedAttribute.c
src/ComputedAttribute/__init__.py
src/ComputedAttribute/tests.py
src/ExtensionClass/ExtensionClass.h
src/ExtensionClass/_ExtensionClass.c
src/ExtensionClass/__init__.py
src/ExtensionClass/tests.py
src/ExtensionClass.egg-info/PKG-INFO
src/ExtensionClass.egg-info/SOURCES.txt
src/ExtensionClass.egg-info/dependency_links.txt
src/ExtensionClass.egg-info/not-zip-safe
src/ExtensionClass.egg-info/top_level.txt
src/ExtensionClass/pickle/pickle.c
src/MethodObject/_MethodObject.c
src/MethodObject/__init__.py
src/MethodObject/tests.py
\ No newline at end of file
ExtensionClass
MethodObject
ComputedAttribute
This diff is collapsed.
This diff is collapsed.
##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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 110581 2010-04-07 15:04:20Z tseaver $
"""
from _ExtensionClass import *
/*
Copyright (c) 2003 Zope Foundation 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___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
This diff is collapsed.
/*****************************************************************************
Copyright (c) 1996-2002 Zope Foundation 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
****************************************************************************/
#include "ExtensionClass/ExtensionClass.h"
static PyObject *
of(PyObject *self, PyObject *args)
{
PyObject *inst;
if(PyArg_Parse(args,"O",&inst)) return PyECMethod_New(self,inst);
else return NULL;
}
struct PyMethodDef Method_methods[] = {
{"__of__",(PyCFunction)of,0,""},
{NULL, NULL} /* sentinel */
};
static struct PyMethodDef methods[] = {{NULL, NULL}};
void
init_MethodObject(void)
{
PyObject *m, *d;
PURE_MIXIN_CLASS(Method,
"Base class for objects that want to be treated as methods\n"
"\n"
"The method class provides a method, __of__, that\n"
"binds an object to an instance. If a method is a subobject\n"
"of an extension-class instance, the the method will be bound\n"
"to the instance and when the resulting object is called, it\n"
"will call the method and pass the instance in addition to\n"
"other arguments. It is the responsibility of Method objects\n"
"to implement (or inherit) a __call__ method.\n",
Method_methods);
/* Create the module and add the functions */
m = Py_InitModule4("_MethodObject", methods,
"Method-object mix-in class module\n\n"
"$Id: _MethodObject.c 110581 2010-04-07 15:04:20Z tseaver $\n",
(PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m);
PyExtensionClass_Export(d,"Method",MethodType);
}
from _MethodObject import *
##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
#
##############################################################################
def test_methodobject():
"""
>>> from ExtensionClass import Base
>>> from MethodObject import Method
>>> class foo(Method):
... def __call__(self, ob, *args, **kw):
... print 'called', ob, args, kw
>>> class bar(Base):
... def __repr__(self):
... return "bar()"
... hi = foo()
>>> x = bar()
>>> hi = x.hi
>>> hi(1,2,3,name='spam')
called bar() (1, 2, 3) {'name': 'spam'}
"""
def test_suite():
import unittest
from doctest import DocTestSuite
return unittest.TestSuite((
DocTestSuite(),
))
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