Debian Codegraph @

pypy/module/_hpy_universal/llapi.py

raw symbols SCIP
1 import py
2 from rpython.rtyper.lltypesystem import lltype, rffi
3 from rpython.translator.tool.cbuild import ExternalCompilationInfo
4 from rpython.translator import cdir
5 from pypy import pypydir
6 from rpython.tool.cparser import CTypeSpace
7
8 PYPYDIR = py.path.local(pypydir)
9 SRC_DIR = PYPYDIR.join('module', '_hpy_universal', 'src')
10 BASE_DIR = PYPYDIR.join('module', '_hpy_universal', '_vendored', 'hpy', 'devel')
11 INCLUDE_DIR = BASE_DIR.join('include')
12 DEBUG_DIR = PYPYDIR.join('module', '_hpy_universal', '_vendored', 'hpy', 'debug', 'src')
13 TRACE_DIR = PYPYDIR.join('module', '_hpy_universal', '_vendored', 'hpy', 'trace', 'src')
14 CAPI_DIR = PYPYDIR.join('module', '_hpy_universal', 'capi_stub')
15
16 HPY_ABI_VERSION = 0
17 HPY_ABI_VERSION_MINOR = 0
18 HPY_ABI_TAG = "hpy0"
19 MODE_INVALID = -1
20 MODE_UNIVERSAL = 0
21 MODE_DEBUG = 1
22 MODE_TRACE = 2
23
24
25 eci = ExternalCompilationInfo(
26 compile_extra = ["-DHPY_ABI_HYBRID", "-DHPY_EMBEDDED_MODULES", "-D_HPY_DEBUG_MEM_PROTECT_USEMMAP"],
27 includes=["hpy.h", "hpyerr.h", "rffi_hacks.h", "dctx.h", "tctx.h"],
28 include_dirs=[
29 cdir, # for precommondefs.h
30 INCLUDE_DIR, # for universal/hpy.h
31 SRC_DIR, # for hpyerr.h
32 DEBUG_DIR, # for debug module
33 DEBUG_DIR.join('include'), # for hpy_debug.h
34 TRACE_DIR, # for trace module
35 TRACE_DIR.join('include'), # for hpy_trace.h
36 CAPI_DIR, # for Python.h stub for hybrid tests
37 ],
38 separate_module_files=[
39 SRC_DIR.join('bridge.c'),
40 SRC_DIR.join('hpyerr.c'),
41 SRC_DIR.join('argparse.c'),
42 # <debug mode>
43 SRC_DIR.join('dctx.c'),
44 DEBUG_DIR.join('debug_ctx.c'),
45 DEBUG_DIR.join('debug_ctx_not_cpython.c'),
46 DEBUG_DIR.join('debug_handles.c'),
47 DEBUG_DIR.join('stacktrace.c'),
48 DEBUG_DIR.join('_debugmod.c'),
49 DEBUG_DIR.join('autogen_debug_wrappers.c'),
50 DEBUG_DIR.join('dhqueue.c'),
51 DEBUG_DIR.join('memprotect.c'),
52 # </debug mode>
53 # <trace mode>
54 SRC_DIR.join('tctx.c'),
55 TRACE_DIR.join('trace_ctx.c'),
56 TRACE_DIR.join('_tracemod.c'),
57 TRACE_DIR.join('autogen_trace_wrappers.c'),
58 TRACE_DIR.join('autogen_trace_func_table.c'),
59 # </trace mode>
60 BASE_DIR.join('src', 'runtime', 'ctx_tracker.c'),
61 ],
62 )
63
64 cts = CTypeSpace()
65 cts.headers.append('stdint.h')
66 cts.parse_source(INCLUDE_DIR.join('hpy', 'autogen_hpyslot.h').read())
67
68 # NOTE: the following C source is NOT seen by the C compiler during
69 # translation: it is used only as a nice way to declare the lltype.* types
70 # which are needed here
71 cts.parse_source("""
72 typedef intptr_t HPy_ssize_t;
73 typedef intptr_t HPy_hash_t;
74
75 struct _HPy_s {
76 HPy_ssize_t _i;
77 };
78 typedef HPy_ssize_t HPy;
79
80 typedef struct _HPyListBuilder_s {
81 HPy_ssize_t _lst;
82 } _struct_HPyListBuilder_s;
83 typedef HPy_ssize_t HPyListBuilder;
84
85 typedef struct _HPyTupleBuilder_s {
86 HPy_ssize_t _lst;
87 } _struct_HPyTupleBuilder_s;
88 typedef HPy_ssize_t HPyTupleBuilder;
89
90 typedef struct _HPyTracker_s {
91 HPy_ssize_t _i;
92 } _struct_HPyTracker_s;
93 typedef HPy_ssize_t HPyTracker;
94
95 typedef struct _HPyField_s {
96 HPy_ssize_t _i;
97 } _struct_HPyField_s;
98 typedef HPy_ssize_t HPyField;
99
100 typedef struct _HPyGlobal_s {
101 HPy_ssize_t _i;
102 } _struct_HPyGlobal_s;
103 typedef HPy_ssize_t HPyGlobal;
104
105 typedef enum {
106 HPyType_BuiltinShape_Legacy = -1,
107 HPyType_BuiltinShape_Object = 0,
108 HPyType_BuiltinShape_Type = 1,
109 HPyType_BuiltinShape_Long = 2,
110 HPyType_BuiltinShape_Float = 3,
111 HPyType_BuiltinShape_Unicode = 4,
112 HPyType_BuiltinShape_Tuple = 5,
113 HPyType_BuiltinShape_List = 6,
114 } HPyType_BuiltinShape;
115
116 /* sync with lib_pypy/hpy/devel/include/hpy/universal/autogen.h */
117 typedef struct _HPyContext_s {
118 const char *name; // used just to make debugging and testing easier
119 void *_private; // used by implementations to store custom data
120 int abi_version;
121 struct _HPy_s h_None;
122 struct _HPy_s h_True;
123 struct _HPy_s h_False;
124 struct _HPy_s h_NotImplemented;
125 struct _HPy_s h_Ellipsis;
126 struct _HPy_s h_BaseException;
127 struct _HPy_s h_Exception;
128 struct _HPy_s h_StopAsyncIteration;
129 struct _HPy_s h_StopIteration;
130 struct _HPy_s h_GeneratorExit;
131 struct _HPy_s h_ArithmeticError;
132 struct _HPy_s h_LookupError;
133 struct _HPy_s h_AssertionError;
134 struct _HPy_s h_AttributeError;
135 struct _HPy_s h_BufferError;
136 struct _HPy_s h_EOFError;
137 struct _HPy_s h_FloatingPointError;
138 struct _HPy_s h_OSError;
139 struct _HPy_s h_ImportError;
140 struct _HPy_s h_ModuleNotFoundError;
141 struct _HPy_s h_IndexError;
142 struct _HPy_s h_KeyError;
143 struct _HPy_s h_KeyboardInterrupt;
144 struct _HPy_s h_MemoryError;
145 struct _HPy_s h_NameError;
146 struct _HPy_s h_OverflowError;
147 struct _HPy_s h_RuntimeError;
148 struct _HPy_s h_RecursionError;
149 struct _HPy_s h_NotImplementedError;
150 struct _HPy_s h_SyntaxError;
151 struct _HPy_s h_IndentationError;
152 struct _HPy_s h_TabError;
153 struct _HPy_s h_ReferenceError;
154 struct _HPy_s h_SystemError;
155 struct _HPy_s h_SystemExit;
156 struct _HPy_s h_TypeError;
157 struct _HPy_s h_UnboundLocalError;
158 struct _HPy_s h_UnicodeError;
159 struct _HPy_s h_UnicodeEncodeError;
160 struct _HPy_s h_UnicodeDecodeError;
161 struct _HPy_s h_UnicodeTranslateError;
162 struct _HPy_s h_ValueError;
163 struct _HPy_s h_ZeroDivisionError;
164 struct _HPy_s h_BlockingIOError;
165 struct _HPy_s h_BrokenPipeError;
166 struct _HPy_s h_ChildProcessError;
167 struct _HPy_s h_ConnectionError;
168 struct _HPy_s h_ConnectionAbortedError;
169 struct _HPy_s h_ConnectionRefusedError;
170 struct _HPy_s h_ConnectionResetError;
171 struct _HPy_s h_FileExistsError;
172 struct _HPy_s h_FileNotFoundError;
173 struct _HPy_s h_InterruptedError;
174 struct _HPy_s h_IsADirectoryError;
175 struct _HPy_s h_NotADirectoryError;
176 struct _HPy_s h_PermissionError;
177 struct _HPy_s h_ProcessLookupError;
178 struct _HPy_s h_TimeoutError;
179 struct _HPy_s h_Warning;
180 struct _HPy_s h_UserWarning;
181 struct _HPy_s h_DeprecationWarning;
182 struct _HPy_s h_PendingDeprecationWarning;
183 struct _HPy_s h_SyntaxWarning;
184 struct _HPy_s h_RuntimeWarning;
185 struct _HPy_s h_FutureWarning;
186 struct _HPy_s h_ImportWarning;
187 struct _HPy_s h_UnicodeWarning;
188 struct _HPy_s h_BytesWarning;
189 struct _HPy_s h_ResourceWarning;
190 struct _HPy_s h_BaseObjectType;
191 struct _HPy_s h_TypeType;
192 struct _HPy_s h_BoolType;
193 struct _HPy_s h_LongType;
194 struct _HPy_s h_FloatType;
195 struct _HPy_s h_UnicodeType;
196 struct _HPy_s h_TupleType;
197 struct _HPy_s h_ListType;
198 void * ctx_Dup;
199 void * ctx_Close;
200 void * ctx_Long_FromInt32_t;
201 void * ctx_Long_FromUInt32_t;
202 void * ctx_Long_FromInt64_t;
203 void * ctx_Long_FromUInt64_t;
204 void * ctx_Long_FromSize_t;
205 void * ctx_Long_FromSsize_t;
206 void * ctx_Long_AsInt32_t;
207 void * ctx_Long_AsUInt32_t;
208 void * ctx_Long_AsUInt32_tMask;
209 void * ctx_Long_AsInt64_t;
210 void * ctx_Long_AsUInt64_t;
211 void * ctx_Long_AsUInt64_tMask;
212 void * ctx_Long_AsSize_t;
213 void * ctx_Long_AsSsize_t;
214 void * ctx_Long_AsVoidPtr;
215 void * ctx_Long_AsDouble;
216 void * ctx_Float_FromDouble;
217 void * ctx_Float_AsDouble;
218 void * ctx_Bool_FromBool;
219 void * ctx_Length;
220 void * ctx_Number_Check;
221 void * ctx_Add;
222 void * ctx_Subtract;
223 void * ctx_Multiply;
224 void * ctx_MatrixMultiply;
225 void * ctx_FloorDivide;
226 void * ctx_TrueDivide;
227 void * ctx_Remainder;
228 void * ctx_Divmod;
229 void * ctx_Power;
230 void * ctx_Negative;
231 void * ctx_Positive;
232 void * ctx_Absolute;
233 void * ctx_Invert;
234 void * ctx_Lshift;
235 void * ctx_Rshift;
236 void * ctx_And;
237 void * ctx_Xor;
238 void * ctx_Or;
239 void * ctx_Index;
240 void * ctx_Long;
241 void * ctx_Float;
242 void * ctx_InPlaceAdd;
243 void * ctx_InPlaceSubtract;
244 void * ctx_InPlaceMultiply;
245 void * ctx_InPlaceMatrixMultiply;
246 void * ctx_InPlaceFloorDivide;
247 void * ctx_InPlaceTrueDivide;
248 void * ctx_InPlaceRemainder;
249 void * ctx_InPlacePower;
250 void * ctx_InPlaceLshift;
251 void * ctx_InPlaceRshift;
252 void * ctx_InPlaceAnd;
253 void * ctx_InPlaceXor;
254 void * ctx_InPlaceOr;
255 void * ctx_Callable_Check;
256 void * ctx_CallTupleDict;
257 void * ctx_FatalError;
258 void * ctx_Err_SetString;
259 void * ctx_Err_SetObject;
260 void * ctx_Err_SetFromErrnoWithFilename;
261 void * ctx_Err_SetFromErrnoWithFilenameObjects;
262 void * ctx_Err_Occurred;
263 void * ctx_Err_ExceptionMatches;
264 void * ctx_Err_NoMemory;
265 void * ctx_Err_Clear;
266 void * ctx_Err_NewException;
267 void * ctx_Err_NewExceptionWithDoc;
268 void * ctx_Err_WarnEx;
269 void * ctx_Err_WriteUnraisable;
270 void * ctx_IsTrue;
271 void * ctx_Type_FromSpec;
272 void * ctx_Type_GenericNew;
273 void * ctx_GetAttr;
274 void * ctx_GetAttr_s;
275 void * ctx_HasAttr;
276 void * ctx_HasAttr_s;
277 void * ctx_SetAttr;
278 void * ctx_SetAttr_s;
279 void * ctx_GetItem;
280 void * ctx_GetItem_i;
281 void * ctx_GetItem_s;
282 void * ctx_Contains;
283 void * ctx_SetItem;
284 void * ctx_SetItem_i;
285 void * ctx_SetItem_s;
286 void * ctx_Type;
287 void * ctx_TypeCheck;
288 void * ctx_Is;
289 void * ctx_AsStruct_Object;
290 void * ctx_AsStruct_Legacy;
291 void * ctx_New;
292 void * ctx_Repr;
293 void * ctx_Str;
294 void * ctx_ASCII;
295 void * ctx_Bytes;
296 void * ctx_RichCompare;
297 void * ctx_RichCompareBool;
298 void * ctx_Hash;
299 void * ctx_Bytes_Check;
300 void * ctx_Bytes_Size;
301 void * ctx_Bytes_GET_SIZE;
302 void * ctx_Bytes_AsString;
303 void * ctx_Bytes_AS_STRING;
304 void * ctx_Bytes_FromString;
305 void * ctx_Bytes_FromStringAndSize;
306 void * ctx_Unicode_FromString;
307 void * ctx_Unicode_Check;
308 void * ctx_Unicode_AsASCIIString;
309 void * ctx_Unicode_AsLatin1String;
310 void * ctx_Unicode_AsUTF8String;
311 void * ctx_Unicode_AsUTF8AndSize;
312 void * ctx_Unicode_FromWideChar;
313 void * ctx_Unicode_DecodeFSDefault;
314 void * ctx_Unicode_DecodeFSDefaultAndSize;
315 void * ctx_Unicode_EncodeFSDefault;
316 void * ctx_Unicode_ReadChar;
317 void * ctx_Unicode_DecodeASCII;
318 void * ctx_Unicode_DecodeLatin1;
319 void * ctx_List_Check;
320 void * ctx_List_New;
321 void * ctx_List_Append;
322 void * ctx_Dict_Check;
323 void * ctx_Dict_New;
324 void * ctx_Tuple_Check;
325 void * ctx_Tuple_FromArray;
326 void * ctx_Import_ImportModule;
327 void * ctx_FromPyObject;
328 void * ctx_AsPyObject;
329 void * ctx_CallRealFunctionFromTrampoline;
330 void * ctx_ListBuilder_New;
331 void * ctx_ListBuilder_Set;
332 void * ctx_ListBuilder_Build;
333 void * ctx_ListBuilder_Cancel;
334 void * ctx_TupleBuilder_New;
335 void * ctx_TupleBuilder_Set;
336 void * ctx_TupleBuilder_Build;
337 void * ctx_TupleBuilder_Cancel;
338 void * ctx_Tracker_New;
339 void * ctx_Tracker_Add;
340 void * ctx_Tracker_ForgetAll;
341 void * ctx_Tracker_Close;
342 void * ctx_Field_Store;
343 void * ctx_Field_Load;
344 void * ctx_ReenterPythonExecution;
345 void * ctx_LeavePythonExecution;
346 void * ctx_Global_Store;
347 void * ctx_Global_Load;
348 void * ctx_Dump;
349 void * ctx_AsStruct_Type;
350 void * ctx_AsStruct_Long;
351 void * ctx_AsStruct_Float;
352 void * ctx_AsStruct_Unicode;
353 void * ctx_AsStruct_Tuple;
354 void * ctx_AsStruct_List;
355 void * ctx_Type_GetBuiltinShape;
356 void * ctx_DelItem;
357 void * ctx_DelItem_i;
358 void * ctx_DelItem_s;
359 struct _HPy_s h_ComplexType;
360 struct _HPy_s h_BytesType;
361 struct _HPy_s h_MemoryViewType;
362 struct _HPy_s h_CapsuleType;
363 struct _HPy_s h_SliceType;
364 struct _HPy_s h_Builtins;
365 struct _HPy_s h_DictType;
366 void * ctx_Capsule_New;
367 void * ctx_Capsule_Get;
368 void * ctx_Capsule_IsValid;
369 void * ctx_Capsule_Set;
370 void * ctx_Compile_s;
371 void * ctx_EvalCode;
372 void * ctx_ContextVar_New;
373 void * ctx_ContextVar_Get;
374 void * ctx_ContextVar_Set;
375 void * ctx_Type_GetName;
376 void * ctx_Type_IsSubtype;
377 void * ctx_Unicode_FromEncodedObject;
378 void * ctx_Unicode_Substring;
379 void * ctx_Dict_Keys;
380 void * ctx_Dict_Copy;
381 void * ctx_Slice_Unpack;
382 void * ctx_SetCallFunction;
383 void * ctx_Call;
384 void * ctx_CallMethod;
385 void * ctx_AsStruct_Dict;
386 void * ctx_List_Insert;
387 void * ctx_GetSlice;
388 void * ctx_SetSlice;
389 void * ctx_DelSlice;
390 void * ctx_GetIter;
391 void * ctx_Iter_Next;
392 void * ctx_Iter_Check;
393 void * ctx_Slice_New;
394 } _struct_HPyContext_s;
395
396 typedef struct _HPyContext_s HPyContext;
397
398 typedef int HPyFunc_Signature;
399
400 /* hpydef.h */
401
402 typedef void* (*HPyCFunction)(void);
403
404 typedef struct {
405 HPySlot_Slot slot; // The slot to fill
406 HPyCFunction impl; // Function pointer to the implementation
407 void *cpy_trampoline; // Used by CPython to call impl
408 } HPySlot;
409
410 typedef struct {
411 const char *name; // The name of the built-in function/method
412 HPyCFunction impl; // Function pointer to the implementation
413 void *cpy_trampoline; // Used by CPython to call impl
414 HPyFunc_Signature signature; // Indicates impl's expected the signature
415 const char *doc; // The __doc__ attribute, or NULL
416 } HPyMeth;
417
418 typedef enum {
419 HPyMember_SHORT = 0,
420 HPyMember_INT = 1,
421 HPyMember_LONG = 2,
422 HPyMember_FLOAT = 3,
423 HPyMember_DOUBLE = 4,
424 HPyMember_STRING = 5,
425 HPyMember_OBJECT = 6,
426 HPyMember_CHAR = 7, /* 1-character string */
427 HPyMember_BYTE = 8, /* 8-bit signed int */
428 /* unsigned variants: */
429 HPyMember_UBYTE = 9,
430 HPyMember_USHORT = 10,
431 HPyMember_UINT = 11,
432 HPyMember_ULONG = 12,
433
434 /* Added by Jack: strings contained in the structure */
435 HPyMember_STRING_INPLACE = 13,
436
437 /* Added by Lillo: bools contained in the structure (assumed char) */
438 HPyMember_BOOL = 14,
439 HPyMember_OBJECT_EX = 16, /* Like T_OBJECT, but raises AttributeError
440 when the value is NULL, instead of
441 converting to None. */
442 HPyMember_LONGLONG = 17,
443 HPyMember_ULONGLONG = 18,
444
445 HPyMember_HPYSSIZET = 19, /* HPy_ssize_t */
446 HPyMember_NONE = 20, /* Value is always None */
447
448 } HPyMember_FieldType;
449
450 typedef struct {
451 const char *name;
452 HPyMember_FieldType type;
453 HPy_ssize_t offset;
454 int readonly;
455 const char *doc;
456 } HPyMember;
457
458 typedef struct {
459 const char *name;
460 void *getter_impl; // Function pointer to the implementation
461 void *setter_impl; // Same; this may be NULL
462 void *getter_cpy_trampoline; // Used by CPython to call getter_impl
463 void *setter_cpy_trampoline; // Same; this may be NULL
464 const char *doc;
465 void *closure;
466 } HPyGetSet;
467
468 typedef enum {
469 HPyDef_Kind_Slot = 1,
470 HPyDef_Kind_Meth = 2,
471 HPyDef_Kind_Member = 3,
472 HPyDef_Kind_GetSet = 4,
473 } HPyDef_Kind;
474
475 typedef struct {
476 HPyDef_Kind kind;
477 //union {
478 // HPySlot slot;
479 HPyMeth meth;
480 // HPyMember member;
481 // HPyGetSet getset;
482 //};
483 } HPyDef;
484
485 // work around rffi's lack of support for unions
486 typedef struct {
487 HPyDef_Kind kind;
488 HPySlot slot;
489 } _pypy_HPyDef_as_slot;
490
491 typedef struct {
492 HPyDef_Kind kind;
493 HPyMember member;
494 } _pypy_HPyDef_as_member;
495
496 typedef struct {
497 HPyDef_Kind kind;
498 HPyGetSet getset;
499 } _pypy_HPyDef_as_getset;
500
501 typedef void (*HPyFunc_Capsule_Destructor)(const char *name, void *pointer, void *context);
502 typedef HPy (*HPyFunc_keywords)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw);
503
504 typedef struct {
505 void *cpy_trampoline;
506 HPyFunc_Capsule_Destructor impl;
507 } HPyCapsule_Destructor;
508
509 typedef struct {
510 void *pointer;
511 const char *name;
512 void *context;
513 HPyCapsule_Destructor destructor;
514 } HPyCapsule;
515
516 typedef struct {
517 void *cpy_trampoline;
518 /* Should be HPyFunc_keywords, but the declaration above is too different
519 * from the actual one (HPy -> HPy_s, func returns a struct) which
520 * confuses RPython
521 */
522 void* impl;
523 } HPyCallFunction;
524
525 /* hpymodule.h */
526
527 typedef void *(*PyCFunction)(void *, void *);
528 struct PyMethodDef {
529 const char *ml_name; /* The name of the built-in function/method */
530 PyCFunction ml_meth; /* The C function that implements it */
531 int ml_flags; /* Combination of METH_xxx flags, which mostly
532 describe the args expected by the C func */
533 const char *ml_doc; /* The __doc__ attribute, or NULL */
534 };
535 typedef struct PyMethodDef PyMethodDef;
536
537 typedef struct {
538 const char* doc;
539 HPy_ssize_t size;
540 PyMethodDef *legacy_methods;
541 HPyDef **defines;
542 /* Should be HPyGlobal**, but after translation HPGlobal is a struct
543 * which confuses RPython
544 */
545 void *globals;
546 } HPyModuleDef;
547
548 /* hpytype.h */
549
550 typedef struct {
551 const char* name;
552 int basicsize;
553 int itemsize;
554 unsigned long flags;
555 HPyType_BuiltinShape builtin_shape;
556 void *legacy_slots; // PyType_Slot *
557 HPyDef **defines; /* points to an array of 'HPyDef *' */
558 const char* doc; /* UTF-8 doc string or NULL */
559 } HPyType_Spec;
560
561 typedef enum {
562 HPyType_SpecParam_Base = 1,
563 HPyType_SpecParam_BasesTuple = 2,
564 HPyType_SpecParam_Metaclass = 3,
565 HPyType_SpecParam_Module = 4,
566 } HPyType_SpecParam_Kind;
567
568 typedef struct {
569 HPyType_SpecParam_Kind kind;
570 struct _HPy_s object;
571 } HPyType_SpecParam;
572
573 /* All types are dynamically allocated */
574 #define _Py_TPFLAGS_HEAPTYPE (1UL << 9)
575 #define _Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
576 #define HPy_TPFLAGS_DEFAULT (_Py_TPFLAGS_HEAPTYPE | _Py_TPFLAGS_HAVE_VERSION_TAG)
577
578 #define HPy_TPFLAGS_INTERNAL_PURE (1UL << 8)
579
580 /* Set if the type allows subclassing */
581 #define HPy_TPFLAGS_BASETYPE (1UL << 10)
582
583 /* macros.h */
584
585 /* Rich comparison opcodes */
586 typedef enum {
587 HPy_LT = 0,
588 HPy_LE = 1,
589 HPy_EQ = 2,
590 HPy_NE = 3,
591 HPy_GT = 4,
592 HPy_GE = 5,
593 } HPy_RichCmpOp;
594
595 /* hpyfunc.h */
596
597 typedef struct {
598 void *buf;
599 struct _HPy_s obj; /* owned reference */
600 HPy_ssize_t len;
601 HPy_ssize_t itemsize;
602 int readonly;
603 int ndim;
604 char *format;
605 HPy_ssize_t *shape;
606 HPy_ssize_t *strides;
607 HPy_ssize_t *suboffsets;
608 void *internal;
609 } HPy_buffer;
610
611 typedef int (*HPyFunc_visitproc)(HPyField *, void *);
612
613 typedef int (*VersionGetterFuncPtr)(void);
614 typedef HPyModuleDef* (*InitFuncPtr)(void);
615 typedef void (*InitContextFuncPtr)(HPyContext*);
616
617 /* autogen_hpyfunc_declare.h */
618
619 typedef HPy (*HPyFunc_noargs)(HPyContext *ctx, HPy self);
620 typedef HPy (*HPyFunc_o)(HPyContext *ctx, HPy self, HPy arg);
621 typedef HPy (*HPyFunc_varargs)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs);
622 typedef HPy (*HPyFunc_unaryfunc)(HPyContext *ctx, HPy);
623 typedef HPy (*HPyFunc_binaryfunc)(HPyContext *ctx, HPy, HPy);
624 typedef HPy (*HPyFunc_ternaryfunc)(HPyContext *ctx, HPy, HPy, HPy);
625 typedef int (*HPyFunc_inquiry)(HPyContext *ctx, HPy);
626 typedef HPy_ssize_t (*HPyFunc_lenfunc)(HPyContext *ctx, HPy);
627 typedef HPy (*HPyFunc_ssizeargfunc)(HPyContext *ctx, HPy, HPy_ssize_t);
628 typedef HPy (*HPyFunc_ssizessizeargfunc)(HPyContext *ctx, HPy, HPy_ssize_t, HPy_ssize_t);
629 typedef int (*HPyFunc_ssizeobjargproc)(HPyContext *ctx, HPy, HPy_ssize_t, HPy);
630 typedef int (*HPyFunc_ssizessizeobjargproc)(HPyContext *ctx, HPy, HPy_ssize_t, HPy_ssize_t, HPy);
631 typedef int (*HPyFunc_objobjargproc)(HPyContext *ctx, HPy, HPy, HPy);
632 typedef void (*HPyFunc_freefunc)(HPyContext *ctx, void *);
633 typedef HPy (*HPyFunc_getattrfunc)(HPyContext *ctx, HPy, char *);
634 typedef HPy (*HPyFunc_getattrofunc)(HPyContext *ctx, HPy, HPy);
635 typedef int (*HPyFunc_setattrfunc)(HPyContext *ctx, HPy, char *, HPy);
636 typedef int (*HPyFunc_setattrofunc)(HPyContext *ctx, HPy, HPy, HPy);
637 typedef HPy (*HPyFunc_reprfunc)(HPyContext *ctx, HPy);
638 typedef HPy_hash_t (*HPyFunc_hashfunc)(HPyContext *ctx, HPy);
639 typedef HPy (*HPyFunc_richcmpfunc)(HPyContext *ctx, HPy, HPy, int);
640 typedef HPy (*HPyFunc_getiterfunc)(HPyContext *ctx, HPy);
641 typedef HPy (*HPyFunc_iternextfunc)(HPyContext *ctx, HPy);
642 typedef HPy (*HPyFunc_descrgetfunc)(HPyContext *ctx, HPy, HPy, HPy);
643 typedef int (*HPyFunc_descrsetfunc)(HPyContext *ctx, HPy, HPy, HPy);
644 typedef int (*HPyFunc_initproc)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw);
645 typedef HPy (*HPyFunc_getter)(HPyContext *ctx, HPy, void *);
646 typedef int (*HPyFunc_setter)(HPyContext *ctx, HPy, HPy, void *);
647 typedef int (*HPyFunc_objobjproc)(HPyContext *ctx, HPy, HPy);
648 typedef int (*HPyFunc_getbufferproc)(HPyContext *ctx, HPy, HPy_buffer *, int);
649 typedef void (*HPyFunc_releasebufferproc)(HPyContext *ctx, HPy, HPy_buffer *);
650 typedef int (*HPyFunc_traverseproc)(void *object, HPyFunc_visitproc visit, void *arg);
651 typedef void (*HPyFunc_destroyfunc)(void *);
652
653 /* hpy.h */
654
655 struct _HPyThreadState { intptr_t _i; };
656 typedef intptr_t HPyThreadState;
657
658 typedef enum {
659 /** Parse isolated expressions (e.g. ``a + b``). */
660 HPy_SourceKind_Expr = 0,
661
662 /**
663 * Parse sequences of statements as read from a file or other source. This
664 * is the symbol to use when compiling arbitrarily long Python source code.
665 */
666 HPy_SourceKind_File = 1,
667
668 /**
669 * Parse a single statement. This is the mode used for the interactive
670 * interpreter loop.
671 */
672 HPy_SourceKind_Single = 2,
673 } HPy_SourceKind;
674
675 typedef enum {
676 HPyCapsule_key_Pointer = 0,
677 HPyCapsule_key_Name = 1,
678 HPyCapsule_key_Context = 2,
679 HPyCapsule_key_Destructor = 3,
680 } _HPyCapsule_key;
681
682 """)
683
684 # HACK! We manually assign _hints['eci'] to ensure that the eci is included in
685 # the translation, else common_header.h does not include hpy.h. A more proper
686 # solution probably involves telling CTypeSpace which eci the types come from?
687 HPyContext = cts.gettype('HPyContext*')
688 HPyContext.TO._hints['eci'] = eci
689
690 # Hack required to allocate contexts statically:
691 HPyContext.TO._hints['get_padding_drop'] = lambda d: [name for name in d if name.startswith('c__pad')]
692
693 HPy_ssize_t = cts.gettype('HPy_ssize_t')
694
695 # for practical reason, we use a primitive type to represent HPy almost
696 # everywhere in RPython: for example, rffi cannot handle functions returning
697 # structs. HOWEVER, the "real" HPy C type is a struct, which is available as
698 # "struct _HPy_s"
699 HPy = cts.gettype('HPy')
700 HPy_NULL = rffi.cast(HPy, 0)
701
702 PyMethodDef = cts.gettype('PyMethodDef')
703 HPyModuleDef = cts.gettype('HPyModuleDef')
704 HPyModuleDefP = cts.gettype('HPyModuleDef *')
705 VersionGetterFuncPtr = cts.gettype('VersionGetterFuncPtr')
706 InitFuncPtr = cts.gettype('InitFuncPtr')
707 InitContextFuncPtr = cts.gettype('InitContextFuncPtr')
708 # CTypeSpace converts "PyMethodDef*" into lltype.Ptr(PyMethodDef), but we
709 # want a CArrayPtr instead
710 HPyModuleDef._flds['c_legacy_methods'] = rffi.CArrayPtr(PyMethodDef)
711
712 # enum HPyFunc_Signature {
713 HPyFunc_VARARGS = 1
714 HPyFunc_KEYWORDS = 2
715 HPyFunc_NOARGS = 3
716 HPyFunc_O = 4
717 # ...
718 # }
719
720 HPyType_SpecParam_Kind = cts.gettype('HPyType_SpecParam_Kind')
721 HPyThreadState = cts.gettype("HPyThreadState")
722
723 HPy_TPFLAGS_INTERNAL_PURE = (1 << 8)
724 HPy_TPFLAGS_HAVE_GC = 1 << 14
725
726 # HPy API functions which are implemented directly in C
727 pypy_HPy_FatalError = rffi.llexternal(
728 'pypy_HPy_FatalError',
729 [HPyContext, rffi.CCHARP],
730 lltype.Void,
731 compilation_info=eci, _nowrapper=True)
732
733 # debug mode
734 hpy_debug_get_ctx = rffi.llexternal(
735 'pypy_hpy_debug_get_ctx', [HPyContext], HPyContext,
736 compilation_info=eci, _nowrapper=True)
737
738 hpy_debug_ctx_init = rffi.llexternal(
739 'pypy_hpy_debug_ctx_init', [HPyContext, HPyContext], rffi.INT_real,
740 compilation_info=eci, _nowrapper=True)
741
742 hpy_debug_set_ctx = rffi.llexternal(
743 'pypy_hpy_debug_set_ctx', [HPyContext], lltype.Void,
744 compilation_info=eci, _nowrapper=True)
745
746 hpy_debug_open_handle = rffi.llexternal(
747 'pypy_hpy_debug_open_handle', [HPyContext, HPy], HPy,
748 compilation_info=eci, _nowrapper=True)
749
750 hpy_debug_unwrap_handle = rffi.llexternal(
751 'pypy_hpy_debug_unwrap_handle', [HPyContext, HPy], HPy,
752 compilation_info=eci, _nowrapper=True)
753
754 hpy_debug_close_handle = rffi.llexternal(
755 'pypy_hpy_debug_close_handle', [HPyContext, HPy], lltype.Void,
756 compilation_info=eci, _nowrapper=True)
757
758 hpy_debug_ctx_before_call = rffi.llexternal(
759 'pypy_hpy_debug_ctx_before_call', [HPyContext], HPyContext,
760 compilation_info=eci, _nowrapper=True)
761
762 hpy_debug_ctx_after_call = rffi.llexternal(
763 'pypy_hpy_debug_ctx_after_call', [HPyContext, HPyContext], lltype.Void,
764 compilation_info=eci, _nowrapper=True)
765
766 hpy_debug_get_current_ctx = rffi.llexternal(
767 'pypy_hpy_debug_get_current_ctx', [], HPyContext,
768 compilation_info=eci, _nowrapper=True)
769
770 HPyInit__debug = rffi.llexternal(
771 'pypy_HPyInit__debug', [], HPyModuleDefP,
772 compilation_info=eci, _nowrapper=True)
773
774 #trace mode
775 hpy_trace_get_ctx = rffi.llexternal(
776 'pypy_hpy_trace_get_ctx', [HPyContext], HPyContext,
777 compilation_info=eci, _nowrapper=True)
778
779 hpy_trace_get_stored_ctx = rffi.llexternal(
780 'pypy_hpy_trace_get_stored_ctx', [], HPyContext,
781 compilation_info=eci, _nowrapper=True)
782
783 hpy_trace_ctx_init = rffi.llexternal(
784 'pypy_hpy_trace_ctx_init', [HPyContext, HPyContext], rffi.INT_real,
785 compilation_info=eci, _nowrapper=True)
786
787 hpy_trace_ctx_free = rffi.llexternal(
788 'pypy_hpy_trace_ctx_free', [HPyContext], rffi.INT_real,
789 compilation_info=eci, _nowrapper=True)
790
791 hpy_trace_get_func_name = rffi.llexternal(
792 'pypy_hpy_trace_get_func_name', [rffi.INT_real], rffi.CCHARP,
793 compilation_info=eci, _nowrapper=True)
794
795 hpy_trace_get_nfunc = rffi.llexternal(
796 'pypy_hpy_trace_get_nfunc', [], rffi.INT_real,
797 compilation_info=eci, _nowrapper=True)
798
799 HPyInit__trace = rffi.llexternal(
800 'pypy_HPyInit__trace', [], HPyModuleDefP,
801 compilation_info=eci, _nowrapper=True)