1 /** 2 Mirror _funcobject.h 3 4 Function object interface 5 6 * Function objects and code objects should not be confused with each other: 7 * 8 * Function objects are created by the execution of the 'def' statement. 9 * They reference a code object in their func_code attribute, which is a 10 * purely syntactic object, i.e. nothing more than a compiled version of some 11 * source code lines. There is one code object per source code "fragment", 12 * but each code object can be referenced by zero or many function objects 13 * depending only on how many times the 'def' statement in the source was 14 * executed so far. 15 */ 16 module deimos.python.funcobject; 17 18 import deimos.python.pyport; 19 import deimos.python.object; 20 21 extern(C): 22 // Python-header-file: Include/funcobject.h: 23 24 /** subclass of PyObject 25 * 26 * Invariant: 27 * func_closure contains the bindings for func_code->co_freevars, so 28 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code) 29 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0). 30 */ 31 struct PyFunctionObject { 32 mixin PyObject_HEAD; 33 /** A code object */ 34 PyObject* func_code; 35 /** A dictionary (other mappings won't do) */ 36 PyObject* func_globals; 37 /** NULL or a tuple */ 38 PyObject* func_defaults; 39 version(Python_3_0_Or_Later) { 40 /** NULL or a dict */ 41 /// Availability: 3.* 42 PyObject* func_kwdefaults; 43 } 44 /** NULL or a tuple of cell objects */ 45 PyObject* func_closure; 46 /** The __doc__ attribute, can be anything */ 47 PyObject* func_doc; 48 /** The __name__ attribute, a string object */ 49 PyObject* func_name; 50 /** The __dict__ attribute, a dict or NULL */ 51 PyObject* func_dict; 52 /** List of weak references */ 53 PyObject* func_weakreflist; 54 /** The __module__ attribute, can be anything */ 55 PyObject* func_module; 56 version(Python_3_0_Or_Later) { 57 /** Annotations, a dict or NULL */ 58 /// Availability: 3.* 59 PyObject* func_annotations; 60 PyObject* func_qualname; 61 } 62 63 version(Python_3_8_Or_Later) { 64 /// Availability: >= 3.8 65 vectorcallfunc vectorcall; 66 } 67 } 68 69 /// _ 70 mixin(PyAPI_DATA!"PyTypeObject PyFunction_Type"); 71 72 // D translation of C macro: 73 /// _ 74 int PyFunction_Check()(PyObject* op) { 75 return op.ob_type == &PyFunction_Type; 76 } 77 78 /// _ 79 PyObject* PyFunction_New(PyObject*, PyObject*); 80 /// _ 81 PyObject_BorrowedRef* PyFunction_GetCode(PyObject*); 82 /// _ 83 PyObject_BorrowedRef* PyFunction_GetGlobals(PyObject*); 84 /// _ 85 PyObject_BorrowedRef* PyFunction_GetModule(PyObject*); 86 /// _ 87 PyObject_BorrowedRef* PyFunction_GetDefaults(PyObject*); 88 /// _ 89 int PyFunction_SetDefaults(PyObject*, PyObject*); 90 version(Python_3_0_Or_Later) { 91 /// Availability: 3.* 92 Borrowed!PyObject* PyFunction_GetKwDefaults(PyObject*); 93 /// Availability: 3.* 94 int PyFunction_SetKwDefaults(PyObject*, PyObject*); 95 } 96 /// _ 97 PyObject_BorrowedRef* PyFunction_GetClosure(PyObject*); 98 /// _ 99 int PyFunction_SetClosure(PyObject*, PyObject*); 100 version(Python_3_0_Or_Later) { 101 /// Availability: 3.* 102 Borrowed!PyObject* PyFunction_GetAnnotations(PyObject*); 103 /// Availability: 3.* 104 int PyFunction_SetAnnotations(PyObject*, PyObject*); 105 } 106 107 /** Macros for direct access to these values. Type checks are *not* 108 done, so use with care. */ 109 Borrowed!PyObject* PyFunction_GET_CODE()(PyObject* func) { 110 return (cast(PyFunctionObject*)func).func_code; 111 } 112 /// ditto 113 Borrowed!PyObject* PyFunction_GET_GLOBALS()(PyObject* func) { 114 return (cast(PyFunctionObject*)func).func_globals; 115 } 116 /// ditto 117 Borrowed!PyObject* PyFunction_GET_MODULE()(PyObject* func) { 118 return (cast(PyFunctionObject*)func).func_module; 119 } 120 /// ditto 121 Borrowed!PyObject* PyFunction_GET_DEFAULTS()(PyObject* func) { 122 return (cast(PyFunctionObject*)func).func_defaults; 123 } 124 version(Python_3_0_Or_Later) { 125 /// Availability: 3.* 126 Borrowed!PyObject* PyFunction_GET_KW_DEFAULTS()(PyObject* func) { 127 return (cast(PyFunctionObject*)func).func_kwdefaults; 128 } 129 } 130 /// _ 131 Borrowed!PyObject* PyFunction_GET_CLOSURE()(PyObject* func) { 132 return (cast(PyFunctionObject*)func).func_closure; 133 } 134 version(Python_3_0_Or_Later) { 135 /// Availability: 3.* 136 Borrowed!PyObject* PyFunction_GET_ANNOTATIONS()(PyObject* func) { 137 return (cast(PyFunctionObject*)func).func_annotations; 138 } 139 } 140 141 /// _ 142 mixin(PyAPI_DATA!"PyTypeObject PyClassMethod_Type"); 143 /// _ 144 mixin(PyAPI_DATA!"PyTypeObject PyStaticMethod_Type"); 145 146 /// _ 147 PyObject* PyClassMethod_New(PyObject*); 148 /// _ 149 PyObject* PyStaticMethod_New(PyObject*); 150 151