this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "cpython-func.h"
3
4#include "api-handle.h"
5#include "capi-state.h"
6#include "module-builtins.h"
7#include "modules.h"
8#include "runtime.h"
9
10namespace py {
11
12PY_EXPORT int PyGILState_Check() {
13 // TODO(T44861733): Make this do something intelligent
14 CHECK(Thread::current()->runtime()->mainThread()->next() == nullptr,
15 "PyGILState_Check doesn't currently work with more than one thread");
16 return 1;
17}
18
19PY_EXPORT PyGILState_STATE PyGILState_Ensure() {
20 // TODO(T44861733): Make this do something intelligent
21 return PyGILState_LOCKED;
22}
23
24PY_EXPORT PyThreadState* PyGILState_GetThisThreadState() {
25 UNIMPLEMENTED("PyGILState_GetThisThreadState");
26}
27
28PY_EXPORT void PyGILState_Release(PyGILState_STATE /* e */) {
29 // TODO(T44861733): Make this do something intelligent
30}
31
32PY_EXPORT void PyInterpreterState_Clear(PyInterpreterState* /* p */) {
33 UNIMPLEMENTED("PyInterpreterState_Clear");
34}
35
36PY_EXPORT void PyInterpreterState_Delete(PyInterpreterState* /* p */) {
37 UNIMPLEMENTED("PyInterpreterState_Delete");
38}
39
40static PyObject* moduleListAt(Runtime* runtime, word index) {
41 Vector<PyObject*>* modules = capiModules(runtime);
42 if (index >= modules->size()) {
43 return nullptr;
44 }
45 return (*modules)[index];
46}
47
48static void moduleListAtPut(Runtime* runtime, word index, PyObject* module) {
49 Vector<PyObject*>* modules = capiModules(runtime);
50 modules->reserve(index + 1);
51 for (int i = index - modules->size(); i >= 0; i--) {
52 modules->push_back(nullptr);
53 }
54 (*modules)[index] = module;
55 Py_INCREF(module);
56}
57
58static int moduleListAdd(Thread* thread, PyObject* module, PyModuleDef* def) {
59 if (def->m_slots != nullptr) {
60 thread->raiseWithFmt(LayoutId::kSystemError,
61 "PyState_AddModule called on module with slots");
62 return -1;
63 }
64 HandleScope scope(thread);
65 Runtime* runtime = thread->runtime();
66 Module module_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(module)));
67 module_obj.setDef(runtime->newIntFromCPtr(def));
68 moduleListAtPut(runtime, def->m_base.m_index, module);
69 return 0;
70}
71
72PY_EXPORT int PyState_AddModule(PyObject* module, PyModuleDef* def) {
73 DCHECK(module != nullptr, "module must not be null");
74 if (def == nullptr) {
75 Py_FatalError("PyState_AddModule: Module Definition is NULL");
76 return -1;
77 }
78 Thread* thread = Thread::current();
79 Runtime* runtime = thread->runtime();
80 if (moduleListAt(runtime, def->m_base.m_index) != nullptr) {
81 Py_FatalError("PyState_AddModule: Module already added!");
82 return -1;
83 }
84 return moduleListAdd(thread, module, def);
85}
86
87PY_EXPORT PyObject* PyState_FindModule(PyModuleDef* def) {
88 if (def->m_slots != nullptr) {
89 return nullptr;
90 }
91 return moduleListAt(Thread::current()->runtime(), def->m_base.m_index);
92}
93
94PY_EXPORT int PyState_RemoveModule(PyModuleDef* /* f */) {
95 UNIMPLEMENTED("PyState_RemoveModule");
96}
97
98PY_EXPORT void PyThreadState_Clear(PyThreadState* /* e */) {
99 UNIMPLEMENTED("PyThreadState_Clear");
100}
101
102PY_EXPORT void PyThreadState_Delete(PyThreadState* /* e */) {
103 UNIMPLEMENTED("PyThreadState_Delete");
104}
105
106PY_EXPORT void PyThreadState_DeleteCurrent() {
107 UNIMPLEMENTED("PyThreadState_DeleteCurrent");
108}
109
110PY_EXPORT PyThreadState* PyThreadState_Get() {
111 return reinterpret_cast<PyThreadState*>(Thread::current());
112}
113
114PY_EXPORT PyObject* PyThreadState_GetDict() {
115 UNIMPLEMENTED("PyThreadState_GetDict");
116}
117
118PY_EXPORT PyThreadState* PyThreadState_New(PyInterpreterState* /* p */) {
119 UNIMPLEMENTED("PyThreadState_New");
120}
121
122PY_EXPORT PyThreadState* PyThreadState_Next(PyThreadState* /* p */) {
123 UNIMPLEMENTED("PyThreadState_Next");
124}
125
126PY_EXPORT int PyThreadState_SetAsyncExc(unsigned long /* d */,
127 PyObject* /* c */) {
128 UNIMPLEMENTED("PyThreadState_SetAsyncExc");
129}
130
131PY_EXPORT PyThreadState* PyThreadState_Swap(PyThreadState* /* s */) {
132 UNIMPLEMENTED("PyThreadState_Swap");
133}
134
135PY_EXPORT void _PyGILState_Reinit(_PyRuntimeState*) {
136 // TODO(T39596544): do nothing until we have a GIL.
137}
138
139PY_EXPORT void _PyInterpreterState_DeleteExceptMain(_PyRuntimeState*) {
140 // TODO(T87097565): Implement instead of making it a noop
141}
142
143PY_EXPORT void _PyRuntimeState_ReInitThreads(_PyRuntimeState*) {
144 // TODO(T87097565): Implement instead of making it a noop
145}
146
147PY_EXPORT int _PyState_AddModule(PyObject* module, PyModuleDef* def) {
148 Thread* thread = Thread::current();
149 if (def == nullptr) {
150 DCHECK(thread->hasPendingException(), "expected raised error");
151 return -1;
152 }
153 return moduleListAdd(thread, module, def);
154}
155
156PY_EXPORT PyThreadState* _PyThreadState_GET_Func(void) {
157 return reinterpret_cast<PyThreadState*>(Thread::current());
158}
159
160PY_EXPORT void _PyThreadState_Init(_PyRuntimeState*, PyThreadState* /* e */) {
161 UNIMPLEMENTED("_PyThreadState_Init");
162}
163
164PY_EXPORT PyThreadState* _PyThreadState_Prealloc(PyInterpreterState* /* p */) {
165 UNIMPLEMENTED("_PyThreadState_Prealloc");
166}
167
168PY_EXPORT PyInterpreterState* PyInterpreterState_Head() {
169 UNIMPLEMENTED("PyInterpreterState_Head");
170}
171
172PY_EXPORT PyInterpreterState* PyInterpreterState_Main() {
173 return reinterpret_cast<PyInterpreterState*>(Thread::current()->runtime());
174}
175
176PY_EXPORT PyInterpreterState* PyInterpreterState_Next(
177 PyInterpreterState* /* p */) {
178 UNIMPLEMENTED("PyInterpreterState_Next");
179}
180
181PY_EXPORT PyThreadState* PyInterpreterState_ThreadHead(
182 PyInterpreterState* /* p */) {
183 UNIMPLEMENTED("PyInterpreterState_ThreadHead");
184}
185
186PY_EXPORT PyInterpreterState* _PyInterpreterState_Get(void) {
187 return reinterpret_cast<PyInterpreterState*>(Thread::current()->runtime());
188}
189
190PY_EXPORT _PyRuntimeState* _PyRuntime_Ptr() {
191 return reinterpret_cast<_PyRuntimeState*>(Thread::current()->runtime());
192}
193
194PY_EXPORT void _PyState_ClearModules() {
195 UNIMPLEMENTED("_PyState_ClearModules");
196}
197
198PY_EXPORT int _PyThreadState_GetRecursionDepth(PyThreadState* ts) {
199 Thread* thread = reinterpret_cast<Thread*>(ts);
200 return thread->recursionDepth();
201}
202
203} // namespace py