this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "api-handle.h"
3#include "runtime.h"
4
5namespace py {
6
7PY_EXPORT int PyMethod_Check_Func(PyObject* obj) {
8 return ApiHandle::asObject(ApiHandle::fromPyObject(obj)).isBoundMethod();
9}
10
11PY_EXPORT int PyInstanceMethod_Check(PyObject* obj) {
12 Thread* thread = Thread::current();
13 HandleScope scope(thread);
14 Object obj_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
15 return obj_obj.isInstanceMethod();
16}
17
18PY_EXPORT PyObject* PyInstanceMethod_GET_FUNCTION_Func(PyObject* obj) {
19 return ApiHandle::borrowedReference(
20 Thread::current()->runtime(),
21 InstanceMethod::cast(ApiHandle::asObject(ApiHandle::fromPyObject(obj)))
22 .function());
23}
24
25PY_EXPORT PyObject* PyInstanceMethod_New(PyObject* obj) {
26 Thread* thread = Thread::current();
27 HandleScope scope(thread);
28 Object callable(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
29 Runtime* runtime = thread->runtime();
30 InstanceMethod method(&scope,
31 runtime->newInstanceWithSize(LayoutId::kInstanceMethod,
32 InstanceMethod::kSize));
33 method.setFunction(*callable);
34 return ApiHandle::newReference(runtime, *method);
35}
36
37PY_EXPORT PyObject* PyMethod_Function(PyObject* obj) {
38 Thread* thread = Thread::current();
39 HandleScope scope(thread);
40 Object method(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
41 if (!method.isBoundMethod()) {
42 thread->raiseBadInternalCall();
43 return nullptr;
44 }
45 return ApiHandle::borrowedReference(thread->runtime(),
46 BoundMethod::cast(*method).function());
47}
48
49PY_EXPORT PyObject* PyMethod_GET_FUNCTION_Func(PyObject* obj) {
50 return ApiHandle::borrowedReference(
51 Thread::current()->runtime(),
52 BoundMethod::cast(ApiHandle::asObject(ApiHandle::fromPyObject(obj))).function());
53}
54
55PY_EXPORT PyObject* PyMethod_New(PyObject* callable, PyObject* self) {
56 DCHECK(callable != nullptr, "callable must be initialized");
57 Thread* thread = Thread::current();
58 if (self == nullptr) {
59 thread->raiseBadInternalCall();
60 return nullptr;
61 }
62 HandleScope scope(thread);
63 Object callable_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(callable)));
64 Object self_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(self)));
65 Runtime* runtime = thread->runtime();
66 return ApiHandle::newReferenceWithManaged(
67 runtime, runtime->newBoundMethod(callable_obj, self_obj));
68}
69
70PY_EXPORT PyObject* PyMethod_Self(PyObject* obj) {
71 Thread* thread = Thread::current();
72 HandleScope scope(thread);
73 Object method(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
74 if (!method.isBoundMethod()) {
75 thread->raiseBadInternalCall();
76 return nullptr;
77 }
78 return ApiHandle::borrowedReference(thread->runtime(),
79 BoundMethod::cast(*method).self());
80}
81
82PY_EXPORT PyObject* PyMethod_GET_SELF_Func(PyObject* obj) {
83 return ApiHandle::borrowedReference(
84 Thread::current()->runtime(),
85 BoundMethod::cast(ApiHandle::asObject(ApiHandle::fromPyObject(obj))).self());
86}
87
88PY_EXPORT PyTypeObject* PyMethod_Type_Ptr() {
89 Runtime* runtime = Thread::current()->runtime();
90 return reinterpret_cast<PyTypeObject*>(ApiHandle::borrowedReference(
91 runtime, runtime->typeAt(LayoutId::kBoundMethod)));
92}
93
94} // namespace py