Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/clinic/unicodeobject.c.h
12 views
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
# include "pycore_gc.h" // PyGC_Head
7
# include "pycore_runtime.h" // _Py_ID()
8
#endif
9
10
11
PyDoc_STRVAR(EncodingMap_size__doc__,
12
"size($self, /)\n"
13
"--\n"
14
"\n"
15
"Return the size (in bytes) of this object.");
16
17
#define ENCODINGMAP_SIZE_METHODDEF \
18
{"size", (PyCFunction)EncodingMap_size, METH_NOARGS, EncodingMap_size__doc__},
19
20
static PyObject *
21
EncodingMap_size_impl(struct encoding_map *self);
22
23
static PyObject *
24
EncodingMap_size(struct encoding_map *self, PyObject *Py_UNUSED(ignored))
25
{
26
return EncodingMap_size_impl(self);
27
}
28
29
PyDoc_STRVAR(unicode_title__doc__,
30
"title($self, /)\n"
31
"--\n"
32
"\n"
33
"Return a version of the string where each word is titlecased.\n"
34
"\n"
35
"More specifically, words start with uppercased characters and all remaining\n"
36
"cased characters have lower case.");
37
38
#define UNICODE_TITLE_METHODDEF \
39
{"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__},
40
41
static PyObject *
42
unicode_title_impl(PyObject *self);
43
44
static PyObject *
45
unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored))
46
{
47
return unicode_title_impl(self);
48
}
49
50
PyDoc_STRVAR(unicode_capitalize__doc__,
51
"capitalize($self, /)\n"
52
"--\n"
53
"\n"
54
"Return a capitalized version of the string.\n"
55
"\n"
56
"More specifically, make the first character have upper case and the rest lower\n"
57
"case.");
58
59
#define UNICODE_CAPITALIZE_METHODDEF \
60
{"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__},
61
62
static PyObject *
63
unicode_capitalize_impl(PyObject *self);
64
65
static PyObject *
66
unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored))
67
{
68
return unicode_capitalize_impl(self);
69
}
70
71
PyDoc_STRVAR(unicode_casefold__doc__,
72
"casefold($self, /)\n"
73
"--\n"
74
"\n"
75
"Return a version of the string suitable for caseless comparisons.");
76
77
#define UNICODE_CASEFOLD_METHODDEF \
78
{"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__},
79
80
static PyObject *
81
unicode_casefold_impl(PyObject *self);
82
83
static PyObject *
84
unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored))
85
{
86
return unicode_casefold_impl(self);
87
}
88
89
PyDoc_STRVAR(unicode_center__doc__,
90
"center($self, width, fillchar=\' \', /)\n"
91
"--\n"
92
"\n"
93
"Return a centered string of length width.\n"
94
"\n"
95
"Padding is done using the specified fill character (default is a space).");
96
97
#define UNICODE_CENTER_METHODDEF \
98
{"center", _PyCFunction_CAST(unicode_center), METH_FASTCALL, unicode_center__doc__},
99
100
static PyObject *
101
unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
102
103
static PyObject *
104
unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
105
{
106
PyObject *return_value = NULL;
107
Py_ssize_t width;
108
Py_UCS4 fillchar = ' ';
109
110
if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
111
goto exit;
112
}
113
{
114
Py_ssize_t ival = -1;
115
PyObject *iobj = _PyNumber_Index(args[0]);
116
if (iobj != NULL) {
117
ival = PyLong_AsSsize_t(iobj);
118
Py_DECREF(iobj);
119
}
120
if (ival == -1 && PyErr_Occurred()) {
121
goto exit;
122
}
123
width = ival;
124
}
125
if (nargs < 2) {
126
goto skip_optional;
127
}
128
if (!convert_uc(args[1], &fillchar)) {
129
goto exit;
130
}
131
skip_optional:
132
return_value = unicode_center_impl(self, width, fillchar);
133
134
exit:
135
return return_value;
136
}
137
138
PyDoc_STRVAR(unicode_encode__doc__,
139
"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
140
"--\n"
141
"\n"
142
"Encode the string using the codec registered for encoding.\n"
143
"\n"
144
" encoding\n"
145
" The encoding in which to encode the string.\n"
146
" errors\n"
147
" The error handling scheme to use for encoding errors.\n"
148
" The default is \'strict\' meaning that encoding errors raise a\n"
149
" UnicodeEncodeError. Other possible values are \'ignore\', \'replace\' and\n"
150
" \'xmlcharrefreplace\' as well as any other name registered with\n"
151
" codecs.register_error that can handle UnicodeEncodeErrors.");
152
153
#define UNICODE_ENCODE_METHODDEF \
154
{"encode", _PyCFunction_CAST(unicode_encode), METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
155
156
static PyObject *
157
unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
158
159
static PyObject *
160
unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
161
{
162
PyObject *return_value = NULL;
163
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
164
165
#define NUM_KEYWORDS 2
166
static struct {
167
PyGC_Head _this_is_not_used;
168
PyObject_VAR_HEAD
169
PyObject *ob_item[NUM_KEYWORDS];
170
} _kwtuple = {
171
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
172
.ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
173
};
174
#undef NUM_KEYWORDS
175
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
176
177
#else // !Py_BUILD_CORE
178
# define KWTUPLE NULL
179
#endif // !Py_BUILD_CORE
180
181
static const char * const _keywords[] = {"encoding", "errors", NULL};
182
static _PyArg_Parser _parser = {
183
.keywords = _keywords,
184
.fname = "encode",
185
.kwtuple = KWTUPLE,
186
};
187
#undef KWTUPLE
188
PyObject *argsbuf[2];
189
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
190
const char *encoding = NULL;
191
const char *errors = NULL;
192
193
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
194
if (!args) {
195
goto exit;
196
}
197
if (!noptargs) {
198
goto skip_optional_pos;
199
}
200
if (args[0]) {
201
if (!PyUnicode_Check(args[0])) {
202
_PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
203
goto exit;
204
}
205
Py_ssize_t encoding_length;
206
encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
207
if (encoding == NULL) {
208
goto exit;
209
}
210
if (strlen(encoding) != (size_t)encoding_length) {
211
PyErr_SetString(PyExc_ValueError, "embedded null character");
212
goto exit;
213
}
214
if (!--noptargs) {
215
goto skip_optional_pos;
216
}
217
}
218
if (!PyUnicode_Check(args[1])) {
219
_PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
220
goto exit;
221
}
222
Py_ssize_t errors_length;
223
errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
224
if (errors == NULL) {
225
goto exit;
226
}
227
if (strlen(errors) != (size_t)errors_length) {
228
PyErr_SetString(PyExc_ValueError, "embedded null character");
229
goto exit;
230
}
231
skip_optional_pos:
232
return_value = unicode_encode_impl(self, encoding, errors);
233
234
exit:
235
return return_value;
236
}
237
238
PyDoc_STRVAR(unicode_expandtabs__doc__,
239
"expandtabs($self, /, tabsize=8)\n"
240
"--\n"
241
"\n"
242
"Return a copy where all tab characters are expanded using spaces.\n"
243
"\n"
244
"If tabsize is not given, a tab size of 8 characters is assumed.");
245
246
#define UNICODE_EXPANDTABS_METHODDEF \
247
{"expandtabs", _PyCFunction_CAST(unicode_expandtabs), METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
248
249
static PyObject *
250
unicode_expandtabs_impl(PyObject *self, int tabsize);
251
252
static PyObject *
253
unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
254
{
255
PyObject *return_value = NULL;
256
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
257
258
#define NUM_KEYWORDS 1
259
static struct {
260
PyGC_Head _this_is_not_used;
261
PyObject_VAR_HEAD
262
PyObject *ob_item[NUM_KEYWORDS];
263
} _kwtuple = {
264
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
265
.ob_item = { &_Py_ID(tabsize), },
266
};
267
#undef NUM_KEYWORDS
268
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
269
270
#else // !Py_BUILD_CORE
271
# define KWTUPLE NULL
272
#endif // !Py_BUILD_CORE
273
274
static const char * const _keywords[] = {"tabsize", NULL};
275
static _PyArg_Parser _parser = {
276
.keywords = _keywords,
277
.fname = "expandtabs",
278
.kwtuple = KWTUPLE,
279
};
280
#undef KWTUPLE
281
PyObject *argsbuf[1];
282
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
283
int tabsize = 8;
284
285
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
286
if (!args) {
287
goto exit;
288
}
289
if (!noptargs) {
290
goto skip_optional_pos;
291
}
292
tabsize = _PyLong_AsInt(args[0]);
293
if (tabsize == -1 && PyErr_Occurred()) {
294
goto exit;
295
}
296
skip_optional_pos:
297
return_value = unicode_expandtabs_impl(self, tabsize);
298
299
exit:
300
return return_value;
301
}
302
303
PyDoc_STRVAR(unicode_isascii__doc__,
304
"isascii($self, /)\n"
305
"--\n"
306
"\n"
307
"Return True if all characters in the string are ASCII, False otherwise.\n"
308
"\n"
309
"ASCII characters have code points in the range U+0000-U+007F.\n"
310
"Empty string is ASCII too.");
311
312
#define UNICODE_ISASCII_METHODDEF \
313
{"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__},
314
315
static PyObject *
316
unicode_isascii_impl(PyObject *self);
317
318
static PyObject *
319
unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
320
{
321
return unicode_isascii_impl(self);
322
}
323
324
PyDoc_STRVAR(unicode_islower__doc__,
325
"islower($self, /)\n"
326
"--\n"
327
"\n"
328
"Return True if the string is a lowercase string, False otherwise.\n"
329
"\n"
330
"A string is lowercase if all cased characters in the string are lowercase and\n"
331
"there is at least one cased character in the string.");
332
333
#define UNICODE_ISLOWER_METHODDEF \
334
{"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__},
335
336
static PyObject *
337
unicode_islower_impl(PyObject *self);
338
339
static PyObject *
340
unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
341
{
342
return unicode_islower_impl(self);
343
}
344
345
PyDoc_STRVAR(unicode_isupper__doc__,
346
"isupper($self, /)\n"
347
"--\n"
348
"\n"
349
"Return True if the string is an uppercase string, False otherwise.\n"
350
"\n"
351
"A string is uppercase if all cased characters in the string are uppercase and\n"
352
"there is at least one cased character in the string.");
353
354
#define UNICODE_ISUPPER_METHODDEF \
355
{"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__},
356
357
static PyObject *
358
unicode_isupper_impl(PyObject *self);
359
360
static PyObject *
361
unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
362
{
363
return unicode_isupper_impl(self);
364
}
365
366
PyDoc_STRVAR(unicode_istitle__doc__,
367
"istitle($self, /)\n"
368
"--\n"
369
"\n"
370
"Return True if the string is a title-cased string, False otherwise.\n"
371
"\n"
372
"In a title-cased string, upper- and title-case characters may only\n"
373
"follow uncased characters and lowercase characters only cased ones.");
374
375
#define UNICODE_ISTITLE_METHODDEF \
376
{"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__},
377
378
static PyObject *
379
unicode_istitle_impl(PyObject *self);
380
381
static PyObject *
382
unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
383
{
384
return unicode_istitle_impl(self);
385
}
386
387
PyDoc_STRVAR(unicode_isspace__doc__,
388
"isspace($self, /)\n"
389
"--\n"
390
"\n"
391
"Return True if the string is a whitespace string, False otherwise.\n"
392
"\n"
393
"A string is whitespace if all characters in the string are whitespace and there\n"
394
"is at least one character in the string.");
395
396
#define UNICODE_ISSPACE_METHODDEF \
397
{"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__},
398
399
static PyObject *
400
unicode_isspace_impl(PyObject *self);
401
402
static PyObject *
403
unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
404
{
405
return unicode_isspace_impl(self);
406
}
407
408
PyDoc_STRVAR(unicode_isalpha__doc__,
409
"isalpha($self, /)\n"
410
"--\n"
411
"\n"
412
"Return True if the string is an alphabetic string, False otherwise.\n"
413
"\n"
414
"A string is alphabetic if all characters in the string are alphabetic and there\n"
415
"is at least one character in the string.");
416
417
#define UNICODE_ISALPHA_METHODDEF \
418
{"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__},
419
420
static PyObject *
421
unicode_isalpha_impl(PyObject *self);
422
423
static PyObject *
424
unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
425
{
426
return unicode_isalpha_impl(self);
427
}
428
429
PyDoc_STRVAR(unicode_isalnum__doc__,
430
"isalnum($self, /)\n"
431
"--\n"
432
"\n"
433
"Return True if the string is an alpha-numeric string, False otherwise.\n"
434
"\n"
435
"A string is alpha-numeric if all characters in the string are alpha-numeric and\n"
436
"there is at least one character in the string.");
437
438
#define UNICODE_ISALNUM_METHODDEF \
439
{"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__},
440
441
static PyObject *
442
unicode_isalnum_impl(PyObject *self);
443
444
static PyObject *
445
unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
446
{
447
return unicode_isalnum_impl(self);
448
}
449
450
PyDoc_STRVAR(unicode_isdecimal__doc__,
451
"isdecimal($self, /)\n"
452
"--\n"
453
"\n"
454
"Return True if the string is a decimal string, False otherwise.\n"
455
"\n"
456
"A string is a decimal string if all characters in the string are decimal and\n"
457
"there is at least one character in the string.");
458
459
#define UNICODE_ISDECIMAL_METHODDEF \
460
{"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__},
461
462
static PyObject *
463
unicode_isdecimal_impl(PyObject *self);
464
465
static PyObject *
466
unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
467
{
468
return unicode_isdecimal_impl(self);
469
}
470
471
PyDoc_STRVAR(unicode_isdigit__doc__,
472
"isdigit($self, /)\n"
473
"--\n"
474
"\n"
475
"Return True if the string is a digit string, False otherwise.\n"
476
"\n"
477
"A string is a digit string if all characters in the string are digits and there\n"
478
"is at least one character in the string.");
479
480
#define UNICODE_ISDIGIT_METHODDEF \
481
{"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__},
482
483
static PyObject *
484
unicode_isdigit_impl(PyObject *self);
485
486
static PyObject *
487
unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
488
{
489
return unicode_isdigit_impl(self);
490
}
491
492
PyDoc_STRVAR(unicode_isnumeric__doc__,
493
"isnumeric($self, /)\n"
494
"--\n"
495
"\n"
496
"Return True if the string is a numeric string, False otherwise.\n"
497
"\n"
498
"A string is numeric if all characters in the string are numeric and there is at\n"
499
"least one character in the string.");
500
501
#define UNICODE_ISNUMERIC_METHODDEF \
502
{"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__},
503
504
static PyObject *
505
unicode_isnumeric_impl(PyObject *self);
506
507
static PyObject *
508
unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
509
{
510
return unicode_isnumeric_impl(self);
511
}
512
513
PyDoc_STRVAR(unicode_isidentifier__doc__,
514
"isidentifier($self, /)\n"
515
"--\n"
516
"\n"
517
"Return True if the string is a valid Python identifier, False otherwise.\n"
518
"\n"
519
"Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
520
"such as \"def\" or \"class\".");
521
522
#define UNICODE_ISIDENTIFIER_METHODDEF \
523
{"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
524
525
static PyObject *
526
unicode_isidentifier_impl(PyObject *self);
527
528
static PyObject *
529
unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
530
{
531
return unicode_isidentifier_impl(self);
532
}
533
534
PyDoc_STRVAR(unicode_isprintable__doc__,
535
"isprintable($self, /)\n"
536
"--\n"
537
"\n"
538
"Return True if the string is printable, False otherwise.\n"
539
"\n"
540
"A string is printable if all of its characters are considered printable in\n"
541
"repr() or if it is empty.");
542
543
#define UNICODE_ISPRINTABLE_METHODDEF \
544
{"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__},
545
546
static PyObject *
547
unicode_isprintable_impl(PyObject *self);
548
549
static PyObject *
550
unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
551
{
552
return unicode_isprintable_impl(self);
553
}
554
555
PyDoc_STRVAR(unicode_join__doc__,
556
"join($self, iterable, /)\n"
557
"--\n"
558
"\n"
559
"Concatenate any number of strings.\n"
560
"\n"
561
"The string whose method is called is inserted in between each given string.\n"
562
"The result is returned as a new string.\n"
563
"\n"
564
"Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'");
565
566
#define UNICODE_JOIN_METHODDEF \
567
{"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__},
568
569
PyDoc_STRVAR(unicode_ljust__doc__,
570
"ljust($self, width, fillchar=\' \', /)\n"
571
"--\n"
572
"\n"
573
"Return a left-justified string of length width.\n"
574
"\n"
575
"Padding is done using the specified fill character (default is a space).");
576
577
#define UNICODE_LJUST_METHODDEF \
578
{"ljust", _PyCFunction_CAST(unicode_ljust), METH_FASTCALL, unicode_ljust__doc__},
579
580
static PyObject *
581
unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
582
583
static PyObject *
584
unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
585
{
586
PyObject *return_value = NULL;
587
Py_ssize_t width;
588
Py_UCS4 fillchar = ' ';
589
590
if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
591
goto exit;
592
}
593
{
594
Py_ssize_t ival = -1;
595
PyObject *iobj = _PyNumber_Index(args[0]);
596
if (iobj != NULL) {
597
ival = PyLong_AsSsize_t(iobj);
598
Py_DECREF(iobj);
599
}
600
if (ival == -1 && PyErr_Occurred()) {
601
goto exit;
602
}
603
width = ival;
604
}
605
if (nargs < 2) {
606
goto skip_optional;
607
}
608
if (!convert_uc(args[1], &fillchar)) {
609
goto exit;
610
}
611
skip_optional:
612
return_value = unicode_ljust_impl(self, width, fillchar);
613
614
exit:
615
return return_value;
616
}
617
618
PyDoc_STRVAR(unicode_lower__doc__,
619
"lower($self, /)\n"
620
"--\n"
621
"\n"
622
"Return a copy of the string converted to lowercase.");
623
624
#define UNICODE_LOWER_METHODDEF \
625
{"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__},
626
627
static PyObject *
628
unicode_lower_impl(PyObject *self);
629
630
static PyObject *
631
unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
632
{
633
return unicode_lower_impl(self);
634
}
635
636
PyDoc_STRVAR(unicode_strip__doc__,
637
"strip($self, chars=None, /)\n"
638
"--\n"
639
"\n"
640
"Return a copy of the string with leading and trailing whitespace removed.\n"
641
"\n"
642
"If chars is given and not None, remove characters in chars instead.");
643
644
#define UNICODE_STRIP_METHODDEF \
645
{"strip", _PyCFunction_CAST(unicode_strip), METH_FASTCALL, unicode_strip__doc__},
646
647
static PyObject *
648
unicode_strip_impl(PyObject *self, PyObject *chars);
649
650
static PyObject *
651
unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
652
{
653
PyObject *return_value = NULL;
654
PyObject *chars = Py_None;
655
656
if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
657
goto exit;
658
}
659
if (nargs < 1) {
660
goto skip_optional;
661
}
662
chars = args[0];
663
skip_optional:
664
return_value = unicode_strip_impl(self, chars);
665
666
exit:
667
return return_value;
668
}
669
670
PyDoc_STRVAR(unicode_lstrip__doc__,
671
"lstrip($self, chars=None, /)\n"
672
"--\n"
673
"\n"
674
"Return a copy of the string with leading whitespace removed.\n"
675
"\n"
676
"If chars is given and not None, remove characters in chars instead.");
677
678
#define UNICODE_LSTRIP_METHODDEF \
679
{"lstrip", _PyCFunction_CAST(unicode_lstrip), METH_FASTCALL, unicode_lstrip__doc__},
680
681
static PyObject *
682
unicode_lstrip_impl(PyObject *self, PyObject *chars);
683
684
static PyObject *
685
unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
686
{
687
PyObject *return_value = NULL;
688
PyObject *chars = Py_None;
689
690
if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
691
goto exit;
692
}
693
if (nargs < 1) {
694
goto skip_optional;
695
}
696
chars = args[0];
697
skip_optional:
698
return_value = unicode_lstrip_impl(self, chars);
699
700
exit:
701
return return_value;
702
}
703
704
PyDoc_STRVAR(unicode_rstrip__doc__,
705
"rstrip($self, chars=None, /)\n"
706
"--\n"
707
"\n"
708
"Return a copy of the string with trailing whitespace removed.\n"
709
"\n"
710
"If chars is given and not None, remove characters in chars instead.");
711
712
#define UNICODE_RSTRIP_METHODDEF \
713
{"rstrip", _PyCFunction_CAST(unicode_rstrip), METH_FASTCALL, unicode_rstrip__doc__},
714
715
static PyObject *
716
unicode_rstrip_impl(PyObject *self, PyObject *chars);
717
718
static PyObject *
719
unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
720
{
721
PyObject *return_value = NULL;
722
PyObject *chars = Py_None;
723
724
if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
725
goto exit;
726
}
727
if (nargs < 1) {
728
goto skip_optional;
729
}
730
chars = args[0];
731
skip_optional:
732
return_value = unicode_rstrip_impl(self, chars);
733
734
exit:
735
return return_value;
736
}
737
738
PyDoc_STRVAR(unicode_replace__doc__,
739
"replace($self, old, new, count=-1, /)\n"
740
"--\n"
741
"\n"
742
"Return a copy with all occurrences of substring old replaced by new.\n"
743
"\n"
744
" count\n"
745
" Maximum number of occurrences to replace.\n"
746
" -1 (the default value) means replace all occurrences.\n"
747
"\n"
748
"If the optional argument count is given, only the first count occurrences are\n"
749
"replaced.");
750
751
#define UNICODE_REPLACE_METHODDEF \
752
{"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL, unicode_replace__doc__},
753
754
static PyObject *
755
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
756
Py_ssize_t count);
757
758
static PyObject *
759
unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
760
{
761
PyObject *return_value = NULL;
762
PyObject *old;
763
PyObject *new;
764
Py_ssize_t count = -1;
765
766
if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
767
goto exit;
768
}
769
if (!PyUnicode_Check(args[0])) {
770
_PyArg_BadArgument("replace", "argument 1", "str", args[0]);
771
goto exit;
772
}
773
old = args[0];
774
if (!PyUnicode_Check(args[1])) {
775
_PyArg_BadArgument("replace", "argument 2", "str", args[1]);
776
goto exit;
777
}
778
new = args[1];
779
if (nargs < 3) {
780
goto skip_optional;
781
}
782
{
783
Py_ssize_t ival = -1;
784
PyObject *iobj = _PyNumber_Index(args[2]);
785
if (iobj != NULL) {
786
ival = PyLong_AsSsize_t(iobj);
787
Py_DECREF(iobj);
788
}
789
if (ival == -1 && PyErr_Occurred()) {
790
goto exit;
791
}
792
count = ival;
793
}
794
skip_optional:
795
return_value = unicode_replace_impl(self, old, new, count);
796
797
exit:
798
return return_value;
799
}
800
801
PyDoc_STRVAR(unicode_removeprefix__doc__,
802
"removeprefix($self, prefix, /)\n"
803
"--\n"
804
"\n"
805
"Return a str with the given prefix string removed if present.\n"
806
"\n"
807
"If the string starts with the prefix string, return string[len(prefix):].\n"
808
"Otherwise, return a copy of the original string.");
809
810
#define UNICODE_REMOVEPREFIX_METHODDEF \
811
{"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
812
813
static PyObject *
814
unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
815
816
static PyObject *
817
unicode_removeprefix(PyObject *self, PyObject *arg)
818
{
819
PyObject *return_value = NULL;
820
PyObject *prefix;
821
822
if (!PyUnicode_Check(arg)) {
823
_PyArg_BadArgument("removeprefix", "argument", "str", arg);
824
goto exit;
825
}
826
prefix = arg;
827
return_value = unicode_removeprefix_impl(self, prefix);
828
829
exit:
830
return return_value;
831
}
832
833
PyDoc_STRVAR(unicode_removesuffix__doc__,
834
"removesuffix($self, suffix, /)\n"
835
"--\n"
836
"\n"
837
"Return a str with the given suffix string removed if present.\n"
838
"\n"
839
"If the string ends with the suffix string and that suffix is not empty,\n"
840
"return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
841
"string.");
842
843
#define UNICODE_REMOVESUFFIX_METHODDEF \
844
{"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
845
846
static PyObject *
847
unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
848
849
static PyObject *
850
unicode_removesuffix(PyObject *self, PyObject *arg)
851
{
852
PyObject *return_value = NULL;
853
PyObject *suffix;
854
855
if (!PyUnicode_Check(arg)) {
856
_PyArg_BadArgument("removesuffix", "argument", "str", arg);
857
goto exit;
858
}
859
suffix = arg;
860
return_value = unicode_removesuffix_impl(self, suffix);
861
862
exit:
863
return return_value;
864
}
865
866
PyDoc_STRVAR(unicode_rjust__doc__,
867
"rjust($self, width, fillchar=\' \', /)\n"
868
"--\n"
869
"\n"
870
"Return a right-justified string of length width.\n"
871
"\n"
872
"Padding is done using the specified fill character (default is a space).");
873
874
#define UNICODE_RJUST_METHODDEF \
875
{"rjust", _PyCFunction_CAST(unicode_rjust), METH_FASTCALL, unicode_rjust__doc__},
876
877
static PyObject *
878
unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
879
880
static PyObject *
881
unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
882
{
883
PyObject *return_value = NULL;
884
Py_ssize_t width;
885
Py_UCS4 fillchar = ' ';
886
887
if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
888
goto exit;
889
}
890
{
891
Py_ssize_t ival = -1;
892
PyObject *iobj = _PyNumber_Index(args[0]);
893
if (iobj != NULL) {
894
ival = PyLong_AsSsize_t(iobj);
895
Py_DECREF(iobj);
896
}
897
if (ival == -1 && PyErr_Occurred()) {
898
goto exit;
899
}
900
width = ival;
901
}
902
if (nargs < 2) {
903
goto skip_optional;
904
}
905
if (!convert_uc(args[1], &fillchar)) {
906
goto exit;
907
}
908
skip_optional:
909
return_value = unicode_rjust_impl(self, width, fillchar);
910
911
exit:
912
return return_value;
913
}
914
915
PyDoc_STRVAR(unicode_split__doc__,
916
"split($self, /, sep=None, maxsplit=-1)\n"
917
"--\n"
918
"\n"
919
"Return a list of the substrings in the string, using sep as the separator string.\n"
920
"\n"
921
" sep\n"
922
" The separator used to split the string.\n"
923
"\n"
924
" When set to None (the default value), will split on any whitespace\n"
925
" character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n"
926
" empty strings from the result.\n"
927
" maxsplit\n"
928
" Maximum number of splits (starting from the left).\n"
929
" -1 (the default value) means no limit.\n"
930
"\n"
931
"Note, str.split() is mainly useful for data that has been intentionally\n"
932
"delimited. With natural text that includes punctuation, consider using\n"
933
"the regular expression module.");
934
935
#define UNICODE_SPLIT_METHODDEF \
936
{"split", _PyCFunction_CAST(unicode_split), METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
937
938
static PyObject *
939
unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
940
941
static PyObject *
942
unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
943
{
944
PyObject *return_value = NULL;
945
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
946
947
#define NUM_KEYWORDS 2
948
static struct {
949
PyGC_Head _this_is_not_used;
950
PyObject_VAR_HEAD
951
PyObject *ob_item[NUM_KEYWORDS];
952
} _kwtuple = {
953
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
954
.ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
955
};
956
#undef NUM_KEYWORDS
957
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
958
959
#else // !Py_BUILD_CORE
960
# define KWTUPLE NULL
961
#endif // !Py_BUILD_CORE
962
963
static const char * const _keywords[] = {"sep", "maxsplit", NULL};
964
static _PyArg_Parser _parser = {
965
.keywords = _keywords,
966
.fname = "split",
967
.kwtuple = KWTUPLE,
968
};
969
#undef KWTUPLE
970
PyObject *argsbuf[2];
971
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
972
PyObject *sep = Py_None;
973
Py_ssize_t maxsplit = -1;
974
975
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
976
if (!args) {
977
goto exit;
978
}
979
if (!noptargs) {
980
goto skip_optional_pos;
981
}
982
if (args[0]) {
983
sep = args[0];
984
if (!--noptargs) {
985
goto skip_optional_pos;
986
}
987
}
988
{
989
Py_ssize_t ival = -1;
990
PyObject *iobj = _PyNumber_Index(args[1]);
991
if (iobj != NULL) {
992
ival = PyLong_AsSsize_t(iobj);
993
Py_DECREF(iobj);
994
}
995
if (ival == -1 && PyErr_Occurred()) {
996
goto exit;
997
}
998
maxsplit = ival;
999
}
1000
skip_optional_pos:
1001
return_value = unicode_split_impl(self, sep, maxsplit);
1002
1003
exit:
1004
return return_value;
1005
}
1006
1007
PyDoc_STRVAR(unicode_partition__doc__,
1008
"partition($self, sep, /)\n"
1009
"--\n"
1010
"\n"
1011
"Partition the string into three parts using the given separator.\n"
1012
"\n"
1013
"This will search for the separator in the string. If the separator is found,\n"
1014
"returns a 3-tuple containing the part before the separator, the separator\n"
1015
"itself, and the part after it.\n"
1016
"\n"
1017
"If the separator is not found, returns a 3-tuple containing the original string\n"
1018
"and two empty strings.");
1019
1020
#define UNICODE_PARTITION_METHODDEF \
1021
{"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
1022
1023
PyDoc_STRVAR(unicode_rpartition__doc__,
1024
"rpartition($self, sep, /)\n"
1025
"--\n"
1026
"\n"
1027
"Partition the string into three parts using the given separator.\n"
1028
"\n"
1029
"This will search for the separator in the string, starting at the end. If\n"
1030
"the separator is found, returns a 3-tuple containing the part before the\n"
1031
"separator, the separator itself, and the part after it.\n"
1032
"\n"
1033
"If the separator is not found, returns a 3-tuple containing two empty strings\n"
1034
"and the original string.");
1035
1036
#define UNICODE_RPARTITION_METHODDEF \
1037
{"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
1038
1039
PyDoc_STRVAR(unicode_rsplit__doc__,
1040
"rsplit($self, /, sep=None, maxsplit=-1)\n"
1041
"--\n"
1042
"\n"
1043
"Return a list of the substrings in the string, using sep as the separator string.\n"
1044
"\n"
1045
" sep\n"
1046
" The separator used to split the string.\n"
1047
"\n"
1048
" When set to None (the default value), will split on any whitespace\n"
1049
" character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n"
1050
" empty strings from the result.\n"
1051
" maxsplit\n"
1052
" Maximum number of splits (starting from the left).\n"
1053
" -1 (the default value) means no limit.\n"
1054
"\n"
1055
"Splitting starts at the end of the string and works to the front.");
1056
1057
#define UNICODE_RSPLIT_METHODDEF \
1058
{"rsplit", _PyCFunction_CAST(unicode_rsplit), METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
1059
1060
static PyObject *
1061
unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
1062
1063
static PyObject *
1064
unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1065
{
1066
PyObject *return_value = NULL;
1067
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1068
1069
#define NUM_KEYWORDS 2
1070
static struct {
1071
PyGC_Head _this_is_not_used;
1072
PyObject_VAR_HEAD
1073
PyObject *ob_item[NUM_KEYWORDS];
1074
} _kwtuple = {
1075
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1076
.ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1077
};
1078
#undef NUM_KEYWORDS
1079
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
1080
1081
#else // !Py_BUILD_CORE
1082
# define KWTUPLE NULL
1083
#endif // !Py_BUILD_CORE
1084
1085
static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1086
static _PyArg_Parser _parser = {
1087
.keywords = _keywords,
1088
.fname = "rsplit",
1089
.kwtuple = KWTUPLE,
1090
};
1091
#undef KWTUPLE
1092
PyObject *argsbuf[2];
1093
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1094
PyObject *sep = Py_None;
1095
Py_ssize_t maxsplit = -1;
1096
1097
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
1098
if (!args) {
1099
goto exit;
1100
}
1101
if (!noptargs) {
1102
goto skip_optional_pos;
1103
}
1104
if (args[0]) {
1105
sep = args[0];
1106
if (!--noptargs) {
1107
goto skip_optional_pos;
1108
}
1109
}
1110
{
1111
Py_ssize_t ival = -1;
1112
PyObject *iobj = _PyNumber_Index(args[1]);
1113
if (iobj != NULL) {
1114
ival = PyLong_AsSsize_t(iobj);
1115
Py_DECREF(iobj);
1116
}
1117
if (ival == -1 && PyErr_Occurred()) {
1118
goto exit;
1119
}
1120
maxsplit = ival;
1121
}
1122
skip_optional_pos:
1123
return_value = unicode_rsplit_impl(self, sep, maxsplit);
1124
1125
exit:
1126
return return_value;
1127
}
1128
1129
PyDoc_STRVAR(unicode_splitlines__doc__,
1130
"splitlines($self, /, keepends=False)\n"
1131
"--\n"
1132
"\n"
1133
"Return a list of the lines in the string, breaking at line boundaries.\n"
1134
"\n"
1135
"Line breaks are not included in the resulting list unless keepends is given and\n"
1136
"true.");
1137
1138
#define UNICODE_SPLITLINES_METHODDEF \
1139
{"splitlines", _PyCFunction_CAST(unicode_splitlines), METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
1140
1141
static PyObject *
1142
unicode_splitlines_impl(PyObject *self, int keepends);
1143
1144
static PyObject *
1145
unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1146
{
1147
PyObject *return_value = NULL;
1148
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1149
1150
#define NUM_KEYWORDS 1
1151
static struct {
1152
PyGC_Head _this_is_not_used;
1153
PyObject_VAR_HEAD
1154
PyObject *ob_item[NUM_KEYWORDS];
1155
} _kwtuple = {
1156
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1157
.ob_item = { &_Py_ID(keepends), },
1158
};
1159
#undef NUM_KEYWORDS
1160
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
1161
1162
#else // !Py_BUILD_CORE
1163
# define KWTUPLE NULL
1164
#endif // !Py_BUILD_CORE
1165
1166
static const char * const _keywords[] = {"keepends", NULL};
1167
static _PyArg_Parser _parser = {
1168
.keywords = _keywords,
1169
.fname = "splitlines",
1170
.kwtuple = KWTUPLE,
1171
};
1172
#undef KWTUPLE
1173
PyObject *argsbuf[1];
1174
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1175
int keepends = 0;
1176
1177
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1178
if (!args) {
1179
goto exit;
1180
}
1181
if (!noptargs) {
1182
goto skip_optional_pos;
1183
}
1184
keepends = PyObject_IsTrue(args[0]);
1185
if (keepends < 0) {
1186
goto exit;
1187
}
1188
skip_optional_pos:
1189
return_value = unicode_splitlines_impl(self, keepends);
1190
1191
exit:
1192
return return_value;
1193
}
1194
1195
PyDoc_STRVAR(unicode_swapcase__doc__,
1196
"swapcase($self, /)\n"
1197
"--\n"
1198
"\n"
1199
"Convert uppercase characters to lowercase and lowercase characters to uppercase.");
1200
1201
#define UNICODE_SWAPCASE_METHODDEF \
1202
{"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
1203
1204
static PyObject *
1205
unicode_swapcase_impl(PyObject *self);
1206
1207
static PyObject *
1208
unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
1209
{
1210
return unicode_swapcase_impl(self);
1211
}
1212
1213
PyDoc_STRVAR(unicode_maketrans__doc__,
1214
"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
1215
"--\n"
1216
"\n"
1217
"Return a translation table usable for str.translate().\n"
1218
"\n"
1219
"If there is only one argument, it must be a dictionary mapping Unicode\n"
1220
"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
1221
"Character keys will be then converted to ordinals.\n"
1222
"If there are two arguments, they must be strings of equal length, and\n"
1223
"in the resulting dictionary, each character in x will be mapped to the\n"
1224
"character at the same position in y. If there is a third argument, it\n"
1225
"must be a string, whose characters will be mapped to None in the result.");
1226
1227
#define UNICODE_MAKETRANS_METHODDEF \
1228
{"maketrans", _PyCFunction_CAST(unicode_maketrans), METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
1229
1230
static PyObject *
1231
unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
1232
1233
static PyObject *
1234
unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
1235
{
1236
PyObject *return_value = NULL;
1237
PyObject *x;
1238
PyObject *y = NULL;
1239
PyObject *z = NULL;
1240
1241
if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
1242
goto exit;
1243
}
1244
x = args[0];
1245
if (nargs < 2) {
1246
goto skip_optional;
1247
}
1248
if (!PyUnicode_Check(args[1])) {
1249
_PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
1250
goto exit;
1251
}
1252
y = args[1];
1253
if (nargs < 3) {
1254
goto skip_optional;
1255
}
1256
if (!PyUnicode_Check(args[2])) {
1257
_PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
1258
goto exit;
1259
}
1260
z = args[2];
1261
skip_optional:
1262
return_value = unicode_maketrans_impl(x, y, z);
1263
1264
exit:
1265
return return_value;
1266
}
1267
1268
PyDoc_STRVAR(unicode_translate__doc__,
1269
"translate($self, table, /)\n"
1270
"--\n"
1271
"\n"
1272
"Replace each character in the string using the given translation table.\n"
1273
"\n"
1274
" table\n"
1275
" Translation table, which must be a mapping of Unicode ordinals to\n"
1276
" Unicode ordinals, strings, or None.\n"
1277
"\n"
1278
"The table must implement lookup/indexing via __getitem__, for instance a\n"
1279
"dictionary or list. If this operation raises LookupError, the character is\n"
1280
"left untouched. Characters mapped to None are deleted.");
1281
1282
#define UNICODE_TRANSLATE_METHODDEF \
1283
{"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
1284
1285
PyDoc_STRVAR(unicode_upper__doc__,
1286
"upper($self, /)\n"
1287
"--\n"
1288
"\n"
1289
"Return a copy of the string converted to uppercase.");
1290
1291
#define UNICODE_UPPER_METHODDEF \
1292
{"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
1293
1294
static PyObject *
1295
unicode_upper_impl(PyObject *self);
1296
1297
static PyObject *
1298
unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
1299
{
1300
return unicode_upper_impl(self);
1301
}
1302
1303
PyDoc_STRVAR(unicode_zfill__doc__,
1304
"zfill($self, width, /)\n"
1305
"--\n"
1306
"\n"
1307
"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
1308
"\n"
1309
"The string is never truncated.");
1310
1311
#define UNICODE_ZFILL_METHODDEF \
1312
{"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
1313
1314
static PyObject *
1315
unicode_zfill_impl(PyObject *self, Py_ssize_t width);
1316
1317
static PyObject *
1318
unicode_zfill(PyObject *self, PyObject *arg)
1319
{
1320
PyObject *return_value = NULL;
1321
Py_ssize_t width;
1322
1323
{
1324
Py_ssize_t ival = -1;
1325
PyObject *iobj = _PyNumber_Index(arg);
1326
if (iobj != NULL) {
1327
ival = PyLong_AsSsize_t(iobj);
1328
Py_DECREF(iobj);
1329
}
1330
if (ival == -1 && PyErr_Occurred()) {
1331
goto exit;
1332
}
1333
width = ival;
1334
}
1335
return_value = unicode_zfill_impl(self, width);
1336
1337
exit:
1338
return return_value;
1339
}
1340
1341
PyDoc_STRVAR(unicode___format____doc__,
1342
"__format__($self, format_spec, /)\n"
1343
"--\n"
1344
"\n"
1345
"Return a formatted version of the string as described by format_spec.");
1346
1347
#define UNICODE___FORMAT___METHODDEF \
1348
{"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
1349
1350
static PyObject *
1351
unicode___format___impl(PyObject *self, PyObject *format_spec);
1352
1353
static PyObject *
1354
unicode___format__(PyObject *self, PyObject *arg)
1355
{
1356
PyObject *return_value = NULL;
1357
PyObject *format_spec;
1358
1359
if (!PyUnicode_Check(arg)) {
1360
_PyArg_BadArgument("__format__", "argument", "str", arg);
1361
goto exit;
1362
}
1363
format_spec = arg;
1364
return_value = unicode___format___impl(self, format_spec);
1365
1366
exit:
1367
return return_value;
1368
}
1369
1370
PyDoc_STRVAR(unicode_sizeof__doc__,
1371
"__sizeof__($self, /)\n"
1372
"--\n"
1373
"\n"
1374
"Return the size of the string in memory, in bytes.");
1375
1376
#define UNICODE_SIZEOF_METHODDEF \
1377
{"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
1378
1379
static PyObject *
1380
unicode_sizeof_impl(PyObject *self);
1381
1382
static PyObject *
1383
unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1384
{
1385
return unicode_sizeof_impl(self);
1386
}
1387
1388
static PyObject *
1389
unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1390
const char *errors);
1391
1392
static PyObject *
1393
unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1394
{
1395
PyObject *return_value = NULL;
1396
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1397
1398
#define NUM_KEYWORDS 3
1399
static struct {
1400
PyGC_Head _this_is_not_used;
1401
PyObject_VAR_HEAD
1402
PyObject *ob_item[NUM_KEYWORDS];
1403
} _kwtuple = {
1404
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1405
.ob_item = { &_Py_ID(object), &_Py_ID(encoding), &_Py_ID(errors), },
1406
};
1407
#undef NUM_KEYWORDS
1408
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
1409
1410
#else // !Py_BUILD_CORE
1411
# define KWTUPLE NULL
1412
#endif // !Py_BUILD_CORE
1413
1414
static const char * const _keywords[] = {"object", "encoding", "errors", NULL};
1415
static _PyArg_Parser _parser = {
1416
.keywords = _keywords,
1417
.fname = "str",
1418
.kwtuple = KWTUPLE,
1419
};
1420
#undef KWTUPLE
1421
PyObject *argsbuf[3];
1422
PyObject * const *fastargs;
1423
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1424
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1425
PyObject *x = NULL;
1426
const char *encoding = NULL;
1427
const char *errors = NULL;
1428
1429
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
1430
if (!fastargs) {
1431
goto exit;
1432
}
1433
if (!noptargs) {
1434
goto skip_optional_pos;
1435
}
1436
if (fastargs[0]) {
1437
x = fastargs[0];
1438
if (!--noptargs) {
1439
goto skip_optional_pos;
1440
}
1441
}
1442
if (fastargs[1]) {
1443
if (!PyUnicode_Check(fastargs[1])) {
1444
_PyArg_BadArgument("str", "argument 'encoding'", "str", fastargs[1]);
1445
goto exit;
1446
}
1447
Py_ssize_t encoding_length;
1448
encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1449
if (encoding == NULL) {
1450
goto exit;
1451
}
1452
if (strlen(encoding) != (size_t)encoding_length) {
1453
PyErr_SetString(PyExc_ValueError, "embedded null character");
1454
goto exit;
1455
}
1456
if (!--noptargs) {
1457
goto skip_optional_pos;
1458
}
1459
}
1460
if (!PyUnicode_Check(fastargs[2])) {
1461
_PyArg_BadArgument("str", "argument 'errors'", "str", fastargs[2]);
1462
goto exit;
1463
}
1464
Py_ssize_t errors_length;
1465
errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1466
if (errors == NULL) {
1467
goto exit;
1468
}
1469
if (strlen(errors) != (size_t)errors_length) {
1470
PyErr_SetString(PyExc_ValueError, "embedded null character");
1471
goto exit;
1472
}
1473
skip_optional_pos:
1474
return_value = unicode_new_impl(type, x, encoding, errors);
1475
1476
exit:
1477
return return_value;
1478
}
1479
/*[clinic end generated code: output=0a71c4aeffdf0bc5 input=a9049054013a1b77]*/
1480
1481