| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -189,7 +189,8 @@ _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) | |||
| 189 | 189 | return 0; | |
| 190 | 190 | } | |
| 191 | 191 | ||
| 192 | - extern PyObject* _PyUnicodeWriter_FinishWithSize( | ||
| 192 | + // Export for '_testcapi' shared extension | ||
| 193 | + PyAPI_FUNC(PyObject*) _PyUnicodeWriter_FinishWithSize( | ||
| 193 | 194 | _PyUnicodeWriter *writer, | |
| 194 | 195 | Py_ssize_t size); | |
| 195 | 196 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2322,6 +2322,19 @@ def test_readonly_optim_large_int(self): | |||
| 2322 | 2322 | self.assertEqual(writer.get_buffer(), expected) | |
| 2323 | 2323 | self.assertEqual(writer.finish(), large_int_str) | |
| 2324 | 2324 | ||
| 2325 | + def test_finish_with_size(self): | ||
| 2326 | + # Test _PyUnicodeWriter_FinishWithSize(). Truncate text requires | ||
| 2327 | + # to change the buffer kind. | ||
| 2328 | + text = 'a\xff\u20ac\U0010ffff' | ||
| 2329 | + expected = (0x10ffff, False) # do not test the buffer size | ||
| 2330 | + for size in range(len(text) + 1): | ||
| 2331 | + writer = self.create_writer(0) | ||
| 2332 | + writer.write_utf8(text.encode(), -1) | ||
| 2333 | + self.assertEqual(writer.get_buffer()[1:], expected) | ||
| 2334 | + self.assertEqual(writer.finish_with_size(size), text[:size]) | ||
| 2335 | + | ||
| 2336 | + # CRASHES writer.finish_with_size(len(text) + 1) | ||
| 2337 | + | ||
| 2325 | 2338 | ||
| 2326 | 2339 | # Test PyUnicodeWriter_Format() | |
| 2327 | 2340 | @unittest.skipIf(ctypes is None, 'need ctypes') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -795,6 +795,31 @@ writer_finish(PyObject *self_raw, PyObject *Py_UNUSED(args)) | |||
| 795 | 795 | } | |
| 796 | 796 | ||
| 797 | 797 | ||
| 798 | + static PyObject* | ||
| 799 | + writer_finish_with_size(PyObject *self_raw, PyObject *args) | ||
| 800 | + { | ||
| 801 | + PyAPI_FUNC(PyObject*) _PyUnicodeWriter_FinishWithSize( | ||
| 802 | + _PyUnicodeWriter *writer, | ||
| 803 | + Py_ssize_t size); | ||
| 804 | + | ||
| 805 | + WriterObject *self = (WriterObject *)self_raw; | ||
| 806 | + if (writer_check(self) < 0) { | ||
| 807 | + return NULL; | ||
| 808 | + } | ||
| 809 | + | ||
| 810 | + Py_ssize_t size; | ||
| 811 | + if (!PyArg_ParseTuple(args, "n", &size)) { | ||
| 812 | + return NULL; | ||
| 813 | + } | ||
| 814 | + | ||
| 815 | + _PyUnicodeWriter *writer = (_PyUnicodeWriter*)self->writer; | ||
| 816 | + PyObject *str = _PyUnicodeWriter_FinishWithSize(writer, size); | ||
| 817 | + PyUnicodeWriter_Discard(self->writer); | ||
| 818 | + self->writer = NULL; | ||
| 819 | + return str; | ||
| 820 | + } | ||
| 821 | + | ||
| 822 | + | ||
| 798 | 823 | static PyMethodDef writer_methods[] = { | |
| 799 | 824 | {"write_char", _PyCFunction_CAST(writer_write_char), METH_VARARGS}, | |
| 800 | 825 | {"write_utf8", _PyCFunction_CAST(writer_write_utf8), METH_VARARGS}, | |
@@ -809,6 +834,7 @@ static PyMethodDef writer_methods[] = { | |||
| 809 | 834 | {"get_pointer", _PyCFunction_CAST(writer_get_pointer), METH_VARARGS}, | |
| 810 | 835 | {"get_buffer", _PyCFunction_CAST(writer_get_buffer), METH_VARARGS}, | |
| 811 | 836 | {"finish", _PyCFunction_CAST(writer_finish), METH_NOARGS}, | |
| 837 | + {"finish_with_size", _PyCFunction_CAST(writer_finish_with_size), METH_VARARGS}, | ||
| 812 | 838 | {NULL, NULL} /* sentinel */ | |
| 813 | 839 | }; | |
| 814 | 840 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments