mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Add a format character %S to PyUnicode_FromFormat() that
embeds the result of PyObject_Unicode() into the resulting unicode string.
This commit is contained in:
parent
6252e10ed9
commit
1be7e3f2ec
1 changed files with 22 additions and 7 deletions
|
@ -504,14 +504,15 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
|
||||||
count = vargs;
|
count = vargs;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
/* step 1: count the number of %R format specifications
|
/* step 1: count the number of %S/%R format specifications
|
||||||
* (we call PyObject_Repr() for these objects once during step 3
|
* (we call PyObject_Unicode()/PyObject_Repr() for these objects
|
||||||
* and put the result in an array) */
|
* once during step 3 and put the result in an array) */
|
||||||
for (f = format; *f; f++) {
|
for (f = format; *f; f++) {
|
||||||
if (*f == '%' && *(f+1)=='R')
|
if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R'))
|
||||||
++callcount;
|
++callcount;
|
||||||
}
|
}
|
||||||
/* step 2: allocate memory for the results of PyObject_Repr() calls */
|
/* step 2: allocate memory for the results of
|
||||||
|
* PyObject_Unicode()/PyObject_Repr() calls */
|
||||||
if (callcount) {
|
if (callcount) {
|
||||||
callresults = PyMem_Malloc(sizeof(PyObject *)*callcount);
|
callresults = PyMem_Malloc(sizeof(PyObject *)*callcount);
|
||||||
if (!callresults) {
|
if (!callresults) {
|
||||||
|
@ -558,6 +559,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
|
||||||
n += PyUnicode_GET_SIZE(obj);
|
n += PyUnicode_GET_SIZE(obj);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'S':
|
||||||
|
{
|
||||||
|
PyObject *obj = va_arg(count, PyObject *);
|
||||||
|
PyObject *str;
|
||||||
|
assert(obj);
|
||||||
|
str = PyObject_Unicode(obj);
|
||||||
|
if (!str)
|
||||||
|
goto fail;
|
||||||
|
n += PyUnicode_GET_SIZE(str);
|
||||||
|
/* Remember the str and switch to the next slot */
|
||||||
|
*callresult++ = str;
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'R':
|
case 'R':
|
||||||
{
|
{
|
||||||
PyObject *obj = va_arg(count, PyObject *);
|
PyObject *obj = va_arg(count, PyObject *);
|
||||||
|
@ -683,6 +697,7 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
|
||||||
*s++ = ucopy[upos++];
|
*s++ = ucopy[upos++];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'S':
|
||||||
case 'R':
|
case 'R':
|
||||||
{
|
{
|
||||||
/* unused, since we already have the result */
|
/* unused, since we already have the result */
|
||||||
|
@ -692,9 +707,9 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
|
||||||
Py_ssize_t upos;
|
Py_ssize_t upos;
|
||||||
for (upos = 0; upos<usize;)
|
for (upos = 0; upos<usize;)
|
||||||
*s++ = ucopy[upos++];
|
*s++ = ucopy[upos++];
|
||||||
/* We're done with the repr() => forget it */
|
/* We're done with the unicode()/repr() => forget it */
|
||||||
Py_DECREF(*callresult);
|
Py_DECREF(*callresult);
|
||||||
/* switch to next repr() result */
|
/* switch to next unicode()/repr() result */
|
||||||
++callresult;
|
++callresult;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue