1 /**
2   Mirror _frameobject.h
3   */
4 module deimos.python.frameobject;
5 
6 import deimos.python.pyport;
7 import deimos.python.object;
8 import deimos.python.code;
9 import deimos.python.pystate;
10 
11 
12 extern(C):
13 // Python-header-file: Include/frameobject.h:
14 
15 /// _
16 struct PyTryBlock {
17     /** what kind of block this is */
18     int b_type;
19     /** where to jump to find handler */
20     int b_handler;
21     /** value stack level to pop to */
22     int b_level;
23 }
24 
25 /// subclass of PyVarObject
26 struct PyFrameObject {
27     mixin PyObject_VAR_HEAD;
28 
29     /** previous frame, or NULL */
30     PyFrameObject* f_back;
31     /** code segment */
32     PyCodeObject* f_code;
33     /** builtin symbol table (PyDictObject) */
34     PyObject* f_builtins;
35     /** global symbol table (PyDictObject) */
36     PyObject* f_globals;
37     /** local symbol table (any mapping) */
38     PyObject* f_locals;
39     /** points after the last local */
40     PyObject** f_valuestack;
41     /** Next free slot in f_valuestack.  Frame creation sets to f_valuestack.
42        Frame evaluation usually NULLs it, but a frame that yields sets it
43        to the current stack top. */
44     PyObject** f_stacktop;
45     /** Trace function */
46     PyObject* f_trace;
47     /** If an exception is raised in this frame, the next three are used to
48      * record the exception info (if any) originally in the thread state.  See
49      * comments before set_exc_info() -- it's not obvious.
50      * Invariant:  if _type is NULL, then so are _value and _traceback.
51      * Desired invariant:  all three are NULL, or all three are non-NULL.  That
52      * one isn't currently true, but "should be".
53      */
54     PyObject* f_exc_type;
55     /// _
56     PyObject* f_exc_value;
57     /// _
58     PyObject* f_exc_traceback;
59     /// _
60     PyThreadState* f_tstate;
61     /** Last instruction if called */
62     int f_lasti;
63     /** Call PyFrame_GetLineNumber() instead of reading this field
64        directly.  As of 2.3 f_lineno is only valid when tracing is
65        active (i.e. when f_trace is set).  At other times we use
66        PyCode_Addr2Line to calculate the line from the current
67        bytecode index. 
68 
69        Current line number 
70      */
71     int f_lineno;
72     version(Python_2_5_Or_Later){
73     }else{
74         /// Availability: 2.4
75         int f_restricted;
76     }
77     /** index in f_blockstack */
78     int f_iblock;
79     /** for try and loop blocks */
80     PyTryBlock[CO_MAXBLOCKS] f_blockstack;
81     version(Python_2_5_Or_Later){
82     }else{
83         /// Availability: 2.4
84         int f_nlocals;
85         /// ditto
86         int f_ncells;
87         /// ditto
88         int f_nfreevars;
89         /// ditto
90         int f_stacksize;
91     }
92     PyObject*[1] _f_localsplus;
93     /** locals+stack, dynamically sized */
94     PyObject** f_localsplus()() {
95         return _f_localsplus.ptr;
96     }
97 }
98 
99 /// _
100 mixin(PyAPI_DATA!"PyTypeObject PyFrame_Type");
101 
102 // D translation of C macro:
103 /// _
104 int PyFrame_Check()(PyObject* op) {
105     return Py_TYPE(op) == &PyFrame_Type;
106 }
107 version(Python_3_0_Or_Later){
108 }else version(Python_2_5_Or_Later){
109     /// Availability: 2.5, 2.6, 2.7
110     int PyFrame_IsRestricted()(PyFrameObject* f) {
111         return f.f_builtins != f.f_tstate.interp.builtins;
112     }
113 }
114 
115 /// _
116 PyFrameObject* PyFrame_New(PyThreadState*, PyCodeObject*,
117         PyObject*, PyObject*);
118 
119 /** Block management functions */
120 void PyFrame_BlockSetup(PyFrameObject*, int, int, int);
121 /// ditto
122 PyTryBlock* PyFrame_BlockPop(PyFrameObject*);
123 /** Extend the value stack */
124 PyObject** PyFrame_ExtendStack(PyFrameObject*, int, int);
125 
126 /** Conversions between "fast locals" and locals in dictionary */
127 void PyFrame_LocalsToFast(PyFrameObject*, int);
128 /// ditto
129 void PyFrame_FastToLocals(PyFrameObject*);
130 version(Python_2_6_Or_Later) {
131     /// Availability: >= 2.6
132     int PyFrame_ClearFreeList();
133 }
134 version(Python_2_7_Or_Later) {
135     /** Return the line of code the frame is currently executing. */
136     /// Availability: >= 2.7
137     int PyFrame_GetLineNumber(PyFrameObject*);
138 }
139 
140