forked from holzschu/python3_ios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPython_Objects.patch
206 lines (194 loc) · 7.6 KB
/
Python_Objects.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
diff -Naur tmp/Python-3.7.1/Objects/call.c Python-3.7.1/Objects/call.c
--- tmp/Python-3.7.1/Objects/call.c 2018-10-20 08:04:19.000000000 +0200
+++ Python-3.7.1/Objects/call.c 2019-06-03 17:55:17.000000000 +0200
@@ -2,7 +2,6 @@
#include "internal/pystate.h"
#include "frameobject.h"
-
int
_PyObject_HasFastCall(PyObject *callable)
{
@@ -587,6 +586,7 @@
}
+
PyObject *
_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self,
PyObject *const *args, Py_ssize_t nargs,
diff -Naur tmp/Python-3.7.1/Objects/methodobject.c Python-3.7.1/Objects/methodobject.c
--- tmp/Python-3.7.1/Objects/methodobject.c 2018-10-20 08:04:19.000000000 +0200
+++ Python-3.7.1/Objects/methodobject.c 2019-05-02 17:38:52.000000000 +0200
@@ -176,6 +176,10 @@
static int
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
{
+ // iOS: module cleanup in PyImport_Cleanup has left some modules with negative ref counts.
+ if ((m->m_self != NULL) && (m->m_self->ob_refcnt < 0)) m->m_self = NULL;
+ if ((m->m_module != NULL) && (m->m_module->ob_refcnt < 0)) m->m_module = NULL;
+ //
Py_VISIT(m->m_self);
Py_VISIT(m->m_module);
return 0;
diff -Naur tmp/Python-3.7.1/Objects/moduleobject.c Python-3.7.1/Objects/moduleobject.c
--- tmp/Python-3.7.1/Objects/moduleobject.c 2018-10-20 08:04:19.000000000 +0200
+++ Python-3.7.1/Objects/moduleobject.c 2019-06-06 15:15:14.000000000 +0200
@@ -187,6 +187,7 @@
if (!PyModuleDef_Init(module))
return NULL;
name = module->m_name;
+
if (!check_api_version(name, module_api_version)) {
return NULL;
}
@@ -214,7 +215,7 @@
}
if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
return NULL;
-
+
if (module->m_size > 0) {
m->md_state = PyMem_MALLOC(module->m_size);
if (!m->md_state) {
@@ -572,9 +573,30 @@
void
_PyModule_Clear(PyObject *m)
{
+#if TARGET_OS_IPHONE
+ // Modules created with Cython + PEP489 have strange issues with number of references
+ // which prevent the freefunc function from being called.
+ // We explicitly call the freefunc function here, before the dictionary is cleared.
+ // (otherwise, "name" is gone)
+ int moduleNeedsCleanup = 0;
+ PyModuleObject *mod = (PyModuleObject *)m;
+ const char* utf8name = PyUnicode_AsUTF8(mod->md_name);
+ if ((strncmp(utf8name, "pandas.", 7) == 0) || (strncmp(utf8name, "numpy.", 6) == 0)) {
+ fprintf(stderr, "Module = %x name = %s refCount = %zd ", mod, utf8name, m->ob_refcnt);
+ if (mod->md_def && mod->md_def->m_free) {
+ fprintf(stderr, "module has a free function: %x", mod->md_def->m_free);
+ moduleNeedsCleanup = 1;
+ }
+ fprintf(stderr, "\n");
+ }
+#endif
PyObject *d = ((PyModuleObject *)m)->md_dict;
if (d != NULL)
_PyModule_ClearDict(d);
+#if TARGET_OS_IPHONE
+ // Cleanup module after clearing dictionary:
+ if (moduleNeedsCleanup) mod->md_def->m_free(mod);
+#endif
}
void
@@ -679,12 +701,14 @@
}
if (m->md_weaklist != NULL)
PyObject_ClearWeakRefs((PyObject *) m);
- if (m->md_def && m->md_def->m_free)
+ if (m->md_def && m->md_def->m_free) {
m->md_def->m_free(m);
+ }
Py_XDECREF(m->md_dict);
Py_XDECREF(m->md_name);
if (m->md_state != NULL)
PyMem_FREE(m->md_state);
+ // This call module->tp_free.
Py_TYPE(m)->tp_free((PyObject *)m);
}
@@ -716,8 +740,13 @@
_Py_IDENTIFIER(__name__);
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
if (mod_name && PyUnicode_Check(mod_name)) {
- PyErr_Format(PyExc_AttributeError,
- "module '%U' has no attribute '%U'", mod_name, name);
+ if (!getattr) {
+ PyErr_Format(PyExc_AttributeError,
+ "module '%U' has no attribute __getattr__", mod_name);
+ } else {
+ PyErr_Format(PyExc_AttributeError,
+ "module '%U' has no attribute '%U'", mod_name, name);
+ }
return NULL;
}
}
diff -Naur tmp/Python-3.7.1/Objects/typeobject.c Python-3.7.1/Objects/typeobject.c
--- tmp/Python-3.7.1/Objects/typeobject.c 2018-10-20 08:04:19.000000000 +0200
+++ Python-3.7.1/Objects/typeobject.c 2019-05-02 09:26:44.000000000 +0200
@@ -1253,7 +1253,7 @@
if (PyType_IS_GC(base))
_PyObject_GC_TRACK(self);
assert(basedealloc);
- basedealloc(self);
+ basedealloc(self);
/* Can't reference self beyond this point. It's possible tp_del switched
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
@@ -4959,11 +4959,18 @@
/* This won't inherit indirect slots (from tp_as_number etc.)
if type doesn't provide the space. */
-
+ if (Py_VerboseFlag) {
+ fprintf(stderr, "type->tp_as_number = %x base->tp_as_number = %x \n", type->tp_as_number, base->tp_as_number);
+ }
if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
basebase = base->tp_base;
if (basebase->tp_as_number == NULL)
basebase = NULL;
+ if (Py_VerboseFlag) {
+ fprintf(stderr, "basebase = %x ", basebase);
+ if (basebase != NULL) fprintf(stderr, "basebase>tp_as_number = %x \n", basebase->tp_as_number);
+ else fprintf(stderr, "\n");
+ }
COPYNUM(nb_add);
COPYNUM(nb_subtract);
COPYNUM(nb_multiply);
@@ -5001,6 +5008,7 @@
COPYNUM(nb_inplace_matrix_multiply);
}
+ if (Py_VerboseFlag) fprintf(stderr, "Inside inherit_slots, after COPYNUM: PyUnicode_Type.tp_as_number.nb_bool = %x \n", (unsigned int) PyUnicode_Type.tp_as_number->nb_bool);
if (type->tp_as_async != NULL && base->tp_as_async != NULL) {
basebase = base->tp_base;
if (basebase->tp_as_async == NULL)
@@ -5106,6 +5114,18 @@
static int add_operators(PyTypeObject *);
+#if TARGET_OS_IPHONE
+// to reset PyTypeObject parameters at cleanup (required for Cython):
+void PyType_Reset(PyTypeObject* pt) {
+ // Need to decrement the tp_dict of the typeObjects too (unless it's NULL):
+ Py_XDECREF(pt->tp_dict);
+ pt->tp_dict = 0;
+ // TODO: should I call PyDict_Cleanup on the typeObjects tp_dict?
+ // And reset the TP_FLAGS_READY flag:
+ pt->tp_flags &= !Py_TPFLAGS_READY;
+}
+#endif
+
int
PyType_Ready(PyTypeObject *type)
{
@@ -5208,17 +5228,22 @@
if (type->tp_base != NULL)
inherit_special(type, type->tp_base);
+ if (Py_VerboseFlag) fprintf(stderr, "Inside PyTypeReady, after inherit_special: PyUnicode_Type.tp_as_number.nb_bool = %x \n", (unsigned int) PyUnicode_Type.tp_as_number->nb_bool); // Not yet
+
/* Initialize tp_dict properly */
bases = type->tp_mro;
assert(bases != NULL);
assert(PyTuple_Check(bases));
n = PyTuple_GET_SIZE(bases);
+ if (Py_VerboseFlag) fprintf(stderr, "Inside PyTypeReady: before inherit_slots = %x \n", (unsigned int) PyUnicode_Type.tp_as_number->nb_bool); // done
for (i = 1; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i);
if (PyType_Check(b))
inherit_slots(type, (PyTypeObject *)b);
}
+ if (Py_VerboseFlag) fprintf(stderr, "Inside PyTypeReady: after inherit_slots = %x \n", (unsigned int) PyUnicode_Type.tp_as_number->nb_bool); // done
+
/* All bases of statically allocated type should be statically allocated */
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
for (i = 0; i < n; i++) {
@@ -5281,7 +5306,6 @@
type->tp_hash = PyObject_HashNotImplemented;
}
}
-
/* Some more special stuff */
base = type->tp_base;
if (base != NULL) {