this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "api-handle.h"
3#include "function-utils.h"
4#include "runtime.h"
5
6namespace py {
7
8PY_EXPORT int PyCFunction_Check_Func(PyObject* obj) {
9 return !getExtensionFunction(ApiHandle::asObject(ApiHandle::fromPyObject(obj)))
10 .isErrorNotFound();
11}
12
13PY_EXPORT PyObject* PyCFunction_New(PyMethodDef* method, PyObject* self) {
14 return PyCFunction_NewEx(method, self, nullptr);
15}
16
17PY_EXPORT PyObject* PyCFunction_NewEx(PyMethodDef* method, PyObject* self,
18 PyObject* module_name) {
19 Thread* thread = Thread::current();
20 HandleScope scope(thread);
21 Object name(&scope, Runtime::internStrFromCStr(thread, method->ml_name));
22 Object self_obj(&scope, self == nullptr
23 ? Unbound::object()
24 : ApiHandle::asObject(ApiHandle::fromPyObject(self)));
25 Object module_name_obj(&scope,
26 module_name != nullptr
27 ? ApiHandle::asObject(ApiHandle::fromPyObject(module_name))
28 : NoneType::object());
29 return ApiHandle::newReferenceWithManaged(
30 thread->runtime(),
31 newCFunction(thread, method, name, self_obj, module_name_obj));
32}
33
34PY_EXPORT int PyCFunction_GetFlags(PyObject* /* p */) {
35 UNIMPLEMENTED("PyCFunction_GetFlags");
36}
37
38PY_EXPORT PyCFunction PyCFunction_GetFunction(PyObject* obj) {
39 Thread* thread = Thread::current();
40 HandleScope scope(thread);
41 Object function(
42 &scope, getExtensionFunction(ApiHandle::asObject(ApiHandle::fromPyObject(obj))));
43 if (function.isErrorNotFound()) {
44 thread->raiseBadInternalCall();
45 return nullptr;
46 }
47 return reinterpret_cast<PyCFunction>(
48 Int::cast(Function::cast(*function).code()).asCPtr());
49}
50
51PY_EXPORT PyObject* PyCFunction_GetSelf(PyObject* obj) {
52 Thread* thread = Thread::current();
53 HandleScope scope(thread);
54 Object bound_method(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
55 Object function(&scope, getExtensionFunction(*bound_method));
56 if (function.isErrorNotFound()) {
57 thread->raiseBadInternalCall();
58 return nullptr;
59 }
60 Object self(&scope, BoundMethod::cast(*bound_method).self());
61 if (self.isUnbound()) {
62 return nullptr;
63 }
64 return ApiHandle::borrowedReference(thread->runtime(), *self);
65}
66
67PY_EXPORT PyObject* PyCFunction_GET_SELF_Func(PyObject* obj) {
68 return PyCFunction_GetSelf(obj);
69}
70
71PY_EXPORT PyObject* PyCFunction_Call(PyObject* /* c */, PyObject* /* s */,
72 PyObject* /* s */) {
73 UNIMPLEMENTED("PyCFunction_Call");
74}
75
76} // namespace py