Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/unicode_format.c
Line
Count
Source
1
/*
2
3
Unicode implementation based on original code by Fredrik Lundh,
4
modified by Marc-Andre Lemburg <mal@lemburg.com>.
5
6
Major speed upgrades to the method implementations at the Reykjavik
7
NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
8
9
Copyright (c) Corporation for National Research Initiatives.
10
11
--------------------------------------------------------------------
12
The original string type implementation is:
13
14
  Copyright (c) 1999 by Secret Labs AB
15
  Copyright (c) 1999 by Fredrik Lundh
16
17
By obtaining, using, and/or copying this software and/or its
18
associated documentation, you agree that you have read, understood,
19
and will comply with the following terms and conditions:
20
21
Permission to use, copy, modify, and distribute this software and its
22
associated documentation for any purpose and without fee is hereby
23
granted, provided that the above copyright notice appears in all
24
copies, and that both that copyright notice and this permission notice
25
appear in supporting documentation, and that the name of Secret Labs
26
AB or the author not be used in advertising or publicity pertaining to
27
distribution of the software without specific, written prior
28
permission.
29
30
SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
31
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
32
FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
33
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
36
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37
--------------------------------------------------------------------
38
39
*/
40
41
// PyUnicode_Format() implementation
42
43
#include "Python.h"
44
#include "pycore_abstract.h"      // _PyIndex_Check()
45
#include "pycore_format.h"        // F_ALT
46
#include "pycore_long.h"          // _PyLong_FormatWriter()
47
#include "pycore_object.h"        // _PyObject_IsUniquelyReferenced()
48
#include "pycore_unicodeobject.h" // _Py_MAX_UNICODE
49
50
51
0
#define MAX_UNICODE _Py_MAX_UNICODE
52
13.5M
#define ensure_unicode _PyUnicode_EnsureUnicode
53
54
struct unicode_formatter_t {
55
    PyObject *args;
56
    int args_owned;
57
    Py_ssize_t arglen, argidx;
58
    PyObject *dict;
59
60
    int fmtkind;
61
    Py_ssize_t fmtcnt, fmtpos;
62
    const void *fmtdata;
63
    PyObject *fmtstr;
64
65
    _PyUnicodeWriter writer;
66
};
67
68
69
struct unicode_format_arg_t {
70
    Py_UCS4 ch;
71
    int flags;
72
    Py_ssize_t width;
73
    int prec;
74
    int sign;
75
    Py_ssize_t fmtstart;
76
    PyObject *key;
77
};
78
79
80
// Use FORMAT_ERROR("...%s", "") when there is no arguments.
81
853k
#define FORMAT_ERROR(EXC, FMT, ...) do {                                    \
82
853k
    if (arg->key != NULL) {                                                 \
83
0
        PyErr_Format((EXC), "format argument %R: " FMT,                     \
84
0
                     arg->key, __VA_ARGS__);                                \
85
0
    }                                                                       \
86
853k
    else if (ctx->argidx >= 0) {                                            \
87
0
        PyErr_Format((EXC), "format argument %zd: " FMT,                    \
88
0
                     ctx->argidx, __VA_ARGS__);                             \
89
0
    }                                                                       \
90
853k
    else {                                                                  \
91
853k
        PyErr_Format((EXC), "format argument: " FMT, __VA_ARGS__);          \
92
853k
    }                                                                       \
93
853k
} while (0)
94
95
96
static PyObject *
97
unicode_format_getnextarg(struct unicode_formatter_t *ctx, int allowone)
98
22.4M
{
99
22.4M
    Py_ssize_t argidx = ctx->argidx;
100
101
22.4M
    if (argidx < ctx->arglen && (allowone || ctx->arglen >= 0)) {
102
22.4M
        ctx->argidx++;
103
22.4M
        if (ctx->arglen >= 0) {
104
14.7M
            return PyTuple_GetItem(ctx->args, argidx);
105
14.7M
        }
106
7.66M
        else if (allowone) {
107
7.66M
            return ctx->args;
108
7.66M
        }
109
22.4M
    }
110
0
    PyErr_Format(PyExc_TypeError,
111
0
                 "not enough arguments for format string (got %zd)",
112
0
                 ctx->arglen < 0 ? 1 : ctx->arglen);
113
0
    return NULL;
114
22.4M
}
115
116
117
/* Returns a new reference to a PyUnicode object, or NULL on failure. */
118
119
/* Format a float into the writer if the writer is not NULL, or into *p_output
120
   otherwise.
121
122
   Return 0 on success, raise an exception and return -1 on error. */
123
static int
124
formatfloat(PyObject *v,
125
            struct unicode_formatter_t *ctx,
126
            struct unicode_format_arg_t *arg,
127
            PyObject **p_output,
128
            _PyUnicodeWriter *writer)
129
87
{
130
87
    char *p;
131
87
    double x;
132
87
    Py_ssize_t len;
133
87
    int prec;
134
87
    int dtoa_flags = 0;
135
136
87
    x = PyFloat_AsDouble(v);
137
87
    if (x == -1.0 && PyErr_Occurred()) {
138
0
        if (PyErr_ExceptionMatches(PyExc_TypeError)) {
139
0
            FORMAT_ERROR(PyExc_TypeError,
140
0
                         "%%%c requires a real number, not %T",
141
0
                         arg->ch, v);
142
0
        }
143
0
        return -1;
144
0
    }
145
146
87
    prec = arg->prec;
147
87
    if (prec < 0)
148
0
        prec = 6;
149
150
87
    if (arg->flags & F_ALT)
151
0
        dtoa_flags |= Py_DTSF_ALT;
152
87
    p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL);
153
87
    if (p == NULL)
154
0
        return -1;
155
87
    len = strlen(p);
156
87
    if (writer) {
157
0
        if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) {
158
0
            PyMem_Free(p);
159
0
            return -1;
160
0
        }
161
0
    }
162
87
    else {
163
87
        *p_output = _PyUnicode_FromASCII(p, len);
164
87
        if (*p_output == NULL) {
165
0
            PyMem_Free(p);
166
0
            return -1;
167
0
        }
168
87
    }
169
87
    PyMem_Free(p);
170
87
    return 0;
171
87
}
172
173
174
/* formatlong() emulates the format codes d, u, o, x and X, and
175
 * the F_ALT flag, for Python's long (unbounded) ints.  It's not used for
176
 * Python's regular ints.
177
 * Return value:  a new PyUnicodeObject*, or NULL if error.
178
 *     The output string is of the form
179
 *         "-"? ("0x" | "0X")? digit+
180
 *     "0x"/"0X" are present only for x and X conversions, with F_ALT
181
 *         set in flags.  The case of hex digits will be correct,
182
 *     There will be at least prec digits, zero-filled on the left if
183
 *         necessary to get that many.
184
 * val          object to be converted
185
 * flags        bitmask of format flags; only F_ALT is looked at
186
 * prec         minimum number of digits; 0-fill on left if needed
187
 * type         a character in [duoxX]; u acts the same as d
188
 *
189
 * CAUTION:  o, x and X conversions on regular ints can never
190
 * produce a '-' sign, but can for Python's unbounded ints.
191
 */
192
PyObject *
193
_PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
194
4.87M
{
195
4.87M
    PyObject *result = NULL;
196
4.87M
    char *buf;
197
4.87M
    Py_ssize_t i;
198
4.87M
    int sign;           /* 1 if '-', else 0 */
199
4.87M
    int len;            /* number of characters */
200
4.87M
    Py_ssize_t llen;
201
4.87M
    int numdigits;      /* len == numnondigits + numdigits */
202
4.87M
    int numnondigits = 0;
203
204
    /* Avoid exceeding SSIZE_T_MAX */
205
4.87M
    if (prec > INT_MAX-3) {
206
0
        PyErr_SetString(PyExc_OverflowError,
207
0
                        "precision too large");
208
0
        return NULL;
209
0
    }
210
211
4.87M
    assert(PyLong_Check(val));
212
213
4.87M
    switch (type) {
214
0
    default:
215
0
        Py_UNREACHABLE();
216
42
    case 'd':
217
42
    case 'i':
218
42
    case 'u':
219
        /* int and int subclasses should print numerically when a numeric */
220
        /* format code is used (see issue18780) */
221
42
        result = PyNumber_ToBase(val, 10);
222
42
        break;
223
57.5k
    case 'o':
224
57.5k
        numnondigits = 2;
225
57.5k
        result = PyNumber_ToBase(val, 8);
226
57.5k
        break;
227
71
    case 'x':
228
4.82M
    case 'X':
229
4.82M
        numnondigits = 2;
230
4.82M
        result = PyNumber_ToBase(val, 16);
231
4.82M
        break;
232
4.87M
    }
233
4.87M
    if (!result)
234
0
        return NULL;
235
236
4.87M
    assert(_PyUnicode_IsModifiable(result));
237
4.87M
    assert(PyUnicode_IS_ASCII(result));
238
239
    /* To modify the string in-place, there can only be one reference. */
240
4.87M
    if (!_PyObject_IsUniquelyReferenced(result)) {
241
0
        Py_DECREF(result);
242
0
        PyErr_BadInternalCall();
243
0
        return NULL;
244
0
    }
245
4.87M
    buf = PyUnicode_DATA(result);
246
4.87M
    llen = PyUnicode_GET_LENGTH(result);
247
4.87M
    if (llen > INT_MAX) {
248
0
        Py_DECREF(result);
249
0
        PyErr_SetString(PyExc_ValueError,
250
0
                        "string too large in _PyUnicode_FormatLong");
251
0
        return NULL;
252
0
    }
253
4.87M
    len = (int)llen;
254
4.87M
    sign = buf[0] == '-';
255
4.87M
    numnondigits += sign;
256
4.87M
    numdigits = len - numnondigits;
257
4.87M
    assert(numdigits > 0);
258
259
    /* Get rid of base marker unless F_ALT */
260
4.87M
    if (((alt) == 0 &&
261
4.87M
        (type == 'o' || type == 'x' || type == 'X'))) {
262
4.87M
        assert(buf[sign] == '0');
263
4.87M
        assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' ||
264
4.87M
               buf[sign+1] == 'o');
265
4.87M
        numnondigits -= 2;
266
4.87M
        buf += 2;
267
4.87M
        len -= 2;
268
4.87M
        if (sign)
269
0
            buf[0] = '-';
270
4.87M
        assert(len == numnondigits + numdigits);
271
4.87M
        assert(numdigits > 0);
272
4.87M
    }
273
274
    /* Fill with leading zeroes to meet minimum width. */
275
4.87M
    if (prec > numdigits) {
276
0
        PyObject *r1 = PyBytes_FromStringAndSize(NULL,
277
0
                                numnondigits + prec);
278
0
        char *b1;
279
0
        if (!r1) {
280
0
            Py_DECREF(result);
281
0
            return NULL;
282
0
        }
283
0
        b1 = PyBytes_AS_STRING(r1);
284
0
        for (i = 0; i < numnondigits; ++i)
285
0
            *b1++ = *buf++;
286
0
        for (i = 0; i < prec - numdigits; i++)
287
0
            *b1++ = '0';
288
0
        for (i = 0; i < numdigits; i++)
289
0
            *b1++ = *buf++;
290
0
        *b1 = '\0';
291
0
        Py_SETREF(result, r1);
292
0
        buf = PyBytes_AS_STRING(result);
293
0
        len = numnondigits + prec;
294
0
    }
295
296
    /* Fix up case for hex conversions. */
297
4.87M
    if (type == 'X') {
298
        /* Need to convert all lower case letters to upper case.
299
           and need to convert 0x to 0X (and -0x to -0X). */
300
33.7M
        for (i = 0; i < len; i++)
301
28.8M
            if (buf[i] >= 'a' && buf[i] <= 'x')
302
6.19M
                buf[i] -= 'a'-'A';
303
4.82M
    }
304
4.87M
    if (!PyUnicode_Check(result)
305
4.87M
        || buf != PyUnicode_DATA(result)) {
306
4.87M
        PyObject *unicode;
307
4.87M
        unicode = _PyUnicode_FromASCII(buf, len);
308
4.87M
        Py_SETREF(result, unicode);
309
4.87M
    }
310
42
    else if (len != PyUnicode_GET_LENGTH(result)) {
311
0
        if (PyUnicode_Resize(&result, len) < 0)
312
0
            Py_CLEAR(result);
313
0
    }
314
4.87M
    return result;
315
4.87M
}
316
317
318
/* Format an integer or a float as an integer.
319
 * Return 1 if the number has been formatted into the writer,
320
 *        0 if the number has been formatted into *p_output
321
 *       -1 and raise an exception on error */
322
static int
323
mainformatlong(PyObject *v,
324
               struct unicode_formatter_t *ctx,
325
               struct unicode_format_arg_t *arg,
326
               PyObject **p_output,
327
               _PyUnicodeWriter *writer)
328
7.79M
{
329
7.79M
    PyObject *iobj, *res;
330
7.79M
    char type = (char)arg->ch;
331
332
7.79M
    if (!PyNumber_Check(v))
333
853k
        goto wrongtype;
334
335
    /* make sure number is a type of integer for o, x, and X */
336
6.93M
    if (!PyLong_Check(v)) {
337
0
        if (type == 'o' || type == 'x' || type == 'X') {
338
0
            iobj = _PyNumber_Index(v);
339
0
        }
340
0
        else {
341
0
            iobj = PyNumber_Long(v);
342
0
        }
343
0
        if (iobj == NULL ) {
344
0
            if (PyErr_ExceptionMatches(PyExc_TypeError))
345
0
                goto wrongtype;
346
0
            return -1;
347
0
        }
348
0
        assert(PyLong_Check(iobj));
349
0
    }
350
6.93M
    else {
351
6.93M
        iobj = Py_NewRef(v);
352
6.93M
    }
353
354
6.93M
    if (PyLong_CheckExact(v)
355
6.93M
        && arg->width == -1 && arg->prec == -1
356
6.87M
        && !(arg->flags & (F_SIGN | F_BLANK))
357
6.87M
        && type != 'X')
358
2.05M
    {
359
        /* Fast path */
360
2.05M
        int alternate = arg->flags & F_ALT;
361
2.05M
        int base;
362
363
2.05M
        switch(type)
364
2.05M
        {
365
0
            default:
366
0
                Py_UNREACHABLE();
367
2.05M
            case 'd':
368
2.05M
            case 'i':
369
2.05M
            case 'u':
370
2.05M
                base = 10;
371
2.05M
                break;
372
0
            case 'o':
373
0
                base = 8;
374
0
                break;
375
25
            case 'x':
376
25
            case 'X':
377
25
                base = 16;
378
25
                break;
379
2.05M
        }
380
381
2.05M
        if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) {
382
0
            Py_DECREF(iobj);
383
0
            return -1;
384
0
        }
385
2.05M
        Py_DECREF(iobj);
386
2.05M
        return 1;
387
2.05M
    }
388
389
4.87M
    res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type);
390
4.87M
    Py_DECREF(iobj);
391
4.87M
    if (res == NULL)
392
0
        return -1;
393
4.87M
    *p_output = res;
394
4.87M
    return 0;
395
396
853k
wrongtype:
397
853k
    switch(type)
398
853k
    {
399
0
        case 'o':
400
0
        case 'x':
401
0
        case 'X':
402
0
            FORMAT_ERROR(PyExc_TypeError,
403
0
                         "%%%c requires an integer, not %T",
404
0
                         arg->ch, v);
405
0
            break;
406
853k
        default:
407
853k
            FORMAT_ERROR(PyExc_TypeError,
408
853k
                         "%%%c requires a real number, not %T",
409
853k
                         arg->ch, v);
410
853k
            break;
411
853k
    }
412
853k
    return -1;
413
853k
}
414
415
416
static Py_UCS4
417
formatchar(PyObject *v,
418
           struct unicode_formatter_t *ctx,
419
           struct unicode_format_arg_t *arg)
420
0
{
421
    /* presume that the buffer is at least 3 characters long */
422
0
    if (PyUnicode_Check(v)) {
423
0
        if (PyUnicode_GET_LENGTH(v) == 1) {
424
0
            return PyUnicode_READ_CHAR(v, 0);
425
0
        }
426
0
        FORMAT_ERROR(PyExc_TypeError,
427
0
                     "%%c requires an integer or a unicode character, "
428
0
                     "not a string of length %zd",
429
0
                     PyUnicode_GET_LENGTH(v));
430
0
        return (Py_UCS4) -1;
431
0
    }
432
0
    else {
433
0
        int overflow;
434
0
        long x = PyLong_AsLongAndOverflow(v, &overflow);
435
0
        if (x == -1 && PyErr_Occurred()) {
436
0
            if (PyErr_ExceptionMatches(PyExc_TypeError)) {
437
0
                FORMAT_ERROR(PyExc_TypeError,
438
0
                             "%%c requires an integer or a unicode character, "
439
0
                             "not %T",
440
0
                             v);
441
0
            }
442
0
            return (Py_UCS4) -1;
443
0
        }
444
445
0
        if (x < 0 || x > MAX_UNICODE) {
446
            /* this includes an overflow in converting to C long */
447
0
            FORMAT_ERROR(PyExc_OverflowError,
448
0
                         "%%c argument not in range(0x110000)%s", "");
449
0
            return (Py_UCS4) -1;
450
0
        }
451
452
0
        return (Py_UCS4) x;
453
0
    }
454
0
}
455
456
457
/* Parse options of an argument: flags, width, precision.
458
   Handle also "%(name)" syntax.
459
460
   Return 0 if the argument has been formatted into arg->str.
461
   Return 1 if the argument has been written into ctx->writer,
462
   Raise an exception and return -1 on error. */
463
static int
464
unicode_format_arg_parse(struct unicode_formatter_t *ctx,
465
                         struct unicode_format_arg_t *arg)
466
22.4M
{
467
22.4M
#define FORMAT_READ(ctx) \
468
22.9M
        PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos)
469
470
22.4M
    PyObject *v;
471
472
22.4M
    if (arg->ch == '(') {
473
        /* Get argument value from a dictionary. Example: "%(name)s". */
474
50.2k
        Py_ssize_t keystart;
475
50.2k
        Py_ssize_t keylen;
476
50.2k
        int pcount = 1;
477
478
50.2k
        if (ctx->dict == NULL) {
479
0
            PyErr_Format(PyExc_TypeError,
480
0
                         "format requires a mapping, not %T",
481
0
                         ctx->args);
482
0
            return -1;
483
0
        }
484
50.2k
        ++ctx->fmtpos;
485
50.2k
        --ctx->fmtcnt;
486
50.2k
        keystart = ctx->fmtpos;
487
        /* Skip over balanced parentheses */
488
451k
        while (pcount > 0 && --ctx->fmtcnt >= 0) {
489
401k
            arg->ch = FORMAT_READ(ctx);
490
401k
            if (arg->ch == ')')
491
50.2k
                --pcount;
492
351k
            else if (arg->ch == '(')
493
0
                ++pcount;
494
401k
            ctx->fmtpos++;
495
401k
        }
496
50.2k
        keylen = ctx->fmtpos - keystart - 1;
497
50.2k
        if (ctx->fmtcnt < 0 || pcount > 0) {
498
0
            PyErr_Format(PyExc_ValueError,
499
0
                         "stray %% or incomplete format key at position %zd",
500
0
                         arg->fmtstart);
501
0
            return -1;
502
0
        }
503
50.2k
        arg->key = PyUnicode_Substring(ctx->fmtstr,
504
50.2k
                                       keystart, keystart + keylen);
505
50.2k
        if (arg->key == NULL)
506
0
            return -1;
507
50.2k
        if (ctx->args_owned) {
508
35.8k
            ctx->args_owned = 0;
509
35.8k
            Py_DECREF(ctx->args);
510
35.8k
        }
511
50.2k
        ctx->args = PyObject_GetItem(ctx->dict, arg->key);
512
50.2k
        if (ctx->args == NULL)
513
0
            return -1;
514
50.2k
        ctx->args_owned = 1;
515
50.2k
        ctx->arglen = -3;
516
50.2k
        ctx->argidx = -4;
517
50.2k
    }
518
22.3M
    else {
519
22.3M
        if (ctx->arglen < -1) {
520
0
            PyErr_Format(PyExc_ValueError,
521
0
                         "format requires a parenthesised mapping key "
522
0
                         "at position %zd",
523
0
                         arg->fmtstart);
524
0
            return -1;
525
0
        }
526
22.3M
    }
527
528
    /* Parse flags. Example: "%+i" => flags=F_SIGN. */
529
22.4M
    while (--ctx->fmtcnt >= 0) {
530
22.4M
        arg->ch = FORMAT_READ(ctx);
531
22.4M
        ctx->fmtpos++;
532
22.4M
        switch (arg->ch) {
533
0
        case '-': arg->flags |= F_LJUST; continue;
534
0
        case '+': arg->flags |= F_SIGN; continue;
535
0
        case ' ': arg->flags |= F_BLANK; continue;
536
25
        case '#': arg->flags |= F_ALT; continue;
537
58.6k
        case '0': arg->flags |= F_ZERO; continue;
538
22.4M
        }
539
22.4M
        break;
540
22.4M
    }
541
542
    /* Parse width. Example: "%10s" => width=10 */
543
22.4M
    if (arg->ch == '*') {
544
47.5k
        if (ctx->arglen < -1) {
545
0
            PyErr_Format(PyExc_ValueError,
546
0
                    "* cannot be used with a parenthesised mapping key "
547
0
                    "at position %zd",
548
0
                    arg->fmtstart);
549
0
            return -1;
550
0
        }
551
47.5k
        v = unicode_format_getnextarg(ctx, 0);
552
47.5k
        if (v == NULL)
553
0
            return -1;
554
47.5k
        if (!PyLong_Check(v)) {
555
0
            FORMAT_ERROR(PyExc_TypeError, "* requires int, not %T", v);
556
0
            return -1;
557
0
        }
558
47.5k
        arg->width = PyLong_AsSsize_t(v);
559
47.5k
        if (arg->width == -1 && PyErr_Occurred()) {
560
0
            if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
561
0
                FORMAT_ERROR(PyExc_OverflowError,
562
0
                             "too big for width%s", "");
563
0
            }
564
0
            return -1;
565
0
        }
566
47.5k
        if (arg->width < 0) {
567
0
            if (arg->width == PY_SSIZE_T_MIN) {
568
0
                FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
569
0
                return -1;
570
0
            }
571
0
            arg->flags |= F_LJUST;
572
0
            arg->width = -arg->width;
573
0
        }
574
47.5k
        if (--ctx->fmtcnt >= 0) {
575
47.5k
            arg->ch = FORMAT_READ(ctx);
576
47.5k
            ctx->fmtpos++;
577
47.5k
        }
578
47.5k
        assert(arg->width >= 0);
579
47.5k
    }
580
22.3M
    else if (arg->ch >= '0' && arg->ch <= '9') {
581
11.0k
        arg->width = arg->ch - '0';
582
11.0k
        while (--ctx->fmtcnt >= 0) {
583
11.0k
            arg->ch = FORMAT_READ(ctx);
584
11.0k
            ctx->fmtpos++;
585
11.0k
            if (arg->ch < '0' || arg->ch > '9')
586
11.0k
                break;
587
            /* Since arg->ch is unsigned, the RHS would end up as unsigned,
588
               mixing signed and unsigned comparison. Since arg->ch is between
589
               '0' and '9', casting to int is safe. */
590
0
            if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) {
591
0
                PyErr_Format(PyExc_ValueError,
592
0
                             "width too big at position %zd",
593
0
                             arg->fmtstart);
594
0
                return -1;
595
0
            }
596
0
            arg->width = arg->width*10 + (arg->ch - '0');
597
0
        }
598
11.0k
        assert(arg->width >= 0);
599
11.0k
    }
600
601
    /* Parse precision. Example: "%.3f" => prec=3 */
602
22.4M
    if (arg->ch == '.') {
603
87
        arg->prec = 0;
604
87
        if (--ctx->fmtcnt >= 0) {
605
87
            arg->ch = FORMAT_READ(ctx);
606
87
            ctx->fmtpos++;
607
87
        }
608
87
        if (arg->ch == '*') {
609
0
            if (ctx->arglen < -1) {
610
0
                PyErr_Format(PyExc_ValueError,
611
0
                        "* cannot be used with a parenthesised mapping key "
612
0
                        "at position %zd",
613
0
                        arg->fmtstart);
614
0
                return -1;
615
0
            }
616
0
            v = unicode_format_getnextarg(ctx, 0);
617
0
            if (v == NULL)
618
0
                return -1;
619
0
            if (!PyLong_Check(v)) {
620
0
                FORMAT_ERROR(PyExc_TypeError, "* requires int, not %T", v);
621
0
                return -1;
622
0
            }
623
0
            arg->prec = PyLong_AsInt(v);
624
0
            if (arg->prec == -1 && PyErr_Occurred()) {
625
0
                if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
626
0
                    FORMAT_ERROR(PyExc_OverflowError,
627
0
                                 "too big for precision%s", "");
628
0
                }
629
0
                return -1;
630
0
            }
631
0
            if (arg->prec < 0)
632
0
                arg->prec = 0;
633
0
            if (--ctx->fmtcnt >= 0) {
634
0
                arg->ch = FORMAT_READ(ctx);
635
0
                ctx->fmtpos++;
636
0
            }
637
0
        }
638
87
        else if (arg->ch >= '0' && arg->ch <= '9') {
639
87
            arg->prec = arg->ch - '0';
640
87
            while (--ctx->fmtcnt >= 0) {
641
87
                arg->ch = FORMAT_READ(ctx);
642
87
                ctx->fmtpos++;
643
87
                if (arg->ch < '0' || arg->ch > '9')
644
87
                    break;
645
0
                if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) {
646
0
                    PyErr_Format(PyExc_ValueError,
647
0
                                 "precision too big at position %zd",
648
0
                                 arg->fmtstart);
649
0
                    return -1;
650
0
                }
651
0
                arg->prec = arg->prec*10 + (arg->ch - '0');
652
0
            }
653
87
        }
654
87
        assert(arg->prec >= 0);
655
87
    }
656
657
    /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */
658
22.4M
    if (ctx->fmtcnt >= 0) {
659
22.4M
        if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') {
660
0
            if (--ctx->fmtcnt >= 0) {
661
0
                arg->ch = FORMAT_READ(ctx);
662
0
                ctx->fmtpos++;
663
0
            }
664
0
        }
665
22.4M
    }
666
22.4M
    if (ctx->fmtcnt < 0) {
667
0
        PyErr_Format(PyExc_ValueError,
668
0
                     "stray %% at position %zd", arg->fmtstart);
669
0
        return -1;
670
0
    }
671
22.4M
    return 0;
672
673
22.4M
#undef FORMAT_READ
674
22.4M
}
675
676
677
/* Format one argument. Supported conversion specifiers:
678
679
   - "s", "r", "a": any type
680
   - "i", "d", "u": int or float
681
   - "o", "x", "X": int
682
   - "e", "E", "f", "F", "g", "G": float
683
   - "c": int or str (1 character)
684
685
   When possible, the output is written directly into the Unicode writer
686
   (ctx->writer). A string is created when padding is required.
687
688
   Return 0 if the argument has been formatted into *p_str,
689
          1 if the argument has been written into ctx->writer,
690
         -1 on error. */
691
static int
692
unicode_format_arg_format(struct unicode_formatter_t *ctx,
693
                          struct unicode_format_arg_t *arg,
694
                          PyObject **p_str)
695
22.4M
{
696
22.4M
    PyObject *v;
697
22.4M
    _PyUnicodeWriter *writer = &ctx->writer;
698
699
22.4M
    if (ctx->fmtcnt == 0)
700
7.69M
        ctx->writer.overallocate = 0;
701
702
22.4M
    v = unicode_format_getnextarg(ctx, 1);
703
22.4M
    if (v == NULL)
704
0
        return -1;
705
706
707
22.4M
    switch (arg->ch) {
708
14.6M
    case 's':
709
14.6M
    case 'r':
710
14.6M
    case 'a':
711
14.6M
        if (arg->width < 0 && arg->prec < 0) {
712
14.6M
            if (arg->ch == 's') {
713
14.6M
                if (PyUnicodeWriter_WriteStr((PyUnicodeWriter*)writer, v) < 0) {
714
0
                    return -1;
715
0
                }
716
14.6M
                return 1;
717
14.6M
            }
718
3.08k
            else if (arg->ch == 'r') {
719
3.08k
                if (PyUnicodeWriter_WriteRepr((PyUnicodeWriter*)writer, v) < 0) {
720
0
                    return -1;
721
0
                }
722
3.08k
                return 1;
723
3.08k
            }
724
14.6M
        }
725
726
0
        if (arg->ch == 's')
727
0
            *p_str = PyObject_Str(v);
728
0
        else if (arg->ch == 'r')
729
0
            *p_str = PyObject_Repr(v);
730
0
        else
731
0
            *p_str = PyObject_ASCII(v);
732
0
        break;
733
734
0
    case 'i':
735
2.91M
    case 'd':
736
2.91M
    case 'u':
737
2.97M
    case 'o':
738
2.97M
    case 'x':
739
7.79M
    case 'X':
740
7.79M
    {
741
7.79M
        int ret = mainformatlong(v, ctx, arg, p_str, writer);
742
7.79M
        if (ret != 0)
743
2.91M
            return ret;
744
4.87M
        arg->sign = 1;
745
4.87M
        break;
746
7.79M
    }
747
748
0
    case 'e':
749
0
    case 'E':
750
87
    case 'f':
751
87
    case 'F':
752
87
    case 'g':
753
87
    case 'G':
754
87
        if (arg->width == -1 && arg->prec == -1
755
0
            && !(arg->flags & (F_SIGN | F_BLANK)))
756
0
        {
757
            /* Fast path */
758
0
            if (formatfloat(v, ctx, arg, NULL, writer) == -1)
759
0
                return -1;
760
0
            return 1;
761
0
        }
762
763
87
        arg->sign = 1;
764
87
        if (formatfloat(v, ctx, arg, p_str, NULL) == -1)
765
0
            return -1;
766
87
        break;
767
768
87
    case 'c':
769
0
    {
770
0
        Py_UCS4 ch = formatchar(v, ctx, arg);
771
0
        if (ch == (Py_UCS4) -1)
772
0
            return -1;
773
0
        if (arg->width == -1 && arg->prec == -1) {
774
            /* Fast path */
775
0
            if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0)
776
0
                return -1;
777
0
            return 1;
778
0
        }
779
0
        *p_str = PyUnicode_FromOrdinal(ch);
780
0
        break;
781
0
    }
782
783
0
    default:
784
0
        if (arg->ch < 128 && Py_ISALPHA(arg->ch)) {
785
0
            PyErr_Format(PyExc_ValueError,
786
0
                         "unsupported format %%%c at position %zd",
787
0
                         (int)arg->ch, arg->fmtstart);
788
0
        }
789
0
        else if (arg->ch == '\'') {
790
0
            PyErr_Format(PyExc_ValueError,
791
0
                         "stray %% at position %zd or unexpected "
792
0
                         "format character \"'\" at position %zd",
793
0
                         arg->fmtstart,
794
0
                         ctx->fmtpos - 1);
795
0
        }
796
0
        else if (arg->ch >= 32 && arg->ch < 127) {
797
0
            PyErr_Format(PyExc_ValueError,
798
0
                         "stray %% at position %zd or unexpected "
799
0
                         "format character '%c' at position %zd",
800
0
                         arg->fmtstart,
801
0
                         (int)arg->ch, ctx->fmtpos - 1);
802
0
        }
803
0
        else if (Py_UNICODE_ISPRINTABLE(arg->ch)) {
804
0
            PyErr_Format(PyExc_ValueError,
805
0
                         "stray %% at position %zd or unexpected "
806
0
                         "format character '%c' (U+%04X) at position %zd",
807
0
                         arg->fmtstart,
808
0
                         (int)arg->ch, (int)arg->ch, ctx->fmtpos - 1);
809
0
        }
810
0
        else {
811
0
            PyErr_Format(PyExc_ValueError,
812
0
                         "stray %% at position %zd or unexpected "
813
0
                         "format character U+%04X at position %zd",
814
0
                         arg->fmtstart, (int)arg->ch, ctx->fmtpos - 1);
815
0
        }
816
0
        return -1;
817
22.4M
    }
818
4.87M
    if (*p_str == NULL)
819
0
        return -1;
820
4.87M
    assert (PyUnicode_Check(*p_str));
821
4.87M
    return 0;
822
4.87M
}
823
824
825
static int
826
unicode_format_arg_output(struct unicode_formatter_t *ctx,
827
                          struct unicode_format_arg_t *arg,
828
                          PyObject *str)
829
4.87M
{
830
4.87M
    Py_ssize_t len;
831
4.87M
    int kind;
832
4.87M
    const void *pbuf;
833
4.87M
    Py_ssize_t pindex;
834
4.87M
    Py_UCS4 signchar;
835
4.87M
    Py_ssize_t buflen;
836
4.87M
    Py_UCS4 maxchar;
837
4.87M
    Py_ssize_t sublen;
838
4.87M
    _PyUnicodeWriter *writer = &ctx->writer;
839
4.87M
    Py_UCS4 fill;
840
841
4.87M
    fill = ' ';
842
4.87M
    if (arg->sign && arg->flags & F_ZERO)
843
58.6k
        fill = '0';
844
845
4.87M
    len = PyUnicode_GET_LENGTH(str);
846
4.87M
    if ((arg->width == -1 || arg->width <= len)
847
4.82M
        && (arg->prec == -1 || arg->prec >= len)
848
4.82M
        && !(arg->flags & (F_SIGN | F_BLANK)))
849
4.82M
    {
850
        /* Fast path */
851
4.82M
        if (_PyUnicodeWriter_WriteStr(writer, str) == -1)
852
0
            return -1;
853
4.82M
        return 0;
854
4.82M
    }
855
856
    /* Truncate the string for "s", "r" and "a" formats
857
       if the precision is set */
858
57.6k
    if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') {
859
0
        if (arg->prec >= 0 && len > arg->prec)
860
0
            len = arg->prec;
861
0
    }
862
863
    /* Adjust sign and width */
864
57.6k
    kind = PyUnicode_KIND(str);
865
57.6k
    pbuf = PyUnicode_DATA(str);
866
57.6k
    pindex = 0;
867
57.6k
    signchar = '\0';
868
57.6k
    if (arg->sign) {
869
57.6k
        Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex);
870
57.6k
        if (ch == '-' || ch == '+') {
871
0
            signchar = ch;
872
0
            len--;
873
0
            pindex++;
874
0
        }
875
57.6k
        else if (arg->flags & F_SIGN)
876
0
            signchar = '+';
877
57.6k
        else if (arg->flags & F_BLANK)
878
0
            signchar = ' ';
879
57.6k
        else
880
57.6k
            arg->sign = 0;
881
57.6k
    }
882
57.6k
    if (arg->width < len)
883
87
        arg->width = len;
884
885
    /* Prepare the writer */
886
57.6k
    maxchar = writer->maxchar;
887
57.6k
    if (!(arg->flags & F_LJUST)) {
888
57.6k
        if (arg->sign) {
889
0
            if ((arg->width-1) > len)
890
0
                maxchar = Py_MAX(maxchar, fill);
891
0
        }
892
57.6k
        else {
893
57.6k
            if (arg->width > len)
894
57.6k
                maxchar = Py_MAX(maxchar, fill);
895
57.6k
        }
896
57.6k
    }
897
57.6k
    if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) {
898
57.4k
        Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len);
899
57.4k
        maxchar = Py_MAX(maxchar, strmaxchar);
900
57.4k
    }
901
902
57.6k
    buflen = arg->width;
903
57.6k
    if (arg->sign && len == arg->width)
904
0
        buflen++;
905
57.6k
    if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1)
906
0
        return -1;
907
908
    /* Write the sign if needed */
909
57.6k
    if (arg->sign) {
910
0
        if (fill != ' ') {
911
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar);
912
0
            writer->pos += 1;
913
0
        }
914
0
        if (arg->width > len)
915
0
            arg->width--;
916
0
    }
917
918
    /* Write the numeric prefix for "x", "X" and "o" formats
919
       if the alternate form is used.
920
       For example, write "0x" for the "%#x" format. */
921
57.6k
    if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) {
922
0
        assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
923
0
        assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch);
924
0
        if (fill != ' ') {
925
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0');
926
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch);
927
0
            writer->pos += 2;
928
0
            pindex += 2;
929
0
        }
930
0
        arg->width -= 2;
931
0
        if (arg->width < 0)
932
0
            arg->width = 0;
933
0
        len -= 2;
934
0
    }
935
936
    /* Pad left with the fill character if needed */
937
57.6k
    if (arg->width > len && !(arg->flags & F_LJUST)) {
938
57.6k
        sublen = arg->width - len;
939
57.6k
        _PyUnicode_Fill(writer->kind, writer->data, fill, writer->pos, sublen);
940
57.6k
        writer->pos += sublen;
941
57.6k
        arg->width = len;
942
57.6k
    }
943
944
    /* If padding with spaces: write sign if needed and/or numeric prefix if
945
       the alternate form is used */
946
57.6k
    if (fill == ' ') {
947
87
        if (arg->sign) {
948
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar);
949
0
            writer->pos += 1;
950
0
        }
951
87
        if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) {
952
0
            assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
953
0
            assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch);
954
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0');
955
0
            PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch);
956
0
            writer->pos += 2;
957
0
            pindex += 2;
958
0
        }
959
87
    }
960
961
    /* Write characters */
962
57.6k
    if (len) {
963
57.6k
        _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
964
57.6k
                                      str, pindex, len);
965
57.6k
        writer->pos += len;
966
57.6k
    }
967
968
    /* Pad right with the fill character if needed */
969
57.6k
    if (arg->width > len) {
970
0
        sublen = arg->width - len;
971
0
        _PyUnicode_Fill(writer->kind, writer->data, ' ', writer->pos, sublen);
972
0
        writer->pos += sublen;
973
0
    }
974
57.6k
    return 0;
975
57.6k
}
976
977
978
/* Helper of PyUnicode_Format(): format one arg.
979
   Return 0 on success, raise an exception and return -1 on error. */
980
static int
981
unicode_format_arg(struct unicode_formatter_t *ctx)
982
22.4M
{
983
22.4M
    struct unicode_format_arg_t arg;
984
22.4M
    PyObject *str;
985
22.4M
    int ret;
986
987
22.4M
    arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos);
988
22.4M
    if (arg.ch == '%') {
989
0
        ctx->fmtpos++;
990
0
        ctx->fmtcnt--;
991
0
        if (_PyUnicodeWriter_WriteCharInline(&ctx->writer, '%') < 0)
992
0
            return -1;
993
0
        return 0;
994
0
    }
995
22.4M
    arg.flags = 0;
996
22.4M
    arg.width = -1;
997
22.4M
    arg.prec = -1;
998
22.4M
    arg.sign = 0;
999
22.4M
    arg.fmtstart = ctx->fmtpos - 1;
1000
22.4M
    arg.key = NULL;
1001
22.4M
    str = NULL;
1002
1003
22.4M
    ret = unicode_format_arg_parse(ctx, &arg);
1004
22.4M
    if (ret == -1) {
1005
0
        goto onError;
1006
0
    }
1007
1008
22.4M
    ret = unicode_format_arg_format(ctx, &arg, &str);
1009
22.4M
    if (ret == -1) {
1010
853k
        goto onError;
1011
853k
    }
1012
1013
21.5M
    if (ret != 1) {
1014
4.87M
        ret = unicode_format_arg_output(ctx, &arg, str);
1015
4.87M
        Py_DECREF(str);
1016
4.87M
        if (ret == -1) {
1017
0
            goto onError;
1018
0
        }
1019
4.87M
    }
1020
1021
21.5M
    if (ctx->dict && (ctx->argidx < ctx->arglen)) {
1022
        // XXX: Never happens?
1023
0
        PyErr_SetString(PyExc_TypeError,
1024
0
                        "not all arguments converted during string formatting");
1025
0
        goto onError;
1026
0
    }
1027
21.5M
    Py_XDECREF(arg.key);
1028
21.5M
    return 0;
1029
1030
853k
  onError:
1031
853k
    Py_XDECREF(arg.key);
1032
853k
    return -1;
1033
21.5M
}
1034
1035
1036
PyObject *
1037
PyUnicode_Format(PyObject *format, PyObject *args)
1038
13.5M
{
1039
13.5M
    struct unicode_formatter_t ctx;
1040
1041
13.5M
    if (format == NULL || args == NULL) {
1042
0
        PyErr_BadInternalCall();
1043
0
        return NULL;
1044
0
    }
1045
1046
13.5M
    if (ensure_unicode(format) < 0)
1047
0
        return NULL;
1048
1049
13.5M
    ctx.fmtstr = format;
1050
13.5M
    ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr);
1051
13.5M
    ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr);
1052
13.5M
    ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr);
1053
13.5M
    ctx.fmtpos = 0;
1054
1055
13.5M
    _PyUnicodeWriter_Init(&ctx.writer);
1056
13.5M
    ctx.writer.min_length = ctx.fmtcnt + 100;
1057
13.5M
    ctx.writer.overallocate = 1;
1058
1059
13.5M
    if (PyTuple_Check(args)) {
1060
5.90M
        ctx.arglen = PyTuple_Size(args);
1061
5.90M
        ctx.argidx = 0;
1062
5.90M
    }
1063
7.63M
    else {
1064
7.63M
        ctx.arglen = -1;
1065
7.63M
        ctx.argidx = -2;
1066
7.63M
    }
1067
13.5M
    ctx.args_owned = 0;
1068
13.5M
    if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args))
1069
15.1k
        ctx.dict = args;
1070
13.5M
    else
1071
13.5M
        ctx.dict = NULL;
1072
13.5M
    ctx.args = args;
1073
1074
61.2M
    while (--ctx.fmtcnt >= 0) {
1075
48.5M
        if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') {
1076
26.1M
            Py_ssize_t nonfmtpos;
1077
1078
26.1M
            nonfmtpos = ctx.fmtpos++;
1079
153M
            while (ctx.fmtcnt >= 0 &&
1080
147M
                   PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') {
1081
127M
                ctx.fmtpos++;
1082
127M
                ctx.fmtcnt--;
1083
127M
            }
1084
26.1M
            if (ctx.fmtcnt < 0) {
1085
5.84M
                ctx.fmtpos--;
1086
5.84M
                ctx.writer.overallocate = 0;
1087
5.84M
            }
1088
1089
26.1M
            if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr,
1090
26.1M
                                                nonfmtpos, ctx.fmtpos) < 0)
1091
0
                goto onError;
1092
26.1M
        }
1093
22.4M
        else {
1094
22.4M
            ctx.fmtpos++;
1095
22.4M
            if (unicode_format_arg(&ctx) == -1)
1096
853k
                goto onError;
1097
22.4M
        }
1098
48.5M
    }
1099
1100
12.6M
    if (ctx.argidx < ctx.arglen && !ctx.dict) {
1101
0
        PyErr_Format(PyExc_TypeError,
1102
0
                     "not all arguments converted during string formatting "
1103
0
                     "(required %zd, got %zd)",
1104
0
                     ctx.arglen < 0 ? 0 : ctx.argidx,
1105
0
                     ctx.arglen < 0 ? 1 : ctx.arglen);
1106
0
        goto onError;
1107
0
    }
1108
1109
12.6M
    if (ctx.args_owned) {
1110
14.3k
        Py_DECREF(ctx.args);
1111
14.3k
    }
1112
12.6M
    return _PyUnicodeWriter_Finish(&ctx.writer);
1113
1114
853k
  onError:
1115
853k
    _PyUnicodeWriter_Dealloc(&ctx.writer);
1116
853k
    if (ctx.args_owned) {
1117
0
        Py_DECREF(ctx.args);
1118
0
    }
1119
    return NULL;
1120
12.6M
}