Skip to content

Commit 3cae656

Browse files
authored
[librt.strings] Make various methods only accept positional args (#20589)
This makes non-native calls a little faster.
1 parent d0129f6 commit 3cae656

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

mypy/typeshed/stubs/librt/librt/strings.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from mypy_extensions import i64, i32, u8
44

55
@final
66
class BytesWriter:
7-
def append(self, /, x: int) -> None: ...
7+
def append(self, x: int, /) -> None: ...
88
def write(self, b: bytes | bytearray, /) -> None: ...
99
def getvalue(self) -> bytes: ...
1010
def truncate(self, size: i64, /) -> None: ...

mypyc/lib-rt/librt_strings.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ static PySequenceMethods BytesWriter_as_sequence = {
199199
.sq_ass_item = (ssizeobjargproc)BytesWriter_ass_item,
200200
};
201201

202-
static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames);
203-
static PyObject* BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames);
202+
static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs);
203+
static PyObject* BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs);
204204
static PyObject* BytesWriter_truncate(PyObject *self, PyObject *const *args, size_t nargs);
205205

206206
static PyMethodDef BytesWriter_methods[] = {
207-
{"append", (PyCFunction) BytesWriter_append, METH_FASTCALL | METH_KEYWORDS,
207+
{"append", (PyCFunction) BytesWriter_append, METH_FASTCALL,
208208
PyDoc_STR("Append a single byte to the buffer")
209209
},
210-
{"write", (PyCFunction) BytesWriter_write, METH_FASTCALL | METH_KEYWORDS,
210+
{"write", (PyCFunction) BytesWriter_write, METH_FASTCALL,
211211
PyDoc_STR("Append bytes to the buffer")
212212
},
213213
{"getvalue", (PyCFunction) BytesWriter_getvalue, METH_NOARGS,
@@ -265,16 +265,16 @@ BytesWriter_write_internal(BytesWriterObject *self, PyObject *value) {
265265
}
266266

267267
static PyObject*
268-
BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) {
269-
static const char * const kwlist[] = {"value", 0};
270-
static CPyArg_Parser parser = {"O:write", kwlist, 0};
271-
PyObject *value;
272-
if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) {
268+
BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs) {
269+
if (unlikely(nargs != 1)) {
270+
PyErr_Format(PyExc_TypeError,
271+
"write() takes exactly 1 argument (%zu given)", nargs);
273272
return NULL;
274273
}
275274
if (!check_bytes_writer(self)) {
276275
return NULL;
277276
}
277+
PyObject *value = args[0];
278278
if (unlikely(!PyBytes_Check(value) && !PyByteArray_Check(value))) {
279279
PyErr_SetString(PyExc_TypeError, "value must be a bytes or bytearray object");
280280
return NULL;
@@ -295,16 +295,16 @@ BytesWriter_append_internal(BytesWriterObject *self, uint8_t value) {
295295
}
296296

297297
static PyObject*
298-
BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) {
299-
static const char * const kwlist[] = {"value", 0};
300-
static CPyArg_Parser parser = {"O:append", kwlist, 0};
301-
PyObject *value;
302-
if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) {
298+
BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs) {
299+
if (unlikely(nargs != 1)) {
300+
PyErr_Format(PyExc_TypeError,
301+
"append() takes exactly 1 argument (%zu given)", nargs);
303302
return NULL;
304303
}
305304
if (!check_bytes_writer(self)) {
306305
return NULL;
307306
}
307+
PyObject *value = args[0];
308308
uint8_t unboxed = CPyLong_AsUInt8(value);
309309
if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
310310
CPy_TypeError("u8", value);
@@ -574,14 +574,14 @@ static PySequenceMethods StringWriter_as_sequence = {
574574
.sq_item = (ssizeargfunc)StringWriter_item,
575575
};
576576

577-
static PyObject* StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames);
578-
static PyObject* StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames);
577+
static PyObject* StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs);
578+
static PyObject* StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs);
579579

580580
static PyMethodDef StringWriter_methods[] = {
581-
{"append", (PyCFunction) StringWriter_append, METH_FASTCALL | METH_KEYWORDS,
581+
{"append", (PyCFunction) StringWriter_append, METH_FASTCALL,
582582
PyDoc_STR("Append a single character (as int codepoint) to the buffer")
583583
},
584-
{"write", (PyCFunction) StringWriter_write, METH_FASTCALL | METH_KEYWORDS,
584+
{"write", (PyCFunction) StringWriter_write, METH_FASTCALL,
585585
PyDoc_STR("Append a string to the buffer")
586586
},
587587
{"getvalue", (PyCFunction) StringWriter_getvalue, METH_NOARGS,
@@ -661,16 +661,16 @@ StringWriter_write_internal(StringWriterObject *self, PyObject *value) {
661661
}
662662

663663
static PyObject*
664-
StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) {
665-
static const char * const kwlist[] = {"value", 0};
666-
static CPyArg_Parser parser = {"O:write", kwlist, 0};
667-
PyObject *value;
668-
if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) {
664+
StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs) {
665+
if (unlikely(nargs != 1)) {
666+
PyErr_Format(PyExc_TypeError,
667+
"write() takes exactly 1 argument (%zu given)", nargs);
669668
return NULL;
670669
}
671670
if (!check_string_writer(self)) {
672671
return NULL;
673672
}
673+
PyObject *value = args[0];
674674
if (unlikely(!PyUnicode_Check(value))) {
675675
PyErr_SetString(PyExc_TypeError, "value must be a str object");
676676
return NULL;
@@ -795,16 +795,16 @@ StringWriter_append_internal(StringWriterObject *self, int32_t value) {
795795
}
796796

797797
static PyObject*
798-
StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) {
799-
static const char * const kwlist[] = {"value", 0};
800-
static CPyArg_Parser parser = {"O:append", kwlist, 0};
801-
PyObject *value;
802-
if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) {
798+
StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs) {
799+
if (unlikely(nargs != 1)) {
800+
PyErr_Format(PyExc_TypeError,
801+
"append() takes exactly 1 argument (%zu given)", nargs);
803802
return NULL;
804803
}
805804
if (!check_string_writer(self)) {
806805
return NULL;
807806
}
807+
PyObject *value = args[0];
808808
int32_t unboxed = CPyLong_AsInt32(value);
809809
if (unlikely(unboxed == CPY_LL_INT_ERROR && PyErr_Occurred())) {
810810
CPy_TypeError("i32", value);

0 commit comments

Comments
 (0)