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 $
Sample extension class program that implements multi-mapping objects.
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.L.C.
info@digicool.com
(540) 371-6909
*****************************************************************************/
Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions, and the disclaimer that follows.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
o Neither the name of Digital Creations nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
IS* AND ANY EXPRESS 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 DIGITAL
CREATIONS OR CONTRIBUTORS 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.
$Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $
If you have questions regarding this software,
contact:
Digital Creations L.C.
info@digicool.com
(540) 371-6909
*/
#include "Python.h"
#include "ExtensionClass.h"
......@@ -70,26 +57,22 @@ typedef struct {
staticforward PyExtensionClass MMtype;
static PyObject *
MM_push(self, args)
MMobject *self;
PyObject *args;
MM_push(MMobject *self, PyObject *args)
{
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;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
MM_pop(self, args)
MMobject *self;
PyObject *args;
MM_pop(MMobject *self, PyObject *args)
{
int i=1, l;
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;
i=l-i;
UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL;
......@@ -101,45 +84,95 @@ err:
}
static PyObject *
MM__init__(self, args)
MMobject *self;
PyObject *args;
MM__init__(MMobject *self, PyObject *args)
{
UNLESS(PyArg_Parse(args, "")) 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);
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[] = {
{"__init__", (PyCFunction)MM__init__, 0,
"__init__() -- Create a new empty multi-mapping"},
{"push", (PyCFunction) MM_push, 0,
{"__init__", (PyCFunction)MM__init__, METH_VARARGS,
"__init__([m1, m2, ...]) -- Create a new empty multi-mapping"},
{"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"},
{"pop", (PyCFunction) MM_pop, 0,
"pop() -- Remove and return the last data source added"},
{"pop", (PyCFunction) MM_pop, METH_VARARGS,
"pop([n]) -- Remove and return the last data source added"},
{NULL, NULL} /* sentinel */
};
static void
MM_dealloc(self)
MMobject *self;
MM_dealloc(MMobject *self)
{
Py_XDECREF(self->data);
PyMem_DEL(self);
}
static PyObject *
MM_getattr(self, name)
MMobject *self;
char *name;
MM_getattr(MMobject *self, char *name)
{
return Py_FindMethod(MM_methods, (PyObject *)self, name);
}
static int
MM_length(self)
MMobject *self;
MM_length(MMobject *self)
{
long l=0, el, i;
PyObject *e=0;
......@@ -154,25 +187,6 @@ MM_length(self)
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 = {
(inquiry)MM_length, /*mp_length*/
(binaryfunc)MM_subscript, /*mp_subscript*/
......@@ -219,12 +233,14 @@ void
initMultiMapping()
{
PyObject *m, *d;
char *rev="$Revision: 1.6 $";
m = Py_InitModule4("MultiMapping", MultiMapping_methods,
"MultiMapping -- Wrap multiple mapping objects for lookup"
"\n\n$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $\n",
(PyObject*)NULL,PYTHON_API_VERSION);
char *rev="$Revision: 1.7 $";
m = Py_InitModule4(
"MultiMapping", MultiMapping_methods,
"MultiMapping -- Wrap multiple mapping objects for lookup"
"\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);
PyExtensionClass_Export(d,"MultiMapping",MMtype);
PyDict_SetItemString(d,"__version__",
......@@ -232,28 +248,3 @@ initMultiMapping()
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 $
Sample extension class program that implements multi-mapping objects.
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.L.C.
info@digicool.com
(540) 371-6909
*****************************************************************************/
Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions, and the disclaimer that follows.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
o Neither the name of Digital Creations nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
IS* AND ANY EXPRESS 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 DIGITAL
CREATIONS OR CONTRIBUTORS 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.
$Id: MultiMapping.c,v 1.7 1998/11/17 20:20:17 jim Exp $
If you have questions regarding this software,
contact:
Digital Creations L.C.
info@digicool.com
(540) 371-6909
*/
#include "Python.h"
#include "ExtensionClass.h"
......@@ -70,26 +57,22 @@ typedef struct {
staticforward PyExtensionClass MMtype;
static PyObject *
MM_push(self, args)
MMobject *self;
PyObject *args;
MM_push(MMobject *self, PyObject *args)
{
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;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
MM_pop(self, args)
MMobject *self;
PyObject *args;
MM_pop(MMobject *self, PyObject *args)
{
int i=1, l;
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;
i=l-i;
UNLESS(r=PySequence_GetItem(self->data,l-1)) return NULL;
......@@ -101,45 +84,95 @@ err:
}
static PyObject *
MM__init__(self, args)
MMobject *self;
PyObject *args;
MM__init__(MMobject *self, PyObject *args)
{
UNLESS(PyArg_Parse(args, "")) 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);
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[] = {
{"__init__", (PyCFunction)MM__init__, 0,
"__init__() -- Create a new empty multi-mapping"},
{"push", (PyCFunction) MM_push, 0,
{"__init__", (PyCFunction)MM__init__, METH_VARARGS,
"__init__([m1, m2, ...]) -- Create a new empty multi-mapping"},
{"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"},
{"pop", (PyCFunction) MM_pop, 0,
"pop() -- Remove and return the last data source added"},
{"pop", (PyCFunction) MM_pop, METH_VARARGS,
"pop([n]) -- Remove and return the last data source added"},
{NULL, NULL} /* sentinel */
};
static void
MM_dealloc(self)
MMobject *self;
MM_dealloc(MMobject *self)
{
Py_XDECREF(self->data);
PyMem_DEL(self);
}
static PyObject *
MM_getattr(self, name)
MMobject *self;
char *name;
MM_getattr(MMobject *self, char *name)
{
return Py_FindMethod(MM_methods, (PyObject *)self, name);
}
static int
MM_length(self)
MMobject *self;
MM_length(MMobject *self)
{
long l=0, el, i;
PyObject *e=0;
......@@ -154,25 +187,6 @@ MM_length(self)
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 = {
(inquiry)MM_length, /*mp_length*/
(binaryfunc)MM_subscript, /*mp_subscript*/
......@@ -219,12 +233,14 @@ void
initMultiMapping()
{
PyObject *m, *d;
char *rev="$Revision: 1.6 $";
m = Py_InitModule4("MultiMapping", MultiMapping_methods,
"MultiMapping -- Wrap multiple mapping objects for lookup"
"\n\n$Id: MultiMapping.c,v 1.6 1997/07/16 19:41:35 jim Exp $\n",
(PyObject*)NULL,PYTHON_API_VERSION);
char *rev="$Revision: 1.7 $";
m = Py_InitModule4(
"MultiMapping", MultiMapping_methods,
"MultiMapping -- Wrap multiple mapping objects for lookup"
"\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);
PyExtensionClass_Export(d,"MultiMapping",MMtype);
PyDict_SetItemString(d,"__version__",
......@@ -232,28 +248,3 @@ initMultiMapping()
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