this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "cpython-data.h"
3
4#include "api-handle.h"
5#include "runtime.h"
6
7namespace py {
8
9PY_EXPORT int PyMemoryView_Check_Func(PyObject* obj) {
10 return ApiHandle::asObject(ApiHandle::fromPyObject(obj)).isMemoryView();
11}
12
13PY_EXPORT PyObject* PyMemoryView_FromMemory(char* memory, Py_ssize_t size,
14 int flags) {
15 DCHECK(memory != nullptr, "memory must not be null");
16 DCHECK(flags == PyBUF_READ || flags == PyBUF_WRITE,
17 "flags must be either PyBUF_READ or PyBUF_WRITE");
18 Thread* thread = Thread::current();
19 HandleScope scope(thread);
20 Runtime* runtime = thread->runtime();
21 Object none(&scope, NoneType::object());
22 return ApiHandle::newReferenceWithManaged(
23 runtime,
24 runtime->newMemoryViewFromCPtr(
25 thread, none, memory, size,
26 flags == PyBUF_READ ? ReadOnly::ReadOnly : ReadOnly::ReadWrite));
27}
28
29PY_EXPORT PyObject* PyMemoryView_FromObject(PyObject* obj) {
30 Thread* thread = Thread::current();
31 HandleScope scope(thread);
32 Object object(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(obj)));
33 Object result(&scope,
34 thread->invokeFunction1(ID(builtins), ID(memoryview), object));
35 if (result.isError()) {
36 return nullptr;
37 }
38 return ApiHandle::newReferenceWithManaged(thread->runtime(), *result);
39}
40
41PY_EXPORT PyObject* PyMemoryView_GetContiguous(PyObject* /* j */, int /* e */,
42 char /* r */) {
43 UNIMPLEMENTED("PyMemoryView_GetContiguous");
44}
45
46PY_EXPORT PyTypeObject* PyMemoryView_Type_Ptr() {
47 Runtime* runtime = Thread::current()->runtime();
48 return reinterpret_cast<PyTypeObject*>(ApiHandle::borrowedReference(
49 runtime, runtime->typeAt(LayoutId::kMemoryView)));
50}
51
52} // namespace py