Skip to content

Commit e582385

Browse files
committed
Optimize bytes_fromformat()
1 parent 68d2fe7 commit e582385

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Lib/test/test_capi/test_bytes.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,25 @@ def test_join(self):
291291

292292

293293
class PyBytesWriterTest(unittest.TestCase):
294-
def create_writer(self, alloc):
295-
return _testcapi.PyBytesWriter(alloc)
294+
def create_writer(self, alloc, string=b''):
295+
return _testcapi.PyBytesWriter(alloc, string)
296296

297297
def test_empty(self):
298298
# Test PyBytesWriter_Create()
299299
writer = self.create_writer(0)
300300
self.assertEqual(writer.get_remaining(), 0)
301301
self.assertEqual(writer.finish(), b'')
302302

303+
def test_abc(self):
304+
# Test PyBytesWriter_Create()
305+
writer = self.create_writer(3, b'abc')
306+
self.assertEqual(writer.get_remaining(), 0)
307+
self.assertEqual(writer.finish(), b'abc')
308+
309+
writer = self.create_writer(10, b'abc')
310+
self.assertEqual(writer.get_remaining(), 7)
311+
self.assertEqual(writer.finish(), b'abc')
312+
303313
def test_write_bytes(self):
304314
# Test PyBytesWriter_WriteBytes()
305315

@@ -352,6 +362,7 @@ def test_format(self):
352362
# Test PyBytesWriter_Format()
353363
writer = self.create_writer(0)
354364
writer.format_i(123456)
365+
self.assertEqual(writer.get_remaining(), 0)
355366
self.assertEqual(writer.finish(), b'123456')
356367

357368

Modules/_testcapi/bytes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,20 @@ writer_init(PyObject *self_raw, PyObject *args, PyObject *kwargs)
8888
}
8989

9090
Py_ssize_t alloc;
91-
if (!PyArg_ParseTuple(args, "n", &alloc)) {
91+
char *str;
92+
Py_ssize_t str_size;
93+
if (!PyArg_ParseTuple(args, "ny#", &alloc, &str, &str_size)) {
9294
return -1;
9395
}
9496

9597
self->buf = PyBytesWriter_Create(&self->writer, alloc);
9698
if (self->buf == NULL) {
9799
return -1;
98100
}
101+
102+
memcpy(self->buf, str, str_size);
103+
self->buf += str_size;
104+
99105
return 0;
100106
}
101107

Objects/bytesobject.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,15 @@ bytes_fromformat(PyBytesWriter *writer, char *s,
215215

216216
#define WRITE_BYTES(str) \
217217
do { \
218-
s = PyBytesWriter_WriteBytes(writer, s, (str), strlen(str)); \
219-
if (s == NULL) { \
220-
goto error; \
218+
Py_ssize_t len = strlen(str); \
219+
if (len > 2) { \
220+
s = PyBytesWriter_Extend(writer, s, len - 2); \
221+
if (s == NULL) { \
222+
goto error; \
223+
} \
221224
} \
225+
memcpy(s, (str), len); \
226+
s += len; \
222227
} while (0)
223228

224229
for (f = format; *f; f++) {
@@ -355,7 +360,10 @@ bytes_fromformat(PyBytesWriter *writer, char *s,
355360

356361
default:
357362
/* invalid format string: copy unformatted string and exit */
358-
WRITE_BYTES(p);
363+
s = PyBytesWriter_WriteBytes(writer, s, p, strlen(p));
364+
if (s == NULL) {
365+
goto error;
366+
}
359367
goto done;
360368
}
361369
}

0 commit comments

Comments
 (0)