[svn] Fix refleaks and API usage in C module. Will need to test the changes though.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Georg Brandl · committed by Georg Brandl · parent 4a51133 · Browse files · 1 file·+82 added−72 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+82 added−72 removed
- other:+82 added−72 removed
Provenance
- Commit
40b02fa- Tree
79f21eb- Author
Georg Brandl <georg@python.org>- Committer
Georg Brandl <georg@python.org>- Parent
4a51133
1 file changed+82 added−72 removed
modified:jinja/_speedups.c+82 added-72 removed
8 unmodified lines9 * Note that if you change semantics here you have to edit the _native.py10 * to in order to support those changes for jinja setups without the11 * speedup module too.12 *13- * TODO:14- * - implement a cgi.escape replacement in this module15- *13 * :copyright: 2007 by Armin Ronacher.14 * :license: BSD, see LICENSE for more details.15 */16 69 unmodified lines86 87 int quotes = 0;88 PyObject *text = NULL;89 90+ /* XXX: "b" is "tiny int", not bool. */91 if (!PyArg_ParseTuple(args, "O|b", &text, "es))92 return NULL;93 in = (PyUnicodeObject*)PyObject_Unicode(text);94 if (!in)95 return NULL;96 97 /* First we need to figure out how long the escaped string will be */98 len = 0;101- for (i = 0;i < in->length; i++) {99+ for (i = 0; i < in->length; i++) {100 switch (in->str[i]) {101 case '&':102 len += 5;103 break;9 unmodified lines113 }114 }115 116 /* Do we need to escape anything at all? */119- if (len == in->length) {120- Py_INCREF(in);117+ if (len == in->length)118 return (PyObject*)in;122- }119+120 out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, len);121 if (!out) {122+ Py_DECREF(in);123 return NULL;124 }125 126 outp = out->str;129- for (i = 0;i < in->length; i++) {127+ for (i = 0; i < in->length; i++) {128 switch (in->str[i]) {129 case '&':130 Py_UNICODE_COPY(outp, amp, 5);131 outp += 5;18 unmodified lines150 *outp++ = in->str[i];151 };152 }153 154+ Py_DECREF(in);155 return (PyObject*)out;156 }157 158 /**33 unmodified lines192 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist,193 &silent, &globals, &initial))194 return -1;195 if (!PyDict_Check(globals) || !PyDict_Check(initial)) {197- PyErr_SetString(PyExc_TypeError, "stack layers must be a dicts.");196+ PyErr_SetString(PyExc_TypeError, "stack layers must be dicts.");197 return -1;198 }199 200 self->silent = PyObject_IsTrue(silent);201+ if (self->silent == -1)202+ return -1;203 203- self->globals = PyMem_Malloc(sizeof(struct StackLayer));204- self->globals->dict = globals;205- Py_INCREF(globals);206- self->globals->prev = NULL;207-208- self->initial = PyMem_Malloc(sizeof(struct StackLayer));209- self->initial->dict = initial;210- Py_INCREF(initial);211- self->initial->prev = self->globals;212-204 self->current = PyMem_Malloc(sizeof(struct StackLayer));205+ self->current->prev = NULL;206 self->current->dict = PyDict_New();207 if (!self->current->dict)208 return -1;209+210+ self->initial = PyMem_Malloc(sizeof(struct StackLayer));211+ self->initial->prev = NULL;212+ self->initial->dict = initial;213+ Py_INCREF(initial);214 self->current->prev = self->initial;215 216+ self->globals = PyMem_Malloc(sizeof(struct StackLayer));217+ self->globals->prev = NULL;218+ self->globals->dict = globals;219+ Py_INCREF(globals);220+ self->initial->prev = self->globals;221+222 self->stacksize = 3;223 return 0;224 }225 2 unmodified lines228 */229 static PyObject*230 BaseContext_pop(BaseContext *self)231 {232+ PyObject *result;233+ struct StackLayer *tmp = self->current;234+235 if (self->stacksize <= 3) {236 PyErr_SetString(PyExc_IndexError, "stack too small.");237 return NULL;238 }233- PyObject *result = self->current->dict;234- struct StackLayer *tmp = self->current;239+ result = self->current->dict;240+ assert(result);241 self->current = tmp->prev;242 PyMem_Free(tmp);243 self->stacksize--;244+ /* Took the reference to result from the struct. */245 return result;246 }247 248 /**4 unmodified lines253 static PyObject*254 BaseContext_push(BaseContext *self, PyObject *args)255 {256 PyObject *value = NULL;257+ struct StackLayer *new;258+259 if (!PyArg_ParseTuple(args, "|O:push", &value))260 return NULL;261 if (!value) {262 value = PyDict_New();5 unmodified lines268 return NULL;269 }270 else271 Py_INCREF(value);263- struct StackLayer *new = PyMem_Malloc(sizeof(struct StackLayer));272+ new = PyMem_Malloc(sizeof(struct StackLayer));273+ if (!new) {274+ Py_DECREF(value);275+ return NULL;276+ }277 new->dict = value;278 new->prev = self->current;279 self->current = new;280 self->stacksize++;7 unmodified lines288 */289 static PyObject*290 BaseContext_getstack(BaseContext *self, void *closure)291 {292+ int idx = 0;293+ struct StackLayer *current = self->current;294 PyObject *result = PyList_New(self->stacksize);295 if (!result)296 return NULL;282- struct StackLayer *current = self->current;283- int idx = 0;297 while (current) {298 Py_INCREF(current->dict);286- PyList_SetItem(result, idx++, current->dict);299+ PyList_SET_ITEM(result, idx++, current->dict);300 current = current->prev;301 }302 PyList_Reverse(result);303 return result;29 unmodified lines333 return self->globals->dict;334 }335 336 /**324- * Generic setter that just raises an exception to notify the user325- * that the attribute is read-only.326- */327-static int328-BaseContext_readonly(BaseContext *self, PyObject *value, void *closure)329-{330- PyErr_SetString(PyExc_AttributeError, "can't set attribute");331- return -1;332-}333-334-/**337 * Implements the context lookup.338 *339 * This works exactly like the native implementation but a lot340 * faster. It disallows access to internal names (names that start1 unmodified line342 */343 static PyObject*344 BaseContext_getitem(BaseContext *self, PyObject *item)345 {346+ PyObject *result;347+ char *name = NULL;348+ int isdeferred;349+ struct StackLayer *current = self->current;350+ 351 if (!PyString_Check(item))352 goto missing;353 354 /* disallow access to internal jinja values */348- char *name = PyString_AS_STRING(item);349- if (name[0] == ':' && name[1] == ':')355+ name = PyString_AS_STRING(item);356+ if (name[0] && name[0] == ':' && name[1] == ':')357 goto missing;358 352- PyObject *result;353- struct StackLayer *current = self->current;359 while (current) {355- result = PyDict_GetItemString(current->dict, name);360+ /* GetItemString just builds a new string from "name" again... */361+ result = PyDict_GetItem(current->dict, item);362 if (!result) {363 current = current->prev;364 continue;365 }360- Py_INCREF(result);361- if (PyObject_IsInstance(result, Deferred)) {362- PyObject *args = PyTuple_New(2);363- if (!args || PyTuple_SetItem(args, 0, (PyObject*)self) ||364- PyTuple_SetItem(args, 1, item))365- return NULL;366-367- PyObject *resolved = PyObject_CallObject(result, args);366+ isdeferred = PyObject_IsInstance(result, Deferred);367+ if (isdeferred == -1)368+ return NULL;369+ else if (isdeferred) {370+ PyObject *namespace;371+ PyObject *resolved = PyObject_CallFunctionObjArgs(372+ result, self, item, NULL);373 if (!resolved)374 return NULL;375 376 /* never touch the globals */372- Py_DECREF(result);373- Py_INCREF(resolved);374- PyObject *namespace;377 if (current == self->globals)378 namespace = self->initial->dict;379 else380 namespace = current->dict;379- PyDict_SetItemString(namespace, name, resolved);381+ if (PyDict_SetItem(namespace, item, resolved) < 0)382+ return NULL;383+ Py_INCREF(resolved);384 return resolved;385 }386+ Py_INCREF(result);387 return result;388 }389 390 missing:391 if (self->silent) {392 Py_INCREF(Undefined);393 return Undefined;394 }395+ if (name)390- PyErr_Format(TemplateRuntimeError, "'%s' is not defined", name);396+ PyErr_Format(TemplateRuntimeError, "'%s' is not defined", name);397+ else398+ PyErr_SetString(TemplateRuntimeError, "value is not a string");399 return NULL;400 }401 402 /**1 unmodified line404 */405 static int406 BaseContext_contains(BaseContext *self, PyObject *item)407 {408+ char *name;409+ struct StackLayer *current = self->current;410+411 if (!PyString_Check(item))412 return 0;413 403- char *name = PyString_AS_STRING(item);414+ name = PyString_AS_STRING(item);415 if (strlen(name) >= 2 && name[0] == ':' && name[1] == ':')416 return 0;417 407- struct StackLayer *current = self->current;418 while (current) {409- if (!PyMapping_HasKeyString(current->dict, name)) {419+ /* XXX: for 2.4 and newer, use PyDict_Contains */420+ if (!PyMapping_HasKey(current->dict, item)) {421 current = current->prev;422 continue;423 }424 return 1;7 unmodified lines432 */433 static int434 BaseContext_setitem(BaseContext *self, PyObject *item, PyObject *value)435 {425- char *name = PyString_AS_STRING(item);436+ if (!PyString_Check(item)) {437+ PyErr_SetString(PyExc_TypeError, "expected string argument");438+ return -1;439+ }440 if (!value)427- return PyDict_DelItemString(self->current->dict, name);428- return PyDict_SetItemString(self->current->dict, name, value);441+ return PyDict_DelItem(self->current->dict, item);442+ return PyDict_SetItem(self->current->dict, item, value);443 }444 445 /**446 * Size of the stack.5 unmodified lines452 }453 454 455 static PyGetSetDef BaseContext_getsetters[] = {442- {"stack", (getter)BaseContext_getstack, (setter)BaseContext_readonly,456+ {"stack", (getter)BaseContext_getstack, NULL,457 "a read only copy of the internal stack", NULL},444- {"current", (getter)BaseContext_getcurrent, (setter)BaseContext_readonly,458+ {"current", (getter)BaseContext_getcurrent, NULL,459 "reference to the current layer on the stack", NULL},446- {"initial", (getter)BaseContext_getinitial, (setter)BaseContext_readonly,460+ {"initial", (getter)BaseContext_getinitial, NULL,461 "reference to the initial layer on the stack", NULL},448- {"globals", (getter)BaseContext_getglobals, (setter)BaseContext_readonly,462+ {"globals", (getter)BaseContext_getglobals, NULL,463 "reference to the global layer on the stack", NULL},464 {NULL} /* Sentinel */465 };466 453-static PyMemberDef BaseContext_members[] = {454- {NULL} /* Sentinel */455-};456-467 static PyMethodDef BaseContext_methods[] = {468 {"pop", (PyCFunction)BaseContext_pop, METH_NOARGS,469 "ctx.pop() -> dict\n\n"470 "Pop the last layer from the stack and return it."},11 unmodified lines482 0, /* sq_item */483 0, /* sq_slice */484 0, /* sq_ass_item */485 0, /* sq_ass_slice */476- BaseContext_contains, /* sq_contains */486+ (objobjproc)BaseContext_contains, /* sq_contains */487 0, /* sq_inplace_concat */488 0 /* sq_inplace_repeat */489 };490 15 unmodified lines506 0, /* tp_setattr */507 0, /* tp_compare */508 0, /* tp_repr */509 0, /* tp_as_number */500- &BaseContext_as_sequence, /* tp_as_sequence */501- &BaseContext_as_mapping, /* tp_as_mapping */510+ BaseContext_as_sequence, /* tp_as_sequence */511+ BaseContext_as_mapping, /* tp_as_mapping */512 0, /* tp_hash */513 0, /* tp_call */514 0, /* tp_str */515 0, /* tp_getattro */7 unmodified lines523 0, /* tp_weaklistoffset */524 0, /* tp_iter */525 0, /* tp_iternext */526 BaseContext_methods, /* tp_methods */517- BaseContext_members, /* tp_members */527+ 0, /* tp_members */528 BaseContext_getsetters, /* tp_getset */529 0, /* tp_base */530 0, /* tp_dict */531 0, /* tp_descr_get */35 unmodified lines