Skip to content

Commit 84b6a70

Browse files
committed
Provide access to the type objects via API
1 parent e4d0c03 commit 84b6a70

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

mypyc/lib-rt/librt_internal.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,16 @@ cache_version(PyObject *self, PyObject *Py_UNUSED(ignored)) {
929929
return PyLong_FromLong(cache_version_internal());
930930
}
931931

932+
static PyTypeObject *
933+
ReadBuffer_type_internal(void) {
934+
return &ReadBufferType; // Return borrowed reference
935+
}
936+
937+
static PyTypeObject *
938+
WriteBuffer_type_internal(void) {
939+
return &WriteBufferType; // Return borrowed reference
940+
};
941+
932942
static PyMethodDef librt_internal_module_methods[] = {
933943
{"write_bool", (PyCFunction)write_bool, METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("write a bool")},
934944
{"read_bool", (PyCFunction)read_bool, METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("read a bool")},
@@ -986,6 +996,8 @@ librt_internal_module_exec(PyObject *m)
986996
(void *)write_bytes_internal,
987997
(void *)read_bytes_internal,
988998
(void *)cache_version_internal,
999+
(void *)ReadBuffer_type_internal,
1000+
(void *)WriteBuffer_type_internal,
9891001
};
9901002
PyObject *c_api_object = PyCapsule_New((void *)NativeInternal_API, "librt.internal._C_API", NULL);
9911003
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {

mypyc/lib-rt/librt_internal.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIBRT_INTERNAL_H
33

44
#define LIBRT_INTERNAL_ABI_VERSION 1
5-
#define LIBRT_INTERNAL_API_LEN 17
5+
#define LIBRT_INTERNAL_API_LEN 19
66

77
#ifdef LIBRT_INTERNAL_MODULE
88

@@ -25,6 +25,8 @@ static int NativeInternal_ABI_Version(void);
2525
static char write_bytes_internal(PyObject *data, PyObject *value);
2626
static PyObject *read_bytes_internal(PyObject *data);
2727
static uint8_t cache_version_internal(void);
28+
static PyTypeObject *ReadBuffer_type_internal(void);
29+
static PyTypeObject *WriteBuffer_type_internal(void);
2830

2931
#else
3032

@@ -47,6 +49,8 @@ static void *NativeInternal_API[LIBRT_INTERNAL_API_LEN];
4749
#define write_bytes_internal (*(char (*)(PyObject *source, PyObject *value)) NativeInternal_API[14])
4850
#define read_bytes_internal (*(PyObject* (*)(PyObject *source)) NativeInternal_API[15])
4951
#define cache_version_internal (*(uint8_t (*)(void)) NativeInternal_API[16])
52+
#define ReadBuffer_type_internal (*(PyTypeObject* (*)(void)) NativeInternal_API[17])
53+
#define WriteBuffer_type_internal (*(PyTypeObject* (*)(void)) NativeInternal_API[18])
5054

5155
static int
5256
import_librt_internal(void)
@@ -67,4 +71,13 @@ import_librt_internal(void)
6771
}
6872

6973
#endif
74+
75+
static inline bool CPyReadBuffer_Check(PyObject *obj) {
76+
return Py_TYPE(obj) == ReadBuffer_type_internal();
77+
}
78+
79+
static inline bool CPyWriteBuffer_Check(PyObject *obj) {
80+
return Py_TYPE(obj) == WriteBuffer_type_internal();
81+
}
82+
7083
#endif // LIBRT_INTERNAL_H

0 commit comments

Comments
 (0)