Commit 0a94c2c2 authored by Jim Fulton's avatar Jim Fulton

- One or more mapping objects can be passed to the MultiMapping

        constructor.

      - MultiMapping objects implement the has_key and get methods as
        defined for Python 1.5 dictionaries.

new copyright.
parent 1de3ed16
/* /*
$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $ Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
All rights reserved.
Sample extension class program that implements multi-mapping objects.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Copyright met:
Copyright 1996 Digital Creations, L.C., 910 Princess Anne o Redistributions of source code must retain the above copyright
Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All notice, this list of conditions, and the disclaimer that follows.
rights reserved. Copyright in this software is owned by DCLC,
unless otherwise indicated. Permission to use, copy and o Redistributions in binary form must reproduce the above copyright
distribute this software is hereby granted, provided that the notice, this list of conditions, and the following disclaimer in
above copyright notice appear in all copies and that both that the documentation and/or other materials provided with the
copyright notice and this permission notice appear. Note that distribution.
any product, process or technology described in this software
may be the subject of other Intellectual Property rights o Neither the name of Digital Creations nor the names of its
reserved by Digital Creations, L.C. and are not licensed contributors may be used to endorse or promote products derived
hereunder. from this software without specific prior written permission.
Trademarks
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
Digital Creations & DCLC, are trademarks of Digital Creations, L.C.. IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
All other trademarks are owned by their respective companies. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
No Warranty CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
The software is provided "as is" without warranty of any kind, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
either express or implied, including, but not limited to, the OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
implied warranties of merchantability, fitness for a particular ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
purpose, or non-infringement. This software could include TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
technical inaccuracies or typographical errors. Changes are USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
periodically made to the software; these changes will be DAMAGE.
incorporated in new editions of the software. DCLC may make
improvements and/or changes in this software at any time $Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $
without notice.
If you have questions regarding this software,
Limitation Of Liability contact:
In no event will DCLC be liable for direct, indirect, special, Digital Creations L.C.
incidental, economic, cover, or consequential damages arising info@digicool.com
out of the use of or inability to use this software even if
advised of the possibility of such damages. Some states do not (540) 371-6909
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.L.C.
info@digicool.com
(540) 371-6909
*****************************************************************************/
#include "Python.h" #include "Python.h"
#include "ExtensionClass.h" #include "ExtensionClass.h"
...@@ -70,26 +57,22 @@ typedef struct { ...@@ -70,26 +57,22 @@ typedef struct {
staticforward PyExtensionClass MMtype; staticforward PyExtensionClass MMtype;
static PyObject * static PyObject *
MM_push(self, args) MM_push(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
PyObject *src; PyObject *src;
UNLESS(PyArg_Parse(args, "O", &src)) return NULL; UNLESS(PyArg_ParseTuple(args, "O", &src)) return NULL;
UNLESS(-1 != PyList_Append(self->data,src)) return NULL; UNLESS(-1 != PyList_Append(self->data,src)) return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static PyObject * static PyObject *
MM_pop(self, args) MM_pop(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
int i=1, l; int i=1, l;
PyObject *r; PyObject *r;
if(args) UNLESS(PyArg_Parse(args, "i", &i)) return NULL; if(args) UNLESS(PyArg_ParseTuple(args, "|i", &i)) return NULL;
if((l=PyList_Size(self->data)) < 0) return NULL; if((l=PyList_Size(self->data)) < 0) return NULL;
i=l-i; i=l-i;
UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL; UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL;
...@@ -101,45 +84,95 @@ err: ...@@ -101,45 +84,95 @@ err:
} }
static PyObject * static PyObject *
MM__init__(self, args) MM__init__(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
UNLESS(PyArg_Parse(args, "")) return NULL;
UNLESS(self->data=PyList_New(0)) return NULL; UNLESS(self->data=PyList_New(0)) return NULL;
if (args)
{
int l, i;
if ((l=PyTuple_Size(args)) < 0) return NULL;
for (i=0; i < l; i++)
if (PyList_Append(self->data, PyTuple_GET_ITEM(args, i)) < 0)
return NULL;
}
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static PyObject *
MM_subscript(MMobject *self, PyObject *key)
{
long i;
PyObject *e;
UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
while(--i >= 0)
{
e=PyList_GetItem(self->data,i);
if((e=PyObject_GetItem(e,key))) return e;
PyErr_Clear();
}
PyErr_SetObject(PyExc_KeyError,key);
return NULL;
}
static PyObject *
MM_has_key(MMobject *self, PyObject *args)
{
PyObject *key;
UNLESS(PyArg_ParseTuple(args,"O",&key)) return NULL;
if((key=MM_subscript(self, key)))
{
Py_DECREF(key);
return PyInt_FromLong(1);
}
PyErr_Clear();
return PyInt_FromLong(0);
}
static PyObject *
MM_get(MMobject *self, PyObject *args)
{
PyObject *key, *d=Py_None;
UNLESS(PyArg_ParseTuple(args,"O|O",&key,&d)) return NULL;
if((key=MM_subscript(self, key))) return key;
PyErr_Clear();
Py_INCREF(d);
return d;
}
static struct PyMethodDef MM_methods[] = { static struct PyMethodDef MM_methods[] = {
{"__init__", (PyCFunction)MM__init__, 0, {"__init__", (PyCFunction)MM__init__, METH_VARARGS,
"__init__() -- Create a new empty multi-mapping"}, "__init__([m1, m2, ...]) -- Create a new empty multi-mapping"},
{"push", (PyCFunction) MM_push, 0, {"get", (PyCFunction) MM_get, METH_VARARGS,
"get(key,[default]) -- Return a value for the given key or a default"},
{"has_key", (PyCFunction) MM_has_key, METH_VARARGS,
"has_key(key) -- Return 1 if the mapping has the key, and 0 otherwise"},
{"push", (PyCFunction) MM_push, METH_VARARGS,
"push(mapping_object) -- Add a data source"}, "push(mapping_object) -- Add a data source"},
{"pop", (PyCFunction) MM_pop, 0, {"pop", (PyCFunction) MM_pop, METH_VARARGS,
"pop() -- Remove and return the last data source added"}, "pop([n]) -- Remove and return the last data source added"},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static void static void
MM_dealloc(self) MM_dealloc(MMobject *self)
MMobject *self;
{ {
Py_XDECREF(self->data); Py_XDECREF(self->data);
PyMem_DEL(self); PyMem_DEL(self);
} }
static PyObject * static PyObject *
MM_getattr(self, name) MM_getattr(MMobject *self, char *name)
MMobject *self;
char *name;
{ {
return Py_FindMethod(MM_methods, (PyObject *)self, name); return Py_FindMethod(MM_methods, (PyObject *)self, name);
} }
static int static int
MM_length(self) MM_length(MMobject *self)
MMobject *self;
{ {
long l=0, el, i; long l=0, el, i;
PyObject *e=0; PyObject *e=0;
...@@ -154,25 +187,6 @@ MM_length(self) ...@@ -154,25 +187,6 @@ MM_length(self)
return l; return l;
} }
static PyObject *
MM_subscript(self, key)
MMobject *self;
PyObject *key;
{
long i;
PyObject *e;
UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
while(--i >= 0)
{
e=PyList_GetItem(self->data,i);
if((e=PyObject_GetItem(e,key))) return e;
PyErr_Clear();
}
PyErr_SetObject(PyExc_KeyError,key);
return NULL;
}
static PyMappingMethods MM_as_mapping = { static PyMappingMethods MM_as_mapping = {
(inquiry)MM_length, /*mp_length*/ (inquiry)MM_length, /*mp_length*/
(binaryfunc)MM_subscript, /*mp_subscript*/ (binaryfunc)MM_subscript, /*mp_subscript*/
...@@ -219,12 +233,14 @@ void ...@@ -219,12 +233,14 @@ void
initMultiMapping() initMultiMapping()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.6 $"; char *rev="$Revision: 1.7 $";
m = Py_InitModule4("MultiMapping", MultiMapping_methods, m = Py_InitModule4(
"MultiMapping -- Wrap multiple mapping objects for lookup" "MultiMapping", MultiMapping_methods,
"\n\n$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $\n", "MultiMapping -- Wrap multiple mapping objects for lookup"
(PyObject*)NULL,PYTHON_API_VERSION); "\n\n"
"$Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $\n",
(PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
PyExtensionClass_Export(d,"MultiMapping",MMtype); PyExtensionClass_Export(d,"MultiMapping",MMtype);
PyDict_SetItemString(d,"__version__", PyDict_SetItemString(d,"__version__",
...@@ -232,28 +248,3 @@ initMultiMapping() ...@@ -232,28 +248,3 @@ initMultiMapping()
if (PyErr_Occurred()) Py_FatalError("can't initialize module MultiMapping"); if (PyErr_Occurred()) Py_FatalError("can't initialize module MultiMapping");
} }
/*****************************************************************************
$Log: MultiMapping.c,v $
Revision 1.6 1997/07/16 19:41:35 jim
Change for win32
Revision 1.5 1997/07/02 20:20:12 jim
Added stupid parens and other changes to make 'gcc -Wall -pedantic'
happy.
Revision 1.4 1997/06/19 19:36:22 jim
Added ident string.
Revision 1.3 1997/02/17 16:34:09 jim
Made changes to be more useful for DocumentTemplates.
Revision 1.2 1996/10/23 18:37:45 jim
Fixed misspelling in class name.
Revision 1.1 1996/10/22 22:27:42 jim
*** empty log message ***
*****************************************************************************/
/* /*
$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $ Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
All rights reserved.
Sample extension class program that implements multi-mapping objects.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Copyright met:
Copyright 1996 Digital Creations, L.C., 910 Princess Anne o Redistributions of source code must retain the above copyright
Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All notice, this list of conditions, and the disclaimer that follows.
rights reserved. Copyright in this software is owned by DCLC,
unless otherwise indicated. Permission to use, copy and o Redistributions in binary form must reproduce the above copyright
distribute this software is hereby granted, provided that the notice, this list of conditions, and the following disclaimer in
above copyright notice appear in all copies and that both that the documentation and/or other materials provided with the
copyright notice and this permission notice appear. Note that distribution.
any product, process or technology described in this software
may be the subject of other Intellectual Property rights o Neither the name of Digital Creations nor the names of its
reserved by Digital Creations, L.C. and are not licensed contributors may be used to endorse or promote products derived
hereunder. from this software without specific prior written permission.
Trademarks
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
Digital Creations & DCLC, are trademarks of Digital Creations, L.C.. IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
All other trademarks are owned by their respective companies. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
No Warranty CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
The software is provided "as is" without warranty of any kind, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
either express or implied, including, but not limited to, the OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
implied warranties of merchantability, fitness for a particular ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
purpose, or non-infringement. This software could include TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
technical inaccuracies or typographical errors. Changes are USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
periodically made to the software; these changes will be DAMAGE.
incorporated in new editions of the software. DCLC may make
improvements and/or changes in this software at any time $Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $
without notice.
If you have questions regarding this software,
Limitation Of Liability contact:
In no event will DCLC be liable for direct, indirect, special, Digital Creations L.C.
incidental, economic, cover, or consequential damages arising info@digicool.com
out of the use of or inability to use this software even if
advised of the possibility of such damages. Some states do not (540) 371-6909
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.L.C.
info@digicool.com
(540) 371-6909
*****************************************************************************/
#include "Python.h" #include "Python.h"
#include "ExtensionClass.h" #include "ExtensionClass.h"
...@@ -70,26 +57,22 @@ typedef struct { ...@@ -70,26 +57,22 @@ typedef struct {
staticforward PyExtensionClass MMtype; staticforward PyExtensionClass MMtype;
static PyObject * static PyObject *
MM_push(self, args) MM_push(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
PyObject *src; PyObject *src;
UNLESS(PyArg_Parse(args, "O", &src)) return NULL; UNLESS(PyArg_ParseTuple(args, "O", &src)) return NULL;
UNLESS(-1 != PyList_Append(self->data,src)) return NULL; UNLESS(-1 != PyList_Append(self->data,src)) return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static PyObject * static PyObject *
MM_pop(self, args) MM_pop(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
int i=1, l; int i=1, l;
PyObject *r; PyObject *r;
if(args) UNLESS(PyArg_Parse(args, "i", &i)) return NULL; if(args) UNLESS(PyArg_ParseTuple(args, "|i", &i)) return NULL;
if((l=PyList_Size(self->data)) < 0) return NULL; if((l=PyList_Size(self->data)) < 0) return NULL;
i=l-i; i=l-i;
UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL; UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL;
...@@ -101,45 +84,95 @@ err: ...@@ -101,45 +84,95 @@ err:
} }
static PyObject * static PyObject *
MM__init__(self, args) MM__init__(MMobject *self, PyObject *args)
MMobject *self;
PyObject *args;
{ {
UNLESS(PyArg_Parse(args, "")) return NULL;
UNLESS(self->data=PyList_New(0)) return NULL; UNLESS(self->data=PyList_New(0)) return NULL;
if (args)
{
int l, i;
if ((l=PyTuple_Size(args)) < 0) return NULL;
for (i=0; i < l; i++)
if (PyList_Append(self->data, PyTuple_GET_ITEM(args, i)) < 0)
return NULL;
}
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static PyObject *
MM_subscript(MMobject *self, PyObject *key)
{
long i;
PyObject *e;
UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
while(--i >= 0)
{
e=PyList_GetItem(self->data,i);
if((e=PyObject_GetItem(e,key))) return e;
PyErr_Clear();
}
PyErr_SetObject(PyExc_KeyError,key);
return NULL;
}
static PyObject *
MM_has_key(MMobject *self, PyObject *args)
{
PyObject *key;
UNLESS(PyArg_ParseTuple(args,"O",&key)) return NULL;
if((key=MM_subscript(self, key)))
{
Py_DECREF(key);
return PyInt_FromLong(1);
}
PyErr_Clear();
return PyInt_FromLong(0);
}
static PyObject *
MM_get(MMobject *self, PyObject *args)
{
PyObject *key, *d=Py_None;
UNLESS(PyArg_ParseTuple(args,"O|O",&key,&d)) return NULL;
if((key=MM_subscript(self, key))) return key;
PyErr_Clear();
Py_INCREF(d);
return d;
}
static struct PyMethodDef MM_methods[] = { static struct PyMethodDef MM_methods[] = {
{"__init__", (PyCFunction)MM__init__, 0, {"__init__", (PyCFunction)MM__init__, METH_VARARGS,
"__init__() -- Create a new empty multi-mapping"}, "__init__([m1, m2, ...]) -- Create a new empty multi-mapping"},
{"push", (PyCFunction) MM_push, 0, {"get", (PyCFunction) MM_get, METH_VARARGS,
"get(key,[default]) -- Return a value for the given key or a default"},
{"has_key", (PyCFunction) MM_has_key, METH_VARARGS,
"has_key(key) -- Return 1 if the mapping has the key, and 0 otherwise"},
{"push", (PyCFunction) MM_push, METH_VARARGS,
"push(mapping_object) -- Add a data source"}, "push(mapping_object) -- Add a data source"},
{"pop", (PyCFunction) MM_pop, 0, {"pop", (PyCFunction) MM_pop, METH_VARARGS,
"pop() -- Remove and return the last data source added"}, "pop([n]) -- Remove and return the last data source added"},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static void static void
MM_dealloc(self) MM_dealloc(MMobject *self)
MMobject *self;
{ {
Py_XDECREF(self->data); Py_XDECREF(self->data);
PyMem_DEL(self); PyMem_DEL(self);
} }
static PyObject * static PyObject *
MM_getattr(self, name) MM_getattr(MMobject *self, char *name)
MMobject *self;
char *name;
{ {
return Py_FindMethod(MM_methods, (PyObject *)self, name); return Py_FindMethod(MM_methods, (PyObject *)self, name);
} }
static int static int
MM_length(self) MM_length(MMobject *self)
MMobject *self;
{ {
long l=0, el, i; long l=0, el, i;
PyObject *e=0; PyObject *e=0;
...@@ -154,25 +187,6 @@ MM_length(self) ...@@ -154,25 +187,6 @@ MM_length(self)
return l; return l;
} }
static PyObject *
MM_subscript(self, key)
MMobject *self;
PyObject *key;
{
long i;
PyObject *e;
UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
while(--i >= 0)
{
e=PyList_GetItem(self->data,i);
if((e=PyObject_GetItem(e,key))) return e;
PyErr_Clear();
}
PyErr_SetObject(PyExc_KeyError,key);
return NULL;
}
static PyMappingMethods MM_as_mapping = { static PyMappingMethods MM_as_mapping = {
(inquiry)MM_length, /*mp_length*/ (inquiry)MM_length, /*mp_length*/
(binaryfunc)MM_subscript, /*mp_subscript*/ (binaryfunc)MM_subscript, /*mp_subscript*/
...@@ -219,12 +233,14 @@ void ...@@ -219,12 +233,14 @@ void
initMultiMapping() initMultiMapping()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.6 $"; char *rev="$Revision: 1.7 $";
m = Py_InitModule4("MultiMapping", MultiMapping_methods, m = Py_InitModule4(
"MultiMapping -- Wrap multiple mapping objects for lookup" "MultiMapping", MultiMapping_methods,
"\n\n$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $\n", "MultiMapping -- Wrap multiple mapping objects for lookup"
(PyObject*)NULL,PYTHON_API_VERSION); "\n\n"
"$Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $\n",
(PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
PyExtensionClass_Export(d,"MultiMapping",MMtype); PyExtensionClass_Export(d,"MultiMapping",MMtype);
PyDict_SetItemString(d,"__version__", PyDict_SetItemString(d,"__version__",
...@@ -232,28 +248,3 @@ initMultiMapping() ...@@ -232,28 +248,3 @@ initMultiMapping()
if (PyErr_Occurred()) Py_FatalError("can't initialize module MultiMapping"); if (PyErr_Occurred()) Py_FatalError("can't initialize module MultiMapping");
} }
/*****************************************************************************
$Log: MultiMapping.c,v $
Revision 1.6 1997/07/16 19:41:35 jim
Change for win32
Revision 1.5 1997/07/02 20:20:12 jim
Added stupid parens and other changes to make 'gcc -Wall -pedantic'
happy.
Revision 1.4 1997/06/19 19:36:22 jim
Added ident string.
Revision 1.3 1997/02/17 16:34:09 jim
Made changes to be more useful for DocumentTemplates.
Revision 1.2 1996/10/23 18:37:45 jim
Fixed misspelling in class name.
Revision 1.1 1996/10/22 22:27:42 jim
*** empty log message ***
*****************************************************************************/
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