SVN: r25745 - in trunk/quixote: . src test
Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 9 Dec 2004 19:12:50 -0500
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: nascheme
Date: 2004-12-09 19:12:12 -0500 (Thu, 09 Dec 2004)
New Revision: 25745
Modified:
trunk/quixote/_py_htmltext.py
trunk/quixote/src/_c_htmltext.c
trunk/quixote/test/utest_html.py
Log:
Fix a string format bug in the C implementation of 'htmltext'. Modify
the Python implementation to match (even though it didn't have the same
bug).
Modified: trunk/quixote/_py_htmltext.py
===================================================================
--- trunk/quixote/_py_htmltext.py 2004-12-09 23:35:01 UTC (rev 25744)
+++ trunk/quixote/_py_htmltext.py 2004-12-10 00:12:12 UTC (rev 25745)
@@ -5,11 +5,6 @@
#$HeadURL$
#$Id$
-import re
-
-_format_codes = 'diouxXeEfFgGcrs%'
-_format_re = re.compile(r'%%[^%s]*[%s]' % (_format_codes, _format_codes))
-
def _escape_string(s):
if not isinstance(s, basestring):
raise TypeError, 'string object required'
@@ -73,20 +68,10 @@
return hash(self.s)
def __mod__(self, args):
- codes = []
- usedict = False
- for format in _format_re.findall(self.s):
- if format[-1] != '%':
- if format[1] == '(':
- usedict = True
- codes.append(format[-1])
- if usedict:
- args = _DictWrapper(args)
+ if isinstance(args, tuple):
+ return htmltext(self.s % tuple(map(_wraparg, args)))
else:
- if len(codes) == 1 and not isinstance(args, tuple):
- args = (args,)
- args = tuple(map(_wraparg, args))
- return htmltext(self.s % args)
+ return htmltext(self.s % _wraparg(args))
def __add__(self, other):
if isinstance(other, basestring):
@@ -165,13 +150,10 @@
def __repr__(self):
return _escape_string(`self.value`)
-class _DictWrapper(object):
- def __init__(self, value):
- self.value = value
-
def __getitem__(self, key):
return _wraparg(self.value[key])
+
def _wraparg(arg):
if (isinstance(arg, htmltext) or
isinstance(arg, int) or
Modified: trunk/quixote/src/_c_htmltext.c
===================================================================
--- trunk/quixote/src/_c_htmltext.c 2004-12-09 23:35:01 UTC (rev 25744)
+++ trunk/quixote/src/_c_htmltext.c 2004-12-10 00:12:12 UTC (rev 25745)
@@ -26,16 +26,6 @@
typedef struct {
PyObject_HEAD
- PyObject *obj;
-} DictWrapperObject;
-
-static PyTypeObject DictWrapper_Type;
-
-#define DictWrapper_Check(v) ((v)->ob_type == &DictWrapper_Type)
-
-
-typedef struct {
- PyObject_HEAD
PyObject *data; /* PyList_Object */
int html;
} TemplateIO_Object;
@@ -300,27 +290,8 @@
}
static PyObject *
-dict_wrapper_new(PyObject *o)
+quote_wrapper_subscript(QuoteWrapperObject *self, PyObject *key)
{
- DictWrapperObject *self;
- self = PyObject_New(DictWrapperObject, &DictWrapper_Type);
- if (self == NULL)
- return NULL;
- Py_INCREF(o);
- self->obj = o;
- return (PyObject *)self;
-}
-
-static void
-dict_wrapper_dealloc(DictWrapperObject *self)
-{
- Py_DECREF(self->obj);
- PyObject_Del(self);
-}
-
-static PyObject *
-dict_wrapper_subscript(DictWrapperObject *self, PyObject *key)
-{
PyObject *v, *w;;
v = PyObject_GetItem(self->obj, key);
if (v == NULL) {
@@ -442,7 +413,7 @@
htmltext_format(htmltextObject *self, PyObject *args)
{
/* wrap the format arguments with QuoteWrapperObject */
- int do_dict = 0, is_unicode;
+ int is_unicode;
PyObject *rv, *wargs;
if (PyUnicode_Check(self->s)) {
is_unicode = 1;
@@ -451,29 +422,7 @@
is_unicode = 0;
assert (PyString_Check(self->s));
}
- if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) &&
- !string_check(args)) {
- Py_UNICODE fmt_char, last_char = 0;
- size_t i, n = PyObject_Size(self->s);
- /* second check necessary since '%s' % {} => '{}' */
- for (i=0; i < n; i++) {
- if (is_unicode)
- fmt_char = PyUnicode_AS_UNICODE(self->s)[i];
- else
- fmt_char = PyString_AS_STRING(self->s)[i];
- if (last_char == '%' && fmt_char == '(') {
- do_dict = 1;
- break;
- }
- last_char = fmt_char;
- }
- }
- if (do_dict) {
- wargs = dict_wrapper_new(args);
- if (wargs == NULL)
- return NULL;
- }
- else if (PyTuple_Check(args)) {
+ if (PyTuple_Check(args)) {
long i, n = PyTuple_GET_SIZE(args);
wargs = PyTuple_New(n);
for (i=0; i < n; i++) {
@@ -487,9 +436,8 @@
}
else {
wargs = wrap_arg(args);
- if (wargs == NULL) {
+ if (wargs == NULL)
return NULL;
- }
}
if (is_unicode)
rv = PyUnicode_Format(self->s, wargs);
@@ -892,6 +840,13 @@
0, /*nb_float*/
};
+static PyMappingMethods quote_wrapper_as_mapping = {
+ 0, /*mp_length*/
+ (binaryfunc)quote_wrapper_subscript, /*mp_subscript*/
+ 0, /*mp_ass_subscript*/
+};
+
+
static PyTypeObject QuoteWrapper_Type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
@@ -907,36 +862,12 @@
(unaryfunc)quote_wrapper_repr,/*tp_repr*/
"e_wrapper_as_number,/*tp_as_number*/
0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
+ "e_wrapper_as_mapping,/*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
(unaryfunc)quote_wrapper_str, /*tp_str*/
};
-static PyMappingMethods dict_wrapper_as_mapping = {
- 0, /*mp_length*/
- (binaryfunc)dict_wrapper_subscript, /*mp_subscript*/
- 0, /*mp_ass_subscript*/
-};
-
-static PyTypeObject DictWrapper_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- "DictWrapper", /*tp_name*/
- sizeof(DictWrapperObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)dict_wrapper_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_compare*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- &dict_wrapper_as_mapping,/*tp_as_mapping*/
-};
-
static PyNumberMethods template_io_as_number = {
0, /*nb_add*/
0, /*nb_subtract*/
Modified: trunk/quixote/test/utest_html.py
===================================================================
--- trunk/quixote/test/utest_html.py 2004-12-09 23:35:01 UTC (rev 25744)
+++ trunk/quixote/test/utest_html.py 2004-12-10 00:12:12 UTC (rev 25745)
@@ -185,6 +185,8 @@
htmltext('%(a)s') % {}
assert 0
except KeyError: pass
+ assert htmltext('') % {} == ''
+ assert htmltext('%%') % {} == '%'
def check_join(self):
assert htmltext(' ').join(['foo', 'bar']) == "foo bar"