forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytesobject.c
3489 lines (3066 loc) · 99.4 KB
/
bytesobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* bytes object implementation */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include "bytes_methods.h"
#include "pystrhex.h"
#include <stddef.h>
/*[clinic input]
class bytes "PyBytesObject *" "&PyBytes_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a238f965d64892b]*/
#include "clinic/bytesobject.c.h"
#ifdef COUNT_ALLOCS
Py_ssize_t _Py_null_strings, _Py_one_strings;
#endif
static PyBytesObject *characters[UCHAR_MAX + 1];
static PyBytesObject *nullstring;
/* PyBytesObject_SIZE gives the basic size of a string; any memory allocation
for a string of length n should request PyBytesObject_SIZE + n bytes.
Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves
3 bytes per string allocation on a typical system.
*/
#define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1)
/* Forward declaration */
Py_LOCAL_INLINE(Py_ssize_t) _PyBytesWriter_GetSize(_PyBytesWriter *writer,
char *str);
/*
For PyBytes_FromString(), the parameter `str' points to a null-terminated
string containing exactly `size' bytes.
For PyBytes_FromStringAndSize(), the parameter `str' is
either NULL or else points to a string containing at least `size' bytes.
For PyBytes_FromStringAndSize(), the string in the `str' parameter does
not have to be null-terminated. (Therefore it is safe to construct a
substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.)
If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1'
bytes (setting the last byte to the null terminating character) and you can
fill in the data yourself. If `str' is non-NULL then the resulting
PyBytes object must be treated as immutable and you must not fill in nor
alter the data yourself, since the strings may be shared.
The PyObject member `op->ob_size', which denotes the number of "extra
items" in a variable-size object, will contain the number of bytes
allocated for string data, not counting the null terminating character.
It is therefore equal to the `size' parameter (for
PyBytes_FromStringAndSize()) or the length of the string in the `str'
parameter (for PyBytes_FromString()).
*/
static PyObject *
_PyBytes_FromSize(Py_ssize_t size, int use_calloc)
{
PyBytesObject *op;
assert(size >= 0);
if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS
_Py_null_strings++;
#endif
Py_INCREF(op);
return (PyObject *)op;
}
if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
PyErr_SetString(PyExc_OverflowError,
"byte string is too large");
return NULL;
}
/* Inline PyObject_NewVar */
if (use_calloc)
op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size);
else
op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
if (op == NULL)
return PyErr_NoMemory();
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
op->ob_shash = -1;
if (!use_calloc)
op->ob_sval[size] = '\0';
/* empty byte string singleton */
if (size == 0) {
nullstring = op;
Py_INCREF(op);
}
return (PyObject *) op;
}
PyObject *
PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
{
PyBytesObject *op;
if (size < 0) {
PyErr_SetString(PyExc_SystemError,
"Negative size passed to PyBytes_FromStringAndSize");
return NULL;
}
if (size == 1 && str != NULL &&
(op = characters[*str & UCHAR_MAX]) != NULL)
{
#ifdef COUNT_ALLOCS
_Py_one_strings++;
#endif
Py_INCREF(op);
return (PyObject *)op;
}
op = (PyBytesObject *)_PyBytes_FromSize(size, 0);
if (op == NULL)
return NULL;
if (str == NULL)
return (PyObject *) op;
memcpy(op->ob_sval, str, size);
/* share short strings */
if (size == 1) {
characters[*str & UCHAR_MAX] = op;
Py_INCREF(op);
}
return (PyObject *) op;
}
PyObject *
PyBytes_FromString(const char *str)
{
size_t size;
PyBytesObject *op;
assert(str != NULL);
size = strlen(str);
if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
PyErr_SetString(PyExc_OverflowError,
"byte string is too long");
return NULL;
}
if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS
_Py_null_strings++;
#endif
Py_INCREF(op);
return (PyObject *)op;
}
if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
#ifdef COUNT_ALLOCS
_Py_one_strings++;
#endif
Py_INCREF(op);
return (PyObject *)op;
}
/* Inline PyObject_NewVar */
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
if (op == NULL)
return PyErr_NoMemory();
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
op->ob_shash = -1;
memcpy(op->ob_sval, str, size+1);
/* share short strings */
if (size == 0) {
nullstring = op;
Py_INCREF(op);
} else if (size == 1) {
characters[*str & UCHAR_MAX] = op;
Py_INCREF(op);
}
return (PyObject *) op;
}
PyObject *
PyBytes_FromFormatV(const char *format, va_list vargs)
{
char *s;
const char *f;
const char *p;
Py_ssize_t prec;
int longflag;
int size_tflag;
/* Longest 64-bit formatted numbers:
- "18446744073709551615\0" (21 bytes)
- "-9223372036854775808\0" (21 bytes)
Decimal takes the most space (it isn't enough for octal.)
Longest 64-bit pointer representation:
"0xffffffffffffffff\0" (19 bytes). */
char buffer[21];
_PyBytesWriter writer;
_PyBytesWriter_Init(&writer);
s = _PyBytesWriter_Alloc(&writer, strlen(format));
if (s == NULL)
return NULL;
writer.overallocate = 1;
#define WRITE_BYTES(str) \
do { \
s = _PyBytesWriter_WriteBytes(&writer, s, (str), strlen(str)); \
if (s == NULL) \
goto error; \
} while (0)
for (f = format; *f; f++) {
if (*f != '%') {
*s++ = *f;
continue;
}
p = f++;
/* ignore the width (ex: 10 in "%10s") */
while (Py_ISDIGIT(*f))
f++;
/* parse the precision (ex: 10 in "%.10s") */
prec = 0;
if (*f == '.') {
f++;
for (; Py_ISDIGIT(*f); f++) {
prec = (prec * 10) + (*f - '0');
}
}
while (*f && *f != '%' && !Py_ISALPHA(*f))
f++;
/* handle the long flag ('l'), but only for %ld and %lu.
others can be added when necessary. */
longflag = 0;
if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
longflag = 1;
++f;
}
/* handle the size_t flag ('z'). */
size_tflag = 0;
if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
size_tflag = 1;
++f;
}
/* subtract bytes preallocated for the format string
(ex: 2 for "%s") */
writer.min_size -= (f - p + 1);
switch (*f) {
case 'c':
{
int c = va_arg(vargs, int);
if (c < 0 || c > 255) {
PyErr_SetString(PyExc_OverflowError,
"PyBytes_FromFormatV(): %c format "
"expects an integer in range [0; 255]");
goto error;
}
writer.min_size++;
*s++ = (unsigned char)c;
break;
}
case 'd':
if (longflag)
sprintf(buffer, "%ld", va_arg(vargs, long));
else if (size_tflag)
sprintf(buffer, "%" PY_FORMAT_SIZE_T "d",
va_arg(vargs, Py_ssize_t));
else
sprintf(buffer, "%d", va_arg(vargs, int));
assert(strlen(buffer) < sizeof(buffer));
WRITE_BYTES(buffer);
break;
case 'u':
if (longflag)
sprintf(buffer, "%lu",
va_arg(vargs, unsigned long));
else if (size_tflag)
sprintf(buffer, "%" PY_FORMAT_SIZE_T "u",
va_arg(vargs, size_t));
else
sprintf(buffer, "%u",
va_arg(vargs, unsigned int));
assert(strlen(buffer) < sizeof(buffer));
WRITE_BYTES(buffer);
break;
case 'i':
sprintf(buffer, "%i", va_arg(vargs, int));
assert(strlen(buffer) < sizeof(buffer));
WRITE_BYTES(buffer);
break;
case 'x':
sprintf(buffer, "%x", va_arg(vargs, int));
assert(strlen(buffer) < sizeof(buffer));
WRITE_BYTES(buffer);
break;
case 's':
{
Py_ssize_t i;
p = va_arg(vargs, const char*);
if (prec <= 0) {
i = strlen(p);
}
else {
i = 0;
while (i < prec && p[i]) {
i++;
}
}
s = _PyBytesWriter_WriteBytes(&writer, s, p, i);
if (s == NULL)
goto error;
break;
}
case 'p':
sprintf(buffer, "%p", va_arg(vargs, void*));
assert(strlen(buffer) < sizeof(buffer));
/* %p is ill-defined: ensure leading 0x. */
if (buffer[1] == 'X')
buffer[1] = 'x';
else if (buffer[1] != 'x') {
memmove(buffer+2, buffer, strlen(buffer)+1);
buffer[0] = '0';
buffer[1] = 'x';
}
WRITE_BYTES(buffer);
break;
case '%':
writer.min_size++;
*s++ = '%';
break;
default:
if (*f == 0) {
/* fix min_size if we reached the end of the format string */
writer.min_size++;
}
/* invalid format string: copy unformatted string and exit */
WRITE_BYTES(p);
return _PyBytesWriter_Finish(&writer, s);
}
}
#undef WRITE_BYTES
return _PyBytesWriter_Finish(&writer, s);
error:
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
PyObject *
PyBytes_FromFormat(const char *format, ...)
{
PyObject* ret;
va_list vargs;
#ifdef HAVE_STDARG_PROTOTYPES
va_start(vargs, format);
#else
va_start(vargs);
#endif
ret = PyBytes_FromFormatV(format, vargs);
va_end(vargs);
return ret;
}
/* Helpers for formatstring */
Py_LOCAL_INLINE(PyObject *)
getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
{
Py_ssize_t argidx = *p_argidx;
if (argidx < arglen) {
(*p_argidx)++;
if (arglen < 0)
return args;
else
return PyTuple_GetItem(args, argidx);
}
PyErr_SetString(PyExc_TypeError,
"not enough arguments for format string");
return NULL;
}
/* Format codes
* F_LJUST '-'
* F_SIGN '+'
* F_BLANK ' '
* F_ALT '#'
* F_ZERO '0'
*/
#define F_LJUST (1<<0)
#define F_SIGN (1<<1)
#define F_BLANK (1<<2)
#define F_ALT (1<<3)
#define F_ZERO (1<<4)
/* Returns a new reference to a PyBytes object, or NULL on failure. */
static char*
formatfloat(PyObject *v, int flags, int prec, int type,
PyObject **p_result, _PyBytesWriter *writer, char *str)
{
char *p;
PyObject *result;
double x;
size_t len;
x = PyFloat_AsDouble(v);
if (x == -1.0 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, "float argument required, "
"not %.200s", Py_TYPE(v)->tp_name);
return NULL;
}
if (prec < 0)
prec = 6;
p = PyOS_double_to_string(x, type, prec,
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
if (p == NULL)
return NULL;
len = strlen(p);
if (writer != NULL) {
str = _PyBytesWriter_Prepare(writer, str, len);
if (str == NULL)
return NULL;
memcpy(str, p, len);
PyMem_Free(p);
str += len;
return str;
}
result = PyBytes_FromStringAndSize(p, len);
PyMem_Free(p);
*p_result = result;
return result != NULL ? str : NULL;
}
static PyObject *
formatlong(PyObject *v, int flags, int prec, int type)
{
PyObject *result, *iobj;
if (type == 'i')
type = 'd';
if (PyLong_Check(v))
return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type);
if (PyNumber_Check(v)) {
/* make sure number is a type of integer for o, x, and X */
if (type == 'o' || type == 'x' || type == 'X')
iobj = PyNumber_Index(v);
else
iobj = PyNumber_Long(v);
if (iobj == NULL) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return NULL;
}
else if (!PyLong_Check(iobj))
Py_CLEAR(iobj);
if (iobj != NULL) {
result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type);
Py_DECREF(iobj);
return result;
}
}
PyErr_Format(PyExc_TypeError,
"%%%c format: %s is required, not %.200s", type,
(type == 'o' || type == 'x' || type == 'X') ? "an integer"
: "a number",
Py_TYPE(v)->tp_name);
return NULL;
}
static int
byte_converter(PyObject *arg, char *p)
{
if (PyBytes_Check(arg) && PyBytes_GET_SIZE(arg) == 1) {
*p = PyBytes_AS_STRING(arg)[0];
return 1;
}
else if (PyByteArray_Check(arg) && PyByteArray_GET_SIZE(arg) == 1) {
*p = PyByteArray_AS_STRING(arg)[0];
return 1;
}
else {
PyObject *iobj;
long ival;
int overflow;
/* make sure number is a type of integer */
if (PyLong_Check(arg)) {
ival = PyLong_AsLongAndOverflow(arg, &overflow);
}
else {
iobj = PyNumber_Index(arg);
if (iobj == NULL) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return 0;
goto onError;
}
ival = PyLong_AsLongAndOverflow(iobj, &overflow);
Py_DECREF(iobj);
}
if (!overflow && ival == -1 && PyErr_Occurred())
goto onError;
if (overflow || !(0 <= ival && ival <= 255)) {
PyErr_SetString(PyExc_OverflowError,
"%c arg not in range(256)");
return 0;
}
*p = (char)ival;
return 1;
}
onError:
PyErr_SetString(PyExc_TypeError,
"%c requires an integer in range(256) or a single byte");
return 0;
}
static PyObject *_PyBytes_FromBuffer(PyObject *x);
static PyObject *
format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
{
PyObject *func, *result;
_Py_IDENTIFIER(__bytes__);
/* is it a bytes object? */
if (PyBytes_Check(v)) {
*pbuf = PyBytes_AS_STRING(v);
*plen = PyBytes_GET_SIZE(v);
Py_INCREF(v);
return v;
}
if (PyByteArray_Check(v)) {
*pbuf = PyByteArray_AS_STRING(v);
*plen = PyByteArray_GET_SIZE(v);
Py_INCREF(v);
return v;
}
/* does it support __bytes__? */
func = _PyObject_LookupSpecial(v, &PyId___bytes__);
if (func != NULL) {
result = _PyObject_CallNoArg(func);
Py_DECREF(func);
if (result == NULL)
return NULL;
if (!PyBytes_Check(result)) {
PyErr_Format(PyExc_TypeError,
"__bytes__ returned non-bytes (type %.200s)",
Py_TYPE(result)->tp_name);
Py_DECREF(result);
return NULL;
}
*pbuf = PyBytes_AS_STRING(result);
*plen = PyBytes_GET_SIZE(result);
return result;
}
/* does it support buffer protocol? */
if (PyObject_CheckBuffer(v)) {
/* maybe we can avoid making a copy of the buffer object here? */
result = _PyBytes_FromBuffer(v);
if (result == NULL)
return NULL;
*pbuf = PyBytes_AS_STRING(result);
*plen = PyBytes_GET_SIZE(result);
return result;
}
PyErr_Format(PyExc_TypeError,
"%%b requires a bytes-like object, "
"or an object that implements __bytes__, not '%.100s'",
Py_TYPE(v)->tp_name);
return NULL;
}
/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
PyObject *
_PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
PyObject *args, int use_bytearray)
{
const char *fmt;
char *res;
Py_ssize_t arglen, argidx;
Py_ssize_t fmtcnt;
int args_owned = 0;
PyObject *dict = NULL;
_PyBytesWriter writer;
if (args == NULL) {
PyErr_BadInternalCall();
return NULL;
}
fmt = format;
fmtcnt = format_len;
_PyBytesWriter_Init(&writer);
writer.use_bytearray = use_bytearray;
res = _PyBytesWriter_Alloc(&writer, fmtcnt);
if (res == NULL)
return NULL;
if (!use_bytearray)
writer.overallocate = 1;
if (PyTuple_Check(args)) {
arglen = PyTuple_GET_SIZE(args);
argidx = 0;
}
else {
arglen = -1;
argidx = -2;
}
if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
!PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) &&
!PyByteArray_Check(args)) {
dict = args;
}
while (--fmtcnt >= 0) {
if (*fmt != '%') {
Py_ssize_t len;
char *pos;
pos = (char *)memchr(fmt + 1, '%', fmtcnt);
if (pos != NULL)
len = pos - fmt;
else
len = fmtcnt + 1;
assert(len != 0);
memcpy(res, fmt, len);
res += len;
fmt += len;
fmtcnt -= (len - 1);
}
else {
/* Got a format specifier */
int flags = 0;
Py_ssize_t width = -1;
int prec = -1;
int c = '\0';
int fill;
PyObject *v = NULL;
PyObject *temp = NULL;
const char *pbuf = NULL;
int sign;
Py_ssize_t len = 0;
char onechar; /* For byte_converter() */
Py_ssize_t alloc;
#ifdef Py_DEBUG
char *before;
#endif
fmt++;
if (*fmt == '%') {
*res++ = '%';
fmt++;
fmtcnt--;
continue;
}
if (*fmt == '(') {
const char *keystart;
Py_ssize_t keylen;
PyObject *key;
int pcount = 1;
if (dict == NULL) {
PyErr_SetString(PyExc_TypeError,
"format requires a mapping");
goto error;
}
++fmt;
--fmtcnt;
keystart = fmt;
/* Skip over balanced parentheses */
while (pcount > 0 && --fmtcnt >= 0) {
if (*fmt == ')')
--pcount;
else if (*fmt == '(')
++pcount;
fmt++;
}
keylen = fmt - keystart - 1;
if (fmtcnt < 0 || pcount > 0) {
PyErr_SetString(PyExc_ValueError,
"incomplete format key");
goto error;
}
key = PyBytes_FromStringAndSize(keystart,
keylen);
if (key == NULL)
goto error;
if (args_owned) {
Py_DECREF(args);
args_owned = 0;
}
args = PyObject_GetItem(dict, key);
Py_DECREF(key);
if (args == NULL) {
goto error;
}
args_owned = 1;
arglen = -1;
argidx = -2;
}
/* Parse flags. Example: "%+i" => flags=F_SIGN. */
while (--fmtcnt >= 0) {
switch (c = *fmt++) {
case '-': flags |= F_LJUST; continue;
case '+': flags |= F_SIGN; continue;
case ' ': flags |= F_BLANK; continue;
case '#': flags |= F_ALT; continue;
case '0': flags |= F_ZERO; continue;
}
break;
}
/* Parse width. Example: "%10s" => width=10 */
if (c == '*') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
if (!PyLong_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"* wants int");
goto error;
}
width = PyLong_AsSsize_t(v);
if (width == -1 && PyErr_Occurred())
goto error;
if (width < 0) {
flags |= F_LJUST;
width = -width;
}
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (c >= 0 && isdigit(c)) {
width = c - '0';
while (--fmtcnt >= 0) {
c = Py_CHARMASK(*fmt++);
if (!isdigit(c))
break;
if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
PyErr_SetString(
PyExc_ValueError,
"width too big");
goto error;
}
width = width*10 + (c - '0');
}
}
/* Parse precision. Example: "%.3f" => prec=3 */
if (c == '.') {
prec = 0;
if (--fmtcnt >= 0)
c = *fmt++;
if (c == '*') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
if (!PyLong_Check(v)) {
PyErr_SetString(
PyExc_TypeError,
"* wants int");
goto error;
}
prec = _PyLong_AsInt(v);
if (prec == -1 && PyErr_Occurred())
goto error;
if (prec < 0)
prec = 0;
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (c >= 0 && isdigit(c)) {
prec = c - '0';
while (--fmtcnt >= 0) {
c = Py_CHARMASK(*fmt++);
if (!isdigit(c))
break;
if (prec > (INT_MAX - ((int)c - '0')) / 10) {
PyErr_SetString(
PyExc_ValueError,
"prec too big");
goto error;
}
prec = prec*10 + (c - '0');
}
}
} /* prec */
if (fmtcnt >= 0) {
if (c == 'h' || c == 'l' || c == 'L') {
if (--fmtcnt >= 0)
c = *fmt++;
}
}
if (fmtcnt < 0) {
PyErr_SetString(PyExc_ValueError,
"incomplete format");
goto error;
}
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
if (fmtcnt == 0) {
/* last write: disable writer overallocation */
writer.overallocate = 0;
}
sign = 0;
fill = ' ';
switch (c) {
case 'r':
// %r is only for 2/3 code; 3 only code should use %a
case 'a':
temp = PyObject_ASCII(v);
if (temp == NULL)
goto error;
assert(PyUnicode_IS_ASCII(temp));
pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
len = PyUnicode_GET_LENGTH(temp);
if (prec >= 0 && len > prec)
len = prec;
break;
case 's':
// %s is only for 2/3 code; 3 only code should use %b
case 'b':
temp = format_obj(v, &pbuf, &len);
if (temp == NULL)
goto error;
if (prec >= 0 && len > prec)
len = prec;
break;
case 'i':
case 'd':
case 'u':
case 'o':
case 'x':
case 'X':
if (PyLong_CheckExact(v)
&& width == -1 && prec == -1
&& !(flags & (F_SIGN | F_BLANK))
&& c != 'X')
{
/* Fast path */
int alternate = flags & F_ALT;
int base;
switch(c)
{
default:
Py_UNREACHABLE();
case 'd':
case 'i':
case 'u':
base = 10;
break;
case 'o':
base = 8;
break;
case 'x':
case 'X':
base = 16;
break;
}
/* Fast path */
writer.min_size -= 2; /* size preallocated for "%d" */
res = _PyLong_FormatBytesWriter(&writer, res,
v, base, alternate);
if (res == NULL)
goto error;
continue;
}
temp = formatlong(v, flags, prec, c);
if (!temp)
goto error;
assert(PyUnicode_IS_ASCII(temp));
pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
len = PyUnicode_GET_LENGTH(temp);
sign = 1;
if (flags & F_ZERO)
fill = '0';
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
if (width == -1 && prec == -1
&& !(flags & (F_SIGN | F_BLANK)))
{
/* Fast path */
writer.min_size -= 2; /* size preallocated for "%f" */
res = formatfloat(v, flags, prec, c, NULL, &writer, res);
if (res == NULL)
goto error;
continue;
}
if (!formatfloat(v, flags, prec, c, &temp, NULL, res))
goto error;
pbuf = PyBytes_AS_STRING(temp);
len = PyBytes_GET_SIZE(temp);
sign = 1;
if (flags & F_ZERO)
fill = '0';
break;
case 'c':
pbuf = &onechar;
len = byte_converter(v, &onechar);
if (!len)
goto error;
if (width == -1) {
/* Fast path */
*res++ = onechar;
continue;
}
break;
default:
PyErr_Format(PyExc_ValueError,
"unsupported format character '%c' (0x%x) "
"at index %zd",
c, c,
(Py_ssize_t)(fmt - 1 - format));
goto error;
}
if (sign) {
if (*pbuf == '-' || *pbuf == '+') {
sign = *pbuf++;
len--;
}
else if (flags & F_SIGN)
sign = '+';
else if (flags & F_BLANK)
sign = ' ';
else
sign = 0;
}
if (width < len)
width = len;
alloc = width;
if (sign != 0 && len == width)
alloc++;
/* 2: size preallocated for %s */
if (alloc > 2) {
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
if (res == NULL)
goto error;
}
#ifdef Py_DEBUG
before = res;
#endif
/* Write the sign if needed */
if (sign) {
if (fill != ' ')
*res++ = sign;
if (width > len)
width--;
}
/* Write the numeric prefix for "x", "X" and "o" formats
if the alternate form is used.
For example, write "0x" for the "%#x" format. */
if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
assert(pbuf[0] == '0');