this repo has no description
at trunk 46 lines 1.8 kB view raw
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 PyObject* PySeqIter_New(PyObject* seq) { 8 Thread* thread = Thread::current(); 9 HandleScope scope(thread); 10 Object seq_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(seq))); 11 Runtime* runtime = thread->runtime(); 12 if (!runtime->isSequence(thread, seq_obj)) { 13 thread->raiseBadInternalCall(); 14 return nullptr; 15 } 16 return ApiHandle::newReferenceWithManaged(runtime, 17 runtime->newSeqIterator(seq_obj)); 18} 19 20PY_EXPORT PyTypeObject* PySeqIter_Type_Ptr() { 21 Runtime* runtime = Thread::current()->runtime(); 22 return reinterpret_cast<PyTypeObject*>(ApiHandle::borrowedReference( 23 runtime, runtime->typeAt(LayoutId::kSeqIterator))); 24} 25 26PY_EXPORT PyObject* PyCallIter_New(PyObject* pycallable, PyObject* pysentinel) { 27 Thread* thread = Thread::current(); 28 HandleScope scope(thread); 29 Object callable(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(pycallable))); 30 Object sentinel(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(pysentinel))); 31 Object result(&scope, 32 thread->invokeFunction2(ID(builtins), ID(callable_iterator), 33 callable, sentinel)); 34 if (result.isErrorException()) return nullptr; 35 return ApiHandle::newReferenceWithManaged(thread->runtime(), *result); 36} 37 38PY_EXPORT int PyIter_Check_Func(PyObject* iter) { 39 DCHECK(iter != nullptr, "expected iter to be non-null"); 40 Thread* thread = Thread::current(); 41 HandleScope scope(thread); 42 Object iterator(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(iter))); 43 return thread->runtime()->isIterator(thread, iterator); 44} 45 46} // namespace py