Debian Codegraph @

mypyc/lib-rt/internal/librt_internal.c

raw
1 #include "pythoncapi_compat.h"
2
3 #define PY_SSIZE_T_CLEAN
4 #include <Python.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include "CPy.h"
8 #define LIBRT_INTERNAL_MODULE
9 #include "librt_internal.h"
10
11 #define START_SIZE 512
12
13 // See comment in read_int_internal() on motivation for these values.
14 #define MIN_ONE_BYTE_INT -10
15 #define MAX_ONE_BYTE_INT 117 // 2 ** 7 - 1 - 10
16 #define MIN_TWO_BYTES_INT -100
17 #define MAX_TWO_BYTES_INT 16283 // 2 ** (8 + 6) - 1 - 100
18 #define MIN_FOUR_BYTES_INT -10000
19 #define MAX_FOUR_BYTES_INT 536860911 // 2 ** (3 * 8 + 5) - 1 - 10000
20
21 #define TWO_BYTES_INT_BIT 1
22 #define FOUR_BYTES_INT_BIT 2
23 #define LONG_INT_BIT 4
24
25 #define FOUR_BYTES_INT_TRAILER 3
26 // We add one reserved bit here so that we can potentially support
27 // 8 bytes format in the future.
28 #define LONG_INT_TRAILER 15
29
30 #define CPY_BOOL_ERROR 2
31
32 #define _CHECK_READ_BUFFER(data, err) if (unlikely(_check_read_buffer(data) == CPY_NONE_ERROR)) \
33 return err;
34 #define _CHECK_WRITE_BUFFER(data, err) if (unlikely(_check_write_buffer(data) == CPY_NONE_ERROR)) \
35 return err;
36 #define _CHECK_WRITE(data, need) if (unlikely(_check_size((WriteBufferObject *)data, need) == CPY_NONE_ERROR)) \
37 return CPY_NONE_ERROR;
38 #define _CHECK_READ(data, size, err) if (unlikely(_check_read((ReadBufferObject *)data, size) == CPY_NONE_ERROR)) \
39 return err;
40
41 #define _READ(result, data, type) \
42 do { \
43 memcpy((void *) result, ((ReadBufferObject *)data)->ptr, sizeof(type)); \
44 ((ReadBufferObject *)data)->ptr += sizeof(type); \
45 } while (0)
46
47 #define _WRITE(data, type, v) \
48 do { \
49 type temp = v; \
50 memcpy(((WriteBufferObject *)data)->ptr, (const void *) &temp, sizeof(type)); \
51 ((WriteBufferObject *)data)->ptr += sizeof(type); \
52 } while (0)
53
54 //
55 // ReadBuffer
56 //
57
58 #if PY_BIG_ENDIAN
59 uint16_t reverse_16(uint16_t number) {
60 return (number << 8) | (number >> 8);
61 }
62
63 uint32_t reverse_32(uint32_t number) {
64 return ((number & 0xFF) << 24) | ((number & 0xFF00) << 8) | ((number & 0xFF0000) >> 8) | (number >> 24);
65 }
66 #endif
67
68 typedef struct {
69 PyObject_HEAD
70 char *ptr; // Current read location in the buffer
71 char *end; // End of the buffer
72 PyObject *source; // The object that contains the buffer
73 } ReadBufferObject;
74
75 static PyTypeObject ReadBufferType;
76
77 static PyObject*
78 ReadBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
79 {
80 if (type != &ReadBufferType) {
81 PyErr_SetString(PyExc_TypeError, "ReadBuffer should not be subclassed");
82 return NULL;
83 }
84
85 ReadBufferObject *self = (ReadBufferObject *)type->tp_alloc(type, 0);
86 if (self != NULL) {
87 self->source = NULL;
88 self->ptr = NULL;
89 self->end = NULL;
90 }
91 return (PyObject *) self;
92 }
93
94 static int
95 ReadBuffer_init_internal(ReadBufferObject *self, PyObject *source) {
96 if (!PyBytes_CheckExact(source)) {
97 PyErr_SetString(PyExc_TypeError, "source must be a bytes object");
98 return -1;
99 }
100 self->source = Py_NewRef(source);
101 self->ptr = PyBytes_AS_STRING(source);
102 self->end = self->ptr + PyBytes_GET_SIZE(source);
103 return 0;
104 }
105
106 static PyObject*
107 ReadBuffer_internal(PyObject *source) {
108 ReadBufferObject *self = (ReadBufferObject *)ReadBufferType.tp_alloc(&ReadBufferType, 0);
109 if (self == NULL)
110 return NULL;
111 self->ptr = NULL;
112 self->end = NULL;
113 self->source = NULL;
114 if (ReadBuffer_init_internal(self, source) == -1) {
115 Py_DECREF(self);
116 return NULL;
117 }
118 return (PyObject *)self;
119 }
120
121 static int
122 ReadBuffer_init(ReadBufferObject *self, PyObject *args, PyObject *kwds)
123 {
124 static char *kwlist[] = {"source", NULL};
125 PyObject *source = NULL;
126
127 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &source))
128 return -1;
129
130 return ReadBuffer_init_internal(self, source);
131 }
132
133 static void
134 ReadBuffer_dealloc(ReadBufferObject *self)
135 {
136 Py_CLEAR(self->source);
137 Py_TYPE(self)->tp_free((PyObject *)self);
138 }
139
140 static PyMethodDef ReadBuffer_methods[] = {
141 {NULL} /* Sentinel */
142 };
143
144 static PyTypeObject ReadBufferType = {
145 .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
146 .tp_name = "ReadBuffer",
147 .tp_doc = PyDoc_STR("Mypy cache buffer objects"),
148 .tp_basicsize = sizeof(ReadBufferObject),
149 .tp_itemsize = 0,
150 .tp_flags = Py_TPFLAGS_DEFAULT,
151 .tp_new = ReadBuffer_new,
152 .tp_init = (initproc) ReadBuffer_init,
153 .tp_dealloc = (destructor) ReadBuffer_dealloc,
154 .tp_methods = ReadBuffer_methods,
155 };
156
157 //
158 // WriteBuffer
159 //
160
161 typedef struct {
162 PyObject_HEAD
163 char *buf; // Beginning of the buffer
164 char *ptr; // Current write location in the buffer
165 char *end; // End of the buffer
166 } WriteBufferObject;
167
168 static PyTypeObject WriteBufferType;
169
170 static PyObject*
171 WriteBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
172 {
173 if (type != &WriteBufferType) {
174 PyErr_SetString(PyExc_TypeError, "WriteBuffer cannot be subclassed");
175 return NULL;
176 }
177
178 WriteBufferObject *self = (WriteBufferObject *)type->tp_alloc(type, 0);
179 if (self != NULL) {
180 self->buf = NULL;
181 self->ptr = NULL;
182 self->end = NULL;
183 }
184 return (PyObject *)self;
185 }
186
187 static int
188 WriteBuffer_init_internal(WriteBufferObject *self) {
189 Py_ssize_t size = START_SIZE;
190 self->buf = PyMem_Malloc(size + 1);
191 if (self->buf == NULL) {
192 PyErr_NoMemory();
193 return -1;
194 }
195 self->ptr = self->buf;
196 self->end = self->buf + size;
197 return 0;
198 }
199
200 static PyObject*
201 WriteBuffer_internal(void) {
202 WriteBufferObject *self = (WriteBufferObject *)WriteBufferType.tp_alloc(&WriteBufferType, 0);
203 if (self == NULL)
204 return NULL;
205 self->buf = NULL;
206 self->ptr = NULL;
207 self->end = NULL;
208 if (WriteBuffer_init_internal(self) == -1) {
209 Py_DECREF(self);
210 return NULL;
211 }
212 return (PyObject *)self;
213 }
214
215 static int
216 WriteBuffer_init(WriteBufferObject *self, PyObject *args, PyObject *kwds)
217 {
218 if (!PyArg_ParseTuple(args, "")) {
219 return -1;
220 }
221
222 if (kwds != NULL && PyDict_Size(kwds) > 0) {
223 PyErr_SetString(PyExc_TypeError,
224 "WriteBuffer() takes no keyword arguments");
225 return -1;
226 }
227
228 return WriteBuffer_init_internal(self);
229 }
230
231 static void
232 WriteBuffer_dealloc(WriteBufferObject *self)
233 {
234 PyMem_Free(self->buf);
235 self->buf = NULL;
236 Py_TYPE(self)->tp_free((PyObject *)self);
237 }
238
239 static PyObject*
240 WriteBuffer_getvalue_internal(PyObject *self)
241 {
242 WriteBufferObject *obj = (WriteBufferObject *)self;
243 return PyBytes_FromStringAndSize(obj->buf, obj->ptr - obj->buf);
244 }
245
246 static PyObject*
247 WriteBuffer_getvalue(WriteBufferObject *self, PyObject *Py_UNUSED(ignored))
248 {
249 return PyBytes_FromStringAndSize(self->buf, self->ptr - self->buf);
250 }
251
252 static PyMethodDef WriteBuffer_methods[] = {
253 {"getvalue", (PyCFunction) WriteBuffer_getvalue, METH_NOARGS,
254 "Return the buffer content as bytes object"
255 },
256 {NULL} /* Sentinel */
257 };
258
259 static PyTypeObject WriteBufferType = {
260 .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
261 .tp_name = "WriteBuffer",
262 .tp_doc = PyDoc_STR("Mypy cache buffer objects"),
263 .tp_basicsize = sizeof(WriteBufferObject),
264 .tp_itemsize = 0,
265 .tp_flags = Py_TPFLAGS_DEFAULT,
266 .tp_new = WriteBuffer_new,
267 .tp_init = (initproc) WriteBuffer_init,
268 .tp_dealloc = (destructor) WriteBuffer_dealloc,
269 .tp_methods = WriteBuffer_methods,
270 };
271
272 // ----------
273
274 static inline char
275 _check_read_buffer(PyObject *data) {
276 if (unlikely(Py_TYPE(data) != &ReadBufferType)) {
277 PyErr_Format(
278 PyExc_TypeError, "data must be a ReadBuffer object, got %s", Py_TYPE(data)->tp_name
279 );
280 return CPY_NONE_ERROR;
281 }
282 return CPY_NONE;
283 }
284
285 static inline char
286 _check_write_buffer(PyObject *data) {
287 if (unlikely(Py_TYPE(data) != &WriteBufferType)) {
288 PyErr_Format(
289 PyExc_TypeError, "data must be a WriteBuffer object, got %s", Py_TYPE(data)->tp_name
290 );
291 return CPY_NONE_ERROR;
292 }
293 return CPY_NONE;
294 }
295
296 static inline char
297 _check_size(WriteBufferObject *data, Py_ssize_t need) {
298 if (data->end - data->ptr >= need)
299 return CPY_NONE;
300 Py_ssize_t index = data->ptr - data->buf;
301 Py_ssize_t target = index + need;
302 Py_ssize_t size = data->end - data->buf;
303 do {
304 size *= 2;
305 } while (target >= size);
306 data->buf = PyMem_Realloc(data->buf, size);
307 if (unlikely(data->buf == NULL)) {
308 PyErr_NoMemory();
309 return CPY_NONE_ERROR;
310 }
311 data->ptr = data->buf + index;
312 data->end = data->buf + size;
313 return CPY_NONE;
314 }
315
316 static inline char
317 _check_read(ReadBufferObject *data, Py_ssize_t need) {
318 if (unlikely((data->end - data->ptr) < need)) {
319 PyErr_SetString(PyExc_ValueError, "reading past the buffer end");
320 return CPY_NONE_ERROR;
321 }
322 return CPY_NONE;
323 }
324
325 /*
326 bool format: single byte
327 \x00 - False
328 \x01 - True
329 */
330
331 static char
332 read_bool_internal(PyObject *data) {
333 _CHECK_READ(data, 1, CPY_BOOL_ERROR)
334 char res;
335 _READ(&res, data, char);
336 if (unlikely((res != 0) & (res != 1))) {
337 PyErr_SetString(PyExc_ValueError, "invalid bool value");
338 return CPY_BOOL_ERROR;
339 }
340 return res;
341 }
342
343 static PyObject*
344 read_bool(PyObject *self, PyObject *const *args, size_t nargs) {
345 if (unlikely(nargs != 1)) {
346 PyErr_Format(PyExc_TypeError,
347 "read_bool() takes exactly 1 argument (%zu given)", nargs);
348 return NULL;
349 }
350 PyObject *data = args[0];
351 _CHECK_READ_BUFFER(data, NULL)
352 char res = read_bool_internal(data);
353 if (unlikely(res == CPY_BOOL_ERROR))
354 return NULL;
355 PyObject *retval = res ? Py_True : Py_False;
356 Py_INCREF(retval);
357 return retval;
358 }
359
360 static char
361 write_bool_internal(PyObject *data, char value) {
362 _CHECK_WRITE(data, 1)
363 _WRITE(data, char, value);
364 return CPY_NONE;
365 }
366
367 static PyObject*
368 write_bool(PyObject *self, PyObject *const *args, size_t nargs) {
369 if (unlikely(nargs != 2)) {
370 PyErr_Format(PyExc_TypeError,
371 "write_bool() takes exactly 2 arguments (%zu given)", nargs);
372 return NULL;
373 }
374 PyObject *data = args[0];
375 PyObject *value = args[1];
376 _CHECK_WRITE_BUFFER(data, NULL)
377 if (unlikely(!PyBool_Check(value))) {
378 PyErr_SetString(PyExc_TypeError, "value must be a bool");
379 return NULL;
380 }
381 if (unlikely(write_bool_internal(data, Py_IsTrue(value)) == CPY_NONE_ERROR)) {
382 return NULL;
383 }
384 Py_INCREF(Py_None);
385 return Py_None;
386 }
387
388 /*
389 str format: size as int (see below) followed by UTF-8 bytes
390 */
391
392 static inline CPyTagged
393 _read_short_int(PyObject *data, uint8_t first) {
394 uint8_t second;
395 uint16_t two_more;
396 if ((first & TWO_BYTES_INT_BIT) == 0) {
397 // Note we use tagged ints since this function can return an error.
398 return ((Py_ssize_t)(first >> 1) + MIN_ONE_BYTE_INT) << 1;
399 }
400 if ((first & FOUR_BYTES_INT_BIT) == 0) {
401 _CHECK_READ(data, 1, CPY_INT_TAG)
402 _READ(&second, data, uint8_t);
403 return ((((Py_ssize_t)second) << 6) + (Py_ssize_t)(first >> 2) + MIN_TWO_BYTES_INT) << 1;
404 }
405 // The caller is responsible to verify this is called only for short ints.
406 _CHECK_READ(data, 3, CPY_INT_TAG)
407 // TODO: check if compilers emit optimal code for these two reads, and tweak if needed.
408 _READ(&second, data, uint8_t);
409 _READ(&two_more, data, uint16_t);
410 #if PY_BIG_ENDIAN
411 two_more = reverse_16(two_more);
412 #endif
413 Py_ssize_t higher = (((Py_ssize_t)two_more) << 13) + (((Py_ssize_t)second) << 5);
414 return (higher + (Py_ssize_t)(first >> 3) + MIN_FOUR_BYTES_INT) << 1;
415 }
416
417 static PyObject*
418 read_str_internal(PyObject *data) {
419 // Read string length.
420 _CHECK_READ(data, 1, NULL)
421 uint8_t first;
422 _READ(&first, data, uint8_t);
423 if (unlikely(first == LONG_INT_TRAILER)) {
424 // Fail fast for invalid/tampered data.
425 PyErr_SetString(PyExc_ValueError, "invalid str size");
426 return NULL;
427 }
428 CPyTagged tagged_size = _read_short_int(data, first);
429 if (tagged_size == CPY_INT_TAG)
430 return NULL;
431 if ((Py_ssize_t)tagged_size < 0) {
432 // Fail fast for invalid/tampered data.
433 PyErr_SetString(PyExc_ValueError, "invalid str size");
434 return NULL;
435 }
436 Py_ssize_t size = tagged_size >> 1;
437 // Read string content.
438 char *ptr = ((ReadBufferObject *)data)->ptr;
439 _CHECK_READ(data, size, NULL)
440 PyObject *res = PyUnicode_FromStringAndSize(ptr, (Py_ssize_t)size);
441 if (unlikely(res == NULL))
442 return NULL;
443 ((ReadBufferObject *)data)->ptr += size;
444 return res;
445 }
446
447 static PyObject*
448 read_str(PyObject *self, PyObject *const *args, size_t nargs) {
449 if (unlikely(nargs != 1)) {
450 PyErr_Format(PyExc_TypeError,
451 "read_str() takes exactly 1 argument (%zu given)", nargs);
452 return NULL;
453 }
454 PyObject *data = args[0];
455 _CHECK_READ_BUFFER(data, NULL)
456 return read_str_internal(data);
457 }
458
459 // The caller *must* check that real_value is within allowed range (29 bits).
460 static inline char
461 _write_short_int(PyObject *data, Py_ssize_t real_value) {
462 if (real_value >= MIN_ONE_BYTE_INT && real_value <= MAX_ONE_BYTE_INT) {
463 _CHECK_WRITE(data, 1)
464 _WRITE(data, uint8_t, (uint8_t)(real_value - MIN_ONE_BYTE_INT) << 1);
465 } else if (real_value >= MIN_TWO_BYTES_INT && real_value <= MAX_TWO_BYTES_INT) {
466 _CHECK_WRITE(data, 2)
467 #if PY_BIG_ENDIAN
468 uint16_t to_write = ((uint16_t)(real_value - MIN_TWO_BYTES_INT) << 2) | TWO_BYTES_INT_BIT;
469 _WRITE(data, uint16_t, reverse_16(to_write));
470 #else
471 _WRITE(data, uint16_t, ((uint16_t)(real_value - MIN_TWO_BYTES_INT) << 2) | TWO_BYTES_INT_BIT);
472 #endif
473 } else {
474 _CHECK_WRITE(data, 4)
475 #if PY_BIG_ENDIAN
476 uint32_t to_write = ((uint32_t)(real_value - MIN_FOUR_BYTES_INT) << 3) | FOUR_BYTES_INT_TRAILER;
477 _WRITE(data, uint32_t, reverse_32(to_write));
478 #else
479 _WRITE(data, uint32_t, ((uint32_t)(real_value - MIN_FOUR_BYTES_INT) << 3) | FOUR_BYTES_INT_TRAILER);
480 #endif
481 }
482 return CPY_NONE;
483 }
484
485 static char
486 write_str_internal(PyObject *data, PyObject *value) {
487 Py_ssize_t size;
488 const char *chunk = PyUnicode_AsUTF8AndSize(value, &size);
489 if (unlikely(chunk == NULL))
490 return CPY_NONE_ERROR;
491
492 // Write string length.
493 if (likely(size >= MIN_FOUR_BYTES_INT && size <= MAX_FOUR_BYTES_INT)) {
494 if (_write_short_int(data, size) == CPY_NONE_ERROR)
495 return CPY_NONE_ERROR;
496 } else {
497 PyErr_SetString(PyExc_ValueError, "str too long to serialize");
498 return CPY_NONE_ERROR;
499 }
500 // Write string content.
501 _CHECK_WRITE(data, size)
502 char *ptr = ((WriteBufferObject *)data)->ptr;
503 memcpy(ptr, chunk, size);
504 ((WriteBufferObject *)data)->ptr += size;
505 return CPY_NONE;
506 }
507
508 static PyObject*
509 write_str(PyObject *self, PyObject *const *args, size_t nargs) {
510 if (unlikely(nargs != 2)) {
511 PyErr_Format(PyExc_TypeError,
512 "write_str() takes exactly 2 arguments (%zu given)", nargs);
513 return NULL;
514 }
515 PyObject *data = args[0];
516 PyObject *value = args[1];
517 _CHECK_WRITE_BUFFER(data, NULL)
518 if (unlikely(!PyUnicode_Check(value))) {
519 PyErr_SetString(PyExc_TypeError, "value must be a str");
520 return NULL;
521 }
522 if (unlikely(write_str_internal(data, value) == CPY_NONE_ERROR)) {
523 return NULL;
524 }
525 Py_INCREF(Py_None);
526 return Py_None;
527 }
528
529 /*
530 bytes format: size as int (see below) followed by bytes
531 */
532
533 static PyObject*
534 read_bytes_internal(PyObject *data) {
535 // Read length.
536 _CHECK_READ(data, 1, NULL)
537 uint8_t first;
538 _READ(&first, data, uint8_t);
539 if (unlikely(first == LONG_INT_TRAILER)) {
540 // Fail fast for invalid/tampered data.
541 PyErr_SetString(PyExc_ValueError, "invalid bytes size");
542 return NULL;
543 }
544 CPyTagged tagged_size = _read_short_int(data, first);
545 if (tagged_size == CPY_INT_TAG)
546 return NULL;
547 if ((Py_ssize_t)tagged_size < 0) {
548 // Fail fast for invalid/tampered data.
549 PyErr_SetString(PyExc_ValueError, "invalid bytes size");
550 return NULL;
551 }
552 Py_ssize_t size = tagged_size >> 1;
553 // Read bytes content.
554 char *ptr = ((ReadBufferObject *)data)->ptr;
555 _CHECK_READ(data, size, NULL)
556 PyObject *res = PyBytes_FromStringAndSize(ptr, (Py_ssize_t)size);
557 if (unlikely(res == NULL))
558 return NULL;
559 ((ReadBufferObject *)data)->ptr += size;
560 return res;
561 }
562
563 static PyObject*
564 read_bytes(PyObject *self, PyObject *const *args, size_t nargs) {
565 if (unlikely(nargs != 1)) {
566 PyErr_Format(PyExc_TypeError,
567 "read_bytes() takes exactly 1 argument (%zu given)", nargs);
568 return NULL;
569 }
570 PyObject *data = args[0];
571 _CHECK_READ_BUFFER(data, NULL)
572 return read_bytes_internal(data);
573 }
574
575 static char
576 write_bytes_internal(PyObject *data, PyObject *value) {
577 const char *chunk = PyBytes_AsString(value);
578 if (unlikely(chunk == NULL))
579 return CPY_NONE_ERROR;
580 Py_ssize_t size = PyBytes_GET_SIZE(value);
581
582 // Write length.
583 if (likely(size >= MIN_FOUR_BYTES_INT && size <= MAX_FOUR_BYTES_INT)) {
584 if (_write_short_int(data, size) == CPY_NONE_ERROR)
585 return CPY_NONE_ERROR;
586 } else {
587 PyErr_SetString(PyExc_ValueError, "bytes too long to serialize");
588 return CPY_NONE_ERROR;
589 }
590 // Write bytes content.
591 _CHECK_WRITE(data, size)
592 char *ptr = ((WriteBufferObject *)data)->ptr;
593 memcpy(ptr, chunk, size);
594 ((WriteBufferObject *)data)->ptr += size;
595 return CPY_NONE;
596 }
597
598 static PyObject*
599 write_bytes(PyObject *self, PyObject *const *args, size_t nargs) {
600 if (unlikely(nargs != 2)) {
601 PyErr_Format(PyExc_TypeError,
602 "write_bytes() takes exactly 2 arguments (%zu given)", nargs);
603 return NULL;
604 }
605 PyObject *data = args[0];
606 PyObject *value = args[1];
607 _CHECK_WRITE_BUFFER(data, NULL)
608 if (unlikely(!PyBytes_Check(value))) {
609 PyErr_SetString(PyExc_TypeError, "value must be a bytes object");
610 return NULL;
611 }
612 if (unlikely(write_bytes_internal(data, value) == CPY_NONE_ERROR)) {
613 return NULL;
614 }
615 Py_INCREF(Py_None);
616 return Py_None;
617 }
618
619 /*
620 float format:
621 stored using PyFloat helpers in little-endian format.
622 */
623
624 static double
625 read_float_internal(PyObject *data) {
626 _CHECK_READ(data, 8, CPY_FLOAT_ERROR)
627 char *ptr = ((ReadBufferObject *)data)->ptr;
628 double res = PyFloat_Unpack8(ptr, 1);
629 if (unlikely((res == -1.0) && PyErr_Occurred()))
630 return CPY_FLOAT_ERROR;
631 ((ReadBufferObject *)data)->ptr += 8;
632 return res;
633 }
634
635 static PyObject*
636 read_float(PyObject *self, PyObject *const *args, size_t nargs) {
637 if (unlikely(nargs != 1)) {
638 PyErr_Format(PyExc_TypeError,
639 "read_float() takes exactly 1 argument (%zu given)", nargs);
640 return NULL;
641 }
642 PyObject *data = args[0];
643 _CHECK_READ_BUFFER(data, NULL)
644 double retval = read_float_internal(data);
645 if (unlikely(retval == CPY_FLOAT_ERROR && PyErr_Occurred())) {
646 return NULL;
647 }
648 return PyFloat_FromDouble(retval);
649 }
650
651 static char
652 write_float_internal(PyObject *data, double value) {
653 _CHECK_WRITE(data, 8)
654 char *ptr = ((WriteBufferObject *)data)->ptr;
655 int res = PyFloat_Pack8(value, ptr, 1);
656 if (unlikely(res == -1))
657 return CPY_NONE_ERROR;
658 ((WriteBufferObject *)data)->ptr += 8;
659 return CPY_NONE;
660 }
661
662 static PyObject*
663 write_float(PyObject *self, PyObject *const *args, size_t nargs) {
664 if (unlikely(nargs != 2)) {
665 PyErr_Format(PyExc_TypeError,
666 "write_float() takes exactly 2 arguments (%zu given)", nargs);
667 return NULL;
668 }
669 PyObject *data = args[0];
670 PyObject *value = args[1];
671 _CHECK_WRITE_BUFFER(data, NULL)
672 if (unlikely(!PyFloat_Check(value))) {
673 PyErr_SetString(PyExc_TypeError, "value must be a float");
674 return NULL;
675 }
676 if (unlikely(write_float_internal(data, PyFloat_AsDouble(value)) == CPY_NONE_ERROR)) {
677 return NULL;
678 }
679 Py_INCREF(Py_None);
680 return Py_None;
681 }
682
683 /*
684 int format:
685 one byte: last bit 0, 7 bits used
686 two bytes: last two bits 01, 14 bits used
687 four bytes: last three bits 011, 29 bits used
688 everything else: 00001111 followed by serialized string representation
689
690 Note: for fixed size formats we skew ranges towards more positive values,
691 since negative integers are much more rare.
692 */
693
694 static CPyTagged
695 read_int_internal(PyObject *data) {
696 _CHECK_READ(data, 1, CPY_INT_TAG)
697
698 uint8_t first;
699 _READ(&first, data, uint8_t);
700 if (likely(first != LONG_INT_TRAILER)) {
701 return _read_short_int(data, first);
702 }
703
704 // Long integer encoding -- byte length and sign, followed by a byte array.
705
706 // Read byte length and sign.
707 _CHECK_READ(data, 1, CPY_INT_TAG)
708 _READ(&first, data, uint8_t);
709 Py_ssize_t size_and_sign = _read_short_int(data, first);
710 if (size_and_sign == CPY_INT_TAG)
711 return CPY_INT_TAG;
712 if ((Py_ssize_t)size_and_sign < 0) {
713 PyErr_SetString(PyExc_ValueError, "invalid int data");
714 return CPY_INT_TAG;
715 }
716 bool sign = (size_and_sign >> 1) & 1;
717 Py_ssize_t size = size_and_sign >> 2;
718
719 // Construct an int object from the byte array.
720 _CHECK_READ(data, size, CPY_INT_TAG)
721 char *ptr = ((ReadBufferObject *)data)->ptr;
722 PyObject *num = _PyLong_FromByteArray((unsigned char *)ptr, size, 1, 0);
723 if (num == NULL)
724 return CPY_INT_TAG;
725 ((ReadBufferObject *)data)->ptr += size;
726 if (sign) {
727 PyObject *old = num;
728 num = PyNumber_Negative(old);
729 Py_DECREF(old);
730 if (num == NULL) {
731 return CPY_INT_TAG;
732 }
733 }
734 return CPyTagged_StealFromObject(num);
735 }
736
737 static PyObject*
738 read_int(PyObject *self, PyObject *const *args, size_t nargs) {
739 if (unlikely(nargs != 1)) {
740 PyErr_Format(PyExc_TypeError,
741 "read_int() takes exactly 1 argument (%zu given)", nargs);
742 return NULL;
743 }
744 PyObject *data = args[0];
745 _CHECK_READ_BUFFER(data, NULL)
746 CPyTagged retval = read_int_internal(data);
747 if (unlikely(retval == CPY_INT_TAG)) {
748 return NULL;
749 }
750 return CPyTagged_StealAsObject(retval);
751 }
752
753
754 static inline int hex_to_int(char c) {
755 if (c >= '0' && c <= '9')
756 return c - '0';
757 else if (c >= 'a' && c <= 'f')
758 return c - 'a' + 10;
759 else
760 return c - 'A' + 10; // Assume valid hex digit
761 }
762
763 static inline char
764 _write_long_int(PyObject *data, CPyTagged value) {
765 _CHECK_WRITE(data, 1)
766 _WRITE(data, uint8_t, LONG_INT_TRAILER);
767
768 PyObject *hex_str = NULL;
769 PyObject* int_value = CPyTagged_AsObject(value);
770 if (unlikely(int_value == NULL))
771 goto error;
772
773 hex_str = PyNumber_ToBase(int_value, 16);
774 if (hex_str == NULL)
775 goto error;
776 Py_DECREF(int_value);
777 int_value = NULL;
778
779 const char *str = PyUnicode_AsUTF8(hex_str);
780 if (str == NULL)
781 goto error;
782 Py_ssize_t len = strlen(str);
783 bool neg;
784 if (str[0] == '-') {
785 str++;
786 len--;
787 neg = true;
788 } else {
789 neg = false;
790 }
791 // Skip the 0x hex prefix.
792 str += 2;
793 len -= 2;
794
795 // Write bytes encoded length and sign.
796 Py_ssize_t size = (len + 1) / 2;
797 Py_ssize_t encoded_size = (size << 1) | neg;
798 if (encoded_size <= MAX_FOUR_BYTES_INT) {
799 if (_write_short_int(data, encoded_size) == CPY_NONE_ERROR)
800 goto error;
801 } else {
802 PyErr_SetString(PyExc_ValueError, "int too long to serialize");
803 goto error;
804 }
805
806 // Write absolute integer value as byte array in a variable-length little endian format.
807 Py_ssize_t i;
808 for (i = len; i > 1; i -= 2) {
809 if (write_tag_internal(
810 data, hex_to_int(str[i - 1]) | (hex_to_int(str[i - 2]) << 4)) == CPY_NONE_ERROR)
811 goto error;
812 }
813 // The final byte may correspond to only one hex digit.
814 if (i == 1) {
815 if (write_tag_internal(data, hex_to_int(str[i - 1])) == CPY_NONE_ERROR)
816 goto error;
817 }
818
819 Py_DECREF(hex_str);
820 return CPY_NONE;
821
822 error:
823
824 Py_XDECREF(int_value);
825 Py_XDECREF(hex_str);
826 return CPY_NONE_ERROR;
827 }
828
829 static char
830 write_int_internal(PyObject *data, CPyTagged value) {
831 if (likely((value & CPY_INT_TAG) == 0)) {
832 Py_ssize_t real_value = CPyTagged_ShortAsSsize_t(value);
833 if (likely(real_value >= MIN_FOUR_BYTES_INT && real_value <= MAX_FOUR_BYTES_INT)) {
834 return _write_short_int(data, real_value);
835 } else {
836 return _write_long_int(data, value);
837 }
838 } else {
839 return _write_long_int(data, value);
840 }
841 }
842
843 static PyObject*
844 write_int(PyObject *self, PyObject *const *args, size_t nargs) {
845 if (unlikely(nargs != 2)) {
846 PyErr_Format(PyExc_TypeError,
847 "write_int() takes exactly 2 arguments (%zu given)", nargs);
848 return NULL;
849 }
850 PyObject *data = args[0];
851 PyObject *value = args[1];
852 _CHECK_WRITE_BUFFER(data, NULL)
853 if (unlikely(!PyLong_Check(value))) {
854 PyErr_SetString(PyExc_TypeError, "value must be an int");
855 return NULL;
856 }
857 CPyTagged tagged_value = CPyTagged_BorrowFromObject(value);
858 if (unlikely(write_int_internal(data, tagged_value) == CPY_NONE_ERROR)) {
859 return NULL;
860 }
861 Py_INCREF(Py_None);
862 return Py_None;
863 }
864
865 /*
866 integer tag format (0 <= t <= 255):
867 stored as a uint8_t
868 */
869
870 static uint8_t
871 read_tag_internal(PyObject *data) {
872 _CHECK_READ(data, 1, CPY_LL_UINT_ERROR)
873 uint8_t ret;
874 _READ(&ret, data, uint8_t);
875 return ret;
876 }
877
878 static PyObject*
879 read_tag(PyObject *self, PyObject *const *args, size_t nargs) {
880 if (unlikely(nargs != 1)) {
881 PyErr_Format(PyExc_TypeError,
882 "read_tag() takes exactly 1 argument (%zu given)", nargs);
883 return NULL;
884 }
885 PyObject *data = args[0];
886 _CHECK_READ_BUFFER(data, NULL)
887 uint8_t retval = read_tag_internal(data);
888 if (unlikely(retval == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
889 return NULL;
890 }
891 return PyLong_FromLong(retval);
892 }
893
894 static char
895 write_tag_internal(PyObject *data, uint8_t value) {
896 _CHECK_WRITE(data, 1)
897 _WRITE(data, uint8_t, value);
898 return CPY_NONE;
899 }
900
901 static PyObject*
902 write_tag(PyObject *self, PyObject *const *args, size_t nargs) {
903 if (unlikely(nargs != 2)) {
904 PyErr_Format(PyExc_TypeError,
905 "write_tag() takes exactly 2 arguments (%zu given)", nargs);
906 return NULL;
907 }
908 PyObject *data = args[0];
909 PyObject *value = args[1];
910 _CHECK_WRITE_BUFFER(data, NULL)
911 uint8_t unboxed = CPyLong_AsUInt8(value);
912 if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
913 CPy_TypeError("u8", value);
914 return NULL;
915 }
916 if (unlikely(write_tag_internal(data, unboxed) == CPY_NONE_ERROR)) {
917 return NULL;
918 }
919 Py_INCREF(Py_None);
920 return Py_None;
921 }
922
923 // All tags must be kept in sync with cache.py, nodes.py, and types.py.
924 // Primitive types.
925 #define LITERAL_FALSE 0
926 #define LITERAL_TRUE 1
927 #define LITERAL_NONE 2
928 #define LITERAL_INT 3
929 #define LITERAL_STR 4
930 #define LITERAL_BYTES 5
931 #define LITERAL_FLOAT 6
932 #define LITERAL_COMPLEX 7
933
934 // Supported builtin collections.
935 #define LIST_GEN 20
936 #define LIST_INT 21
937 #define LIST_STR 22
938 #define LIST_BYTES 23
939 #define TUPLE_GEN 24
940 #define DICT_STR_GEN 30
941
942 // This is the smallest custom class tag.
943 #define MYPY_FILE 50
944
945 // Instance class has special formats.
946 #define INSTANCE 80
947 #define INSTANCE_SIMPLE 81
948 #define INSTANCE_GENERIC 82
949 #define INSTANCE_STR 83
950 #define INSTANCE_FUNCTION 84
951 #define INSTANCE_INT 85
952 #define INSTANCE_BOOL 86
953 #define INSTANCE_OBJECT 87
954
955 #define RESERVED 254
956 #define END_TAG 255
957
958 // Forward declaration.
959 static char _skip_object(PyObject *data, uint8_t tag);
960
961 static inline char
962 _skip(PyObject *data, Py_ssize_t size) {
963 // We are careful about error conditions, so all
964 // _skip_xxx() functions can return an error value.
965 _CHECK_READ(data, size, CPY_NONE_ERROR)
966 ((ReadBufferObject *)data)->ptr += size;
967 return CPY_NONE;
968 }
969
970 static inline char
971 _skip_short_int(PyObject *data, uint8_t first) {
972 if ((first & TWO_BYTES_INT_BIT) == 0)
973 return CPY_NONE;
974 if ((first & FOUR_BYTES_INT_BIT) == 0)
975 return _skip(data, 1);
976 return _skip(data, 3);
977 }
978
979 static inline char
980 _skip_int(PyObject *data) {
981 _CHECK_READ(data, 1, CPY_NONE_ERROR)
982
983 uint8_t first;
984 _READ(&first, data, uint8_t);
985 if (likely(first != LONG_INT_TRAILER)) {
986 return _skip_short_int(data, first);
987 }
988
989 _CHECK_READ(data, 1, CPY_NONE_ERROR)
990 _READ(&first, data, uint8_t);
991 Py_ssize_t size_and_sign = _read_short_int(data, first);
992 if (size_and_sign == CPY_INT_TAG)
993 return CPY_NONE_ERROR;
994 if ((Py_ssize_t)size_and_sign < 0) {
995 PyErr_SetString(PyExc_ValueError, "invalid int data");
996 return CPY_NONE_ERROR;
997 }
998 Py_ssize_t size = size_and_sign >> 2;
999 return _skip(data, size);
1000 }
1001
1002 // This is essentially a wrapper around _read_short_int() that makes
1003 // sure the result is valid.
1004 static inline Py_ssize_t
1005 _read_size(PyObject *data) {
1006 _CHECK_READ(data, 1, -1)
1007 uint8_t first;
1008 _READ(&first, data, uint8_t);
1009 // We actually allow serializing lists/dicts with over 4 billion items,
1010 // but we don't really need to, fail with ValueError just in case.
1011 if (unlikely(first == LONG_INT_TRAILER)) {
1012 PyErr_SetString(PyExc_ValueError, "unsupported size");
1013 return -1;
1014 }
1015 CPyTagged tagged_size = _read_short_int(data, first);
1016 if (tagged_size == CPY_INT_TAG)
1017 return -1;
1018 if ((Py_ssize_t)tagged_size < 0) {
1019 PyErr_SetString(PyExc_ValueError, "invalid size");
1020 return -1;
1021 }
1022 Py_ssize_t size = tagged_size >> 1;
1023 return size;
1024 }
1025
1026 static inline char
1027 _skip_str_bytes(PyObject *data) {
1028 Py_ssize_t size = _read_size(data);
1029 if (size < 0)
1030 return CPY_NONE_ERROR;
1031 return _skip(data, size);
1032 }
1033
1034 // List/dict logic should be kept in sync with mypy/cache.py
1035 static inline char
1036 _skip_list_gen(PyObject *data) {
1037 Py_ssize_t size = _read_size(data);
1038 if (size < 0)
1039 return CPY_NONE_ERROR;
1040 Py_ssize_t i;
1041 for (i = 0; i < size; i++) {
1042 uint8_t tag = read_tag_internal(data);
1043 if (unlikely(tag == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
1044 return CPY_NONE_ERROR;
1045 }
1046 if (unlikely(_skip_object(data, tag) == CPY_NONE_ERROR))
1047 return CPY_NONE_ERROR;
1048 }
1049 return CPY_NONE;
1050 }
1051
1052 static inline char
1053 _skip_list_int(PyObject *data) {
1054 Py_ssize_t size = _read_size(data);
1055 if (size < 0)
1056 return CPY_NONE_ERROR;
1057 Py_ssize_t i;
1058 for (i = 0; i < size; i++) {
1059 if (unlikely(_skip_int(data) == CPY_NONE_ERROR))
1060 return CPY_NONE_ERROR;
1061 }
1062 return CPY_NONE;
1063 }
1064
1065 static inline char
1066 _skip_list_str_bytes(PyObject *data) {
1067 Py_ssize_t size = _read_size(data);
1068 if (size < 0)
1069 return CPY_NONE_ERROR;
1070 Py_ssize_t i;
1071 for (i = 0; i < size; i++) {
1072 if (unlikely(_skip_str_bytes(data) == CPY_NONE_ERROR))
1073 return CPY_NONE_ERROR;
1074 }
1075 return CPY_NONE;
1076 }
1077
1078 static inline char
1079 _skip_dict_str_gen(PyObject *data) {
1080 Py_ssize_t size = _read_size(data);
1081 if (size < 0)
1082 return CPY_NONE_ERROR;
1083 Py_ssize_t i;
1084 for (i = 0; i < size; i++) {
1085 // Bare key followed by tagged value.
1086 if (unlikely(_skip_str_bytes(data) == CPY_NONE_ERROR))
1087 return CPY_NONE_ERROR;
1088 uint8_t tag = read_tag_internal(data);
1089 if (unlikely(tag == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
1090 return CPY_NONE_ERROR;
1091 }
1092 if (unlikely(_skip_object(data, tag) == CPY_NONE_ERROR))
1093 return CPY_NONE_ERROR;
1094 }
1095 return CPY_NONE;
1096 }
1097
1098 // Similar to mypy/cache.py, the convention is that the caller reads
1099 // the opening tag for custom classes.
1100 static inline char
1101 _skip_class(PyObject *data) {
1102 while (1) {
1103 uint8_t tag = read_tag_internal(data);
1104 if (unlikely(tag == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
1105 return CPY_NONE_ERROR;
1106 }
1107 if (tag == END_TAG) {
1108 return CPY_NONE;
1109 }
1110 if (unlikely(_skip_object(data, tag) == CPY_NONE_ERROR)) {
1111 return CPY_NONE_ERROR;
1112 }
1113 }
1114 }
1115
1116 // Instance has special compact layout (as an important optimization).
1117 static inline char
1118 _skip_instance(PyObject *data) {
1119 uint8_t second_tag = read_tag_internal(data);
1120 if (unlikely(second_tag == CPY_LL_UINT_ERROR && PyErr_Occurred())) {
1121 return CPY_NONE_ERROR;
1122 }
1123 if (second_tag >= INSTANCE_STR && second_tag <= INSTANCE_OBJECT) {
1124 return CPY_NONE;
1125 }
1126 if (second_tag == INSTANCE_SIMPLE) {
1127 return _skip_str_bytes(data);
1128 }
1129 if (second_tag == INSTANCE_GENERIC) {
1130 return _skip_class(data);
1131 }
1132 PyErr_Format(PyExc_ValueError, "Unexpected instance tag: %d", second_tag);
1133 return CPY_NONE_ERROR;
1134 }
1135
1136 // This is the main dispatch point. Branches are ordered manually
1137 // based roughly on frequency in self-check.
1138 static char
1139 _skip_object(PyObject *data, uint8_t tag) {
1140 if (tag == LITERAL_STR || tag == LITERAL_BYTES)
1141 return _skip_str_bytes(data);
1142 if (tag == LITERAL_NONE || tag == LITERAL_FALSE || tag == LITERAL_TRUE)
1143 return CPY_NONE;
1144 if (tag == LIST_GEN || tag == TUPLE_GEN)
1145 return _skip_list_gen(data);
1146 if (tag == LITERAL_INT)
1147 return _skip_int(data);
1148 if (tag == INSTANCE)
1149 return _skip_instance(data);
1150 // We intentionally exclude MypyFile as a sanity check. Module symbols should
1151 // be always handled via cross_ref, and never appear in a symbol table as is.
1152 if (tag > MYPY_FILE && tag < RESERVED)
1153 return _skip_class(data);
1154 if (tag == LIST_INT)
1155 return _skip_list_int(data);
1156 if (tag == LIST_STR || tag == LIST_BYTES)
1157 return _skip_list_str_bytes(data);
1158 if (tag == DICT_STR_GEN)
1159 return _skip_dict_str_gen(data);
1160 if (tag == LITERAL_FLOAT)
1161 return _skip(data, 8);
1162 if (tag == LITERAL_COMPLEX)
1163 return _skip(data, 16);
1164 PyErr_Format(PyExc_ValueError, "Unsupported tag: %d", tag);
1165 return CPY_NONE_ERROR;
1166 }
1167
1168 static PyObject*
1169 extract_symbol_internal(PyObject *data) {
1170 char *ptr = ((ReadBufferObject *)data)->ptr;
1171 if (unlikely(_skip_class(data) == CPY_NONE_ERROR))
1172 return NULL;
1173 Py_ssize_t size = ((ReadBufferObject *)data)->ptr - ptr;
1174 PyObject *res = PyBytes_FromStringAndSize(ptr, size);
1175 if (unlikely(res == NULL))
1176 return NULL;
1177 return res;
1178 }
1179
1180 static PyObject*
1181 extract_symbol(PyObject *self, PyObject *const *args, size_t nargs) {
1182 if (unlikely(nargs != 1)) {
1183 PyErr_Format(PyExc_TypeError,
1184 "extract_symbol() takes exactly 1 argument (%zu given)", nargs);
1185 return NULL;
1186 }
1187 PyObject *data = args[0];
1188 _CHECK_READ_BUFFER(data, NULL)
1189 return extract_symbol_internal(data);
1190 }
1191
1192 static uint8_t
1193 cache_version_internal(void) {
1194 return 0;
1195 }
1196
1197 static PyObject*
1198 cache_version(PyObject *self, PyObject *Py_UNUSED(ignored)) {
1199 return PyLong_FromLong(cache_version_internal());
1200 }
1201
1202 static PyTypeObject *
1203 ReadBuffer_type_internal(void) {
1204 return &ReadBufferType; // Return borrowed reference
1205 }
1206
1207 static PyTypeObject *
1208 WriteBuffer_type_internal(void) {
1209 return &WriteBufferType; // Return borrowed reference
1210 };
1211
1212 static PyMethodDef librt_internal_module_methods[] = {
1213 {"write_bool", (PyCFunction)write_bool, METH_FASTCALL, PyDoc_STR("write a bool")},
1214 {"read_bool", (PyCFunction)read_bool, METH_FASTCALL, PyDoc_STR("read a bool")},
1215 {"write_str", (PyCFunction)write_str, METH_FASTCALL, PyDoc_STR("write a string")},
1216 {"read_str", (PyCFunction)read_str, METH_FASTCALL, PyDoc_STR("read a string")},
1217 {"write_bytes", (PyCFunction)write_bytes, METH_FASTCALL, PyDoc_STR("write bytes")},
1218 {"read_bytes", (PyCFunction)read_bytes, METH_FASTCALL, PyDoc_STR("read bytes")},
1219 {"write_float", (PyCFunction)write_float, METH_FASTCALL, PyDoc_STR("write a float")},
1220 {"read_float", (PyCFunction)read_float, METH_FASTCALL, PyDoc_STR("read a float")},
1221 {"write_int", (PyCFunction)write_int, METH_FASTCALL, PyDoc_STR("write an int")},
1222 {"read_int", (PyCFunction)read_int, METH_FASTCALL, PyDoc_STR("read an int")},
1223 {"write_tag", (PyCFunction)write_tag, METH_FASTCALL, PyDoc_STR("write a short int")},
1224 {"read_tag", (PyCFunction)read_tag, METH_FASTCALL, PyDoc_STR("read a short int")},
1225 {"cache_version", (PyCFunction)cache_version, METH_NOARGS, PyDoc_STR("cache format version")},
1226 {"extract_symbol", (PyCFunction)extract_symbol, METH_FASTCALL, PyDoc_STR("extract bytes for a mypy symbol")},
1227 {NULL, NULL, 0, NULL}
1228 };
1229
1230 static int
1231 NativeInternal_ABI_Version(void) {
1232 return LIBRT_INTERNAL_ABI_VERSION;
1233 }
1234
1235 static int
1236 NativeInternal_API_Version(void) {
1237 return LIBRT_INTERNAL_API_VERSION;
1238 }
1239
1240 static int
1241 librt_internal_module_exec(PyObject *m)
1242 {
1243 if (PyType_Ready(&ReadBufferType) < 0) {
1244 return -1;
1245 }
1246 if (PyType_Ready(&WriteBufferType) < 0) {
1247 return -1;
1248 }
1249 if (PyModule_AddObjectRef(m, "ReadBuffer", (PyObject *) &ReadBufferType) < 0) {
1250 return -1;
1251 }
1252 if (PyModule_AddObjectRef(m, "WriteBuffer", (PyObject *) &WriteBufferType) < 0) {
1253 return -1;
1254 }
1255
1256 // Export mypy internal C API, be careful with the order!
1257 static void *NativeInternal_API[LIBRT_INTERNAL_API_LEN] = {
1258 (void *)ReadBuffer_internal,
1259 (void *)WriteBuffer_internal,
1260 (void *)WriteBuffer_getvalue_internal,
1261 (void *)write_bool_internal,
1262 (void *)read_bool_internal,
1263 (void *)write_str_internal,
1264 (void *)read_str_internal,
1265 (void *)write_float_internal,
1266 (void *)read_float_internal,
1267 (void *)write_int_internal,
1268 (void *)read_int_internal,
1269 (void *)write_tag_internal,
1270 (void *)read_tag_internal,
1271 (void *)NativeInternal_ABI_Version,
1272 (void *)write_bytes_internal,
1273 (void *)read_bytes_internal,
1274 (void *)cache_version_internal,
1275 (void *)ReadBuffer_type_internal,
1276 (void *)WriteBuffer_type_internal,
1277 (void *)NativeInternal_API_Version,
1278 (void *)extract_symbol_internal
1279 };
1280 PyObject *c_api_object = PyCapsule_New((void *)NativeInternal_API, "librt.internal._C_API", NULL);
1281 if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
1282 return -1;
1283 }
1284 return 0;
1285 }
1286
1287 static PyModuleDef_Slot librt_internal_module_slots[] = {
1288 {Py_mod_exec, librt_internal_module_exec},
1289 #ifdef Py_MOD_GIL_NOT_USED
1290 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
1291 #endif
1292 {0, NULL}
1293 };
1294
1295 static PyModuleDef librt_internal_module = {
1296 .m_base = PyModuleDef_HEAD_INIT,
1297 .m_name = "internal",
1298 .m_doc = "Mypy cache serialization utils",
1299 .m_size = 0,
1300 .m_methods = librt_internal_module_methods,
1301 .m_slots = librt_internal_module_slots,
1302 };
1303
1304 PyMODINIT_FUNC
1305 PyInit_internal(void)
1306 {
1307 return PyModuleDef_Init(&librt_internal_module);
1308 }