Skip to content

Commit c929df3

Browse files
authored
bpo-20180: complete AC conversion of Objects/stringlib/transmogrify.h (GH-8039)
* converted bytes methods: expandtabs, ljust, rjust, center, zfill * updated char_convertor to properly set the C default value
1 parent 7943c5e commit c929df3

File tree

6 files changed

+265
-78
lines changed

6 files changed

+265
-78
lines changed

Objects/bytearrayobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,16 +2138,15 @@ bytearray_methods[] = {
21382138
BYTEARRAY_APPEND_METHODDEF
21392139
{"capitalize", stringlib_capitalize, METH_NOARGS,
21402140
_Py_capitalize__doc__},
2141-
{"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
2141+
STRINGLIB_CENTER_METHODDEF
21422142
BYTEARRAY_CLEAR_METHODDEF
21432143
BYTEARRAY_COPY_METHODDEF
21442144
{"count", (PyCFunction)bytearray_count, METH_VARARGS,
21452145
_Py_count__doc__},
21462146
BYTEARRAY_DECODE_METHODDEF
21472147
{"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
21482148
_Py_endswith__doc__},
2149-
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
2150-
_Py_expandtabs__doc__},
2149+
STRINGLIB_EXPANDTABS_METHODDEF
21512150
BYTEARRAY_EXTEND_METHODDEF
21522151
{"find", (PyCFunction)bytearray_find, METH_VARARGS,
21532152
_Py_find__doc__},
@@ -2172,7 +2171,7 @@ bytearray_methods[] = {
21722171
{"isupper", stringlib_isupper, METH_NOARGS,
21732172
_Py_isupper__doc__},
21742173
BYTEARRAY_JOIN_METHODDEF
2175-
{"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
2174+
STRINGLIB_LJUST_METHODDEF
21762175
{"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
21772176
BYTEARRAY_LSTRIP_METHODDEF
21782177
BYTEARRAY_MAKETRANS_METHODDEF
@@ -2183,7 +2182,7 @@ bytearray_methods[] = {
21832182
BYTEARRAY_REVERSE_METHODDEF
21842183
{"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
21852184
{"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
2186-
{"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
2185+
STRINGLIB_RJUST_METHODDEF
21872186
BYTEARRAY_RPARTITION_METHODDEF
21882187
BYTEARRAY_RSPLIT_METHODDEF
21892188
BYTEARRAY_RSTRIP_METHODDEF
@@ -2197,7 +2196,7 @@ bytearray_methods[] = {
21972196
{"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
21982197
BYTEARRAY_TRANSLATE_METHODDEF
21992198
{"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2200-
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
2199+
STRINGLIB_ZFILL_METHODDEF
22012200
{NULL}
22022201
};
22032202

Objects/bytes_methods.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -845,33 +845,3 @@ _Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args)
845845
{
846846
return _Py_bytes_tailmatch(str, len, "endswith", args, +1);
847847
}
848-
849-
PyDoc_STRVAR_shared(_Py_expandtabs__doc__,
850-
"B.expandtabs(tabsize=8) -> copy of B\n\
851-
\n\
852-
Return a copy of B where all tab characters are expanded using spaces.\n\
853-
If tabsize is not given, a tab size of 8 characters is assumed.");
854-
855-
PyDoc_STRVAR_shared(_Py_ljust__doc__,
856-
"B.ljust(width[, fillchar]) -> copy of B\n"
857-
"\n"
858-
"Return B left justified in a string of length width. Padding is\n"
859-
"done using the specified fill character (default is a space).");
860-
861-
PyDoc_STRVAR_shared(_Py_rjust__doc__,
862-
"B.rjust(width[, fillchar]) -> copy of B\n"
863-
"\n"
864-
"Return B right justified in a string of length width. Padding is\n"
865-
"done using the specified fill character (default is a space)");
866-
867-
PyDoc_STRVAR_shared(_Py_center__doc__,
868-
"B.center(width[, fillchar]) -> copy of B\n"
869-
"\n"
870-
"Return B centered in a string of length width. Padding is\n"
871-
"done using the specified fill character (default is a space).");
872-
873-
PyDoc_STRVAR_shared(_Py_zfill__doc__,
874-
"B.zfill(width) -> copy of B\n"
875-
"\n"
876-
"Pad a numeric string B with zeros on the left, to fill a field\n"
877-
"of the specified width. B is never truncated.");

Objects/bytesobject.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,15 +2441,13 @@ bytes_methods[] = {
24412441
{"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
24422442
{"capitalize", stringlib_capitalize, METH_NOARGS,
24432443
_Py_capitalize__doc__},
2444-
{"center", (PyCFunction)stringlib_center, METH_VARARGS,
2445-
_Py_center__doc__},
2444+
STRINGLIB_CENTER_METHODDEF
24462445
{"count", (PyCFunction)bytes_count, METH_VARARGS,
24472446
_Py_count__doc__},
24482447
BYTES_DECODE_METHODDEF
24492448
{"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
24502449
_Py_endswith__doc__},
2451-
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
2452-
_Py_expandtabs__doc__},
2450+
STRINGLIB_EXPANDTABS_METHODDEF
24532451
{"find", (PyCFunction)bytes_find, METH_VARARGS,
24542452
_Py_find__doc__},
24552453
BYTES_FROMHEX_METHODDEF
@@ -2472,15 +2470,15 @@ bytes_methods[] = {
24722470
{"isupper", stringlib_isupper, METH_NOARGS,
24732471
_Py_isupper__doc__},
24742472
BYTES_JOIN_METHODDEF
2475-
{"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
2473+
STRINGLIB_LJUST_METHODDEF
24762474
{"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
24772475
BYTES_LSTRIP_METHODDEF
24782476
BYTES_MAKETRANS_METHODDEF
24792477
BYTES_PARTITION_METHODDEF
24802478
BYTES_REPLACE_METHODDEF
24812479
{"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, _Py_rfind__doc__},
24822480
{"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, _Py_rindex__doc__},
2483-
{"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
2481+
STRINGLIB_RJUST_METHODDEF
24842482
BYTES_RPARTITION_METHODDEF
24852483
BYTES_RSPLIT_METHODDEF
24862484
BYTES_RSTRIP_METHODDEF
@@ -2494,7 +2492,7 @@ bytes_methods[] = {
24942492
{"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
24952493
BYTES_TRANSLATE_METHODDEF
24962494
{"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2497-
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
2495+
STRINGLIB_ZFILL_METHODDEF
24982496
{NULL, NULL} /* sentinel */
24992497
};
25002498

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(stringlib_expandtabs__doc__,
6+
"expandtabs($self, /, tabsize=8)\n"
7+
"--\n"
8+
"\n"
9+
"Return a copy where all tab characters are expanded using spaces.\n"
10+
"\n"
11+
"If tabsize is not given, a tab size of 8 characters is assumed.");
12+
13+
#define STRINGLIB_EXPANDTABS_METHODDEF \
14+
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_FASTCALL|METH_KEYWORDS, stringlib_expandtabs__doc__},
15+
16+
static PyObject *
17+
stringlib_expandtabs_impl(PyObject *self, int tabsize);
18+
19+
static PyObject *
20+
stringlib_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
21+
{
22+
PyObject *return_value = NULL;
23+
static const char * const _keywords[] = {"tabsize", NULL};
24+
static _PyArg_Parser _parser = {"|i:expandtabs", _keywords, 0};
25+
int tabsize = 8;
26+
27+
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
28+
&tabsize)) {
29+
goto exit;
30+
}
31+
return_value = stringlib_expandtabs_impl(self, tabsize);
32+
33+
exit:
34+
return return_value;
35+
}
36+
37+
PyDoc_STRVAR(stringlib_ljust__doc__,
38+
"ljust($self, width, fillchar=b\' \', /)\n"
39+
"--\n"
40+
"\n"
41+
"Return a left-justified string of length width.\n"
42+
"\n"
43+
"Padding is done using the specified fill character.");
44+
45+
#define STRINGLIB_LJUST_METHODDEF \
46+
{"ljust", (PyCFunction)stringlib_ljust, METH_FASTCALL, stringlib_ljust__doc__},
47+
48+
static PyObject *
49+
stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
50+
51+
static PyObject *
52+
stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
53+
{
54+
PyObject *return_value = NULL;
55+
Py_ssize_t width;
56+
char fillchar = ' ';
57+
58+
if (!_PyArg_ParseStack(args, nargs, "n|c:ljust",
59+
&width, &fillchar)) {
60+
goto exit;
61+
}
62+
return_value = stringlib_ljust_impl(self, width, fillchar);
63+
64+
exit:
65+
return return_value;
66+
}
67+
68+
PyDoc_STRVAR(stringlib_rjust__doc__,
69+
"rjust($self, width, fillchar=b\' \', /)\n"
70+
"--\n"
71+
"\n"
72+
"Return a right-justified string of length width.\n"
73+
"\n"
74+
"Padding is done using the specified fill character.");
75+
76+
#define STRINGLIB_RJUST_METHODDEF \
77+
{"rjust", (PyCFunction)stringlib_rjust, METH_FASTCALL, stringlib_rjust__doc__},
78+
79+
static PyObject *
80+
stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
81+
82+
static PyObject *
83+
stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
84+
{
85+
PyObject *return_value = NULL;
86+
Py_ssize_t width;
87+
char fillchar = ' ';
88+
89+
if (!_PyArg_ParseStack(args, nargs, "n|c:rjust",
90+
&width, &fillchar)) {
91+
goto exit;
92+
}
93+
return_value = stringlib_rjust_impl(self, width, fillchar);
94+
95+
exit:
96+
return return_value;
97+
}
98+
99+
PyDoc_STRVAR(stringlib_center__doc__,
100+
"center($self, width, fillchar=b\' \', /)\n"
101+
"--\n"
102+
"\n"
103+
"Return a centered string of length width.\n"
104+
"\n"
105+
"Padding is done using the specified fill character.");
106+
107+
#define STRINGLIB_CENTER_METHODDEF \
108+
{"center", (PyCFunction)stringlib_center, METH_FASTCALL, stringlib_center__doc__},
109+
110+
static PyObject *
111+
stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
112+
113+
static PyObject *
114+
stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
115+
{
116+
PyObject *return_value = NULL;
117+
Py_ssize_t width;
118+
char fillchar = ' ';
119+
120+
if (!_PyArg_ParseStack(args, nargs, "n|c:center",
121+
&width, &fillchar)) {
122+
goto exit;
123+
}
124+
return_value = stringlib_center_impl(self, width, fillchar);
125+
126+
exit:
127+
return return_value;
128+
}
129+
130+
PyDoc_STRVAR(stringlib_zfill__doc__,
131+
"zfill($self, width, /)\n"
132+
"--\n"
133+
"\n"
134+
"Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
135+
"\n"
136+
"The original string is never truncated.");
137+
138+
#define STRINGLIB_ZFILL_METHODDEF \
139+
{"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__},
140+
141+
static PyObject *
142+
stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
143+
144+
static PyObject *
145+
stringlib_zfill(PyObject *self, PyObject *arg)
146+
{
147+
PyObject *return_value = NULL;
148+
Py_ssize_t width;
149+
150+
if (!PyArg_Parse(arg, "n:zfill", &width)) {
151+
goto exit;
152+
}
153+
return_value = stringlib_zfill_impl(self, width);
154+
155+
exit:
156+
return return_value;
157+
}
158+
/*[clinic end generated code: output=336620159a1fc70d input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)