Skip to content

Commit c9712ee

Browse files
committed
Remove string size limit; Fix Windows
1 parent 705ffe4 commit c9712ee

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

mypyc/lib-rt/native_internal.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
#define START_SIZE 512
88

9-
#define STR_LEN_TYPE unsigned int
10-
#define MAX_STR_SIZE (1L << sizeof(STR_LEN_TYPE) * 8)
11-
129
typedef struct {
1310
PyObject_HEAD
1411
Py_ssize_t pos;
@@ -119,7 +116,7 @@ static PyMethodDef Buffer_methods[] = {
119116
};
120117

121118
static PyTypeObject BufferType = {
122-
.ob_base = PyVarObject_HEAD_INIT(&PyType_Type, 0)
119+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
123120
.tp_name = "Buffer",
124121
.tp_doc = PyDoc_STR("Mypy cache buffer objects"),
125122
.tp_basicsize = sizeof(BufferObject),
@@ -231,12 +228,12 @@ read_str_internal(PyObject *data) {
231228
if (_check_buffer(data) == 2)
232229
return NULL;
233230

234-
if (_check_read((BufferObject *)data, sizeof(STR_LEN_TYPE)) == 2)
231+
if (_check_read((BufferObject *)data, sizeof(Py_ssize_t)) == 2)
235232
return NULL;
236233
char *buf = ((BufferObject *)data)->buf;
237234
// Read string length.
238-
STR_LEN_TYPE size = *(STR_LEN_TYPE *)(buf + ((BufferObject *)data)->pos);
239-
((BufferObject *)data)->pos += sizeof(STR_LEN_TYPE);
235+
Py_ssize_t size = *(Py_ssize_t *)(buf + ((BufferObject *)data)->pos);
236+
((BufferObject *)data)->pos += sizeof(Py_ssize_t);
240237
if (_check_read((BufferObject *)data, size) == 2)
241238
return NULL;
242239
// Read string content.
@@ -267,24 +264,14 @@ write_str_internal(PyObject *data, PyObject *value) {
267264
const char *chunk = PyUnicode_AsUTF8AndSize(value, &size);
268265
if (!chunk)
269266
return 2;
270-
if (size > MAX_STR_SIZE) {
271-
// This is a micro-optimization to reduce cache size, if someone
272-
// will complain about their 65K-long string literals we can adjust.
273-
PyErr_Format(
274-
PyExc_OverflowError,
275-
"cannot store string longer than %d bytes",
276-
MAX_STR_SIZE
277-
);
278-
return 2;
279-
}
280-
Py_ssize_t need = size + sizeof(STR_LEN_TYPE);
267+
Py_ssize_t need = size + sizeof(Py_ssize_t);
281268
if (_check_size((BufferObject *)data, need) == 2)
282269
return 2;
283270

284271
char *buf = ((BufferObject *)data)->buf;
285272
// Write string length.
286-
*(STR_LEN_TYPE *)(buf + ((BufferObject *)data)->pos) = (STR_LEN_TYPE)size;
287-
((BufferObject *)data)->pos += sizeof(STR_LEN_TYPE);
273+
*(Py_ssize_t *)(buf + ((BufferObject *)data)->pos) = size;
274+
((BufferObject *)data)->pos += sizeof(Py_ssize_t);
288275
// Write string content.
289276
memcpy(buf + ((BufferObject *)data)->pos, chunk, size);
290277
((BufferObject *)data)->pos += size;

0 commit comments

Comments
 (0)