|
| 1 | + |
| 2 | +## API design rules |
| 3 | + |
| 4 | +These rules should be applied when adding to the API. |
| 5 | +They exist to provide a consid |
| 6 | + |
| 7 | +The overall design of the API must adhere to the desing principles. |
| 8 | +Here is one possible design |
| 9 | + |
| 10 | +### Return structs |
| 11 | + |
| 12 | +Many functions return a result, but may also raise an exception. |
| 13 | +To handle thsi API functions should return a `struct` containing both |
| 14 | +the error code and result or exception. |
| 15 | + |
| 16 | +```C |
| 17 | +typedef struct _py_returnval { |
| 18 | + int kind; |
| 19 | + PyRef value; |
| 20 | +} PyResult; |
| 21 | +``` |
| 22 | + |
| 23 | +Different functions may return variations on the above, but the `kind` |
| 24 | +must obey the following rules: |
| 25 | + |
| 26 | +A value of zero is always a success, and `value` must be the result. |
| 27 | +A negative value is always an error, and `value` must be the exception raised. |
| 28 | + |
| 29 | +Positive values can either be failures or additional success codes. |
| 30 | +No function may return both failures and additional success codes. |
| 31 | + |
| 32 | +For exaample, to get a value from a dictionary might have the following API: |
| 33 | + |
| 34 | +```C |
| 35 | +typedef enum _py_lookup_kind { |
| 36 | + ERROR = -1, |
| 37 | + FOUND = 0, |
| 38 | + MISSING = 1, |
| 39 | +} PyLookupKind; |
| 40 | + |
| 41 | +typedef struct _py_lookup { |
| 42 | + PyLookupKind kind; |
| 43 | + PyRef value; |
| 44 | +} PyLookupResult; |
| 45 | + |
| 46 | +PyLookupResult PyAPi_Dict_Get(PyDictRef dict, PyRef key); |
| 47 | +``` |
| 48 | +
|
| 49 | +Even in the case of `MISSING`, `value` should be set to a valid value to |
| 50 | +minimize the chance of crashes should `value` be used. |
| 51 | +The following use, although incorrect, will not corrupt the VM or memory: |
| 52 | +```C |
| 53 | + PyRef result = PyAPi_Dict_Get(self, k).value; |
| 54 | +``` |
| 55 | + |
| 56 | +### Naming |
| 57 | + |
| 58 | +All API function and struct names should adhere to simple rules. |
| 59 | +For example, function names should take the form: |
| 60 | +Prefix_NameSpace_Operation[_REF_CONSUMPTION] |
| 61 | +E.g. |
| 62 | +```C |
| 63 | +PyResult PyApi_Tuple_FromArray(uintptr_t len, PyRef *array); |
| 64 | +``` |
| 65 | +
|
| 66 | +### Use standard C99 types, not custom ones. |
| 67 | +
|
| 68 | +In other words, use `intptr_t` not `Py_ssize_t`. |
| 69 | +
|
| 70 | +
|
| 71 | +### Consumption of argument references |
| 72 | +
|
| 73 | +For effficiency, there is a natural consumption of references in some API |
| 74 | +functions. For example, appending an item to a list naturally consumes the |
| 75 | +reference to the item, but not the list. |
| 76 | +We denote borrowed references by `B` and consumed references by `C`. |
| 77 | +
|
| 78 | +Consequently we want the low-level API/ABI function to be: |
| 79 | +
|
| 80 | +```C |
| 81 | +int PyApi_List_Append_BC(PyListRef list, PyRef item); |
| 82 | +``` |
| 83 | + |
| 84 | +All ABI functionsshoudl get a higher level API function without a suffix. |
| 85 | +All non-suffix functions borrow the references to all their arguments. |
| 86 | + |
| 87 | +```C |
| 88 | +int PyApi_List_Append(PyListRef list, PyRef item); |
| 89 | +``` |
| 90 | +is equivalent to `PyApi_List_Append_BB`. |
| 91 | +
|
| 92 | +Functions taking arrays must consume all the references in the array, |
| 93 | +or borrow all references in the array. |
| 94 | +
|
| 95 | +The reference behavior must be the safe regardless of the return value or |
| 96 | +result. |
| 97 | +
|
| 98 | +Note that this doesn't impact the portability of the API as the borrow |
| 99 | +or consume forms can be mechanically create from the other. |
| 100 | +
|
| 101 | +
|
| 102 | +### Opaque, linear references |
| 103 | +
|
| 104 | +The C-API will refer to Python objects through opaque references |
| 105 | +which must have exactly one owner. This design has been shown to |
| 106 | +be efficient, robust and portable by the HPy project, where the |
| 107 | +references are known as "handles". |
| 108 | +As each reference has exactly one owner, there will be no |
| 109 | +incrementing or decrementing of reference counts. References can |
| 110 | +be duplicated with |
| 111 | +```C |
| 112 | +PyRef PyRef_Dup(PyRef ref); |
| 113 | +``` |
| 114 | +and destroyed by |
| 115 | +```C |
| 116 | +void PyRef_Clear(PyRef ref); |
| 117 | +``` |
| 118 | +
|
| 119 | +Type specific variants will be provided for subtypes like `PyListRef`. |
| 120 | +
|
| 121 | +
|
| 122 | +### ABI functions should be efficient, API functions easy to use |
| 123 | +
|
| 124 | +There is a tension between ease of use and performance. |
| 125 | +For example, it is the common case when creating a tuple that |
| 126 | +the length is known, yet the function needs to treat length zero |
| 127 | +differently, returning the empty tuple singleton. |
| 128 | +
|
| 129 | +We handle this tension by providing an efficient, but difficult use |
| 130 | +ABI function: |
| 131 | +```C |
| 132 | +PyResult PyApi_Tuple_FromNonEmptyArray_nC(uintptr_tlen, PyRef *array); |
| 133 | +``` |
| 134 | +and the easier to use API function |
| 135 | +```C |
| 136 | +PyResult PyApi_Tuple_FromArray(uintptr_tlen, PyRef *array); |
| 137 | +``` |
| 138 | +
|
| 139 | +But we can do better, as the API can include macros, we can implement |
| 140 | +```C |
| 141 | +PyTupleResult PyApi_Tuple_FromFixedArray(array); |
| 142 | +``` |
| 143 | +something like this: |
| 144 | +``` |
| 145 | +#define PyResult PyApi_Tuple_FromFixedArray(array) \ |
| 146 | + ((sizeof(array) == 0) ? \ |
| 147 | + PyApi_NewRefEmptyTuple() \ |
| 148 | + : \ |
| 149 | + PyApi_Tuple_FromNonEmptyArray(sizeof(array)/sizeof(PyRef), &array) |
| 150 | +
F987
) |
| 151 | +``` |
| 152 | +Allowing it be used like this: |
| 153 | +``` |
| 154 | +PyRef args[4] = { |
| 155 | + PyNone, |
| 156 | + arg1, |
| 157 | + arg2, |
| 158 | + PyNone |
| 159 | +}; |
| 160 | +PyTupleResult new_tuple = PyApi_Tuple_FromFixedArray(args); |
| 161 | +``` |
| 162 | + |
| 163 | +### The API should include versions of functions that take result types. |
| 164 | + |
| 165 | +For most* API functions, at least those that take one or two `PyRef` arguments, |
| 166 | +there should be a version that takes a `PyResult` as the first argument. |
| 167 | + |
| 168 | +This function gets an `M` suffix. |
| 169 | + |
| 170 | +This allows chaining of calls without being overwhelmed by error handling. |
| 171 | + |
| 172 | +Suppose we want to write a function that returns the name of the class of |
| 173 | +the argument. |
| 174 | + |
| 175 | +Using the `M` forms we can implement this as: |
| 176 | +```C |
| 177 | +PyStrResult pop_and_pair(PyRef o) |
| 178 | +{ |
| 179 | + return Py_Type_GetName_M(PyApi_Object_GetType(o)); |
| 180 | +} |
| 181 | +``` |
| 182 | +
|
| 183 | +The implementation is straightforward and can be automatically generated: |
| 184 | +``` |
| 185 | +inline PyResult Py_Type_GetName_M(PyResult r) |
| 186 | +{ |
| 187 | + if (r.kind < 0) { |
| 188 | + return r; |
| 189 | + } |
| 190 | + return Py_Type_GetName(r.value); |
| 191 | +} |
| 192 | +``` |
| 193 | +
|
| 194 | +For the technically minded, this pattern is known as the "error monad". |
| 195 | +
|
| 196 | +*Probably all, as we automatically generate these. |
0 commit comments