8000 Clarify use of C99 integer types, and a couple of other edits. · markshannon/New-C-API-for-Python@ace0b6b · GitHub
[go: up one dir, main page]

Skip to content

Commit ace0b6b

Browse files
committed
Clarify use of C99 integer types, and a couple of other edits.
1 parent a0dcbf9 commit ace0b6b

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

DesignRules.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,25 @@ E.g.
7777
int PyApi_Tuple_FromArray(uintptr_t len, PyRef *array, PyRef *result);
7878
```
7979
80-
## Use standard C99 types, not custom ones.
80+
## Use C99 <stdint.h> types
81+
82+
### Use C99 integers types, not custom ones
8183
8284
In other words, use `intptr_t` not `Py_ssize_t`.
8385
This helps portability, and wrapping for other languages.
8486
87+
### Use C99 integers types, not legacy ones
88+
89+
Use `int32_t` or `intptr_t` not `int` or `long`.
90+
`long` should never be used, as it differs in size even on the same hardware.
91+
`int` is acceptable only as a return `kind`.
92+
If the return value can represent a value, then a `<stdint.h>` type should be used.
93+
94+
E.g.
95+
* `int PyApi_Tuple_FromArray(uintptr_t len, PyRef *array, PyRef *result)` is OK.
96+
* But, `int PyApi_Tuple_GetSize(PyTupleRef *ref)` is not OK as it returns a value, not just a `kind`.
97+
* It should be `uintptr_t PyApi_Tuple_GetSize(PyTupleRef *ref)`
98+
8599
## No variable length argument lists.
86100
87101
In other words no `...` at the end of the parameters.
@@ -158,9 +172,6 @@ we should specify it as
158172
PyCodeRef PyApi_Function_GetCode(PyFunctionRef f);
159173
```
160174

161-
This may force us to add some extra casts to support the `M` form,
162-
but should keep code cleaner overall.
163-
164175
## Provide a safe and easy to use set of casts
165176

166177
If we want to use specific types, we need casts.
@@ -180,6 +191,9 @@ Downcasts are tricky, because we can't return a more type specific ``PyRef`` typ
180191
Either the ``PyRef`` is unsafe, due to potential errors, or it is useless as
181192
the result of the cast, being as general as its input.
182193
194+
* `PyListRef PyApi_List_DownCast(PyRef obj)`: unsafe
195+
* `PyRef PyApi_List_DownCast(PyRef obj)`: useless
196+
183197
Consequently the API contains macros to wrap the test then unsafe cast idiom.
184198
185199
For example support we want to treat an reference as a reference to a `list`.

0 commit comments

Comments
 (0)
0