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 } 61 } 62 63 /// _ 64 mixin(PyAPI_DATA!"PyTypeObject PyFunction_Type"); 65 66 // D translation of C macro: 67 /// _ 68 int PyFunction_Check()(PyObject* op) { 69 return op.ob_type == &PyFunction_Type; 70 } 71 72 /// _ 73 PyObject* PyFunction_New(PyObject*, PyObject*); 74 /// _ 75 PyObject_BorrowedRef* PyFunction_GetCode(PyObject*); 76 /// _ 77 PyObject_BorrowedRef* PyFunction_GetGlobals(PyObject*); 78 /// _ 79 PyObject_BorrowedRef* PyFunction_GetModule(PyObject*); 80 /// _ 81 PyObject_BorrowedRef* PyFunction_GetDefaults(PyObject*); 82 /// _ 83 int PyFunction_SetDefaults(PyObject*, PyObject*); 84 version(Python_3_0_Or_Later) { 85 /// Availability: 3.* 86 Borrowed!PyObject* PyFunction_GetKwDefaults(PyObject*); 87 /// Availability: 3.* 88 int PyFunction_SetKwDefaults(PyObject*, PyObject*); 89 } 90 /// _ 91 PyObject_BorrowedRef* PyFunction_GetClosure(PyObject*); 92 /// _ 93 int PyFunction_SetClosure(PyObject*, PyObject*); 94 version(Python_3_0_Or_Later) { 95 /// Availability: 3.* 96 Borrowed!PyObject* PyFunction_GetAnnotations(PyObject*); 97 /// Availability: 3.* 98 int PyFunction_SetAnnotations(PyObject*, PyObject*); 99 } 100 101 /** Macros for direct access to these values. Type checks are *not* 102 done, so use with care. */ 103 Borrowed!PyObject* PyFunction_GET_CODE()(PyObject* func) { 104 return (cast(PyFunctionObject*)func).func_code; 105 } 106 /// ditto 107 Borrowed!PyObject* PyFunction_GET_GLOBALS()(PyObject* func) { 108 return (cast(PyFunctionObject*)func).func_globals; 109 } 110 /// ditto 111 Borrowed!PyObject* PyFunction_GET_MODULE()(PyObject* func) { 112 return (cast(PyFunctionObject*)func).func_module; 113 } 114 /// ditto 115 Borrowed!PyObject* PyFunction_GET_DEFAULTS()(PyObject* func) { 116 return (cast(PyFunctionObject*)func).func_defaults; 117 } 118 version(Python_3_0_Or_Later) { 119 /// Availability: 3.* 120 Borrowed!PyObject* PyFunction_GET_KW_DEFAULTS()(PyObject* func) { 121 return (cast(PyFunctionObject*)func).func_kwdefaults; 122 } 123 } 124 /// _ 125 Borrowed!PyObject* PyFunction_GET_CLOSURE()(PyObject* func) { 126 return (cast(PyFunctionObject*)func).func_closure; 127 } 128 version(Python_3_0_Or_Later) { 129 /// Availability: 3.* 130 Borrowed!PyObject* PyFunction_GET_ANNOTATIONS()(PyObject* func) { 131 return (cast(PyFunctionObject*)func).func_annotations; 132 } 133 } 134 135 /// _ 136 mixin(PyAPI_DATA!"PyTypeObject PyClassMethod_Type"); 137 /// _ 138 mixin(PyAPI_DATA!"PyTypeObject PyStaticMethod_Type"); 139 140 /// _ 141 PyObject* PyClassMethod_New(PyObject*); 142 /// _ 143 PyObject* PyStaticMethod_New(PyObject*); 144 145