tested django-newsletter
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/*****************************************************************************
|
||||
|
||||
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"
|
||||
#include "ExtensionClass/_compat.h"
|
||||
|
||||
static PyObject *
|
||||
of(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *inst;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &inst)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyECMethod_New(self, inst);
|
||||
}
|
||||
|
||||
struct PyMethodDef Method_methods[] = {
|
||||
{"__of__",(PyCFunction)of,METH_VARARGS,""},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
static struct PyMethodDef methods[] = {{NULL, NULL}};
|
||||
|
||||
#ifdef PY3K
|
||||
static struct PyModuleDef moduledef =
|
||||
{
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_MethodObject", /* m_name */
|
||||
"Method-object mix-in class module\n\n", /* m_doc */
|
||||
-1, /* m_size */
|
||||
methods, /* m_methods */
|
||||
NULL, /* m_reload */
|
||||
NULL, /* m_traverse */
|
||||
NULL, /* m_clear */
|
||||
NULL, /* m_free */
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
static PyObject*
|
||||
module_init(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);
|
||||
|
||||
#ifdef PY3K
|
||||
m = PyModule_Create(&moduledef);
|
||||
#else
|
||||
m = Py_InitModule3(
|
||||
"_MethodObject",
|
||||
methods,
|
||||
"Method-object mix-in class module\n\n");
|
||||
#endif
|
||||
|
||||
if (m == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
d = PyModule_GetDict(m);
|
||||
if (d == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyExtensionClass_Export(d, "Method", MethodType);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
#ifdef PY3K
|
||||
PyMODINIT_FUNC PyInit__MethodObject(void)
|
||||
{
|
||||
return module_init();
|
||||
}
|
||||
#else
|
||||
PyMODINIT_FUNC init_MethodObject(void)
|
||||
{
|
||||
module_init();
|
||||
}
|
||||
#endif
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,24 @@
|
||||
from types import MethodType
|
||||
|
||||
from ExtensionClass import Base
|
||||
from ExtensionClass import C_EXTENSION
|
||||
|
||||
|
||||
class Method(Base):
|
||||
"""Base class for objects that want to be treated as methods
|
||||
|
||||
The method class provides a method, __of__, that
|
||||
binds an object to an instance. If a method is a subobject
|
||||
of an extension-class instance, the the method will be bound
|
||||
to the instance and when the resulting object is called, it
|
||||
will call the method and pass the instance in addition to
|
||||
other arguments. It is the responsibility of Method objects
|
||||
to implement (or inherit) a __call__ method.
|
||||
"""
|
||||
|
||||
def __of__(self, inst):
|
||||
return MethodType(self, inst)
|
||||
|
||||
|
||||
if C_EXTENSION: # pragma no cover
|
||||
from ._MethodObject import * # NOQA
|
||||
@@ -0,0 +1,51 @@
|
||||
##############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class TestMethodObject(unittest.TestCase):
|
||||
|
||||
def test_compilation(self):
|
||||
from ExtensionClass import _IS_PYPY
|
||||
try:
|
||||
from MethodObject import _MethodObject
|
||||
except ImportError: # pragma: no cover
|
||||
self.assertTrue(_IS_PYPY)
|
||||
else:
|
||||
self.assertTrue(hasattr(_MethodObject, 'Method'))
|
||||
|
||||
def test_methodobject(self):
|
||||
from ExtensionClass import Base
|
||||
from MethodObject import Method
|
||||
|
||||
class Callable(Method):
|
||||
def __call__(self, ob, *args, **kw):
|
||||
return (repr(ob), args, kw)
|
||||
|
||||
class ExClass(Base):
|
||||
def __repr__(self):
|
||||
return "bar()"
|
||||
|
||||
hi = Callable()
|
||||
|
||||
x = ExClass()
|
||||
hi = x.hi
|
||||
result = hi(1, 2, 3, name='spam')
|
||||
|
||||
self.assertEqual(result,
|
||||
("bar()", (1, 2, 3), {'name': 'spam'}))
|
||||
|
||||
def test_suite():
|
||||
return unittest.defaultTestLoader.loadTestsFromName(__name__)
|
||||
Reference in New Issue
Block a user