Skip to content

Commit 9f569d7

Browse files
committed
Add static data
1 parent 72cff30 commit 9f569d7

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

mypyc/lib-rt/init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Python.h>
22
#include "CPy.h"
3+
#include "static_data.c"
34

45
struct ExcDummyStruct _CPy_ExcDummyStruct = { PyObject_HEAD_INIT(NULL) };
56
PyObject *_CPy_ExcDummy = (PyObject *)&_CPy_ExcDummyStruct;
@@ -12,6 +13,7 @@ PyObject * __mypyc_empty_tuple__ = NULL;
1213
// other dynamic libraries. This means we need to initialize
1314
// things at load time.
1415
void CPy_Init(void) {
16+
intern_strings();
1517
_CPy_ExcDummyStruct.ob_base.ob_type = &PyBaseObject_Type;
1618

1719
// Initialize system-wide empty tuple constant

mypyc/lib-rt/pythonsupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pythoncapi_compat.h"
1212
#include <frameobject.h>
1313
#include <assert.h>
14+
#include "static_data.h"
1415
#include "mypyc_util.h"
1516

1617
#if CPY_3_13_FEATURES

mypyc/lib-rt/static_data.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef STATIC_DATA
2+
#define STATIC_DATA
3+
4+
#include "static_data.h"
5+
6+
// Adopted from numpy 2.4.0: numpy/_core/src/multiarry/npy_static_data.c
7+
8+
mypyc_interned_str_struct mypyc_interned_str;
9+
10+
#define INTERN_STRING(struct_member, string) \
11+
assert(mypyc_interned_str.struct_member == NULL); \
12+
mypyc_interned_str.struct_member = PyUnicode_InternFromString(string); \
13+
if (mypyc_interned_str.struct_member == NULL) { \
14+
return -1; \
15+
}
16+
17+
int
18+
intern_strings(void) {
19+
INTERN_STRING(__init_subclass__, "__init_subclass__");
20+
INTERN_STRING(__mro_entries__, "__mro_entries__");
21+
INTERN_STRING(__name__, "__name__");
22+
INTERN_STRING(__radd__, "__radd__");
23+
INTERN_STRING(__rsub__, "__rsub__");
24+
INTERN_STRING(__rmul__, "__rmul__");
25+
INTERN_STRING(__rtruediv__, "__rtruediv__");
26+
INTERN_STRING(__rmod__, "__rmod__");
27+
INTERN_STRING(__rdivmod__, "__rdivmod__");
28+
INTERN_STRING(__rfloordiv__, "__rfloordiv__");
29+
INTERN_STRING(__rpow__, "__rpow__");
30+
INTERN_STRING(__rmatmul__, "__rmatmul__");
31+
INTERN_STRING(__rand__, "__rand__");
32+
INTERN_STRING(__ror__, "__ror__");
33+
INTERN_STRING(__rxor__, "__rxor__");
34+
INTERN_STRING(__rlshift__, "__rlshift__");
35+
INTERN_STRING(__rrshift__, "__rrshift__");
36+
INTERN_STRING(__eq__, "__eq__");
37+
INTERN_STRING(__ne__, "__ne__");
38+
INTERN_STRING(__gt__, "__gt__");
39+
INTERN_STRING(__le__, "__le__");
40+
INTERN_STRING(__lt__, "__lt__");
41+
INTERN_STRING(__ge__, "__ge__");
42+
INTERN_STRING(clear, "clear");
43+
INTERN_STRING(close_, "close");
44+
INTERN_STRING(copy, "copy");
45+
INTERN_STRING(keys, "keys");
46+
INTERN_STRING(items, "items");
47+
INTERN_STRING(join, "join");
48+
INTERN_STRING(send, "send");
49+
INTERN_STRING(setdefault, "setdefault");
50+
INTERN_STRING(startswith, "startswith");
51+
INTERN_STRING(throw_, "throw");
52+
INTERN_STRING(translate, "translate");
53+
INTERN_STRING(update, "update");
54+
INTERN_STRING(values, "values");
55+
return 0;
56+
}
57+
58+
#endif

mypyc/lib-rt/static_data.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef STATIC_DATA_H
2+
#define STATIC_DATA_H
3+
4+
#include <Python.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Adopted from numpy 2.4.0: numpy/_core/src/multiarry/npy_static_data.h
11+
12+
int intern_strings(void);
13+
14+
typedef struct mypyc_interned_str_struct {
15+
PyObject *__init_subclass__;
16+
PyObject *__mro_entries__;
17+
PyObject *__name__;
18+
PyObject *__radd__;
19+
PyObject *__rsub__;
20+
PyObject *__rmul__;
21+
PyObject *__rtruediv__;
22+
PyObject *__rmod__;
23+
PyObject *__rdivmod__;
24+
PyObject *__rfloordiv__;
25+
PyObject *__rpow__;
26+
PyObject *__rmatmul__;
27+
PyObject *__rand__;
28+
PyObject *__ror__;
29+
PyObject *__rxor__;
30+
PyObject *__rlshift__;
31+
PyObject *__rrshift__;
32+
PyObject *__eq__;
33+
PyObject *__ne__;
34+
PyObject *__gt__;
35+
PyObject *__le__;
36+
PyObject *__lt__;
37+
PyObject *__ge__;
38+
PyObject *clear;
39+
PyObject *close_;
40+
PyObject *copy;
41+
PyObject *keys;
42+
PyObject *items;
43+
PyObject *join;
44+
PyObject *send;
45+
PyObject *setdefault;
46+
PyObject *startswith;
47+
PyObject *throw_;
48+
PyObject *translate;
49+
PyObject *update;
50+
PyObject *values;
51+
} mypyc_interned_str_struct;
52+
53+
extern mypyc_interned_str_struct mypyc_interned_str;
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif
58+
59+
#endif

0 commit comments

Comments
 (0)