2007-08-24 23:26:07 -03:00
|
|
|
/*
|
2011-09-28 02:41:54 -03:00
|
|
|
unicode_format.h -- implementation of str.format().
|
2007-08-24 23:26:07 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*********** Global data structures and forward declarations *********/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
A SubString consists of the characters between two string or
|
|
|
|
unicode pointers.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2011-09-28 02:41:54 -03:00
|
|
|
PyObject *str; /* borrowed reference */
|
|
|
|
Py_ssize_t start, end;
|
2007-08-24 23:26:07 -03:00
|
|
|
} SubString;
|
|
|
|
|
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
typedef enum {
|
|
|
|
ANS_INIT,
|
|
|
|
ANS_AUTO,
|
2009-05-01 05:59:13 -03:00
|
|
|
ANS_MANUAL
|
2009-03-14 09:29:34 -03:00
|
|
|
} AutoNumberState; /* Keep track if we're auto-numbering fields */
|
|
|
|
|
|
|
|
/* Keeps track of our auto-numbering state, and which number field we're on */
|
|
|
|
typedef struct {
|
|
|
|
AutoNumberState an_state;
|
|
|
|
int an_field_number;
|
|
|
|
} AutoNumber;
|
|
|
|
|
|
|
|
|
2007-08-24 23:26:07 -03:00
|
|
|
/* forward declaration for recursion */
|
|
|
|
static PyObject *
|
|
|
|
build_string(SubString *input, PyObject *args, PyObject *kwargs,
|
2009-03-14 09:29:34 -03:00
|
|
|
int recursion_depth, AutoNumber *auto_number);
|
2007-08-24 23:26:07 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/************************** Utility functions ************************/
|
|
|
|
/************************************************************************/
|
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
static void
|
|
|
|
AutoNumber_Init(AutoNumber *auto_number)
|
|
|
|
{
|
|
|
|
auto_number->an_state = ANS_INIT;
|
|
|
|
auto_number->an_field_number = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-24 23:26:07 -03:00
|
|
|
/* fill in a SubString from a pointer and length */
|
|
|
|
Py_LOCAL_INLINE(void)
|
2011-10-06 10:34:41 -03:00
|
|
|
SubString_init(SubString *str, PyObject *s, Py_ssize_t start, Py_ssize_t end)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
str->str = s;
|
|
|
|
str->start = start;
|
|
|
|
str->end = end;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
/* return a new string. if str->str is NULL, return None */
|
2007-08-24 23:26:07 -03:00
|
|
|
Py_LOCAL_INLINE(PyObject *)
|
|
|
|
SubString_new_object(SubString *str)
|
|
|
|
{
|
2017-01-23 04:23:58 -04:00
|
|
|
if (str->str == NULL)
|
|
|
|
Py_RETURN_NONE;
|
2011-09-28 02:41:54 -03:00
|
|
|
return PyUnicode_Substring(str->str, str->start, str->end);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
}
|
|
|
|
|
2015-08-26 15:10:32 -03:00
|
|
|
/* return a new string. if str->str is NULL, return a new empty string */
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
Py_LOCAL_INLINE(PyObject *)
|
|
|
|
SubString_new_object_or_empty(SubString *str)
|
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
if (str->str == NULL) {
|
2011-11-30 22:18:59 -04:00
|
|
|
return PyUnicode_New(0, 0);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
return SubString_new_object(str);
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
/* Return 1 if an error has been detected switching between automatic
|
|
|
|
field numbering and manual field specification, else return 0. Set
|
|
|
|
ValueError on error. */
|
|
|
|
static int
|
|
|
|
autonumber_state_error(AutoNumberState state, int field_name_is_empty)
|
|
|
|
{
|
|
|
|
if (state == ANS_MANUAL) {
|
|
|
|
if (field_name_is_empty) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "cannot switch from "
|
|
|
|
"manual field specification to "
|
|
|
|
"automatic field numbering");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!field_name_is_empty) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "cannot switch from "
|
|
|
|
"automatic field numbering to "
|
|
|
|
"manual field specification");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 23:26:07 -03:00
|
|
|
/************************************************************************/
|
|
|
|
/*********** Format string parsing -- integers and identifiers *********/
|
|
|
|
/************************************************************************/
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
static Py_ssize_t
|
|
|
|
get_integer(const SubString *str)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2007-08-26 19:27:13 -03:00
|
|
|
Py_ssize_t accumulator = 0;
|
|
|
|
Py_ssize_t digitval;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_ssize_t i;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/* empty string is an error */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (str->start >= str->end)
|
2007-08-26 19:27:13 -03:00
|
|
|
return -1;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
for (i = str->start; i < str->end; i++) {
|
|
|
|
digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str->str, i));
|
2007-08-24 23:26:07 -03:00
|
|
|
if (digitval < 0)
|
2007-08-26 19:27:13 -03:00
|
|
|
return -1;
|
2007-08-24 23:26:07 -03:00
|
|
|
/*
|
2011-09-25 11:34:32 -03:00
|
|
|
Detect possible overflow before it happens:
|
|
|
|
|
|
|
|
accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
|
|
|
|
accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
|
2007-08-24 23:26:07 -03:00
|
|
|
*/
|
2011-09-25 11:34:32 -03:00
|
|
|
if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
|
2007-08-24 23:26:07 -03:00
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"Too many decimal digits in format string");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-25 11:34:32 -03:00
|
|
|
accumulator = accumulator * 10 + digitval;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-26 19:27:13 -03:00
|
|
|
return accumulator;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/************************************************************************/
|
|
|
|
/******** Functions to get field objects and specification strings ******/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/* do the equivalent of obj.name */
|
|
|
|
static PyObject *
|
|
|
|
getattr(PyObject *obj, SubString *name)
|
|
|
|
{
|
|
|
|
PyObject *newobj;
|
2007-08-27 20:30:47 -03:00
|
|
|
PyObject *str = SubString_new_object(name);
|
2007-08-26 19:27:13 -03:00
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
newobj = PyObject_GetAttr(obj, str);
|
|
|
|
Py_DECREF(str);
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the equivalent of obj[idx], where obj is a sequence */
|
|
|
|
static PyObject *
|
|
|
|
getitem_sequence(PyObject *obj, Py_ssize_t idx)
|
|
|
|
{
|
|
|
|
return PySequence_GetItem(obj, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the equivalent of obj[idx], where obj is not a sequence */
|
|
|
|
static PyObject *
|
|
|
|
getitem_idx(PyObject *obj, Py_ssize_t idx)
|
|
|
|
{
|
|
|
|
PyObject *newobj;
|
2007-12-02 10:31:20 -04:00
|
|
|
PyObject *idx_obj = PyLong_FromSsize_t(idx);
|
2007-08-26 19:27:13 -03:00
|
|
|
if (idx_obj == NULL)
|
|
|
|
return NULL;
|
|
|
|
newobj = PyObject_GetItem(obj, idx_obj);
|
|
|
|
Py_DECREF(idx_obj);
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the equivalent of obj[name] */
|
2007-08-24 23:26:07 -03:00
|
|
|
static PyObject *
|
2007-08-26 19:27:13 -03:00
|
|
|
getitem_str(PyObject *obj, SubString *name)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2007-08-26 19:27:13 -03:00
|
|
|
PyObject *newobj;
|
2007-08-27 20:30:47 -03:00
|
|
|
PyObject *str = SubString_new_object(name);
|
2007-08-26 19:27:13 -03:00
|
|
|
if (str == NULL)
|
2007-08-24 23:26:07 -03:00
|
|
|
return NULL;
|
2007-08-26 19:27:13 -03:00
|
|
|
newobj = PyObject_GetItem(obj, str);
|
|
|
|
Py_DECREF(str);
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* the entire string we're parsing. we assume that someone else
|
|
|
|
is managing its lifetime, and that it will exist for the
|
|
|
|
lifetime of the iterator. can be empty */
|
|
|
|
SubString str;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
/* index to where we are inside field_name */
|
|
|
|
Py_ssize_t index;
|
2007-08-26 19:27:13 -03:00
|
|
|
} FieldNameIterator;
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2011-09-28 02:41:54 -03:00
|
|
|
FieldNameIterator_init(FieldNameIterator *self, PyObject *s,
|
|
|
|
Py_ssize_t start, Py_ssize_t end)
|
2007-08-26 19:27:13 -03:00
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(&self->str, s, start, end);
|
|
|
|
self->index = start;
|
2007-08-26 19:27:13 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
|
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c;
|
2007-08-26 19:27:13 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
name->str = self->str.str;
|
|
|
|
name->start = self->index;
|
2007-08-26 19:27:13 -03:00
|
|
|
|
|
|
|
/* return everything until '.' or '[' */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (self->index < self->str.end) {
|
|
|
|
c = PyUnicode_READ_CHAR(self->str.str, self->index++);
|
|
|
|
switch (c) {
|
2007-08-26 19:27:13 -03:00
|
|
|
case '[':
|
|
|
|
case '.':
|
|
|
|
/* backup so that we this character will be seen next time */
|
2011-09-28 02:41:54 -03:00
|
|
|
self->index--;
|
2007-08-26 19:27:13 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-26 19:27:13 -03:00
|
|
|
/* end of string is okay */
|
2011-09-28 02:41:54 -03:00
|
|
|
name->end = self->index;
|
2007-08-26 19:27:13 -03:00
|
|
|
return 1;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
static int
|
|
|
|
_FieldNameIterator_item(FieldNameIterator *self, SubString *name)
|
|
|
|
{
|
2007-09-03 05:40:29 -03:00
|
|
|
int bracket_seen = 0;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
name->str = self->str.str;
|
|
|
|
name->start = self->index;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/* return everything until ']' */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (self->index < self->str.end) {
|
|
|
|
c = PyUnicode_READ_CHAR(self->str.str, self->index++);
|
|
|
|
switch (c) {
|
2007-08-26 19:27:13 -03:00
|
|
|
case ']':
|
2007-09-03 05:40:29 -03:00
|
|
|
bracket_seen = 1;
|
2007-08-26 19:27:13 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-09-03 05:40:29 -03:00
|
|
|
/* make sure we ended with a ']' */
|
|
|
|
if (!bracket_seen) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Missing ']' in format string");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/* end of string is okay */
|
|
|
|
/* don't include the ']' */
|
2011-09-28 02:41:54 -03:00
|
|
|
name->end = self->index-1;
|
2007-08-26 19:27:13 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 on error, 1 on non-error termination, and 2 if it returns a value */
|
|
|
|
static int
|
|
|
|
FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
|
|
|
|
Py_ssize_t *name_idx, SubString *name)
|
|
|
|
{
|
|
|
|
/* check at end of input */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (self->index >= self->str.end)
|
2007-08-26 19:27:13 -03:00
|
|
|
return 1;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
switch (PyUnicode_READ_CHAR(self->str.str, self->index++)) {
|
2007-08-26 19:27:13 -03:00
|
|
|
case '.':
|
|
|
|
*is_attribute = 1;
|
2007-09-03 05:40:29 -03:00
|
|
|
if (_FieldNameIterator_attr(self, name) == 0)
|
2007-08-26 19:27:13 -03:00
|
|
|
return 0;
|
|
|
|
*name_idx = -1;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
*is_attribute = 0;
|
2007-09-03 05:40:29 -03:00
|
|
|
if (_FieldNameIterator_item(self, name) == 0)
|
2007-08-26 19:27:13 -03:00
|
|
|
return 0;
|
|
|
|
*name_idx = get_integer(name);
|
2010-06-07 19:31:26 -03:00
|
|
|
if (*name_idx == -1 && PyErr_Occurred())
|
|
|
|
return 0;
|
2007-08-26 19:27:13 -03:00
|
|
|
break;
|
|
|
|
default:
|
2009-05-23 11:23:22 -03:00
|
|
|
/* Invalid character follows ']' */
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Only '.' or '[' may "
|
|
|
|
"follow ']' in format field specifier");
|
2007-08-26 19:27:13 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* empty string is an error */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (name->start == name->end) {
|
2007-08-26 19:27:13 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* input: field_name
|
|
|
|
output: 'first' points to the part before the first '[' or '.'
|
|
|
|
'first_idx' is -1 if 'first' is not an integer, otherwise
|
|
|
|
it's the value of first converted to an integer
|
|
|
|
'rest' is an iterator to return the rest
|
2007-08-24 23:26:07 -03:00
|
|
|
*/
|
2007-08-26 19:27:13 -03:00
|
|
|
static int
|
2011-09-28 02:41:54 -03:00
|
|
|
field_name_split(PyObject *str, Py_ssize_t start, Py_ssize_t end, SubString *first,
|
2009-03-14 09:29:34 -03:00
|
|
|
Py_ssize_t *first_idx, FieldNameIterator *rest,
|
|
|
|
AutoNumber *auto_number)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c;
|
|
|
|
Py_ssize_t i = start;
|
2009-03-14 09:29:34 -03:00
|
|
|
int field_name_is_empty;
|
|
|
|
int using_numeric_index;
|
2007-08-26 19:27:13 -03:00
|
|
|
|
|
|
|
/* find the part up until the first '.' or '[' */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (i < end) {
|
|
|
|
switch (c = PyUnicode_READ_CHAR(str, i++)) {
|
2007-08-26 19:27:13 -03:00
|
|
|
case '[':
|
|
|
|
case '.':
|
|
|
|
/* backup so that we this character is available to the
|
|
|
|
"rest" iterator */
|
2011-09-28 02:41:54 -03:00
|
|
|
i--;
|
2007-08-26 19:27:13 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up the return values */
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(first, str, start, i);
|
|
|
|
FieldNameIterator_init(rest, str, i, end);
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/* see if "first" is an integer, in which case it's used as an index */
|
|
|
|
*first_idx = get_integer(first);
|
2010-06-07 19:31:26 -03:00
|
|
|
if (*first_idx == -1 && PyErr_Occurred())
|
|
|
|
return 0;
|
2007-08-26 19:27:13 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
field_name_is_empty = first->start >= first->end;
|
2009-03-14 09:29:34 -03:00
|
|
|
|
|
|
|
/* If the field name is omitted or if we have a numeric index
|
|
|
|
specified, then we're doing numeric indexing into args. */
|
|
|
|
using_numeric_index = field_name_is_empty || *first_idx != -1;
|
|
|
|
|
|
|
|
/* We always get here exactly one time for each field we're
|
|
|
|
processing. And we get here in field order (counting by left
|
|
|
|
braces). So this is the perfect place to handle automatic field
|
|
|
|
numbering if the field name is omitted. */
|
|
|
|
|
|
|
|
/* Check if we need to do the auto-numbering. It's not needed if
|
|
|
|
we're called from string.Format routines, because it's handled
|
|
|
|
in that class by itself. */
|
|
|
|
if (auto_number) {
|
|
|
|
/* Initialize our auto numbering state if this is the first
|
|
|
|
time we're either auto-numbering or manually numbering. */
|
|
|
|
if (auto_number->an_state == ANS_INIT && using_numeric_index)
|
|
|
|
auto_number->an_state = field_name_is_empty ?
|
|
|
|
ANS_AUTO : ANS_MANUAL;
|
|
|
|
|
|
|
|
/* Make sure our state is consistent with what we're doing
|
|
|
|
this time through. Only check if we're using a numeric
|
|
|
|
index. */
|
|
|
|
if (using_numeric_index)
|
|
|
|
if (autonumber_state_error(auto_number->an_state,
|
|
|
|
field_name_is_empty))
|
|
|
|
return 0;
|
|
|
|
/* Zero length field means we want to do auto-numbering of the
|
|
|
|
fields. */
|
|
|
|
if (field_name_is_empty)
|
|
|
|
*first_idx = (auto_number->an_field_number)++;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-26 19:27:13 -03:00
|
|
|
|
|
|
|
return 1;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
|
2007-08-24 23:26:07 -03:00
|
|
|
/*
|
|
|
|
get_field_object returns the object inside {}, before the
|
|
|
|
format_spec. It handles getindex and getattr lookups and consumes
|
|
|
|
the entire input string.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
2009-03-14 09:29:34 -03:00
|
|
|
get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
|
|
|
|
AutoNumber *auto_number)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2007-08-26 19:27:13 -03:00
|
|
|
PyObject *obj = NULL;
|
|
|
|
int ok;
|
|
|
|
int is_attribute;
|
|
|
|
SubString name;
|
|
|
|
SubString first;
|
2007-08-24 23:26:07 -03:00
|
|
|
Py_ssize_t index;
|
2007-08-26 19:27:13 -03:00
|
|
|
FieldNameIterator rest;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (!field_name_split(input->str, input->start, input->end, &first,
|
2009-03-14 09:29:34 -03:00
|
|
|
&index, &rest, auto_number)) {
|
2007-08-26 19:27:13 -03:00
|
|
|
goto error;
|
|
|
|
}
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
if (index == -1) {
|
|
|
|
/* look up in kwargs */
|
2007-08-27 20:30:47 -03:00
|
|
|
PyObject *key = SubString_new_object(&first);
|
2017-08-03 05:45:23 -03:00
|
|
|
if (key == NULL) {
|
2007-08-26 19:27:13 -03:00
|
|
|
goto error;
|
2017-08-03 05:45:23 -03:00
|
|
|
}
|
|
|
|
if (kwargs == NULL) {
|
2007-09-04 20:04:22 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
2007-08-26 19:27:13 -03:00
|
|
|
Py_DECREF(key);
|
|
|
|
goto error;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2017-08-03 05:45:23 -03:00
|
|
|
/* Use PyObject_GetItem instead of PyDict_GetItem because this
|
|
|
|
code is no longer just used with kwargs. It might be passed
|
|
|
|
a non-dict when called through format_map. */
|
|
|
|
obj = PyObject_GetItem(kwargs, key);
|
2007-08-27 04:24:17 -03:00
|
|
|
Py_DECREF(key);
|
2017-08-03 05:45:23 -03:00
|
|
|
if (obj == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2007-08-27 22:07:27 -03:00
|
|
|
}
|
|
|
|
else {
|
2011-07-18 15:03:41 -03:00
|
|
|
/* If args is NULL, we have a format string with a positional field
|
|
|
|
with only kwargs to retrieve it from. This can only happen when
|
|
|
|
used with format_map(), where positional arguments are not
|
|
|
|
allowed. */
|
|
|
|
if (args == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Format string contains "
|
|
|
|
"positional fields");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2007-08-26 19:27:13 -03:00
|
|
|
/* look up in args */
|
|
|
|
obj = PySequence_GetItem(args, index);
|
2007-09-04 20:04:22 -03:00
|
|
|
if (obj == NULL)
|
2007-08-26 19:27:13 -03:00
|
|
|
goto error;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-26 19:27:13 -03:00
|
|
|
|
|
|
|
/* iterate over the rest of the field_name */
|
|
|
|
while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
|
|
|
|
&name)) == 2) {
|
|
|
|
PyObject *tmp;
|
|
|
|
|
|
|
|
if (is_attribute)
|
|
|
|
/* getattr lookup "." */
|
|
|
|
tmp = getattr(obj, &name);
|
|
|
|
else
|
|
|
|
/* getitem lookup "[]" */
|
|
|
|
if (index == -1)
|
|
|
|
tmp = getitem_str(obj, &name);
|
|
|
|
else
|
|
|
|
if (PySequence_Check(obj))
|
|
|
|
tmp = getitem_sequence(obj, index);
|
|
|
|
else
|
|
|
|
/* not a sequence */
|
|
|
|
tmp = getitem_idx(obj, index);
|
|
|
|
if (tmp == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* assign to obj */
|
|
|
|
Py_DECREF(obj);
|
|
|
|
obj = tmp;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-26 19:27:13 -03:00
|
|
|
/* end of iterator, this is the non-error case */
|
|
|
|
if (ok == 1)
|
|
|
|
return obj;
|
|
|
|
error:
|
|
|
|
Py_XDECREF(obj);
|
2007-08-24 23:26:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/***************** Field rendering functions **************************/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
render_field() is the main function in this section. It takes the
|
|
|
|
field object and field specification string generated by
|
|
|
|
get_field_and_spec, and renders the field into the output string.
|
|
|
|
|
|
|
|
render_field calls fieldobj.__format__(format_spec) method, and
|
|
|
|
appends to the output.
|
|
|
|
*/
|
|
|
|
static int
|
2012-05-09 17:25:00 -03:00
|
|
|
render_field(PyObject *fieldobj, SubString *format_spec, _PyUnicodeWriter *writer)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
|
|
|
int ok = 0;
|
2008-02-17 15:48:00 -04:00
|
|
|
PyObject *result = NULL;
|
2008-05-30 22:40:08 -03:00
|
|
|
PyObject *format_spec_object = NULL;
|
2012-05-29 07:57:52 -03:00
|
|
|
int (*formatter) (_PyUnicodeWriter*, PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL;
|
|
|
|
int err;
|
2011-11-03 20:22:48 -03:00
|
|
|
|
2008-05-30 22:40:08 -03:00
|
|
|
/* If we know the type exactly, skip the lookup of __format__ and just
|
|
|
|
call the formatter directly. */
|
|
|
|
if (PyUnicode_CheckExact(fieldobj))
|
2012-05-29 07:57:52 -03:00
|
|
|
formatter = _PyUnicode_FormatAdvancedWriter;
|
2008-05-30 22:40:08 -03:00
|
|
|
else if (PyLong_CheckExact(fieldobj))
|
2012-05-29 07:57:52 -03:00
|
|
|
formatter = _PyLong_FormatAdvancedWriter;
|
2008-05-30 22:40:08 -03:00
|
|
|
else if (PyFloat_CheckExact(fieldobj))
|
2012-05-29 07:57:52 -03:00
|
|
|
formatter = _PyFloat_FormatAdvancedWriter;
|
|
|
|
else if (PyComplex_CheckExact(fieldobj))
|
|
|
|
formatter = _PyComplex_FormatAdvancedWriter;
|
2008-06-02 11:57:32 -03:00
|
|
|
|
|
|
|
if (formatter) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* we know exactly which formatter will be called when __format__ is
|
|
|
|
looked up, so call it directly, instead. */
|
2012-05-29 07:57:52 -03:00
|
|
|
err = formatter(writer, fieldobj, format_spec->str,
|
|
|
|
format_spec->start, format_spec->end);
|
|
|
|
return (err == 0);
|
2008-06-02 11:57:32 -03:00
|
|
|
}
|
2008-05-30 22:40:08 -03:00
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* We need to create an object out of the pointers we have, because
|
|
|
|
__format__ takes a string/unicode object for format_spec. */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (format_spec->str)
|
|
|
|
format_spec_object = PyUnicode_Substring(format_spec->str,
|
|
|
|
format_spec->start,
|
|
|
|
format_spec->end);
|
|
|
|
else
|
|
|
|
format_spec_object = PyUnicode_New(0, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (format_spec_object == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
result = PyObject_Format(fieldobj, format_spec_object);
|
2008-05-30 22:40:08 -03:00
|
|
|
}
|
2012-05-09 17:24:08 -03:00
|
|
|
if (result == NULL)
|
|
|
|
goto done;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2012-05-29 07:57:52 -03:00
|
|
|
if (_PyUnicodeWriter_WriteStr(writer, result) == -1)
|
2012-05-09 17:24:08 -03:00
|
|
|
goto done;
|
|
|
|
ok = 1;
|
2012-05-29 07:57:52 -03:00
|
|
|
|
2007-08-24 23:26:07 -03:00
|
|
|
done:
|
2008-05-30 22:40:08 -03:00
|
|
|
Py_XDECREF(format_spec_object);
|
2007-08-24 23:26:07 -03:00
|
|
|
Py_XDECREF(result);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_field(SubString *str, SubString *field_name, SubString *format_spec,
|
2013-05-17 20:22:31 -03:00
|
|
|
int *format_spec_needs_expanding, Py_UCS4 *conversion)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2009-03-14 09:29:34 -03:00
|
|
|
/* Note this function works if the field name is zero length,
|
|
|
|
which is good. Zero length field names are handled later, in
|
|
|
|
field_name_split. */
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c = 0;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
|
|
|
/* initialize these, as they may be empty */
|
|
|
|
*conversion = '\0';
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(format_spec, NULL, 0, 0);
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
/* Search for the field name. it's terminated by the end of
|
|
|
|
the string, or a ':' or '!' */
|
2011-09-28 02:41:54 -03:00
|
|
|
field_name->str = str->str;
|
|
|
|
field_name->start = str->start;
|
|
|
|
while (str->start < str->end) {
|
|
|
|
switch ((c = PyUnicode_READ_CHAR(str->str, str->start++))) {
|
2013-05-17 20:22:31 -03:00
|
|
|
case '{':
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unexpected '{' in field name");
|
|
|
|
return 0;
|
|
|
|
case '[':
|
|
|
|
for (; str->start < str->end; str->start++)
|
|
|
|
if (PyUnicode_READ_CHAR(str->str, str->start) == ']')
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
case '}':
|
2007-08-24 23:26:07 -03:00
|
|
|
case ':':
|
|
|
|
case '!':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-17 20:22:31 -03:00
|
|
|
field_name->end = str->start - 1;
|
2007-08-24 23:26:07 -03:00
|
|
|
if (c == '!' || c == ':') {
|
2013-05-17 20:22:31 -03:00
|
|
|
Py_ssize_t count;
|
2007-08-24 23:26:07 -03:00
|
|
|
/* we have a format specifier and/or a conversion */
|
|
|
|
/* don't include the last character */
|
|
|
|
|
|
|
|
/* see if there's a conversion specifier */
|
|
|
|
if (c == '!') {
|
|
|
|
/* there must be another character present */
|
2013-05-17 20:22:31 -03:00
|
|
|
if (str->start >= str->end) {
|
2007-08-24 23:26:07 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2013-05-17 20:22:31 -03:00
|
|
|
"end of string while looking for conversion "
|
2007-08-24 23:26:07 -03:00
|
|
|
"specifier");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-17 20:22:31 -03:00
|
|
|
*conversion = PyUnicode_READ_CHAR(str->str, str->start++);
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2013-05-17 20:22:31 -03:00
|
|
|
if (str->start < str->end) {
|
|
|
|
c = PyUnicode_READ_CHAR(str->str, str->start++);
|
|
|
|
if (c == '}')
|
|
|
|
return 1;
|
2007-08-24 23:26:07 -03:00
|
|
|
if (c != ':') {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2013-05-17 20:22:31 -03:00
|
|
|
"expected ':' after conversion specifier");
|
2007-08-24 23:26:07 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 20:22:31 -03:00
|
|
|
format_spec->str = str->str;
|
|
|
|
format_spec->start = str->start;
|
|
|
|
count = 1;
|
|
|
|
while (str->start < str->end) {
|
|
|
|
switch ((c = PyUnicode_READ_CHAR(str->str, str->start++))) {
|
|
|
|
case '{':
|
|
|
|
*format_spec_needs_expanding = 1;
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
count--;
|
|
|
|
if (count == 0) {
|
|
|
|
format_spec->end = str->start - 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unmatched '{' in format spec");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (c != '}') {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "expected '}' before end of string");
|
|
|
|
return 0;
|
2007-08-27 22:07:27 -03:00
|
|
|
}
|
2009-03-14 09:29:34 -03:00
|
|
|
|
|
|
|
return 1;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/******* Output string allocation and escape-to-markup processing ******/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/* MarkupIterator breaks the string into pieces of either literal
|
|
|
|
text, or things inside {} that need to be marked up. it is
|
|
|
|
designed to make it easy to wrap a Python iterator around it, for
|
|
|
|
use with the Formatter class */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SubString str;
|
|
|
|
} MarkupIterator;
|
|
|
|
|
|
|
|
static int
|
2011-11-03 20:22:48 -03:00
|
|
|
MarkupIterator_init(MarkupIterator *self, PyObject *str,
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_ssize_t start, Py_ssize_t end)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(&self->str, str, start, end);
|
2007-08-24 23:26:07 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 on error, 1 on non-error termination, and 2 if it got a
|
|
|
|
string (or something to be expanded) */
|
|
|
|
static int
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
MarkupIterator_next(MarkupIterator *self, SubString *literal,
|
2009-03-14 09:29:34 -03:00
|
|
|
int *field_present, SubString *field_name,
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString *format_spec, Py_UCS4 *conversion,
|
2007-08-24 23:26:07 -03:00
|
|
|
int *format_spec_needs_expanding)
|
|
|
|
{
|
2013-05-17 20:22:31 -03:00
|
|
|
int at_end;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c = 0;
|
|
|
|
Py_ssize_t start;
|
2007-08-24 23:26:07 -03:00
|
|
|
Py_ssize_t len;
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
int markup_follows = 0;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* initialize all of the output variables */
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(literal, NULL, 0, 0);
|
|
|
|
SubString_init(field_name, NULL, 0, 0);
|
|
|
|
SubString_init(format_spec, NULL, 0, 0);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
*conversion = '\0';
|
2007-08-24 23:26:07 -03:00
|
|
|
*format_spec_needs_expanding = 0;
|
2009-03-14 09:29:34 -03:00
|
|
|
*field_present = 0;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* No more input, end of iterator. This is the normal exit
|
|
|
|
path. */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (self->str.start >= self->str.end)
|
2007-08-24 23:26:07 -03:00
|
|
|
return 1;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
start = self->str.start;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* First read any literal text. Read until the end of string, an
|
|
|
|
escaped '{' or '}', or an unescaped '{'. In order to never
|
|
|
|
allocate memory and so I can just pass pointers around, if
|
|
|
|
there's an escaped '{' or '}' then we'll return the literal
|
|
|
|
including the brace, but no format object. The next time
|
|
|
|
through, we'll return the rest of the literal, skipping past
|
|
|
|
the second consecutive brace. */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (self->str.start < self->str.end) {
|
|
|
|
switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
markup_follows = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
break;
|
|
|
|
}
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
at_end = self->str.start >= self->str.end;
|
|
|
|
len = self->str.start - start;
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
|
2011-11-03 20:22:48 -03:00
|
|
|
if ((c == '}') && (at_end ||
|
|
|
|
(c != PyUnicode_READ_CHAR(self->str.str,
|
2011-09-28 02:41:54 -03:00
|
|
|
self->str.start)))) {
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Single '}' encountered "
|
|
|
|
"in format string");
|
|
|
|
return 0;
|
2007-08-27 22:07:27 -03:00
|
|
|
}
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
if (at_end && c == '{') {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Single '{' encountered "
|
|
|
|
"in format string");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!at_end) {
|
2011-09-28 02:41:54 -03:00
|
|
|
if (c == PyUnicode_READ_CHAR(self->str.str, self->str.start)) {
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* escaped } or {, skip it in the input. there is no
|
|
|
|
markup object following us, just this literal text */
|
2011-09-28 02:41:54 -03:00
|
|
|
self->str.start++;
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
markup_follows = 0;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
else
|
|
|
|
len--;
|
|
|
|
}
|
2007-08-24 23:26:07 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* record the literal text */
|
2011-09-28 02:41:54 -03:00
|
|
|
literal->str = self->str.str;
|
|
|
|
literal->start = start;
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
literal->end = start + len;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
if (!markup_follows)
|
|
|
|
return 2;
|
|
|
|
|
2013-05-17 20:22:31 -03:00
|
|
|
/* this is markup; parse the field */
|
2009-03-14 09:29:34 -03:00
|
|
|
*field_present = 1;
|
2013-05-17 20:22:31 -03:00
|
|
|
if (!parse_field(&self->str, field_name, format_spec,
|
|
|
|
format_spec_needs_expanding, conversion))
|
|
|
|
return 0;
|
|
|
|
return 2;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* do the !r or !s conversion on obj */
|
|
|
|
static PyObject *
|
2011-09-28 02:41:54 -03:00
|
|
|
do_conversion(PyObject *obj, Py_UCS4 conversion)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
|
|
|
/* XXX in pre-3.0, do we need to convert this to unicode, since it
|
|
|
|
might have returned a string? */
|
|
|
|
switch (conversion) {
|
|
|
|
case 'r':
|
|
|
|
return PyObject_Repr(obj);
|
|
|
|
case 's':
|
2011-09-28 02:41:54 -03:00
|
|
|
return PyObject_Str(obj);
|
2008-06-11 15:37:52 -03:00
|
|
|
case 'a':
|
2011-09-28 02:41:54 -03:00
|
|
|
return PyObject_ASCII(obj);
|
2007-08-24 23:26:07 -03:00
|
|
|
default:
|
2010-05-09 12:52:27 -03:00
|
|
|
if (conversion > 32 && conversion < 127) {
|
|
|
|
/* It's the ASCII subrange; casting to char is safe
|
|
|
|
(assuming the execution character set is an ASCII
|
|
|
|
superset). */
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
2008-04-07 00:22:07 -03:00
|
|
|
"Unknown conversion specifier %c",
|
|
|
|
(char)conversion);
|
2010-05-09 12:52:27 -03:00
|
|
|
} else
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"Unknown conversion specifier \\x%x",
|
|
|
|
(unsigned int)conversion);
|
2007-08-24 23:26:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given:
|
|
|
|
|
|
|
|
{field_name!conversion:format_spec}
|
|
|
|
|
|
|
|
compute the result and write it to output.
|
|
|
|
format_spec_needs_expanding is an optimization. if it's false,
|
|
|
|
just output the string directly, otherwise recursively expand the
|
2009-03-14 09:29:34 -03:00
|
|
|
format_spec string.
|
|
|
|
|
|
|
|
field_name is allowed to be zero length, in which case we
|
|
|
|
are doing auto field numbering.
|
|
|
|
*/
|
2007-08-24 23:26:07 -03:00
|
|
|
|
|
|
|
static int
|
|
|
|
output_markup(SubString *field_name, SubString *format_spec,
|
2011-09-28 02:41:54 -03:00
|
|
|
int format_spec_needs_expanding, Py_UCS4 conversion,
|
2012-05-09 17:25:00 -03:00
|
|
|
_PyUnicodeWriter *writer, PyObject *args, PyObject *kwargs,
|
2009-03-14 09:29:34 -03:00
|
|
|
int recursion_depth, AutoNumber *auto_number)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
|
|
|
PyObject *tmp = NULL;
|
|
|
|
PyObject *fieldobj = NULL;
|
|
|
|
SubString expanded_format_spec;
|
|
|
|
SubString *actual_format_spec;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
/* convert field_name to an object */
|
2009-03-14 09:29:34 -03:00
|
|
|
fieldobj = get_field_object(field_name, args, kwargs, auto_number);
|
2007-08-24 23:26:07 -03:00
|
|
|
if (fieldobj == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (conversion != '\0') {
|
|
|
|
tmp = do_conversion(fieldobj, conversion);
|
2011-09-28 02:41:54 -03:00
|
|
|
if (tmp == NULL || PyUnicode_READY(tmp) == -1)
|
2007-08-24 23:26:07 -03:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* do the assignment, transferring ownership: fieldobj = tmp */
|
|
|
|
Py_DECREF(fieldobj);
|
|
|
|
fieldobj = tmp;
|
|
|
|
tmp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if needed, recurively compute the format_spec */
|
|
|
|
if (format_spec_needs_expanding) {
|
2009-03-14 09:29:34 -03:00
|
|
|
tmp = build_string(format_spec, args, kwargs, recursion_depth-1,
|
|
|
|
auto_number);
|
2011-09-28 02:41:54 -03:00
|
|
|
if (tmp == NULL || PyUnicode_READY(tmp) == -1)
|
2007-08-24 23:26:07 -03:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* note that in the case we're expanding the format string,
|
|
|
|
tmp must be kept around until after the call to
|
|
|
|
render_field. */
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(&expanded_format_spec, tmp, 0, PyUnicode_GET_LENGTH(tmp));
|
2007-08-24 23:26:07 -03:00
|
|
|
actual_format_spec = &expanded_format_spec;
|
2007-08-27 22:07:27 -03:00
|
|
|
}
|
|
|
|
else
|
2007-08-24 23:26:07 -03:00
|
|
|
actual_format_spec = format_spec;
|
|
|
|
|
2012-05-07 07:47:02 -03:00
|
|
|
if (render_field(fieldobj, actual_format_spec, writer) == 0)
|
2007-08-24 23:26:07 -03:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
result = 1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
Py_XDECREF(fieldobj);
|
|
|
|
Py_XDECREF(tmp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-02-17 15:48:00 -04:00
|
|
|
do_markup is the top-level loop for the format() method. It
|
2007-08-24 23:26:07 -03:00
|
|
|
searches through the format string for escapes to markup codes, and
|
|
|
|
calls other functions to move non-markup text to the output,
|
|
|
|
and to perform the markup to the output.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
do_markup(SubString *input, PyObject *args, PyObject *kwargs,
|
2012-05-09 17:25:00 -03:00
|
|
|
_PyUnicodeWriter *writer, int recursion_depth, AutoNumber *auto_number)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
|
|
|
MarkupIterator iter;
|
|
|
|
int format_spec_needs_expanding;
|
|
|
|
int result;
|
2009-03-14 09:29:34 -03:00
|
|
|
int field_present;
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
SubString literal;
|
2007-08-24 23:26:07 -03:00
|
|
|
SubString field_name;
|
|
|
|
SubString format_spec;
|
2013-04-02 20:48:39 -03:00
|
|
|
Py_UCS4 conversion;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
MarkupIterator_init(&iter, input->str, input->start, input->end);
|
2009-03-14 09:29:34 -03:00
|
|
|
while ((result = MarkupIterator_next(&iter, &literal, &field_present,
|
|
|
|
&field_name, &format_spec,
|
|
|
|
&conversion,
|
2007-08-24 23:26:07 -03:00
|
|
|
&format_spec_needs_expanding)) == 2) {
|
2013-04-02 20:48:39 -03:00
|
|
|
if (literal.end != literal.start) {
|
|
|
|
if (!field_present && iter.str.start == iter.str.end)
|
|
|
|
writer->overallocate = 0;
|
|
|
|
if (_PyUnicodeWriter_WriteSubstring(writer, literal.str,
|
|
|
|
literal.start, literal.end) < 0)
|
2012-05-09 17:24:08 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:57:52 -03:00
|
|
|
if (field_present) {
|
|
|
|
if (iter.str.start == iter.str.end)
|
2012-06-04 17:52:12 -03:00
|
|
|
writer->overallocate = 0;
|
2007-08-24 23:26:07 -03:00
|
|
|
if (!output_markup(&field_name, &format_spec,
|
2012-05-07 07:47:02 -03:00
|
|
|
format_spec_needs_expanding, conversion, writer,
|
2009-03-14 09:29:34 -03:00
|
|
|
args, kwargs, recursion_depth, auto_number))
|
2007-08-24 23:26:07 -03:00
|
|
|
return 0;
|
2012-05-29 07:57:52 -03:00
|
|
|
}
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
build_string allocates the output string and then
|
|
|
|
calls do_markup to do the heavy lifting.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
build_string(SubString *input, PyObject *args, PyObject *kwargs,
|
2009-03-14 09:29:34 -03:00
|
|
|
int recursion_depth, AutoNumber *auto_number)
|
2007-08-24 23:26:07 -03:00
|
|
|
{
|
2012-05-09 17:25:00 -03:00
|
|
|
_PyUnicodeWriter writer;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
|
|
|
/* check the recursion level */
|
2007-09-04 23:02:43 -03:00
|
|
|
if (recursion_depth <= 0) {
|
2007-08-24 23:26:07 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"Max string recursion exceeded");
|
2011-10-06 21:26:47 -03:00
|
|
|
return NULL;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2013-04-17 18:02:17 -03:00
|
|
|
_PyUnicodeWriter_Init(&writer);
|
|
|
|
writer.overallocate = 1;
|
|
|
|
writer.min_length = PyUnicode_GET_LENGTH(input->str) + 100;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2012-05-07 07:47:02 -03:00
|
|
|
if (!do_markup(input, args, kwargs, &writer, recursion_depth,
|
2009-03-14 09:29:34 -03:00
|
|
|
auto_number)) {
|
2012-05-09 17:25:00 -03:00
|
|
|
_PyUnicodeWriter_Dealloc(&writer);
|
2011-10-06 21:26:47 -03:00
|
|
|
return NULL;
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
2012-05-09 17:25:00 -03:00
|
|
|
return _PyUnicodeWriter_Finish(&writer);
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*********** main routine ***********************************************/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/* this is the main entry point */
|
|
|
|
static PyObject *
|
|
|
|
do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
SubString input;
|
|
|
|
|
|
|
|
/* PEP 3101 says only 2 levels, so that
|
|
|
|
"{0:{1}}".format('abc', 's') # works
|
|
|
|
"{0:{1:{2}}}".format('abc', 's', '') # fails
|
|
|
|
*/
|
2007-09-04 23:02:43 -03:00
|
|
|
int recursion_depth = 2;
|
2007-08-24 23:26:07 -03:00
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
AutoNumber auto_number;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(self) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
AutoNumber_Init(&auto_number);
|
2011-09-28 02:41:54 -03:00
|
|
|
SubString_init(&input, self, 0, PyUnicode_GET_LENGTH(self));
|
2009-03-14 09:29:34 -03:00
|
|
|
return build_string(&input, args, kwargs, recursion_depth, &auto_number);
|
2007-08-24 23:26:07 -03:00
|
|
|
}
|
2007-08-27 20:52:26 -03:00
|
|
|
|
2010-11-04 14:06:58 -03:00
|
|
|
static PyObject *
|
|
|
|
do_string_format_map(PyObject *self, PyObject *obj)
|
|
|
|
{
|
|
|
|
return do_string_format(self, NULL, obj);
|
|
|
|
}
|
2007-08-27 20:52:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*********** formatteriterator ******************************************/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/* This is used to implement string.Formatter.vparse(). It exists so
|
|
|
|
Formatter can share code with the built in unicode.format() method.
|
|
|
|
It's really just a wrapper around MarkupIterator that is callable
|
|
|
|
from Python. */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
2011-11-03 20:22:48 -03:00
|
|
|
PyObject *str;
|
2007-08-27 20:52:26 -03:00
|
|
|
MarkupIterator it_markup;
|
|
|
|
} formatteriterobject;
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatteriter_dealloc(formatteriterobject *it)
|
|
|
|
{
|
|
|
|
Py_XDECREF(it->str);
|
|
|
|
PyObject_FREE(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a tuple:
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
(literal, field_name, format_spec, conversion)
|
|
|
|
|
|
|
|
literal is any literal text to output. might be zero length
|
|
|
|
field_name is the string before the ':'. might be None
|
|
|
|
format_spec is the string after the ':'. mibht be None
|
|
|
|
conversion is either None, or the string after the '!'
|
2007-08-27 20:52:26 -03:00
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
formatteriter_next(formatteriterobject *it)
|
|
|
|
{
|
|
|
|
SubString literal;
|
|
|
|
SubString field_name;
|
|
|
|
SubString format_spec;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 conversion;
|
2007-08-27 20:52:26 -03:00
|
|
|
int format_spec_needs_expanding;
|
2009-03-14 09:29:34 -03:00
|
|
|
int field_present;
|
|
|
|
int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,
|
|
|
|
&field_name, &format_spec, &conversion,
|
2007-08-27 20:52:26 -03:00
|
|
|
&format_spec_needs_expanding);
|
|
|
|
|
|
|
|
/* all of the SubString objects point into it->str, so no
|
|
|
|
memory management needs to be done on them */
|
|
|
|
assert(0 <= result && result <= 2);
|
2007-08-27 22:07:27 -03:00
|
|
|
if (result == 0 || result == 1)
|
2007-08-27 20:52:26 -03:00
|
|
|
/* if 0, error has already been set, if 1, iterator is empty */
|
|
|
|
return NULL;
|
2007-08-27 22:07:27 -03:00
|
|
|
else {
|
2007-08-27 20:52:26 -03:00
|
|
|
PyObject *literal_str = NULL;
|
|
|
|
PyObject *field_name_str = NULL;
|
|
|
|
PyObject *format_spec_str = NULL;
|
|
|
|
PyObject *conversion_str = NULL;
|
|
|
|
PyObject *tuple = NULL;
|
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
literal_str = SubString_new_object(&literal);
|
|
|
|
if (literal_str == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
field_name_str = SubString_new_object(&field_name);
|
|
|
|
if (field_name_str == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* if field_name is non-zero length, return a string for
|
|
|
|
format_spec (even if zero length), else return None */
|
2009-03-14 09:29:34 -03:00
|
|
|
format_spec_str = (field_present ?
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
SubString_new_object_or_empty :
|
|
|
|
SubString_new_object)(&format_spec);
|
|
|
|
if (format_spec_str == NULL)
|
|
|
|
goto done;
|
2007-08-27 20:52:26 -03:00
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
/* if the conversion is not specified, return a None,
|
|
|
|
otherwise create a one length string with the conversion
|
|
|
|
character */
|
|
|
|
if (conversion == '\0') {
|
|
|
|
conversion_str = Py_None;
|
2007-08-27 20:52:26 -03:00
|
|
|
Py_INCREF(conversion_str);
|
|
|
|
}
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
else
|
2011-09-28 02:41:54 -03:00
|
|
|
conversion_str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
|
|
|
|
&conversion, 1);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
if (conversion_str == NULL)
|
|
|
|
goto done;
|
|
|
|
|
2007-08-28 08:15:20 -03:00
|
|
|
tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str,
|
2007-08-27 20:52:26 -03:00
|
|
|
conversion_str);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
done:
|
2007-08-27 20:52:26 -03:00
|
|
|
Py_XDECREF(literal_str);
|
|
|
|
Py_XDECREF(field_name_str);
|
|
|
|
Py_XDECREF(format_spec_str);
|
|
|
|
Py_XDECREF(conversion_str);
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef formatteriter_methods[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2007-08-27 20:52:26 -03:00
|
|
|
};
|
|
|
|
|
2008-02-17 15:48:00 -04:00
|
|
|
static PyTypeObject PyFormatterIter_Type = {
|
2007-08-27 20:52:26 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
"formatteriterator", /* tp_name */
|
|
|
|
sizeof(formatteriterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
2007-08-27 20:52:26 -03:00
|
|
|
/* methods */
|
2010-05-09 12:52:27 -03:00
|
|
|
(destructor)formatteriter_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_reserved */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)formatteriter_next, /* tp_iternext */
|
|
|
|
formatteriter_methods, /* tp_methods */
|
2007-08-27 20:52:26 -03:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* unicode_formatter_parser is used to implement
|
|
|
|
string.Formatter.vformat. it parses a string and returns tuples
|
|
|
|
describing the parsed elements. It's a wrapper around
|
|
|
|
stringlib/string_format.h's MarkupIterator */
|
|
|
|
static PyObject *
|
2011-11-03 20:22:48 -03:00
|
|
|
formatter_parser(PyObject *ignored, PyObject *self)
|
2007-08-27 20:52:26 -03:00
|
|
|
{
|
|
|
|
formatteriterobject *it;
|
|
|
|
|
2011-01-29 07:15:35 -04:00
|
|
|
if (!PyUnicode_Check(self)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(self) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2007-08-27 20:52:26 -03:00
|
|
|
it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
|
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* take ownership, give the object to the iterator */
|
|
|
|
Py_INCREF(self);
|
|
|
|
it->str = self;
|
|
|
|
|
|
|
|
/* initialize the contained MarkupIterator */
|
2011-09-28 02:41:54 -03:00
|
|
|
MarkupIterator_init(&it->it_markup, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
|
2007-08-27 20:52:26 -03:00
|
|
|
return (PyObject *)it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*********** fieldnameiterator ******************************************/
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* This is used to implement string.Formatter.vparse(). It parses the
|
|
|
|
field name into attribute and item values. It's a Python-callable
|
|
|
|
wrapper around FieldNameIterator */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
2011-11-03 20:22:48 -03:00
|
|
|
PyObject *str;
|
2007-08-27 20:52:26 -03:00
|
|
|
FieldNameIterator it_field;
|
|
|
|
} fieldnameiterobject;
|
|
|
|
|
|
|
|
static void
|
|
|
|
fieldnameiter_dealloc(fieldnameiterobject *it)
|
|
|
|
{
|
|
|
|
Py_XDECREF(it->str);
|
|
|
|
PyObject_FREE(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a tuple:
|
|
|
|
(is_attr, value)
|
|
|
|
is_attr is true if we used attribute syntax (e.g., '.foo')
|
|
|
|
false if we used index syntax (e.g., '[foo]')
|
|
|
|
value is an integer or string
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
fieldnameiter_next(fieldnameiterobject *it)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
int is_attr;
|
|
|
|
Py_ssize_t idx;
|
|
|
|
SubString name;
|
|
|
|
|
|
|
|
result = FieldNameIterator_next(&it->it_field, &is_attr,
|
|
|
|
&idx, &name);
|
2007-08-27 22:07:27 -03:00
|
|
|
if (result == 0 || result == 1)
|
2007-08-27 20:52:26 -03:00
|
|
|
/* if 0, error has already been set, if 1, iterator is empty */
|
|
|
|
return NULL;
|
2007-08-27 22:07:27 -03:00
|
|
|
else {
|
2007-08-27 20:52:26 -03:00
|
|
|
PyObject* result = NULL;
|
|
|
|
PyObject* is_attr_obj = NULL;
|
|
|
|
PyObject* obj = NULL;
|
|
|
|
|
|
|
|
is_attr_obj = PyBool_FromLong(is_attr);
|
|
|
|
if (is_attr_obj == NULL)
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
goto done;
|
2007-08-27 20:52:26 -03:00
|
|
|
|
|
|
|
/* either an integer or a string */
|
|
|
|
if (idx != -1)
|
2007-12-02 10:31:20 -04:00
|
|
|
obj = PyLong_FromSsize_t(idx);
|
2007-08-27 20:52:26 -03:00
|
|
|
else
|
|
|
|
obj = SubString_new_object(&name);
|
|
|
|
if (obj == NULL)
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
goto done;
|
2007-08-27 20:52:26 -03:00
|
|
|
|
|
|
|
/* return a tuple of values */
|
|
|
|
result = PyTuple_Pack(2, is_attr_obj, obj);
|
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
done:
|
2007-08-27 20:52:26 -03:00
|
|
|
Py_XDECREF(is_attr_obj);
|
|
|
|
Py_XDECREF(obj);
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
return result;
|
2007-08-27 20:52:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef fieldnameiter_methods[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2007-08-27 20:52:26 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject PyFieldNameIter_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
"fieldnameiterator", /* tp_name */
|
|
|
|
sizeof(fieldnameiterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
2007-08-27 20:52:26 -03:00
|
|
|
/* methods */
|
2010-05-09 12:52:27 -03:00
|
|
|
(destructor)fieldnameiter_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_reserved */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)fieldnameiter_next, /* tp_iternext */
|
|
|
|
fieldnameiter_methods, /* tp_methods */
|
2007-08-27 20:52:26 -03:00
|
|
|
0};
|
|
|
|
|
|
|
|
/* unicode_formatter_field_name_split is used to implement
|
2016-04-17 02:32:47 -03:00
|
|
|
string.Formatter.vformat. it takes a PEP 3101 "field name", and
|
2007-08-27 20:52:26 -03:00
|
|
|
returns a tuple of (first, rest): "first", the part before the
|
|
|
|
first '.' or '['; and "rest", an iterator for the rest of the field
|
|
|
|
name. it's a wrapper around stringlib/string_format.h's
|
|
|
|
field_name_split. The iterator it returns is a
|
|
|
|
FieldNameIterator */
|
|
|
|
static PyObject *
|
2011-11-03 20:22:48 -03:00
|
|
|
formatter_field_name_split(PyObject *ignored, PyObject *self)
|
2007-08-27 20:52:26 -03:00
|
|
|
{
|
|
|
|
SubString first;
|
|
|
|
Py_ssize_t first_idx;
|
|
|
|
fieldnameiterobject *it;
|
|
|
|
|
|
|
|
PyObject *first_obj = NULL;
|
|
|
|
PyObject *result = NULL;
|
|
|
|
|
2011-01-29 07:15:35 -04:00
|
|
|
if (!PyUnicode_Check(self)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(self) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2007-08-27 20:52:26 -03:00
|
|
|
it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
|
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* take ownership, give the object to the iterator. this is
|
|
|
|
just to keep the field_name alive */
|
|
|
|
Py_INCREF(self);
|
|
|
|
it->str = self;
|
|
|
|
|
2009-03-14 09:29:34 -03:00
|
|
|
/* Pass in auto_number = NULL. We'll return an empty string for
|
|
|
|
first_obj in that case. */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (!field_name_split((PyObject*)self, 0, PyUnicode_GET_LENGTH(self),
|
2009-03-14 09:29:34 -03:00
|
|
|
&first, &first_idx, &it->it_field, NULL))
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
goto done;
|
2007-08-27 20:52:26 -03:00
|
|
|
|
2007-08-27 22:07:27 -03:00
|
|
|
/* first becomes an integer, if possible; else a string */
|
2007-08-27 20:52:26 -03:00
|
|
|
if (first_idx != -1)
|
2007-12-02 10:31:20 -04:00
|
|
|
first_obj = PyLong_FromSsize_t(first_idx);
|
2007-08-27 20:52:26 -03:00
|
|
|
else
|
|
|
|
/* convert "first" into a string object */
|
|
|
|
first_obj = SubString_new_object(&first);
|
|
|
|
if (first_obj == NULL)
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
goto done;
|
2007-08-27 20:52:26 -03:00
|
|
|
|
|
|
|
/* return a tuple of values */
|
|
|
|
result = PyTuple_Pack(2, first_obj, it);
|
|
|
|
|
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion).
literal will always be a string, but might be of zero length.
field_name will be None if there is no markup text
format_spec will be a (possibly zero length) string if
field_name is non-None
conversion will be a one character string, or None
This makes the Formatter class, and especially it's parse()
method, easier to understand.
Suggestion was by Jim Jewett, inspired by the "tail" of an
elementtree node.
Also, fixed a reference leak in fieldnameiter_next.
2007-08-29 00:22:59 -03:00
|
|
|
done:
|
2007-08-27 20:52:26 -03:00
|
|
|
Py_XDECREF(it);
|
|
|
|
Py_XDECREF(first_obj);
|
|
|
|
return result;
|
|
|
|
}
|