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#include "traceback-builtins.h"
5
6namespace py {
7
8struct PyFrameObject;
9
10PY_EXPORT int PyTraceBack_Check_Func(PyObject* obj) {
11 return ApiHandle::asObject(ApiHandle::fromPyObject(obj)).isTraceback();
12}
13
14PY_EXPORT int PyTraceBack_Here(PyFrameObject* frame) {
15 Thread* thread = Thread::current();
16 HandleScope scope(thread);
17 FrameProxy proxy(
18 &scope,
19 ApiHandle::asObject(ApiHandle::fromPyObject(reinterpret_cast<PyObject*>(frame))));
20 Traceback new_tb(&scope, thread->runtime()->newTraceback());
21 new_tb.setFunction(proxy.function());
22 new_tb.setLasti(proxy.lasti());
23 new_tb.setNext(thread->pendingExceptionTraceback());
24 thread->setPendingExceptionTraceback(*new_tb);
25 return 0;
26}
27
28PY_EXPORT int PyTraceBack_Print(PyObject* traceback, PyObject* file) {
29 if (traceback == nullptr) {
30 return 0;
31 }
32
33 Thread* thread = Thread::current();
34 HandleScope scope(thread);
35 Object tb_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(traceback)));
36 if (!tb_obj.isTraceback()) {
37 thread->raiseBadInternalCall();
38 return -1;
39 }
40
41 Traceback tb(&scope, *tb_obj);
42 Object file_obj(&scope, ApiHandle::asObject(ApiHandle::fromPyObject(file)));
43 return tracebackWrite(thread, tb, file_obj).isErrorException() ? -1 : 0;
44}
45
46PY_EXPORT void _PyTraceback_Add(const char* funcname, const char* filename,
47 int lineno) {
48 Thread* thread = Thread::current();
49 HandleScope scope(thread);
50 Runtime* runtime = thread->runtime();
51 Object empty_bytes(&scope, Bytes::empty());
52 Object empty_tuple(&scope, runtime->emptyTuple());
53 Object filename_obj(&scope, Runtime::internStrFromCStr(thread, filename));
54 Object name_obj(&scope, Runtime::internStrFromCStr(thread, funcname));
55 Code code(&scope, runtime->newCode(/*argcount=*/0,
56 /*posonlyargcount=*/0,
57 /*kwonlyargcount=*/0,
58 /*nlocals=*/0,
59 /*stacksize=*/0,
60 /*flags=*/0,
61 /*code=*/empty_bytes,
62 /*consts=*/empty_tuple,
63 /*names=*/empty_tuple,
64 /*varnames=*/empty_tuple,
65 /*freevars=*/empty_tuple,
66 /*cellvars=*/empty_tuple,
67 /*filename=*/filename_obj,
68 /*name=*/name_obj,
69 /*firstlineno=*/lineno,
70 /*lnotab=*/empty_bytes));
71 Object module(&scope, runtime->findModuleById(ID(builtins)));
72
73 Traceback new_tb(&scope, runtime->newTraceback());
74 new_tb.setFunction(
75 runtime->newFunctionWithCode(thread, name_obj, code, module));
76 new_tb.setLineno(SmallInt::fromWord(lineno));
77 new_tb.setNext(thread->pendingExceptionTraceback());
78 thread->setPendingExceptionTraceback(*new_tb);
79}
80
81} // namespace py