[Zope-dev] FIX for the infamous "__call__" bug - testers needed!

Brian Lloyd brian@digicool.com
Mon, 13 Nov 2000 11:52:16 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_012B_01C04D68.2B1778F0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi all - 

One of the things I _really_ _really_ wanted to fix for 
the upcoming 2.2.3 release is the infamous "__call__" bug. 

I have a fix that works according to my limited testing - 
what I really need is for a few volunteers familiar with 
the problem to take a few minutes to verify the fix and 
let me know if the problem goes away for them as well.

Attached is an updated version of cDocumentTemplate.c. You 
can drop this into the lib/python/DocumentTemplate directory 
of your installation and rebuild it - after restarting the 
Zope site, the __call__ problem should be gone.

I'd really like to hear from some folks on this so I can 
get the 2.2.3 release wrapped up - thanks!


Brian Lloyd        brian@digicool.com
Software Engineer  540.371.6909              
Digital Creations  http://www.digicool.com 



------=_NextPart_000_012B_01C04D68.2B1778F0
Content-Type: application/octet-stream;
	name="cDocumentTemplate.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="cDocumentTemplate.c"

/************************************************************************=
*****=0A=
  =0A=
  Zope Public License (ZPL) Version 1.0=0A=
  -------------------------------------=0A=
  =0A=
  Copyright (c) Digital Creations.  All rights reserved.=0A=
  =0A=
  This license has been certified as Open Source(tm).=0A=
  =0A=
  Redistribution and use in source and binary forms, with or without=0A=
  modification, are permitted provided that the following conditions are=0A=
  met:=0A=
  =0A=
  1. Redistributions in source code must retain the above copyright=0A=
     notice, this list of conditions, and the following disclaimer.=0A=
  =0A=
  2. Redistributions in binary form must reproduce the above copyright=0A=
     notice, this list of conditions, and the following disclaimer in=0A=
     the documentation and/or other materials provided with the=0A=
     distribution.=0A=
  =0A=
  3. Digital Creations requests that attribution be given to Zope=0A=
     in any manner possible. Zope includes a "Powered by Zope"=0A=
     button that is installed by default. While it is not a license=0A=
     violation to remove this button, it is requested that the=0A=
     attribution remain. A significant investment has been put=0A=
     into Zope, and this effort will continue if the Zope community=0A=
     continues to grow. This is one way to assure that growth.=0A=
  =0A=
  4. All advertising materials and documentation mentioning=0A=
     features derived from or use of this software must display=0A=
     the following acknowledgement:=0A=
  =0A=
       "This product includes software developed by Digital Creations=0A=
       for use in the Z Object Publishing Environment=0A=
       (http://www.zope.org/)."=0A=
  =0A=
     In the event that the product being advertised includes an=0A=
     intact Zope distribution (with copyright and license included)=0A=
     then this clause is waived.=0A=
  =0A=
  5. Names associated with Zope or Digital Creations must not be used to=0A=
     endorse or promote products derived from this software without=0A=
     prior written permission from Digital Creations.=0A=
  =0A=
  6. Modified redistributions of any form whatsoever must retain=0A=
     the following acknowledgment:=0A=
  =0A=
       "This product includes software developed by Digital Creations=0A=
       for use in the Z Object Publishing Environment=0A=
       (http://www.zope.org/)."=0A=
  =0A=
     Intact (re-)distributions of any official Zope release do not=0A=
     require an external acknowledgement.=0A=
  =0A=
  7. Modifications are encouraged but must be packaged separately as=0A=
     patches to official Zope releases.  Distributions that do not=0A=
     clearly separate the patches from the original work must be clearly=0A=
     labeled as unofficial distributions.  Modifications which do not=0A=
     carry the name Zope may be packaged in any form, as long as they=0A=
     conform to all of the clauses above.=0A=
  =0A=
  =0A=
  Disclaimer=0A=
  =0A=
    THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY=0A=
    EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE=0A=
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR=0A=
    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS=0A=
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,=0A=
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT=0A=
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF=0A=
    USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND=0A=
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,=0A=
    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT=0A=
    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF=0A=
    SUCH DAMAGE.=0A=
  =0A=
  =0A=
  This software consists of contributions made by Digital Creations and=0A=
  many individuals on behalf of Digital Creations.  Specific=0A=
  attributions are listed in the accompanying credits file.=0A=
  =0A=
 =
*************************************************************************=
***/=0A=
static char cDocumentTemplate_module_documentation[] =3D =0A=
""=0A=
"\n$Id: cDocumentTemplate.c,v 1.32.18.2 2000/11/13 16:11:20 brian Exp $"=0A=
;=0A=
=0A=
#include "ExtensionClass.h"=0A=
=0A=
static PyObject *py_isDocTemp=3D0, *py_blocks=3D0, *py_=3D0, *join=3D0, =
*py_acquire;=0A=
static PyObject *py___call__, *py___roles__, *py_AUTHENTICATED_USER;=0A=
static PyObject *py_hasRole, *py__proxy_roles, *py_Unauthorized;=0A=
static PyObject *py_Unauthorized_fmt, *py_validate;=0A=
static PyObject *py__push, *py__pop, *py_aq_base;=0A=
=0A=
/* ----------------------------------------------------- */=0A=
=0A=
static void PyVar_Assign(PyObject **v, PyObject *e) { Py_XDECREF(*v); =
*v=3De;}=0A=
#define ASSIGN(V,E) PyVar_Assign(&(V),(E))=0A=
#define UNLESS(E) if (!(E))=0A=
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E); UNLESS(V)=0A=
#define OBJECT(O)(((PyObject*)O))=0A=
=0A=
typedef struct {=0A=
  PyObject_HEAD=0A=
  PyObject *inst;=0A=
  PyObject *cache;=0A=
  PyObject *namespace;=0A=
  PyObject *validate;=0A=
} InstanceDictobject;=0A=
=0A=
staticforward PyExtensionClass InstanceDictType;=0A=
=0A=
static PyObject *=0A=
InstanceDict___init__(InstanceDictobject *self, PyObject *args)=0A=
{=0A=
  self->validate=3DNULL;=0A=
  UNLESS(PyArg_ParseTuple(args, "OO|O",=0A=
			  &(self->inst),=0A=
			  &(self->namespace),=0A=
			  &(self->validate)))=0A=
    return NULL;=0A=
  Py_INCREF(self->inst);=0A=
  Py_INCREF(self->namespace);=0A=
  if (self->validate)=0A=
    Py_INCREF(self->validate);=0A=
  else=0A=
    UNLESS(self->validate=3DPyObject_GetAttr(self->namespace, =
py_validate))=0A=
       return NULL;=0A=
    =0A=
  UNLESS(self->cache=3DPyDict_New()) return NULL;=0A=
  Py_INCREF(Py_None);=0A=
  return Py_None;=0A=
}=0A=
=0A=
static struct PyMethodDef InstanceDict_methods[] =3D {=0A=
  {"__init__",	(PyCFunction)InstanceDict___init__, 1,=0A=
   ""},=0A=
  =0A=
  {NULL,		NULL}		/* sentinel */=0A=
};=0A=
=0A=
/* ---------- */=0A=
=0A=
static void=0A=
InstanceDict_dealloc(InstanceDictobject *self)=0A=
{=0A=
  Py_XDECREF(self->inst);=0A=
  Py_XDECREF(self->cache);=0A=
  Py_XDECREF(self->namespace);=0A=
  Py_XDECREF(self->validate);=0A=
  Py_DECREF(self->ob_type);=0A=
  PyMem_DEL(self);=0A=
}=0A=
=0A=
static PyObject *=0A=
InstanceDict_getattr(InstanceDictobject *self, PyObject *name)=0A=
{=0A=
  return Py_FindAttr((PyObject *)self, name);=0A=
}=0A=
=0A=
static PyObject *=0A=
InstanceDict_repr(InstanceDictobject *self)=0A=
{=0A=
  return PyObject_Repr(self->inst);=0A=
}=0A=
=0A=
/* Code to access InstanceDict objects as mappings */=0A=
=0A=
static int=0A=
InstanceDict_length( InstanceDictobject *self)=0A=
{=0A=
  return 1;=0A=
}=0A=
=0A=
static PyObject *=0A=
InstanceDict_subscript( InstanceDictobject *self, PyObject *key)=0A=
{=0A=
  PyObject *r, *v;=0A=
  char *name;=0A=
  =0A=
  /* Try to get value from the cache */=0A=
  if (r=3DPyObject_GetItem(self->cache, key)) return r;=0A=
  PyErr_Clear();=0A=
  =0A=
  /* Check for __str__ */=0A=
  UNLESS(name=3DPyString_AsString(key)) return NULL;=0A=
  if (*name=3D=3D'_')=0A=
    {=0A=
      UNLESS(strcmp(name,"__str__")=3D=3D0) goto KeyError;=0A=
      return PyObject_Str(self->inst);=0A=
    }=0A=
  =0A=
  /* Do explicit acquisition with "roles" rule */=0A=
  if (r=3DPyObject_GetAttr(self->inst, py_acquire))=0A=
    {=0A=
      /* Sanity check in case of explicit Aq */=0A=
      if (v=3DPyObject_GetAttr(self->inst, key)) Py_DECREF(v);  =0A=
      else =0A=
	{=0A=
	  Py_DECREF(r);=0A=
	  goto KeyError;=0A=
	}=0A=
=0A=
      if (self->validate !=3D Py_None)=0A=
	{=0A=
	  UNLESS_ASSIGN(r,PyObject_CallFunction(=0A=
		 r, "OOO", key, self->validate, self->namespace))=0A=
	    {=0A=
	      PyObject *tb;=0A=
=0A=
	      PyErr_Fetch(&r, &v, &tb);=0A=
	      if (r !=3D PyExc_AttributeError || PyObject_Compare(v,key))=0A=
		{=0A=
		  PyErr_Restore(r,v,tb);=0A=
		  return NULL;=0A=
		}=0A=
	      Py_XDECREF(r);=0A=
	      Py_XDECREF(v);=0A=
	      Py_XDECREF(tb);=0A=
	      =0A=
	      goto KeyError;=0A=
	    }=0A=
	}=0A=
      else=0A=
	UNLESS_ASSIGN(r, PyObject_GetAttr(self->inst, key)) goto KeyError;=0A=
    }  =0A=
  else=0A=
    {=0A=
      PyErr_Clear();=0A=
=0A=
      /* OK, use getattr */=0A=
      UNLESS(r=3DPyObject_GetAttr(self->inst, key)) goto KeyError;=0A=
=0A=
      if (self->validate !=3D Py_None)=0A=
	{=0A=
	  UNLESS(v=3DPyObject_CallFunction(=0A=
	    self->validate,"OOOOO",=0A=
	    self->inst, self->inst, key, r, self->namespace))=0A=
	    return NULL;=0A=
	  Py_DECREF(v);=0A=
	}=0A=
    }=0A=
  =0A=
  if (r && PyObject_SetItem(self->cache, key, r) < 0) PyErr_Clear();=0A=
  =0A=
  return r;=0A=
  =0A=
KeyError:=0A=
  PyErr_SetObject(PyExc_KeyError, key);=0A=
  return NULL;=0A=
}=0A=
=0A=
static int=0A=
InstanceDict_ass_sub( InstanceDictobject *self, PyObject *v, PyObject *w)=0A=
{=0A=
  PyErr_SetString(PyExc_TypeError,=0A=
		  "InstanceDict objects do not support item assignment");=0A=
  return -1;=0A=
}=0A=
=0A=
static PyMappingMethods InstanceDict_as_mapping =3D {=0A=
  (inquiry)InstanceDict_length,		/*mp_length*/=0A=
  (binaryfunc)InstanceDict_subscript,		/*mp_subscript*/=0A=
  (objobjargproc)InstanceDict_ass_sub,	/*mp_ass_subscript*/=0A=
};=0A=
=0A=
/* -------------------------------------------------------- */=0A=
=0A=
=0A=
static char InstanceDicttype__doc__[] =3D =0A=
""=0A=
;=0A=
=0A=
static PyExtensionClass InstanceDictType =3D {=0A=
  PyObject_HEAD_INIT(NULL)=0A=
  0,				/*ob_size*/=0A=
  "InstanceDict",			/*tp_name*/=0A=
  sizeof(InstanceDictobject),	/*tp_basicsize*/=0A=
  0,				/*tp_itemsize*/=0A=
  /* methods */=0A=
  (destructor)InstanceDict_dealloc,	/*tp_dealloc*/=0A=
  (printfunc)0,	/*tp_print*/=0A=
  (getattrfunc)0,		/*obsolete tp_getattr*/=0A=
  (setattrfunc)0,		/*obsolete tp_setattr*/=0A=
  (cmpfunc)0,	/*tp_compare*/=0A=
  (reprfunc)InstanceDict_repr,		/*tp_repr*/=0A=
  0,		/*tp_as_number*/=0A=
  0,		/*tp_as_sequence*/=0A=
  &InstanceDict_as_mapping,		/*tp_as_mapping*/=0A=
  (hashfunc)0,		/*tp_hash*/=0A=
  (ternaryfunc)0,	/*tp_call*/=0A=
  (reprfunc)0,		/*tp_str*/=0A=
  (getattrofunc)InstanceDict_getattr,			/*tp_getattro*/=0A=
  0,			/*tp_setattro*/=0A=
  =0A=
  /* Space for future expansion */=0A=
  0L,0L,=0A=
  InstanceDicttype__doc__, /* Documentation string */=0A=
  METHOD_CHAIN(InstanceDict_methods)=0A=
};=0A=
=0A=
typedef struct {=0A=
  PyObject_HEAD=0A=
  int level;=0A=
  PyObject *dict;=0A=
  PyObject *data;=0A=
} MM;=0A=
=0A=
staticforward PyExtensionClass MMtype;=0A=
=0A=
static PyObject *=0A=
MM_push(self, args)=0A=
	MM *self;=0A=
	PyObject *args;=0A=
{=0A=
  PyObject *src;=0A=
  UNLESS(PyArg_Parse(args, "O", &src)) return NULL;=0A=
  UNLESS(-1 !=3D PyList_Append(self->data,src)) return NULL;=0A=
  Py_INCREF(Py_None);=0A=
  return Py_None;=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_pop(self, args)=0A=
	MM *self;=0A=
	PyObject *args;=0A=
{=0A=
  int i=3D1, l;=0A=
  PyObject *r;=0A=
=0A=
  if (args) UNLESS(PyArg_Parse(args, "i", &i)) return NULL;=0A=
  if ((l=3DPyList_Size(self->data)) < 0) return NULL;=0A=
  i=3Dl-i;=0A=
  UNLESS(r=3DPySequence_GetItem(self->data,l-1)) return NULL;=0A=
  if (PyList_SetSlice(self->data,i,l,NULL) < 0) goto err;=0A=
  return r;=0A=
err:=0A=
  Py_DECREF(r);=0A=
  return NULL;=0A=
}=0A=
=0A=
static PyObject *=0A=
MM__init__(self, args)=0A=
     MM *self;=0A=
     PyObject *args;=0A=
{=0A=
  UNLESS(PyArg_Parse(args, "")) return NULL;=0A=
  UNLESS(self->data=3DPyList_New(0)) return NULL;=0A=
  self->dict=3DNULL;=0A=
  self->level=3D0;=0A=
  Py_INCREF(Py_None);=0A=
  return Py_None;=0A=
}=0A=
=0A=
=0A=
=0A=
static int =0A=
dtObjectIsCallable(PyObject *ob) {=0A=
  PyObject *base=3D0;=0A=
  int result=3D0;=0A=
=0A=
  /* Ensure that an object is really callable by unwrapping it */=0A=
  UNLESS(base=3DPyObject_GetAttr(ob, py_aq_base)) {=0A=
    PyErr_Clear();=0A=
    return PyCallable_Check(ob);=0A=
  }=0A=
  result=3DPyCallable_Check(base);=0A=
  Py_DECREF(base);=0A=
  return result;=0A=
}=0A=
=0A=
static int=0A=
dtObjectIsDocTemp(PyObject *ob) {=0A=
  PyObject *base=3D0;=0A=
  PyObject *value=3D0;=0A=
  int result=3D0;=0A=
=0A=
  /* Ensure that 'isDocTemp' is not acquired */=0A=
  UNLESS(base=3DPyObject_GetAttr(ob, py_aq_base)) {=0A=
    PyErr_Clear();=0A=
    base =3D ob;=0A=
    Py_INCREF(base);=0A=
  }=0A=
=0A=
  if ( value =3D PyObject_GetAttr(base, py_isDocTemp) ) {=0A=
    if (PyObject_IsTrue(value)) {=0A=
      result =3D 1;=0A=
    }=0A=
    Py_DECREF(value);=0A=
  }=0A=
  else PyErr_Clear();=0A=
=0A=
  Py_DECREF(base);=0A=
  return result;=0A=
}=0A=
=0A=
=0A=
static PyObject *=0A=
MM_cget(MM *self, PyObject *key, int call)=0A=
{=0A=
  long i;=0A=
  int dt=3D0;=0A=
  PyObject *e, *t, *rr, *tb;=0A=
=0A=
  UNLESS(-1 !=3D (i=3DPyList_Size(self->data))) return NULL;=0A=
  while (--i >=3D 0)=0A=
    {=0A=
      e=3DPyList_GetItem(self->data,i);=0A=
      if (e=3DPyObject_GetItem(e,key))=0A=
	{=0A=
	  dt=3D0;=0A=
=0A=
=0A=
=0A=
	  /*	  if (PyCallable_Check(e)) */=0A=
=0A=
	  if (dtObjectIsCallable(e))=0A=
	    {=0A=
=0A=
	      =0A=
	      /* Decide whether we have a document template =0A=
	      if (rr=3DPyObject_GetAttr(e,py_isDocTemp))=0A=
		{=0A=
		  if (PyObject_IsTrue(rr)) dt=3D1;=0A=
		  Py_DECREF(rr);=0A=
		}=0A=
	      else PyErr_Clear();=0A=
	      */=0A=
=0A=
	      dt =3D dtObjectIsDocTemp(e);=0A=
=0A=
=0A=
	      /* Try calling the object */=0A=
	      if (call)=0A=
		{=0A=
		  if (dt)=0A=
		    {=0A=
		      ASSIGN(e,PyObject_CallFunction(e,"OO", Py_None, self));=0A=
		      UNLESS(e)=0A=
			return NULL;=0A=
		    }=0A=
		  else=0A=
		    {=0A=
		      rr=3DPyObject_CallObject(e,NULL);=0A=
		      if (rr) ASSIGN(e,rr);=0A=
		      else=0A=
			{=0A=
			  PyErr_Fetch(&t, &rr, &tb);=0A=
			  if (t!=3DPyExc_AttributeError ||=0A=
			     PyObject_Compare(rr,py___call__) !=3D 0)=0A=
			    {=0A=
			      PyErr_Restore(t,rr,tb);=0A=
			      Py_DECREF(e);=0A=
			      return NULL;=0A=
			    }=0A=
			  /* =0A=
                            Added by Brian on 08/30/99. We need to be =
sure=0A=
                            to DECREF the exception in the event of an =0A=
                            AttributeError to avoid leaking.=0A=
                          */=0A=
			  else {=0A=
			    Py_XDECREF(t);=0A=
			    Py_XDECREF(rr);=0A=
			    Py_XDECREF(tb);=0A=
			  }=0A=
=0A=
			}=0A=
		    }=0A=
		}=0A=
	    }=0A=
=0A=
	  return e;=0A=
	}=0A=
      PyErr_Fetch(&e, &rr, &tb);=0A=
      if (dt || e !=3D PyExc_KeyError)=0A=
	{=0A=
	  PyErr_Restore(e,rr,tb);=0A=
	  return NULL;=0A=
	}=0A=
      Py_XDECREF(e);=0A=
      Py_XDECREF(rr);=0A=
      Py_XDECREF(tb);=0A=
    }=0A=
  PyErr_SetObject(PyExc_KeyError,key);=0A=
  return NULL;=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_get(MM *self, PyObject *args)=0A=
{=0A=
  PyObject *key, *call=3DPy_None;=0A=
=0A=
  UNLESS(PyArg_ParseTuple(args,"O|O",&key,&call)) return NULL;=0A=
  return MM_cget(self, key, PyObject_IsTrue(call));=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_has_key(MM *self, PyObject *args)=0A=
{=0A=
  PyObject *key;=0A=
=0A=
  UNLESS(PyArg_ParseTuple(args,"O",&key)) return NULL;=0A=
  if ((key=3DMM_cget(self, key, 0)))=0A=
    {=0A=
      Py_DECREF(key);=0A=
      return PyInt_FromLong(1);=0A=
    }=0A=
  PyErr_Clear();=0A=
  return PyInt_FromLong(0);=0A=
}=0A=
=0A=
static struct PyMethodDef MM_methods[] =3D {=0A=
  {"__init__", (PyCFunction)MM__init__, 0,=0A=
   "__init__() -- Create a new empty multi-mapping"},=0A=
  {"_push", (PyCFunction) MM_push, 0,=0A=
   "_push(mapping_object) -- Add a data source"},=0A=
  {"_pop",  (PyCFunction) MM_pop,  0,=0A=
   "_pop() -- Remove and return the last data source added"}, =0A=
  {"getitem",  (PyCFunction) MM_get,  METH_VARARGS,=0A=
   "getitem(key[,call]) -- Get a value\n\n"=0A=
   "Normally, callable objects that can be called without arguments =
are\n"=0A=
   "called during retrieval. This can be suppressed by providing a\n"=0A=
   "second argument that is false.\n"=0A=
  }, =0A=
  {"has_key",  (PyCFunction) MM_has_key,  METH_VARARGS,=0A=
   "has_key(key) -- Test whether the mapping has the given key"=0A=
  }, =0A=
  {NULL,		NULL}		/* sentinel */=0A=
};=0A=
=0A=
static void=0A=
MM_dealloc(self)=0A=
     MM *self;=0A=
{=0A=
  Py_XDECREF(self->data);=0A=
  Py_XDECREF(self->dict);=0A=
  Py_DECREF(self->ob_type);=0A=
  PyMem_DEL(self);=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_getattro(MM *self, PyObject *name)=0A=
{=0A=
  if (PyString_Check(name))=0A=
    {=0A=
      if (strcmp(PyString_AsString(name),"level")=3D=3D0)=0A=
	return PyInt_FromLong(self->level);=0A=
    }=0A=
  =0A=
  if (self->dict)=0A=
    {=0A=
      PyObject *v;=0A=
=0A=
      if (v=3DPyDict_GetItem(self->dict, name))=0A=
	{=0A=
	  Py_INCREF(v);=0A=
	  return v;=0A=
	}=0A=
    }=0A=
  =0A=
  return Py_FindAttr((PyObject *)self, name);=0A=
}=0A=
=0A=
static int=0A=
MM_setattro(MM *self, PyObject *name, PyObject *v)=0A=
{=0A=
  if (v && PyString_Check(name))=0A=
    {=0A=
      if (strcmp(PyString_AsString(name),"level")=3D=3D0)=0A=
	{=0A=
	  self->level=3DPyInt_AsLong(v);=0A=
	  if (PyErr_Occurred()) return -1;=0A=
	  return 0;=0A=
	}=0A=
    }=0A=
=0A=
  if (! self->dict && ! (self->dict=3DPyDict_New())) return -1;=0A=
  =0A=
  if (v) return PyDict_SetItem(self->dict, name, v);=0A=
  else  return PyDict_DelItem(self->dict, name);=0A=
}=0A=
=0A=
static int=0A=
MM_length(self)=0A=
	MM *self;=0A=
{=0A=
  long l=3D0, el, i;=0A=
  PyObject *e=3D0;=0A=
=0A=
  UNLESS(-1 !=3D (i=3DPyList_Size(self->data))) return -1;=0A=
  while (--i >=3D 0)=0A=
    {=0A=
      e=3DPyList_GetItem(self->data,i);=0A=
      UNLESS(-1 !=3D (el=3DPyObject_Length(e))) return -1;=0A=
      l+=3Del;=0A=
    }=0A=
  return l;=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_subscript(MM *self, PyObject *key)=0A=
{=0A=
  return MM_cget(self, key, 1);=0A=
}=0A=
=0A=
typedef struct {=0A=
  PyObject_HEAD=0A=
  PyObject *data;=0A=
} DictInstance;=0A=
=0A=
static void=0A=
DictInstance_dealloc(DictInstance *self)=0A=
{=0A=
  Py_DECREF(self->data);=0A=
  PyMem_DEL(self);=0A=
}=0A=
=0A=
static PyObject *=0A=
DictInstance_getattr(DictInstance *self, PyObject *name)=0A=
{=0A=
  PyObject *r;=0A=
=0A=
  if ((r=3DPyObject_GetItem(self->data, name))) return r;=0A=
  PyErr_SetObject(PyExc_AttributeError, name);=0A=
  return NULL;=0A=
}=0A=
=0A=
static PyTypeObject DictInstanceType =3D {=0A=
  PyObject_HEAD_INIT(NULL)=0A=
  0,				/*ob_size*/=0A=
  "DictInstance",			/*tp_name*/=0A=
  sizeof(DictInstance),		/*tp_basicsize*/=0A=
  0,				/*tp_itemsize*/=0A=
  (destructor)DictInstance_dealloc,=0A=
  (printfunc)0,=0A=
  (getattrfunc)0,=0A=
  (setattrfunc)0,=0A=
  (cmpfunc)0,=0A=
  (reprfunc)0,=0A=
  0, 0, 0,=0A=
  (hashfunc)0,=0A=
  (ternaryfunc)0,=0A=
  (reprfunc)0,=0A=
  (getattrofunc)DictInstance_getattr,=0A=
  (setattrofunc)0,=0A=
  0L,0L,=0A=
  "Wrap a mapping object to look like an instance"=0A=
};=0A=
=0A=
static DictInstance *=0A=
newDictInstance(PyObject *data)=0A=
{=0A=
  DictInstance *self;=0A=
	=0A=
  UNLESS(self =3D PyObject_NEW(DictInstance, &DictInstanceType)) return =
NULL;=0A=
  self->data=3Ddata;=0A=
  Py_INCREF(data);=0A=
  return self;=0A=
}=0A=
=0A=
static PyObject *=0A=
MM_call(MM *self, PyObject *args, PyObject *kw)=0A=
{=0A=
  PyObject *r, *t;=0A=
  int i, l=3D0;=0A=
=0A=
  if (args && (l=3DPyTuple_Size(args)) < 0) return NULL;=0A=
  if (l)=0A=
    {=0A=
      UNLESS(r=3DPyObject_CallObject(OBJECT(self->ob_type), NULL)) =
return NULL;=0A=
      for (i=3D0; i < l; i++) =0A=
	if (PyList_Append(((MM*)r)->data, PyTuple_GET_ITEM(args, i)) < 0) =0A=
	  goto err;=0A=
      if (kw && PyList_Append(((MM*)r)->data, kw) < 0) goto err;=0A=
    }=0A=
  else=0A=
    {=0A=
      if (!kw) =0A=
	{=0A=
	  Py_INCREF(Py_None);=0A=
	  return Py_None;=0A=
	}=0A=
      r=3Dkw;=0A=
      Py_INCREF(r);=0A=
    }=0A=
=0A=
  ASSIGN(r, OBJECT(newDictInstance(r)));=0A=
  UNLESS(t=3DPyTuple_New(1)) goto err;=0A=
  PyTuple_SET_ITEM(t, 0, r);=0A=
  return t;=0A=
=0A=
err:=0A=
  Py_XDECREF(r);=0A=
  return NULL;=0A=
}=0A=
=0A=
static PyMappingMethods MM_as_mapping =3D {=0A=
	(inquiry)MM_length,		/*mp_length*/=0A=
	(binaryfunc)MM_subscript,      	/*mp_subscript*/=0A=
	(objobjargproc)NULL,		/*mp_ass_subscript*/=0A=
};=0A=
=0A=
/* -------------------------------------------------------- */=0A=
=0A=
static char MMtype__doc__[] =3D =0A=
"TemplateDict -- Combine multiple mapping objects for lookup"=0A=
;=0A=
=0A=
static PyExtensionClass MMtype =3D {=0A=
	PyObject_HEAD_INIT(NULL)=0A=
	0,				/*ob_size*/=0A=
	"TemplateDict",			/*tp_name*/=0A=
	sizeof(MM),			/*tp_basicsize*/=0A=
	0,				/*tp_itemsize*/=0A=
	/* methods */=0A=
	(destructor)MM_dealloc,		/*tp_dealloc*/=0A=
	(printfunc)0,			/*tp_print*/=0A=
	(getattrfunc)0,			/*tp_getattr*/=0A=
	(setattrfunc)0,			/*tp_setattr*/=0A=
	(cmpfunc)0,			/*tp_compare*/=0A=
	(reprfunc)0,			/*tp_repr*/=0A=
	0,				/*tp_as_number*/=0A=
	0,				/*tp_as_sequence*/=0A=
	&MM_as_mapping,			/*tp_as_mapping*/=0A=
	(hashfunc)0,			/*tp_hash*/=0A=
	(ternaryfunc)MM_call,		/*tp_call*/=0A=
	(reprfunc)0,			/*tp_str*/=0A=
	(getattrofunc)MM_getattro,	/*tp_getattro*/=0A=
	(setattrofunc)MM_setattro,	/*tp_setattro*/=0A=
=0A=
	/* Space for future expansion */=0A=
	0L,0L,=0A=
	MMtype__doc__, /* Documentation string */=0A=
	METHOD_CHAIN(MM_methods)=0A=
};=0A=
=0A=
static struct PyMethodDef TemplateDict_methods[] =3D {=0A=
  {NULL,		NULL}		/* sentinel */=0A=
};=0A=
=0A=
=0A=
=0A=
/* List of methods defined in the module */=0A=
=0A=
static int=0A=
if_finally(PyObject *md, int err)=0A=
{=0A=
  PyObject *t, *v, *tb;=0A=
=0A=
  if (err) PyErr_Fetch(&t, &v, &tb);=0A=
=0A=
  md=3DPyObject_GetAttr(md,py__pop);=0A=
  if (md) ASSIGN(md, PyObject_CallObject(md,NULL));=0A=
  =0A=
  if (err) PyErr_Restore(t,v,tb);=0A=
  =0A=
  if (md)=0A=
    {=0A=
      Py_DECREF(md);=0A=
      return -1;=0A=
    }=0A=
  else =0A=
    return -2;=0A=
}=0A=
=0A=
static int=0A=
render_blocks_(PyObject *blocks, PyObject *rendered,=0A=
	       PyObject *md, PyObject *mda)=0A=
{=0A=
  PyObject *block;=0A=
  int l, i, k=3D0, append;=0A=
=0A=
  if ((l=3DPyList_Size(blocks)) < 0) return -1;=0A=
  for (i=3D0; i < l; i++)=0A=
    {=0A=
      block=3DPyList_GET_ITEM(((PyListObject*)blocks), i);=0A=
      append=3D1;=0A=
=0A=
      if (PyTuple_Check(block))=0A=
	{=0A=
	  int bs;=0A=
=0A=
	  bs=3D((PyTupleObject*)block)->ob_size;=0A=
	  =0A=
	  if (bs=3D=3D1)=0A=
	    {=0A=
	      /* Simple var */=0A=
	      block=3DPyTuple_GET_ITEM(block,0);=0A=
	      if (PyString_Check(block)) block=3DPyObject_GetItem(md,block);=0A=
	      else block=3DPyObject_CallObject(block,mda);=0A=
	      if (block) ASSIGN(block, PyObject_Str(block));=0A=
	      UNLESS(block) return -1;=0A=
	    }=0A=
	  else=0A=
	    {=0A=
	      /* if */=0A=
	      int icond, m;=0A=
	      PyObject *cond, *n, *cache;=0A=
=0A=
	      UNLESS(cache=3DPyDict_New()) return -1;=0A=
	      cond=3DPyObject_GetAttr(md,py__push);=0A=
	      if (cond) ASSIGN(cond, PyObject_CallFunction(cond,"O",cache));=0A=
	      Py_DECREF(cache);=0A=
	      if (cond) Py_DECREF(cond);=0A=
	      else return -1;=0A=
	      =0A=
	      append=3D0;=0A=
	      m=3Dbs-1;=0A=
	      for (icond=3D0; icond < m; icond +=3D 2)=0A=
		{=0A=
		  cond=3DPyTuple_GET_ITEM(block,icond);=0A=
		  if (PyString_Check(cond))=0A=
		    {=0A=
		      /* We have to be careful to handle key errors here */=0A=
		      n=3Dcond;=0A=
		      if (cond=3DPyObject_GetItem(md,cond))=0A=
			{=0A=
			  if (PyDict_SetItem(cache, n, cond) < 0)=0A=
			    {=0A=
			      Py_DECREF(cond);=0A=
			      return if_finally(md,1);=0A=
			    }=0A=
			}=0A=
		      else=0A=
			{=0A=
			  PyObject *t, *v, *tb;=0A=
=0A=
			  PyErr_Fetch(&t, &v, &tb);=0A=
			  if (t !=3D PyExc_KeyError || PyObject_Compare(v,n))=0A=
			    {=0A=
			      PyErr_Restore(t,v,tb);=0A=
			      return if_finally(md,1);=0A=
			    }=0A=
			  Py_XDECREF(t);=0A=
			  Py_XDECREF(v);=0A=
			  Py_XDECREF(tb);=0A=
			  cond=3DPy_None;=0A=
			  Py_INCREF(cond);=0A=
			}=0A=
		    }=0A=
		  else=0A=
		    UNLESS(cond=3DPyObject_CallObject(cond,mda))=0A=
		       return if_finally(md,1);=0A=
=0A=
		  if (PyObject_IsTrue(cond))=0A=
		    {=0A=
		      Py_DECREF(cond);=0A=
		      block=3DPyTuple_GET_ITEM(block,icond+1);=0A=
		      if (block!=3DPy_None &&=0A=
			 render_blocks_(block, rendered, md, mda) < 0)=0A=
			return if_finally(md,1);=0A=
		      m=3D-1;=0A=
		      break;=0A=
		    }=0A=
		  else Py_DECREF(cond);=0A=
		}=0A=
		if (icond=3D=3Dm)=0A=
		  {=0A=
		    block=3DPyTuple_GET_ITEM(block,icond);=0A=
		    if (block!=3DPy_None &&=0A=
		       render_blocks_(block, rendered, md, mda) < 0)=0A=
		      return if_finally(md,1);=0A=
		  }=0A=
=0A=
		if (if_finally(md,0) =3D=3D -2) return -1;=0A=
	    }=0A=
	}=0A=
      else if (PyString_Check(block))=0A=
	{=0A=
	  Py_INCREF(block);=0A=
	}=0A=
      else=0A=
	{=0A=
	  UNLESS(block=3DPyObject_CallObject(block,mda)) return -1;=0A=
	}=0A=
=0A=
      if (append && PyObject_IsTrue(block))=0A=
	{=0A=
	  k=3DPyList_Append(rendered,block);=0A=
	  Py_DECREF(block);=0A=
	  if (k < 0) return -1;=0A=
	}=0A=
    }=0A=
=0A=
  return 0;=0A=
}=0A=
=0A=
static PyObject *=0A=
render_blocks(PyObject *self, PyObject *args)=0A=
{=0A=
  PyObject *md, *blocks, *mda=3D0, *rendered=3D0;=0A=
  int l;=0A=
=0A=
  UNLESS(PyArg_ParseTuple(args,"OO", &blocks, &md)) return NULL;=0A=
  UNLESS(rendered=3DPyList_New(0)) goto err;=0A=
  UNLESS(mda=3DPy_BuildValue("(O)",md)) goto err;=0A=
  =0A=
  if (render_blocks_(blocks, rendered, md, mda) < 0) goto err;=0A=
=0A=
  Py_DECREF(mda);=0A=
=0A=
  l=3DPyList_Size(rendered);=0A=
  if (l=3D=3D0)=0A=
    {=0A=
      Py_INCREF(py_);=0A=
      ASSIGN(rendered, py_);=0A=
    }=0A=
  else if (l=3D=3D1)=0A=
    ASSIGN(rendered, PySequence_GetItem(rendered,0));=0A=
  else=0A=
    ASSIGN(rendered, PyObject_CallFunction(join,"OO",rendered,py_));=0A=
=0A=
  return rendered;=0A=
=0A=
err:=0A=
  Py_XDECREF(mda);=0A=
  Py_XDECREF(rendered);=0A=
  return NULL;=0A=
}  =0A=
  =0A=
static struct PyMethodDef Module_Level__methods[] =3D {=0A=
  {"render_blocks", (PyCFunction)render_blocks,	METH_VARARGS,=0A=
   ""},=0A=
  {NULL, (PyCFunction)NULL, 0, NULL}		/* sentinel */=0A=
};=0A=
=0A=
void=0A=
initcDocumentTemplate()=0A=
{=0A=
  PyObject *m, *d;=0A=
  char *rev=3D"$Revision: 1.32.18.2 $";=0A=
=0A=
  DictInstanceType.ob_type=3D&PyType_Type;=0A=
=0A=
  UNLESS(py_isDocTemp=3DPyString_FromString("isDocTemp")) return;=0A=
  UNLESS(py_blocks=3DPyString_FromString("blocks")) return;=0A=
  UNLESS(py_acquire=3DPyString_FromString("aq_acquire")) return;=0A=
  UNLESS(py___call__=3DPyString_FromString("__call__")) return;=0A=
  UNLESS(py___roles__=3DPyString_FromString("__roles__")) return;=0A=
  UNLESS(py__proxy_roles=3DPyString_FromString("_proxy_roles")) return;=0A=
  UNLESS(py_hasRole=3DPyString_FromString("hasRole")) return;=0A=
  UNLESS(py_validate=3DPyString_FromString("validate")) return;=0A=
  UNLESS(py__push=3DPyString_FromString("_push")) return;=0A=
  UNLESS(py__pop=3DPyString_FromString("_pop")) return;=0A=
  UNLESS(py_aq_base=3DPyString_FromString("aq_base")) return;=0A=
  UNLESS(py_Unauthorized=3DPyString_FromString("Unauthorized")) return;=0A=
  UNLESS(py_Unauthorized_fmt=3DPyString_FromString(=0A=
	 "You are not authorized to access <em>%s</em>.")) return;=0A=
=0A=
  =
UNLESS(py_AUTHENTICATED_USER=3DPyString_FromString("AUTHENTICATED_USER"))=0A=
    return;=0A=
=0A=
  UNLESS(py_=3DPyString_FromString("")) return;=0A=
  UNLESS(join=3DPyImport_ImportModule("string")) return;=0A=
  ASSIGN(join,PyObject_GetAttrString(join,"join"));=0A=
  UNLESS(join) return;=0A=
  UNLESS(ExtensionClassImported) return;=0A=
=0A=
  m =3D Py_InitModule4("cDocumentTemplate", Module_Level__methods,=0A=
		     cDocumentTemplate_module_documentation,=0A=
		     (PyObject*)NULL,PYTHON_API_VERSION);=0A=
=0A=
  d =3D PyModule_GetDict(m);=0A=
=0A=
  PyExtensionClass_Export(d,"InstanceDict",InstanceDictType);=0A=
  PyExtensionClass_Export(d,"TemplateDict",MMtype);=0A=
=0A=
  PyDict_SetItemString(d, "__version__",=0A=
		       PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));=0A=
=0A=
  if (PyErr_Occurred())=0A=
    Py_FatalError("can't initialize module cDocumentTemplate");=0A=
}=0A=
=0A=

------=_NextPart_000_012B_01C04D68.2B1778F0--