Skip to content

Commit

Permalink
Document Template accelerations and additonal C Security Manager acce…
Browse files Browse the repository at this point in the history
…lerations

from cAccessControl-review-branch.
  • Loading branch information
matt@zope.com committed Oct 26, 2001
1 parent d7aa2f7 commit 5982dc9
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 107 deletions.
8 changes: 4 additions & 4 deletions DT_If.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@
variable is not reevaluated.
'''
__rcs_id__='$Id: DT_If.py,v 1.16 1999/03/10 00:15:07 klm Exp $'
__version__='$Revision: 1.16 $'[11:-2]
__rcs_id__='$Id: DT_If.py,v 1.17 2001/10/26 16:07:50 matt Exp $'
__version__='$Revision: 1.17 $'[11:-2]

from DT_Util import ParseError, parse_params, name_param, str

Expand Down Expand Up @@ -192,7 +192,7 @@ def __init__(self, blocks):

if elses is not None: sections.append(elses)

self.simple_form=tuple(sections)
self.simple_form=('i',)+tuple(sections)

class Unless:
name='unless'
Expand All @@ -204,7 +204,7 @@ def __init__(self, blocks):
name,expr=name_param(args,'unless',1)
if expr is None: cond=name
else: cond=expr.eval
self.simple_form=(cond,None,section.blocks)
self.simple_form=('i',cond,None,section.blocks)

class Else(Unless):
# The else tag is included for backward compatibility and is deprecated.
Expand Down
12 changes: 8 additions & 4 deletions DT_Var.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@
''' # '
__rcs_id__='$Id: DT_Var.py,v 1.44 2001/10/02 14:23:32 shane Exp $'
__version__='$Revision: 1.44 $'[11:-2]
__rcs_id__='$Id: DT_Var.py,v 1.45 2001/10/26 16:07:50 matt Exp $'
__version__='$Revision: 1.45 $'[11:-2]

from DT_Util import parse_params, name_param, str
import re, string, sys
Expand Down Expand Up @@ -254,7 +254,11 @@ def __init__(self, args, fmt='s'):
if len(args)==1 and fmt=='s':
if expr is None: expr=name
else: expr=expr.eval
self.simple_form=expr,
self.simple_form=('v', expr)
elif len(args)==2 and fmt=='s' and args.has_key('html_quote'):
if expr is None: expr=name
else: expr=expr.eval
self.simple_form=('v', expr, 'h')

def render(self, md):
args=self.args
Expand Down Expand Up @@ -353,7 +357,7 @@ def __init__(self, args):
name, expr = name_param(args,'call',1)
if expr is None: expr=name
else: expr=expr.eval
self.simple_form=expr,None
self.simple_form=('i', expr, None)


def url_quote(v, name='(Unknown name)', md={}):
Expand Down
235 changes: 136 additions & 99 deletions cDocumentTemplate.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
****************************************************************************/
static char cDocumentTemplate_module_documentation[] =
""
"\n$Id: cDocumentTemplate.c,v 1.39 2001/06/21 19:08:59 shane Exp $"
"\n$Id: cDocumentTemplate.c,v 1.40 2001/10/26 16:07:50 matt Exp $"
;

#include "ExtensionClass.h"
Expand All @@ -94,7 +94,7 @@ static PyObject *py___call__, *py___roles__, *py_AUTHENTICATED_USER;
static PyObject *py_hasRole, *py__proxy_roles, *py_Unauthorized;
static PyObject *py_Unauthorized_fmt, *py_guarded_getattr;
static PyObject *py__push, *py__pop, *py_aq_base, *py_renderNS;
static PyObject *py___class__;
static PyObject *py___class__, *html_quote;

/* ----------------------------------------------------- */

Expand Down Expand Up @@ -398,13 +398,29 @@ static PyObject *
MM_cget(MM *self, PyObject *key, int call)
{
long i;
PyObject *e, *rr, *tb;
PyObject *e, *rr;

UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
while (--i >= 0)
{
e=PyList_GetItem(self->data,i);
if ((e=PyObject_GetItem(e,key)))
e=PyList_GET_ITEM(self->data,i);
if (PyDict_Check(e))
{
e=PyDict_GetItem(e, key);
Py_XINCREF(e);
}
else
{
UNLESS (e=PyObject_GetItem(e,key))
{
if (PyErr_Occurred() == PyExc_KeyError)
PyErr_Clear();
else
return NULL;
}
}

if (e)
{
if (!call) return e;

Expand Down Expand Up @@ -437,17 +453,8 @@ MM_cget(MM *self, PyObject *key, int call)
}
return e;
}
PyErr_Fetch(&e, &rr, &tb);
if (e != PyExc_KeyError)
{
PyErr_Restore(e,rr,tb);
return NULL;
}
Py_XDECREF(e);
Py_XDECREF(rr);
Py_XDECREF(tb);
}
PyErr_SetObject(PyExc_KeyError,key);
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}

Expand Down Expand Up @@ -726,7 +733,7 @@ static int
render_blocks_(PyObject *blocks, PyObject *rendered,
PyObject *md, PyObject *mda)
{
PyObject *block;
PyObject *block, *t;
int l, i, k=0, append;

if ((l=PyList_Size(blocks)) < 0) return -1;
Expand All @@ -735,95 +742,123 @@ render_blocks_(PyObject *blocks, PyObject *rendered,
block=PyList_GET_ITEM(((PyListObject*)blocks), i);
append=1;

if (PyTuple_Check(block))
if (PyTuple_Check(block)
&& PyTuple_GET_SIZE(block) > 1
&& PyTuple_GET_ITEM(block, 0)
&& PyString_Check(PyTuple_GET_ITEM(block, 0)))
{
int bs;
switch (PyString_AS_STRING(PyTuple_GET_ITEM(block, 0))[0])
{
case 'v': /* var */
t=PyTuple_GET_ITEM(block,1);

bs=((PyTupleObject*)block)->ob_size;

if (bs==1)
{
/* Simple var */
block=PyTuple_GET_ITEM(block,0);
if (PyString_Check(block)) block=PyObject_GetItem(md,block);
else block=PyObject_CallObject(block,mda);
if (block) ASSIGN(block, PyObject_Str(block));
UNLESS(block) return -1;
}
else
{
/* if */
int icond, m;
PyObject *cond, *n, *cache;

UNLESS(cache=PyDict_New()) return -1;
cond=PyObject_GetAttr(md,py__push);
if (cond) ASSIGN(cond, PyObject_CallFunction(cond,"O",cache));
Py_DECREF(cache);
if (cond) Py_DECREF(cond);
else return -1;
if (t == NULL) return -1;

if (PyString_Check(t)) t=PyObject_GetItem(md, t);
else t=PyObject_CallObject(t, mda);

if (t == NULL || (! PyString_Check(t)))
{
if (t) ASSIGN(t, PyObject_Str(t));
UNLESS(t) return -1;
}

if (PyString_Check(t)
&& PyTuple_GET_SIZE(block) == 3) /* html_quote */
{
if (strchr(PyString_AS_STRING(t), '&')
&& strchr(PyString_AS_STRING(t), '<')
&& strchr(PyString_AS_STRING(t), '>')
&& strchr(PyString_AS_STRING(t), '"')
)
ASSIGN(t, PyObject_CallFunction(html_quote, "O", t));
if (t == NULL) return -1;
}

block = t;
break;
case 'i': /* if */
{
int icond, m, bs;
PyObject *cond, *n, *cache;

bs = PyTuple_GET_SIZE(block) - 1; /* subtract code */

UNLESS(cache=PyDict_New()) return -1;
cond=PyObject_GetAttr(md,py__push);
if (cond) ASSIGN(cond, PyObject_CallFunction(cond,"O",cache));
Py_DECREF(cache);
if (cond) Py_DECREF(cond);
else return -1;

append=0;
m=bs-1;
for (icond=0; icond < m; icond += 2)
{
cond=PyTuple_GET_ITEM(block,icond);
if (PyString_Check(cond))
{
/* We have to be careful to handle key errors here */
n=cond;
if ((cond=PyObject_GetItem(md,cond)))
{
if (PyDict_SetItem(cache, n, cond) < 0)
{
Py_DECREF(cond);
return if_finally(md,1);
}
}
else
{
PyObject *t, *v, *tb;

PyErr_Fetch(&t, &v, &tb);
if (t != PyExc_KeyError || PyObject_Compare(v,n))
{
PyErr_Restore(t,v,tb);
return if_finally(md,1);
}
Py_XDECREF(t);
Py_XDECREF(v);
Py_XDECREF(tb);
cond=Py_None;
Py_INCREF(cond);
}
}
else
UNLESS(cond=PyObject_CallObject(cond,mda))
return if_finally(md,1);

if (PyObject_IsTrue(cond))
{
Py_DECREF(cond);
block=PyTuple_GET_ITEM(block,icond+1);
if (block!=Py_None &&
render_blocks_(block, rendered, md, mda) < 0)
return if_finally(md,1);
m=-1;
break;
}
else Py_DECREF(cond);
}
append=0;
m=bs-1;
for (icond=0; icond < m; icond += 2)
{
cond=PyTuple_GET_ITEM(block,icond+1);
if (PyString_Check(cond))
{
/* We have to be careful to handle key errors here */
n=cond;
if ((cond=PyObject_GetItem(md,cond)))
{
if (PyDict_SetItem(cache, n, cond) < 0)
{
Py_DECREF(cond);
return if_finally(md,1);
}
}
else
{
PyObject *t, *v, *tb;
PyErr_Fetch(&t, &v, &tb);
if (t != PyExc_KeyError || PyObject_Compare(v,n))
{
PyErr_Restore(t,v,tb);
return if_finally(md,1);
}
Py_XDECREF(t);
Py_XDECREF(v);
Py_XDECREF(tb);
cond=Py_None;
Py_INCREF(cond);
}
}
else
UNLESS(cond=PyObject_CallObject(cond,mda))
return if_finally(md,1);
if (PyObject_IsTrue(cond))
{
Py_DECREF(cond);
block=PyTuple_GET_ITEM(block,icond+1+1);
if (block!=Py_None &&
render_blocks_(block, rendered, md, mda) < 0)
return if_finally(md,1);
m=-1;
break;
}
else Py_DECREF(cond);
}
if (icond==m)
{
block=PyTuple_GET_ITEM(block,icond);
block=PyTuple_GET_ITEM(block,icond+1);
if (block!=Py_None &&
render_blocks_(block, rendered, md, mda) < 0)
render_blocks_(block, rendered, md, mda) < 0)
return if_finally(md,1);
}

if (if_finally(md,0) == -2) return -1;
}
}
}
break;
default:
PyErr_Format(PyExc_ValueError,
"Invalid DTML command code, %s",
PyString_AS_STRING(PyTuple_GET_ITEM(block, 0)));
return -1;
}
}
else if (PyString_Check(block))
{
Py_INCREF(block);
Expand Down Expand Up @@ -904,10 +939,14 @@ void
initcDocumentTemplate(void)
{
PyObject *m, *d;
char *rev="$Revision: 1.39 $";
char *rev="$Revision: 1.40 $";

DictInstanceType.ob_type=&PyType_Type;

UNLESS (html_quote = PyImport_ImportModule("html_quote")) return;
ASSIGN(html_quote, PyObject_GetAttrString(html_quote, "html_quote"));
UNLESS (html_quote) return;

UNLESS(py_isDocTemp=PyString_FromString("isDocTemp")) return;
UNLESS(py_renderNS=PyString_FromString("__render_with_namespace__")) return;
UNLESS(py_blocks=PyString_FromString("blocks")) return;
Expand Down Expand Up @@ -946,6 +985,4 @@ initcDocumentTemplate(void)
PyDict_SetItemString(d, "__version__",
PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));

if (PyErr_Occurred())
Py_FatalError("can't initialize module cDocumentTemplate");
}

0 comments on commit 5982dc9

Please sign in to comment.