Commit e2b4302a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Move some more stuff from capi.cpp to capi/

I guess our goal should be to eventually get rid of this directory -- this stuff
should mostly be just taken from CPython, and we could/should use their directory
names for that.  But for now it feels better than putting it all into a single file.
parent 787c7d7c
// Copyright (c) 2014 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Hopefully soon we will be able to switch to CPython's modsupport.c, instead of having
// to reimplement it. For now, it's easier to create simple versions of the functions
// instead of trying to support all of the internals of modsupport.c
#include <dlfcn.h>
#include <stdarg.h>
#include <string.h>
#include "Python.h"
#include "capi/types.h"
#include "codegen/compvars.h"
#include "core/threading.h"
#include "core/types.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
namespace pyston {
#define FLAG_SIZE_T 1
extern "C" PyObject* _Py_BuildValue_SizeT(const char* arg0, ...) {
RELEASE_ASSERT(*arg0 == '\0', "");
return None;
}
extern "C" PyObject* Py_BuildValue(const char* arg0, ...) {
RELEASE_ASSERT(*arg0 == '\0', "");
return None;
}
extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, const char* doc, PyObject* self,
int apiver) {
BoxedModule* module = createModule(name, "__builtin__");
Box* passthrough = static_cast<Box*>(self);
if (!passthrough)
passthrough = None;
while (methods->ml_name) {
if (VERBOSITY())
printf("Loading method %s\n", methods->ml_name);
assert((methods->ml_flags & (~(METH_VARARGS | METH_KEYWORDS))) == 0);
module->giveAttr(methods->ml_name,
new BoxedCApiFunction(methods->ml_flags, passthrough, methods->ml_name, methods->ml_meth));
methods++;
}
if (doc) {
module->setattr("__doc__", boxStrConstant(doc), NULL);
}
return module;
}
extern "C" int PyModule_AddIntConstant(PyObject* _m, const char* name, long value) {
BoxedModule* m = static_cast<BoxedModule*>(_m);
assert(m->cls == module_cls);
m->setattr(name, boxInt(value), NULL);
return 0;
}
} // namespace pyston
// Copyright (c) 2014 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PYSTON_CAPI_TYPES_H
#define PYSTON_CAPI_TYPES_H
#include "runtime/types.h"
namespace pyston {
extern BoxedClass* capifunc_cls;
class BoxedCApiFunction : public Box {
private:
int ml_flags;
Box* passthrough;
const char* name;
PyCFunction func;
public:
BoxedCApiFunction(int ml_flags, Box* passthrough, const char* name, PyCFunction func)
: Box(capifunc_cls), ml_flags(ml_flags), passthrough(passthrough), name(name), func(func) {}
static BoxedString* __repr__(BoxedCApiFunction* self) {
assert(self->cls == capifunc_cls);
return boxStrConstant(self->name);
}
static Box* __call__(BoxedCApiFunction* self, BoxedTuple* varargs, BoxedDict* kwargs) {
assert(self->cls == capifunc_cls);
assert(varargs->cls == tuple_cls);
assert(kwargs->cls == dict_cls);
threading::GLPromoteRegion _gil_lock;
Box* rtn;
if (self->ml_flags == METH_VARARGS) {
assert(kwargs->d.size() == 0);
rtn = (Box*)self->func(self->passthrough, varargs);
} else if (self->ml_flags == (METH_VARARGS | METH_KEYWORDS)) {
rtn = (Box*)((PyCFunctionWithKeywords)self->func)(self->passthrough, varargs, kwargs);
} else {
RELEASE_ASSERT(0, "0x%x", self->ml_flags);
}
assert(rtn);
return rtn;
}
};
}
#endif
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "Python.h" #include "Python.h"
#include "capi/types.h"
#include "codegen/compvars.h" #include "codegen/compvars.h"
#include "core/threading.h" #include "core/threading.h"
#include "core/types.h" #include "core/types.h"
...@@ -66,14 +67,6 @@ extern "C" PyObject* PyModule_GetDict(PyObject* _m) { ...@@ -66,14 +67,6 @@ extern "C" PyObject* PyModule_GetDict(PyObject* _m) {
return makeAttrWrapper(m); return makeAttrWrapper(m);
} }
extern "C" int PyModule_AddIntConstant(PyObject* _m, const char* name, long value) {
BoxedModule* m = static_cast<BoxedModule*>(_m);
assert(m->cls == module_cls);
m->setattr(name, boxInt(value), NULL);
return 0;
}
#define MAKE_CHECK(NAME, cls_name) \ #define MAKE_CHECK(NAME, cls_name) \
extern "C" bool Py##NAME##_Check(PyObject* op) { return isSubclass(op->cls, cls_name); } extern "C" bool Py##NAME##_Check(PyObject* op) { return isSubclass(op->cls, cls_name); }
...@@ -144,68 +137,6 @@ extern "C" int PyDict_SetItemString(PyObject* mp, const char* key, PyObject* ite ...@@ -144,68 +137,6 @@ extern "C" int PyDict_SetItemString(PyObject* mp, const char* key, PyObject* ite
BoxedClass* capifunc_cls; BoxedClass* capifunc_cls;
class BoxedCApiFunction : public Box {
private:
int ml_flags;
Box* passthrough;
const char* name;
PyCFunction func;
public:
BoxedCApiFunction(int ml_flags, Box* passthrough, const char* name, PyCFunction func)
: Box(capifunc_cls), ml_flags(ml_flags), passthrough(passthrough), name(name), func(func) {}
static BoxedString* __repr__(BoxedCApiFunction* self) {
assert(self->cls == capifunc_cls);
return boxStrConstant(self->name);
}
static Box* __call__(BoxedCApiFunction* self, BoxedTuple* varargs, BoxedDict* kwargs) {
assert(self->cls == capifunc_cls);
assert(varargs->cls == tuple_cls);
assert(kwargs->cls == dict_cls);
threading::GLPromoteRegion _gil_lock;
Box* rtn;
if (self->ml_flags == METH_VARARGS) {
assert(kwargs->d.size() == 0);
rtn = (Box*)self->func(self->passthrough, varargs);
} else if (self->ml_flags == (METH_VARARGS | METH_KEYWORDS)) {
rtn = (Box*)((PyCFunctionWithKeywords)self->func)(self->passthrough, varargs, kwargs);
} else {
RELEASE_ASSERT(0, "0x%x", self->ml_flags);
}
assert(rtn);
return rtn;
}
};
extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, const char* doc, PyObject* self,
int apiver) {
BoxedModule* module = createModule(name, "__builtin__");
Box* passthrough = static_cast<Box*>(self);
if (!passthrough)
passthrough = None;
while (methods->ml_name) {
if (VERBOSITY())
printf("Loading method %s\n", methods->ml_name);
assert((methods->ml_flags & (~(METH_VARARGS | METH_KEYWORDS))) == 0);
module->giveAttr(methods->ml_name,
new BoxedCApiFunction(methods->ml_flags, passthrough, methods->ml_name, methods->ml_meth));
methods++;
}
if (doc) {
module->setattr("__doc__", boxStrConstant(doc), NULL);
}
return module;
}
extern "C" void conservativeGCHandler(GCVisitor* v, Box* b) { extern "C" void conservativeGCHandler(GCVisitor* v, Box* b) {
v->visitPotentialRange((void* const*)b, (void* const*)((char*)b + b->cls->tp_basicsize)); v->visitPotentialRange((void* const*)b, (void* const*)((char*)b + b->cls->tp_basicsize));
...@@ -291,11 +222,6 @@ extern "C" int PyType_Ready(PyTypeObject* cls) { ...@@ -291,11 +222,6 @@ extern "C" int PyType_Ready(PyTypeObject* cls) {
return 0; return 0;
} }
extern "C" PyObject* Py_BuildValue(const char* arg0, ...) {
assert(*arg0 == '\0');
return None;
}
// copied from CPython's getargs.c: // copied from CPython's getargs.c:
extern "C" int PyBuffer_FillInfo(Py_buffer* view, PyObject* obj, void* buf, Py_ssize_t len, int readonly, int flags) { extern "C" int PyBuffer_FillInfo(Py_buffer* view, PyObject* obj, void* buf, Py_ssize_t len, int readonly, int flags) {
if (view == NULL) if (view == NULL)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment