Commit 94e3a51e authored by Jim Fulton's avatar Jim Fulton

Fixed __len__ bugs and added free lists for methods and wrappers

parent 38b0ef8d
/*
$Id: Acquisition.c,v 1.14 1998/01/05 13:38:31 jim Exp $
$Id: Acquisition.c,v 1.15 1998/01/21 19:00:48 jim Exp $
Acquisition Wrappers -- Implementation of acquisition through wrappers
......@@ -147,6 +147,8 @@ typedef struct {
PyObject *container;
} Wrapper;
static Wrapper *freeWrappers=0;
staticforward PyExtensionClass Wrappertype, XaqWrappertype;
#define isWrapper(O) ((O)->ob_type==(PyTypeObject*)&Wrappertype || \
......@@ -175,7 +177,16 @@ newWrapper(PyObject *obj, PyObject *container, PyTypeObject *Wrappertype)
{
Wrapper *self;
if(freeWrappers)
{
self=freeWrappers;
freeWrappers=(Wrapper*)self->obj;
self->ob_type=Wrappertype;
self->ob_refcnt=1;
}
else
UNLESS(self = PyObject_NEW(Wrapper, Wrappertype)) return NULL;
Py_INCREF(obj);
Py_INCREF(container);
self->obj=obj;
......@@ -189,7 +200,8 @@ Wrapper_dealloc(Wrapper *self)
{
Py_DECREF(self->obj);
Py_DECREF(self->container);
PyMem_DEL(self);
self->obj=OBJECT(freeWrappers);
freeWrappers=self;
}
static PyObject *
......@@ -518,7 +530,33 @@ Wrapper_length(Wrapper *self)
long l;
PyObject *r;
UNLESS(r=CallMethodO(OBJECT(self),py__len__,NULL,NULL)) return -1;
UNLESS(r=PyObject_GetAttr(OBJECT(self), py__len__))
{
/* Hm. Maybe we are being checked to see if we are true.
Check to see if we have a __getitem__. If we don't, then
answer that we are true, otherwise raise an error.
*/
PyErr_Clear();
if(r=PyObject_GetAttr(OBJECT(self), py__getitem__))
{
/* Hm, we have getitem, must be error */
Py_DECREF(r);
PyErr_SetObject(PyExc_AttributeError, py__len__);
return -1;
}
PyErr_Clear();
/* Try nonzero */
UNLESS(r=PyObject_GetAttr(OBJECT(self), py__nonzero__))
{
/* No nonzero, it's true :-) */
PyErr_Clear();
return 1;
}
}
UNLESS_ASSIGN(r,PyObject_CallObject(r,NULL)) return -1;
l=PyInt_AsLong(r);
Py_DECREF(r);
return l;
......@@ -766,7 +804,7 @@ void
initAcquisition()
{
PyObject *m, *d;
char *rev="$Revision: 1.14 $";
char *rev="$Revision: 1.15 $";
PURE_MIXIN_CLASS(Acquirer,
"Base class for objects that implicitly"
" acquire attributes from containers\n"
......@@ -785,7 +823,7 @@ initAcquisition()
/* Create the module and add the functions */
m = Py_InitModule4("Acquisition", methods,
"Provide base classes for acquiring objects\n\n"
"$Id: Acquisition.c,v 1.14 1998/01/05 13:38:31 jim Exp $\n",
"$Id: Acquisition.c,v 1.15 1998/01/21 19:00:48 jim Exp $\n",
OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m);
......@@ -808,6 +846,9 @@ initAcquisition()
/*****************************************************************************
$Log: Acquisition.c,v $
Revision 1.15 1998/01/21 19:00:48 jim
Fixed __len__ bugs and added free lists for methods and wrappers
Revision 1.14 1998/01/05 13:38:31 jim
Added special module variable, 'Acquired'. If the value of this
variable is assigned to an attribute, then the value of the attribute
......
/*
$Id: ExtensionClass.c,v 1.22 1998/01/02 18:18:28 jim Exp $
$Id: ExtensionClass.c,v 1.23 1998/01/21 19:00:49 jim Exp $
Extension Class
......@@ -65,7 +65,7 @@ static char ExtensionClass_module_documentation[] =
" - They provide access to unbound methods,\n"
" - They can be called to create instances.\n"
"\n"
"$Id: ExtensionClass.c,v 1.22 1998/01/02 18:18:28 jim Exp $\n"
"$Id: ExtensionClass.c,v 1.23 1998/01/21 19:00:49 jim Exp $\n"
;
#include <stdio.h>
......@@ -102,6 +102,15 @@ staticforward PyExtensionClass ECType;
(((PyExtensionClass*)((O)->ob_type))->class_flags & \
EXTENSIONCLASS_METHODHOOK_FLAG))
#define ALLOC_FREE(T) \
if(free ## T) { \
self=free ## T; \
free ## T=(T*)self->self; \
self->ob_refcnt=1; \
} \
else UNLESS(self = PyObject_NEW(T, & T ## Type)) return NULL;
static PyObject *py__add__, *py__sub__, *py__mul__, *py__div__,
*py__mod__, *py__pow__, *py__divmod__, *py__lshift__, *py__rshift__,
*py__and__, *py__or__, *py__xor__, *py__coerce__, *py__neg__,
......@@ -349,14 +358,16 @@ CMethod_issubclass(PyExtensionClass *sub, PyExtensionClass *type)
(PyExtensionClass *)(C2))
static CMethod *freeCMethod=0;
static PyObject *
newCMethod(PyExtensionClass *type, PyObject *inst,
char *name, PyCFunction meth, int flags, char *doc)
{
CMethod *self;
ALLOC_FREE(CMethod);
UNLESS(self = PyObject_NEW(CMethod, &CMethodType)) return NULL;
Py_INCREF(type);
Py_XINCREF(inst);
self->type=(PyTypeObject*)type;
......@@ -382,7 +393,7 @@ bindCMethod(CMethod *m, PyObject *inst)
return m;
}
UNLESS(self = PyObject_NEW(CMethod, &CMethodType)) return NULL;
ALLOC_FREE(CMethod);
Py_INCREF(inst);
Py_INCREF(m->type);
......@@ -403,7 +414,8 @@ CMethod_dealloc(CMethod *self)
#endif
Py_XDECREF(self->type);
Py_XDECREF(self->self);
PyMem_DEL(self);
self->self=(PyObject*)freeCMethod;
freeCMethod=self;
}
static PyObject *
......@@ -618,6 +630,7 @@ static PyTypeObject CMethodType = {
/* PMethod objects: */
#define PMethod PyECMethodObject
#define PMethodType PyECMethodObjectType
staticforward PyTypeObject PMethodType;
......@@ -630,12 +643,15 @@ staticforward PyTypeObject PMethodType;
&& ! ((PMethod*)(O))->self)
static PMethod *freePMethod=0;
static PyObject *
newPMethod(PyExtensionClass *type, PyObject *meth)
{
PMethod *self;
UNLESS(self = PyObject_NEW(PMethod, &PMethodType)) return NULL;
ALLOC_FREE(PMethod);
Py_INCREF(type);
Py_INCREF(meth);
self->type=(PyTypeObject*)type;
......@@ -659,7 +675,7 @@ bindPMethod(PMethod *m, PyObject *inst)
return (PyObject*)m;
}
UNLESS(self = PyObject_NEW(PMethod, &PMethodType)) return NULL;
ALLOC_FREE(PMethod);
Py_INCREF(inst);
Py_INCREF(m->type);
......@@ -692,7 +708,8 @@ PMethod_dealloc(PMethod *self)
#endif
Py_XDECREF(self->type);
Py_XDECREF(self->self);
PyMem_DEL(self);
self->self=(PyObject*)freePMethod;
freePMethod=self;
#ifdef TRACE_DEALLOC
fprintf(stderr," Done Deallocating PM\n");
#endif
......@@ -2508,7 +2525,24 @@ subclass_length(PyObject *self)
long r;
PyExtensionClass *t;
UNLESS(m=subclass_getspecial(self,py__len__)) return -1;
UNLESS(m=subclass_getspecial(self,py__len__))
{
/* Hm. Maybe we are being checked to see if we are true.
Check to see if we have a __getitem__. If we don't, then
answer that we are true.
*/
PyErr_Clear();
if(m=subclass_getspecial(self,py__getitem__))
{
/* Hm, we have getitem, must be error */
Py_DECREF(m);
PyErr_SetObject(PyExc_AttributeError, py__len__);
return -1;
}
PyErr_Clear();
return subclass_nonzero(self);
}
if(UnboundCMethod_Check(m) && AsCMethod(m)->meth==length_by_name
&& SubclassInstance_Check(self,AsCMethod(m)->type)
&& ! HasMethodHook(self))
......@@ -3285,7 +3319,7 @@ void
initExtensionClass()
{
PyObject *m, *d;
char *rev="$Revision: 1.22 $";
char *rev="$Revision: 1.23 $";
PURE_MIXIN_CLASS(Base, "Minimalbase class for Extension Classes", NULL);
PMethodType.ob_type=&PyType_Type;
......@@ -3326,6 +3360,9 @@ initExtensionClass()
/****************************************************************************
$Log: ExtensionClass.c,v $
Revision 1.23 1998/01/21 19:00:49 jim
Fixed __len__ bugs and added free lists for methods and wrappers
Revision 1.22 1998/01/02 18:18:28 jim
Fixed bug in instance getattr so that instances don't get __bases__
from their class.
......
#!/bin/env python
#
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved. Copyright in this software is owned by DCLC,
# unless otherwise indicated. Permission to use, copy and
# distribute this software is hereby granted, provided that the
# above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear. Note that
# any product, process or technology described in this software
# may be the subject of other Intellectual Property rights
# reserved by Digital Creations, L.C. and are not licensed
# hereunder.
#
# Trademarks
#
# Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
# All other trademarks are owned by their respective companies.
#
# No Warranty
#
# The software is provided "as is" without warranty of any kind,
# either express or implied, including, but not limited to, the
# implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement. This software could include
# technical inaccuracies or typographical errors. Changes are
# periodically made to the software; these changes will be
# incorporated in new editions of the software. DCLC may make
# improvements and/or changes in this software at any time
# without notice.
#
# Limitation Of Liability
#
# In no event will DCLC be liable for direct, indirect, special,
# incidental, economic, cover, or consequential damages arising
# out of the use of or inability to use this software even if
# advised of the possibility of such damages. Some states do not
# allow the exclusion or limitation of implied warranties or
# limitation of liability for incidental or consequential
# damages, so the above limitation or exclusion may not apply to
# you.
#
#
# If you have questions regarding this software, contact:
#
# Digital Creations, L.C.
# 910 Princess Ann Street
# Fredericksburge, Virginia 22401
#
# info@digicool.com
#
# (540) 371-6909
#
__doc__='''Synchronized Classes
Module to support classes that "synchronize" method calls, meaning
that only one thread can be in an instance\'s method at one time.
Note that no protection is provided for attribute access. At least,
not at this time
$Id: Sync.py,v 1.1 1997/04/11 21:50:29 jim Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
import ThreadLock
from ExtensionClass import Base
class Synchronized(Base):
def __call_method__(self,f,a,k={}):
try: lock=self._sync__lock
except AttributeError: lock=self._sync__lock=ThreadLock.allocate_lock()
try:
lock.acquire()
return apply(f,a,k)
finally:
lock.release()
/*
$Id: Acquisition.c,v 1.14 1998/01/05 13:38:31 jim Exp $
$Id: Acquisition.c,v 1.15 1998/01/21 19:00:48 jim Exp $
Acquisition Wrappers -- Implementation of acquisition through wrappers
......@@ -147,6 +147,8 @@ typedef struct {
PyObject *container;
} Wrapper;
static Wrapper *freeWrappers=0;
staticforward PyExtensionClass Wrappertype, XaqWrappertype;
#define isWrapper(O) ((O)->ob_type==(PyTypeObject*)&Wrappertype || \
......@@ -175,7 +177,16 @@ newWrapper(PyObject *obj, PyObject *container, PyTypeObject *Wrappertype)
{
Wrapper *self;
if(freeWrappers)
{
self=freeWrappers;
freeWrappers=(Wrapper*)self->obj;
self->ob_type=Wrappertype;
self->ob_refcnt=1;
}
else
UNLESS(self = PyObject_NEW(Wrapper, Wrappertype)) return NULL;
Py_INCREF(obj);
Py_INCREF(container);
self->obj=obj;
......@@ -189,7 +200,8 @@ Wrapper_dealloc(Wrapper *self)
{
Py_DECREF(self->obj);
Py_DECREF(self->container);
PyMem_DEL(self);
self->obj=OBJECT(freeWrappers);
freeWrappers=self;
}
static PyObject *
......@@ -518,7 +530,33 @@ Wrapper_length(Wrapper *self)
long l;
PyObject *r;
UNLESS(r=CallMethodO(OBJECT(self),py__len__,NULL,NULL)) return -1;
UNLESS(r=PyObject_GetAttr(OBJECT(self), py__len__))
{
/* Hm. Maybe we are being checked to see if we are true.
Check to see if we have a __getitem__. If we don't, then
answer that we are true, otherwise raise an error.
*/
PyErr_Clear();
if(r=PyObject_GetAttr(OBJECT(self), py__getitem__))
{
/* Hm, we have getitem, must be error */
Py_DECREF(r);
PyErr_SetObject(PyExc_AttributeError, py__len__);
return -1;
}
PyErr_Clear();
/* Try nonzero */
UNLESS(r=PyObject_GetAttr(OBJECT(self), py__nonzero__))
{
/* No nonzero, it's true :-) */
PyErr_Clear();
return 1;
}
}
UNLESS_ASSIGN(r,PyObject_CallObject(r,NULL)) return -1;
l=PyInt_AsLong(r);
Py_DECREF(r);
return l;
......@@ -766,7 +804,7 @@ void
initAcquisition()
{
PyObject *m, *d;
char *rev="$Revision: 1.14 $";
char *rev="$Revision: 1.15 $";
PURE_MIXIN_CLASS(Acquirer,
"Base class for objects that implicitly"
" acquire attributes from containers\n"
......@@ -785,7 +823,7 @@ initAcquisition()
/* Create the module and add the functions */
m = Py_InitModule4("Acquisition", methods,
"Provide base classes for acquiring objects\n\n"
"$Id: Acquisition.c,v 1.14 1998/01/05 13:38:31 jim Exp $\n",
"$Id: Acquisition.c,v 1.15 1998/01/21 19:00:48 jim Exp $\n",
OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m);
......@@ -808,6 +846,9 @@ initAcquisition()
/*****************************************************************************
$Log: Acquisition.c,v $
Revision 1.15 1998/01/21 19:00:48 jim
Fixed __len__ bugs and added free lists for methods and wrappers
Revision 1.14 1998/01/05 13:38:31 jim
Added special module variable, 'Acquired'. If the value of this
variable is assigned to an attribute, then the value of the attribute
......
/*
$Id: ExtensionClass.c,v 1.22 1998/01/02 18:18:28 jim Exp $
$Id: ExtensionClass.c,v 1.23 1998/01/21 19:00:49 jim Exp $
Extension Class
......@@ -65,7 +65,7 @@ static char ExtensionClass_module_documentation[] =
" - They provide access to unbound methods,\n"
" - They can be called to create instances.\n"
"\n"
"$Id: ExtensionClass.c,v 1.22 1998/01/02 18:18:28 jim Exp $\n"
"$Id: ExtensionClass.c,v 1.23 1998/01/21 19:00:49 jim Exp $\n"
;
#include <stdio.h>
......@@ -102,6 +102,15 @@ staticforward PyExtensionClass ECType;
(((PyExtensionClass*)((O)->ob_type))->class_flags & \
EXTENSIONCLASS_METHODHOOK_FLAG))
#define ALLOC_FREE(T) \
if(free ## T) { \
self=free ## T; \
free ## T=(T*)self->self; \
self->ob_refcnt=1; \
} \
else UNLESS(self = PyObject_NEW(T, & T ## Type)) return NULL;
static PyObject *py__add__, *py__sub__, *py__mul__, *py__div__,
*py__mod__, *py__pow__, *py__divmod__, *py__lshift__, *py__rshift__,
*py__and__, *py__or__, *py__xor__, *py__coerce__, *py__neg__,
......@@ -349,14 +358,16 @@ CMethod_issubclass(PyExtensionClass *sub, PyExtensionClass *type)
(PyExtensionClass *)(C2))
static CMethod *freeCMethod=0;
static PyObject *
newCMethod(PyExtensionClass *type, PyObject *inst,
char *name, PyCFunction meth, int flags, char *doc)
{
CMethod *self;
ALLOC_FREE(CMethod);
UNLESS(self = PyObject_NEW(CMethod, &CMethodType)) return NULL;
Py_INCREF(type);
Py_XINCREF(inst);
self->type=(PyTypeObject*)type;
......@@ -382,7 +393,7 @@ bindCMethod(CMethod *m, PyObject *inst)
return m;
}
UNLESS(self = PyObject_NEW(CMethod, &CMethodType)) return NULL;
ALLOC_FREE(CMethod);
Py_INCREF(inst);
Py_INCREF(m->type);
......@@ -403,7 +414,8 @@ CMethod_dealloc(CMethod *self)
#endif
Py_XDECREF(self->type);
Py_XDECREF(self->self);
PyMem_DEL(self);
self->self=(PyObject*)freeCMethod;
freeCMethod=self;
}
static PyObject *
......@@ -618,6 +630,7 @@ static PyTypeObject CMethodType = {
/* PMethod objects: */
#define PMethod PyECMethodObject
#define PMethodType PyECMethodObjectType
staticforward PyTypeObject PMethodType;
......@@ -630,12 +643,15 @@ staticforward PyTypeObject PMethodType;
&& ! ((PMethod*)(O))->self)
static PMethod *freePMethod=0;
static PyObject *
newPMethod(PyExtensionClass *type, PyObject *meth)
{
PMethod *self;
UNLESS(self = PyObject_NEW(PMethod, &PMethodType)) return NULL;
ALLOC_FREE(PMethod);
Py_INCREF(type);
Py_INCREF(meth);
self->type=(PyTypeObject*)type;
......@@ -659,7 +675,7 @@ bindPMethod(PMethod *m, PyObject *inst)
return (PyObject*)m;
}
UNLESS(self = PyObject_NEW(PMethod, &PMethodType)) return NULL;
ALLOC_FREE(PMethod);
Py_INCREF(inst);
Py_INCREF(m->type);
......@@ -692,7 +708,8 @@ PMethod_dealloc(PMethod *self)
#endif
Py_XDECREF(self->type);
Py_XDECREF(self->self);
PyMem_DEL(self);
self->self=(PyObject*)freePMethod;
freePMethod=self;
#ifdef TRACE_DEALLOC
fprintf(stderr," Done Deallocating PM\n");
#endif
......@@ -2508,7 +2525,24 @@ subclass_length(PyObject *self)
long r;
PyExtensionClass *t;
UNLESS(m=subclass_getspecial(self,py__len__)) return -1;
UNLESS(m=subclass_getspecial(self,py__len__))
{
/* Hm. Maybe we are being checked to see if we are true.
Check to see if we have a __getitem__. If we don't, then
answer that we are true.
*/
PyErr_Clear();
if(m=subclass_getspecial(self,py__getitem__))
{
/* Hm, we have getitem, must be error */
Py_DECREF(m);
PyErr_SetObject(PyExc_AttributeError, py__len__);
return -1;
}
PyErr_Clear();
return subclass_nonzero(self);
}
if(UnboundCMethod_Check(m) && AsCMethod(m)->meth==length_by_name
&& SubclassInstance_Check(self,AsCMethod(m)->type)
&& ! HasMethodHook(self))
......@@ -3285,7 +3319,7 @@ void
initExtensionClass()
{
PyObject *m, *d;
char *rev="$Revision: 1.22 $";
char *rev="$Revision: 1.23 $";
PURE_MIXIN_CLASS(Base, "Minimalbase class for Extension Classes", NULL);
PMethodType.ob_type=&PyType_Type;
......@@ -3326,6 +3360,9 @@ initExtensionClass()
/****************************************************************************
$Log: ExtensionClass.c,v $
Revision 1.23 1998/01/21 19:00:49 jim
Fixed __len__ bugs and added free lists for methods and wrappers
Revision 1.22 1998/01/02 18:18:28 jim
Fixed bug in instance getattr so that instances don't get __bases__
from their class.
......
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