aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/cffi/py2/c/ffi_obj.c
blob: f154146621e7eae0222251062f3fd6d4249fab6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
/* An FFI object has methods like ffi.new().  It is also a container
   for the type declarations (typedefs and structs) that you can use,
   say in ffi.new().

   CTypeDescrObjects are internally stored in the dict 'types_dict'.
   The types_dict is lazily filled with CTypeDescrObjects made from
   reading a _cffi_type_context_s structure.

   In "modern" mode, the FFI instance is made by the C extension
   module originally created by recompile().  The _cffi_type_context_s
   structure comes from global data in the C extension module.

   In "compatibility" mode, an FFI instance is created explicitly by
   the user, and its _cffi_type_context_s is initially empty.  You
   need to call ffi.cdef() to add more information to it.
*/

#define FFI_COMPLEXITY_OUTPUT   1200     /* xxx should grow as needed */

#define FFIObject_Check(op) PyObject_TypeCheck(op, &FFI_Type)
#define LibObject_Check(ob)  ((Py_TYPE(ob) == &Lib_Type))

struct FFIObject_s {
    PyObject_HEAD
    PyObject *gc_wrefs, *gc_wrefs_freelist;
    PyObject *init_once_cache;
    struct _cffi_parse_info_s info;
    char ctx_is_static, ctx_is_nonempty;
    builder_c_t types_builder;
};

static FFIObject *ffi_internal_new(PyTypeObject *ffitype,
                                 const struct _cffi_type_context_s *static_ctx)
{
    static _cffi_opcode_t internal_output[FFI_COMPLEXITY_OUTPUT];

    FFIObject *ffi;
    if (static_ctx != NULL) {
        ffi = (FFIObject *)PyObject_GC_New(FFIObject, ffitype);
        /* we don't call PyObject_GC_Track() here: from _cffi_init_module()
           it is not needed, because in this case the ffi object is immortal */
    }
    else {
        ffi = (FFIObject *)ffitype->tp_alloc(ffitype, 0);
    }
    if (ffi == NULL)
        return NULL;

    if (init_builder_c(&ffi->types_builder, static_ctx) < 0) {
        Py_DECREF(ffi);
        return NULL;
    }
    ffi->gc_wrefs = NULL;
    ffi->gc_wrefs_freelist = NULL;
    ffi->init_once_cache = NULL;
    ffi->info.ctx = &ffi->types_builder.ctx;
    ffi->info.output = internal_output;
    ffi->info.output_size = FFI_COMPLEXITY_OUTPUT;
    ffi->ctx_is_static = (static_ctx != NULL);
    ffi->ctx_is_nonempty = (static_ctx != NULL);
    return ffi;
}

static void ffi_dealloc(FFIObject *ffi)
{
    PyObject_GC_UnTrack(ffi);
    Py_XDECREF(ffi->gc_wrefs);
    Py_XDECREF(ffi->gc_wrefs_freelist);
    Py_XDECREF(ffi->init_once_cache);

    free_builder_c(&ffi->types_builder, ffi->ctx_is_static);

    Py_TYPE(ffi)->tp_free((PyObject *)ffi);
}

static int ffi_traverse(FFIObject *ffi, visitproc visit, void *arg)
{
    Py_VISIT(ffi->types_builder.types_dict);
    Py_VISIT(ffi->types_builder.included_ffis);
    Py_VISIT(ffi->types_builder.included_libs);
    Py_VISIT(ffi->gc_wrefs);
    return 0;
}

static PyObject *ffiobj_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    /* user-facing initialization code, for explicit FFI() calls */
    return (PyObject *)ffi_internal_new(type, NULL);
}

/* forward, declared in cdlopen.c because it's mostly useful for this case */
static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds);

static PyObject *ffi_fetch_int_constant(FFIObject *ffi, const char *name,
                                        int recursion)
{
    int index;

    index = search_in_globals(&ffi->types_builder.ctx, name, strlen(name));
    if (index >= 0) {
        const struct _cffi_global_s *g;
        g = &ffi->types_builder.ctx.globals[index];

        switch (_CFFI_GETOP(g->type_op)) {
        case _CFFI_OP_CONSTANT_INT:
        case _CFFI_OP_ENUM:
            return realize_global_int(&ffi->types_builder, index);

        default:
            PyErr_Format(FFIError,
                         "function, global variable or non-integer constant "
                         "'%.200s' must be fetched from its original 'lib' "
                         "object", name);
            return NULL;
        }
    }

    if (ffi->types_builder.included_ffis != NULL) {
        Py_ssize_t i;
        PyObject *included_ffis = ffi->types_builder.included_ffis;

        if (recursion > 100) {
            PyErr_SetString(PyExc_RuntimeError,
                            "recursion overflow in ffi.include() delegations");
            return NULL;
        }

        for (i = 0; i < PyTuple_GET_SIZE(included_ffis); i++) {
            FFIObject *ffi1;
            PyObject *x;

            ffi1 = (FFIObject *)PyTuple_GET_ITEM(included_ffis, i);
            x = ffi_fetch_int_constant(ffi1, name, recursion + 1);
            if (x != NULL || PyErr_Occurred())
                return x;
        }
    }
    return NULL;     /* no exception set, means "not found" */
}

#define ACCEPT_STRING   1
#define ACCEPT_CTYPE    2
#define ACCEPT_CDATA    4
#define ACCEPT_ALL      (ACCEPT_STRING | ACCEPT_CTYPE | ACCEPT_CDATA)
#define CONSIDER_FN_AS_FNPTR  8

static CTypeDescrObject *_ffi_bad_type(FFIObject *ffi, const char *input_text)
{
    size_t length = strlen(input_text);
    char *extra;

    if (length > 500) {
        extra = "";
    }
    else {
        char *p;
        size_t i, num_spaces = ffi->info.error_location;
        extra = alloca(length + num_spaces + 4);
        p = extra;
        *p++ = '\n';
        for (i = 0; i < length; i++) {
            if (' ' <= input_text[i] && input_text[i] < 0x7f)
                *p++ = input_text[i];
            else if (input_text[i] == '\t' || input_text[i] == '\n')
                *p++ = ' ';
            else
                *p++ = '?';
        }
        *p++ = '\n';
        memset(p, ' ', num_spaces);
        p += num_spaces;
        *p++ = '^';
        *p++ = 0;
    }
    PyErr_Format(FFIError, "%s%s", ffi->info.error_message, extra);
    return NULL;
}

static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
                                   int accept)
{
    /* Returns the CTypeDescrObject from the user-supplied 'arg'.
       Does not return a new reference!
    */
    if ((accept & ACCEPT_STRING) && PyText_Check(arg)) {
        PyObject *types_dict = ffi->types_builder.types_dict;
        PyObject *x = PyDict_GetItem(types_dict, arg);

        if (x == NULL) {
            const char *input_text = PyText_AS_UTF8(arg);
            int err, index = parse_c_type(&ffi->info, input_text);
            if (index < 0)
                return _ffi_bad_type(ffi, input_text);

            x = realize_c_type_or_func(&ffi->types_builder,
                                       ffi->info.output, index);
            if (x == NULL)
                return NULL;

            /* Cache under the name given by 'arg', in addition to the
               fact that the same ct is probably already cached under
               its standardized name.  In a few cases, it is not, e.g.
               if it is a primitive; for the purpose of this function,
               the important point is the following line, which makes
               sure that in any case the next _ffi_type() with the same
               'arg' will succeed early, in PyDict_GetItem() above.
            */
            err = PyDict_SetItem(types_dict, arg, x);
            Py_DECREF(x); /* we know it was written in types_dict (unless out
                             of mem), so there is at least that ref left */
            if (err < 0)
                return NULL;
        }

        if (CTypeDescr_Check(x))
            return (CTypeDescrObject *)x;
        else if (accept & CONSIDER_FN_AS_FNPTR)
            return unwrap_fn_as_fnptr(x);
        else
            return unexpected_fn_type(x);
    }
    else if ((accept & ACCEPT_CTYPE) && CTypeDescr_Check(arg)) {
        return (CTypeDescrObject *)arg;
    }
    else if ((accept & ACCEPT_CDATA) && CData_Check(arg)) {
        return ((CDataObject *)arg)->c_type;
    }
#if PY_MAJOR_VERSION < 3
    else if (PyUnicode_Check(arg)) {
        CTypeDescrObject *result;
        arg = PyUnicode_AsASCIIString(arg);
        if (arg == NULL)
            return NULL;
        result = _ffi_type(ffi, arg, accept);
        Py_DECREF(arg);
        return result;
    }
#endif
    else {
        const char *m1 = (accept & ACCEPT_STRING) ? "string" : "";
        const char *m2 = (accept & ACCEPT_CTYPE) ? "ctype object" : "";
        const char *m3 = (accept & ACCEPT_CDATA) ? "cdata object" : "";
        const char *s12 = (*m1 && (*m2 || *m3)) ? " or " : "";
        const char *s23 = (*m2 && *m3) ? " or " : "";
        PyErr_Format(PyExc_TypeError, "expected a %s%s%s%s%s, got '%.200s'",
                     m1, s12, m2, s23, m3,
                     Py_TYPE(arg)->tp_name);
        return NULL;
    }
}

PyDoc_STRVAR(ffi_sizeof_doc,
"Return the size in bytes of the argument.\n"
"It can be a string naming a C type, or a 'cdata' instance.");

static PyObject *ffi_sizeof(FFIObject *self, PyObject *arg)
{
    Py_ssize_t size;

    if (CData_Check(arg)) {
        size = direct_sizeof_cdata((CDataObject *)arg);
    }
    else {
        CTypeDescrObject *ct = _ffi_type(self, arg, ACCEPT_ALL);
        if (ct == NULL)
            return NULL;
        size = ct->ct_size;
        if (size < 0) {
            PyErr_Format(FFIError, "don't know the size of ctype '%s'",
                         ct->ct_name);
            return NULL;
        }
    }
    return PyInt_FromSsize_t(size);
}

PyDoc_STRVAR(ffi_alignof_doc,
"Return the natural alignment size in bytes of the argument.\n"
"It can be a string naming a C type, or a 'cdata' instance.");

static PyObject *ffi_alignof(FFIObject *self, PyObject *arg)
{
    int align;
    CTypeDescrObject *ct = _ffi_type(self, arg, ACCEPT_ALL);
    if (ct == NULL)
        return NULL;

    align = get_alignment(ct);
    if (align < 0)
        return NULL;
    return PyInt_FromLong(align);
}

PyDoc_STRVAR(ffi_typeof_doc,
"Parse the C type given as a string and return the\n"
"corresponding <ctype> object.\n"
"It can also be used on 'cdata' instance to get its C type.");

static PyObject *_cpyextfunc_type_index(PyObject *x);  /* forward */

static PyObject *ffi_typeof(FFIObject *self, PyObject *arg)
{
    PyObject *x = (PyObject *)_ffi_type(self, arg, ACCEPT_STRING|ACCEPT_CDATA);
    if (x != NULL) {
        Py_INCREF(x);
    }
    else {
        x = _cpyextfunc_type_index(arg);
    }
    return x;
}

PyDoc_STRVAR(ffi_new_doc,
"Allocate an instance according to the specified C type and return a\n"
"pointer to it.  The specified C type must be either a pointer or an\n"
"array: ``new('X *')`` allocates an X and returns a pointer to it,\n"
"whereas ``new('X[n]')`` allocates an array of n X'es and returns an\n"
"array referencing it (which works mostly like a pointer, like in C).\n"
"You can also use ``new('X[]', n)`` to allocate an array of a\n"
"non-constant length n.\n"
"\n"
"The memory is initialized following the rules of declaring a global\n"
"variable in C: by default it is zero-initialized, but an explicit\n"
"initializer can be given which can be used to fill all or part of the\n"
"memory.\n"
"\n"
"When the returned <cdata> object goes out of scope, the memory is\n"
"freed.  In other words the returned <cdata> object has ownership of\n"
"the value of type 'cdecl' that it points to.  This means that the raw\n"
"data can be used as long as this object is kept alive, but must not be\n"
"used for a longer time.  Be careful about that when copying the\n"
"pointer to the memory somewhere else, e.g. into another structure.");

static PyObject *_ffi_new(FFIObject *self, PyObject *args, PyObject *kwds,
                          const cffi_allocator_t *allocator)
{
    CTypeDescrObject *ct;
    PyObject *arg, *init = Py_None;
    static char *keywords[] = {"cdecl", "init", NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:new", keywords,
                                     &arg, &init))
        return NULL;

    ct = _ffi_type(self, arg, ACCEPT_STRING|ACCEPT_CTYPE);
    if (ct == NULL)
        return NULL;

    return direct_newp(ct, init, allocator);
}

static PyObject *ffi_new(FFIObject *self, PyObject *args, PyObject *kwds)
{
    return _ffi_new(self, args, kwds, &default_allocator);
}

static PyObject *_ffi_new_with_allocator(PyObject *allocator, PyObject *args,
                                         PyObject *kwds)
{
    cffi_allocator_t alloc1;
    PyObject *my_alloc, *my_free;
    my_alloc = PyTuple_GET_ITEM(allocator, 1);
    my_free  = PyTuple_GET_ITEM(allocator, 2);
    alloc1.ca_alloc = (my_alloc == Py_None ? NULL : my_alloc);
    alloc1.ca_free  = (my_free  == Py_None ? NULL : my_free);
    alloc1.ca_dont_clear = (PyTuple_GET_ITEM(allocator, 3) == Py_False);

    return _ffi_new((FFIObject *)PyTuple_GET_ITEM(allocator, 0),
                    args, kwds, &alloc1);
}

PyDoc_STRVAR(ffi_new_allocator_doc,
"Return a new allocator, i.e. a function that behaves like ffi.new()\n"
"but uses the provided low-level 'alloc' and 'free' functions.\n"
"\n"
"'alloc' is called with the size as argument.  If it returns NULL, a\n"
"MemoryError is raised.  'free' is called with the result of 'alloc'\n"
"as argument.  Both can be either Python functions or directly C\n"
"functions.  If 'free' is None, then no free function is called.\n"
"If both 'alloc' and 'free' are None, the default is used.\n"
"\n"
"If 'should_clear_after_alloc' is set to False, then the memory\n"
"returned by 'alloc' is assumed to be already cleared (or you are\n"
"fine with garbage); otherwise CFFI will clear it.");

static PyObject *ffi_new_allocator(FFIObject *self, PyObject *args,
                                   PyObject *kwds)
{
    PyObject *allocator, *result;
    PyObject *my_alloc = Py_None, *my_free = Py_None;
    int should_clear_after_alloc = 1;
    static char *keywords[] = {"alloc", "free", "should_clear_after_alloc",
                               NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:new_allocator", keywords,
                                     &my_alloc, &my_free,
                                     &should_clear_after_alloc))
        return NULL;

    if (my_alloc == Py_None && my_free != Py_None) {
        PyErr_SetString(PyExc_TypeError, "cannot pass 'free' without 'alloc'");
        return NULL;
    }

    allocator = PyTuple_Pack(4,
                             (PyObject *)self,
                             my_alloc,
                             my_free,
                             PyBool_FromLong(should_clear_after_alloc));
    if (allocator == NULL)
        return NULL;

    {
        static PyMethodDef md = {"allocator",
                                 (PyCFunction)_ffi_new_with_allocator,
                                 METH_VARARGS | METH_KEYWORDS};
        result = PyCFunction_New(&md, allocator);
    }
    Py_DECREF(allocator);
    return result;
}

PyDoc_STRVAR(ffi_cast_doc,
"Similar to a C cast: returns an instance of the named C\n"
"type initialized with the given 'source'.  The source is\n"
"casted between integers or pointers of any type.");

static PyObject *ffi_cast(FFIObject *self, PyObject *args)
{
    CTypeDescrObject *ct;
    PyObject *ob, *arg;
    if (!PyArg_ParseTuple(args, "OO:cast", &arg, &ob))
        return NULL;

    ct = _ffi_type(self, arg, ACCEPT_STRING|ACCEPT_CTYPE);
    if (ct == NULL)
        return NULL;

    return do_cast(ct, ob);
}

PyDoc_STRVAR(ffi_string_doc,
"Return a Python string (or unicode string) from the 'cdata'.  If\n"
"'cdata' is a pointer or array of characters or bytes, returns the\n"
"null-terminated string.  The returned string extends until the first\n"
"null character, or at most 'maxlen' characters.  If 'cdata' is an\n"
"array then 'maxlen' defaults to its length.\n"
"\n"
"If 'cdata' is a pointer or array of wchar_t, returns a unicode string\n"
"following the same rules.\n"
"\n"
"If 'cdata' is a single character or byte or a wchar_t, returns it as a\n"
"string or unicode string.\n"
"\n"
"If 'cdata' is an enum, returns the value of the enumerator as a\n"
"string, or 'NUMBER' if the value is out of range.");

#define ffi_string  b_string     /* ffi_string() => b_string()
                                    from _cffi_backend.c */

PyDoc_STRVAR(ffi_unpack_doc,
"Unpack an array of C data of the given length,\n"
"returning a Python string/unicode/list.\n"
"\n"
"If 'cdata' is a pointer to 'char', returns a byte string.\n"
"It does not stop at the first null.  This is equivalent to:\n"
"ffi.buffer(cdata, length)[:]\n"
"\n"
"If 'cdata' is a pointer to 'wchar_t', returns a unicode string.\n"
"'length' is measured in wchar_t's; it is not the size in bytes.\n"
"\n"
"If 'cdata' is a pointer to anything else, returns a list of\n"
"'length' items.  This is a faster equivalent to:\n"
"[cdata[i] for i in range(length)]");

#define ffi_unpack  b_unpack     /* ffi_unpack() => b_unpack()
                                    from _cffi_backend.c */


PyDoc_STRVAR(ffi_offsetof_doc,
"Return the offset of the named field inside the given structure or\n"
"array, which must be given as a C type name.  You can give several\n"
"field names in case of nested structures.  You can also give numeric\n"
"values which correspond to array items, in case of an array type.");

static PyObject *ffi_offsetof(FFIObject *self, PyObject *args)
{
    PyObject *arg;
    CTypeDescrObject *ct;
    Py_ssize_t i, offset;

    if (PyTuple_Size(args) < 2) {
        PyErr_SetString(PyExc_TypeError,
                        "offsetof() expects at least 2 arguments");
        return NULL;
    }

    arg = PyTuple_GET_ITEM(args, 0);
    ct = _ffi_type(self, arg, ACCEPT_STRING|ACCEPT_CTYPE);
    if (ct == NULL)
        return NULL;

    offset = 0;
    for (i = 1; i < PyTuple_GET_SIZE(args); i++) {
        Py_ssize_t ofs1;
        ct = direct_typeoffsetof(ct, PyTuple_GET_ITEM(args, i), i > 1, &ofs1);
        if (ct == NULL)
            return NULL;
        offset += ofs1;
    }
    return PyInt_FromSsize_t(offset);
}

PyDoc_STRVAR(ffi_addressof_doc,
"Limited equivalent to the '&' operator in C:\n"
"\n"
"1. ffi.addressof(<cdata 'struct-or-union'>) returns a cdata that is a\n"
"pointer to this struct or union.\n"
"\n"
"2. ffi.addressof(<cdata>, field-or-index...) returns the address of a\n"
"field or array item inside the given structure or array, recursively\n"
"in case of nested structures or arrays.\n"
"\n"
"3. ffi.addressof(<library>, \"name\") returns the address of the named\n"
"function or global variable.");

static PyObject *address_of_global_var(PyObject *args);  /* forward */

static PyObject *ffi_addressof(FFIObject *self, PyObject *args)
{
    PyObject *arg, *z, *result;
    CTypeDescrObject *ct;
    Py_ssize_t i, offset = 0;
    int accepted_flags;

    if (PyTuple_Size(args) < 1) {
        PyErr_SetString(PyExc_TypeError,
                        "addressof() expects at least 1 argument");
        return NULL;
    }

    arg = PyTuple_GET_ITEM(args, 0);
    if (LibObject_Check(arg)) {
        /* case 3 in the docstring */
        return address_of_global_var(args);
    }

    ct = _ffi_type(self, arg, ACCEPT_CDATA);
    if (ct == NULL)
        return NULL;

    if (PyTuple_GET_SIZE(args) == 1) {
        /* case 1 in the docstring */
        accepted_flags = CT_STRUCT | CT_UNION | CT_ARRAY;
        if ((ct->ct_flags & accepted_flags) == 0) {
            PyErr_SetString(PyExc_TypeError,
                            "expected a cdata struct/union/array object");
            return NULL;
        }
    }
    else {
        /* case 2 in the docstring */
        accepted_flags = CT_STRUCT | CT_UNION | CT_ARRAY | CT_POINTER;
        if ((ct->ct_flags & accepted_flags) == 0) {
            PyErr_SetString(PyExc_TypeError,
                        "expected a cdata struct/union/array/pointer object");
            return NULL;
        }
        for (i = 1; i < PyTuple_GET_SIZE(args); i++) {
            Py_ssize_t ofs1;
            ct = direct_typeoffsetof(ct, PyTuple_GET_ITEM(args, i),
                                     i > 1, &ofs1);
            if (ct == NULL)
                return NULL;
            offset += ofs1;
        }
    }

    z = new_pointer_type(ct);
    if (z == NULL)
        return NULL;

    result = new_simple_cdata(((CDataObject *)arg)->c_data + offset,
                              (CTypeDescrObject *)z);
    Py_DECREF(z);
    return result;
}

static PyObject *_combine_type_name_l(CTypeDescrObject *ct,
                                      size_t extra_text_len)
{
    size_t base_name_len;
    PyObject *result;
    char *p;

    base_name_len = strlen(ct->ct_name);
    result = PyBytes_FromStringAndSize(NULL, base_name_len + extra_text_len);
    if (result == NULL)
        return NULL;

    p = PyBytes_AS_STRING(result);
    memcpy(p, ct->ct_name, ct->ct_name_position);
    p += ct->ct_name_position;
    p += extra_text_len;
    memcpy(p, ct->ct_name + ct->ct_name_position,
           base_name_len - ct->ct_name_position);
    return result;
}

PyDoc_STRVAR(ffi_getctype_doc,
"Return a string giving the C type 'cdecl', which may be itself a\n"
"string or a <ctype> object.  If 'replace_with' is given, it gives\n"
"extra text to append (or insert for more complicated C types), like a\n"
"variable name, or '*' to get actually the C type 'pointer-to-cdecl'.");

static PyObject *ffi_getctype(FFIObject *self, PyObject *args, PyObject *kwds)
{
    PyObject *c_decl, *res;
    char *p, *replace_with = "";
    int add_paren, add_space;
    CTypeDescrObject *ct;
    size_t replace_with_len;
    static char *keywords[] = {"cdecl", "replace_with", NULL};
#if PY_MAJOR_VERSION >= 3
    PyObject *u;
#endif

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:getctype", keywords,
                                     &c_decl, &replace_with))
        return NULL;

    ct = _ffi_type(self, c_decl, ACCEPT_STRING|ACCEPT_CTYPE);
    if (ct == NULL)
        return NULL;

    while (replace_with[0] != 0 && isspace(replace_with[0]))
        replace_with++;
    replace_with_len = strlen(replace_with);
    while (replace_with_len > 0 && isspace(replace_with[replace_with_len - 1]))
        replace_with_len--;

    add_paren = (replace_with[0] == '*' &&
                 ((ct->ct_flags & CT_ARRAY) != 0));
    add_space = (!add_paren && replace_with_len > 0 &&
                 replace_with[0] != '[' && replace_with[0] != '(');

    res = _combine_type_name_l(ct, replace_with_len + add_space + 2*add_paren);
    if (res == NULL)
        return NULL;

    p = PyBytes_AS_STRING(res) + ct->ct_name_position;
    if (add_paren)
        *p++ = '(';
    if (add_space)
        *p++ = ' ';
    memcpy(p, replace_with, replace_with_len);
    if (add_paren)
        p[replace_with_len] = ')';

#if PY_MAJOR_VERSION >= 3
    /* bytes -> unicode string */
    u = PyUnicode_DecodeLatin1(PyBytes_AS_STRING(res),
                               PyBytes_GET_SIZE(res),
                               NULL);
    Py_DECREF(res);
    res = u;
#endif

    return res;
}

PyDoc_STRVAR(ffi_new_handle_doc,
"Return a non-NULL cdata of type 'void *' that contains an opaque\n"
"reference to the argument, which can be any Python object.  To cast it\n"
"back to the original object, use from_handle().  You must keep alive\n"
"the cdata object returned by new_handle()!");

static PyObject *ffi_new_handle(FFIObject *self, PyObject *arg)
{
    /* g_ct_voidp is equal to <ctype 'void *'> */
    return newp_handle(g_ct_voidp, arg);
}

PyDoc_STRVAR(ffi_from_handle_doc,
"Cast a 'void *' back to a Python object.  Must be used *only* on the\n"
"pointers returned by new_handle(), and *only* as long as the exact\n"
"cdata object returned by new_handle() is still alive (somewhere else\n"
"in the program).  Failure to follow these rules will crash.");

#define ffi_from_handle  b_from_handle   /* ffi_from_handle => b_from_handle
                                            from _cffi_backend.c */

PyDoc_STRVAR(ffi_from_buffer_doc,
"Return a <cdata 'char[]'> that points to the data of the given Python\n"
"object, which must support the buffer interface.  Note that this is\n"
"not meant to be used on the built-in types str or unicode\n"
"(you can build 'char[]' arrays explicitly) but only on objects\n"
"containing large quantities of raw data in some other format, like\n"
"'array.array' or numpy arrays.");

static PyObject *ffi_from_buffer(FFIObject *self, PyObject *args,
                                 PyObject *kwds)
{
    PyObject *cdecl1, *python_buf = NULL;
    CTypeDescrObject *ct;
    int require_writable = 0;
    static char *keywords[] = {"cdecl", "python_buffer",
                               "require_writable", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:from_buffer", keywords,
                                     &cdecl1, &python_buf, &require_writable))
        return NULL;

    if (python_buf == NULL) {
        python_buf = cdecl1;
        ct = g_ct_chararray;
    }
    else {
        ct = _ffi_type(self, cdecl1, ACCEPT_STRING|ACCEPT_CTYPE);
        if (ct == NULL)
            return NULL;
    }
    return direct_from_buffer(ct, python_buf, require_writable);
}

PyDoc_STRVAR(ffi_gc_doc,
"Return a new cdata object that points to the same data.\n"
"Later, when this new cdata object is garbage-collected,\n"
"'destructor(old_cdata_object)' will be called.\n"
"\n"
"The optional 'size' gives an estimate of the size, used to\n"
"trigger the garbage collection more eagerly.  So far only used\n"
"on PyPy.  It tells the GC that the returned object keeps alive\n"
"roughly 'size' bytes of external memory.");

#define ffi_gc  b_gcp     /* ffi_gc() => b_gcp()
                             from _cffi_backend.c */

PyDoc_STRVAR(ffi_def_extern_doc,
"A decorator.  Attaches the decorated Python function to the C code\n"
"generated for the 'extern \"Python\"' function of the same name.\n"
"Calling the C function will then invoke the Python function.\n"
"\n"
"Optional arguments: 'name' is the name of the C function, if\n"
"different from the Python function; and 'error' and 'onerror'\n"
"handle what occurs if the Python function raises an exception\n"
"(see the docs for details).");

/* forward; see call_python.c */
static PyObject *_ffi_def_extern_decorator(PyObject *, PyObject *);

static PyObject *ffi_def_extern(FFIObject *self, PyObject *args,
                                PyObject *kwds)
{
    static PyMethodDef md = {"def_extern_decorator",
                             (PyCFunction)_ffi_def_extern_decorator, METH_O};
    PyObject *name = Py_None, *error = Py_None;
    PyObject *res, *onerror = Py_None;
    static char *keywords[] = {"name", "error", "onerror", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", keywords,
                                     &name, &error, &onerror))
        return NULL;

    args = Py_BuildValue("(OOOO)", (PyObject *)self, name, error, onerror);
    if (args == NULL)
        return NULL;

    res = PyCFunction_New(&md, args);
    Py_DECREF(args);
    return res;
}

PyDoc_STRVAR(ffi_callback_doc,
"Return a callback object or a decorator making such a callback object.\n"
"'cdecl' must name a C function pointer type.  The callback invokes the\n"
"specified 'python_callable' (which may be provided either directly or\n"
"via a decorator).  Important: the callback object must be manually\n"
"kept alive for as long as the callback may be invoked from the C code.");

static PyObject *_ffi_callback_decorator(PyObject *outer_args, PyObject *fn)
{
    PyObject *res, *old;

    old = PyTuple_GET_ITEM(outer_args, 1);
    PyTuple_SET_ITEM(outer_args, 1, fn);
    res = b_callback(NULL, outer_args);
    PyTuple_SET_ITEM(outer_args, 1, old);
    return res;
}

static PyObject *ffi_callback(FFIObject *self, PyObject *args, PyObject *kwds)
{
    PyObject *c_decl, *python_callable = Py_None, *error = Py_None;
    PyObject *res, *onerror = Py_None;
    static char *keywords[] = {"cdecl", "python_callable", "error",
                               "onerror", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", keywords,
                                     &c_decl, &python_callable, &error,
                                     &onerror))
        return NULL;

    c_decl = (PyObject *)_ffi_type(self, c_decl, ACCEPT_STRING | ACCEPT_CTYPE |
                                                 CONSIDER_FN_AS_FNPTR);
    if (c_decl == NULL)
        return NULL;

    args = Py_BuildValue("(OOOO)", c_decl, python_callable, error, onerror);
    if (args == NULL)
        return NULL;

    if (python_callable != Py_None) {
        res = b_callback(NULL, args);
    }
    else {
        static PyMethodDef md = {"callback_decorator",
                                 (PyCFunction)_ffi_callback_decorator, METH_O};
        res = PyCFunction_New(&md, args);
    }
    Py_DECREF(args);
    return res;
}

#ifdef MS_WIN32
PyDoc_STRVAR(ffi_getwinerror_doc,
"Return either the GetLastError() or the error number given by the\n"
"optional 'code' argument, as a tuple '(code, message)'.");

#define ffi_getwinerror  b_getwinerror  /* ffi_getwinerror() => b_getwinerror()
                                           from misc_win32.h */
#endif

PyDoc_STRVAR(ffi_errno_doc, "the value of 'errno' from/to the C calls");

static PyObject *ffi_get_errno(PyObject *self, void *closure)
{
    /* xxx maybe think about how to make the saved errno local
       to an ffi instance */
    return b_get_errno(NULL, NULL);
}

static int ffi_set_errno(PyObject *self, PyObject *newval, void *closure)
{
    PyObject *x = b_set_errno(NULL, newval);
    if (x == NULL)
        return -1;
    Py_DECREF(x);
    return 0;
}

PyDoc_STRVAR(ffi_dlopen_doc,
"Load and return a dynamic library identified by 'name'.  The standard\n"
"C library can be loaded by passing None.\n"
"\n"
"Note that functions and types declared with 'ffi.cdef()' are not\n"
"linked to a particular library, just like C headers.  In the library\n"
"we only look for the actual (untyped) symbols at the time of their\n"
"first access.");

PyDoc_STRVAR(ffi_dlclose_doc,
"Close a library obtained with ffi.dlopen().  After this call, access to\n"
"functions or variables from the library will fail (possibly with a\n"
"segmentation fault).");

static PyObject *ffi_dlopen(PyObject *self, PyObject *args);  /* forward */
static PyObject *ffi_dlclose(PyObject *self, PyObject *args);  /* forward */

PyDoc_STRVAR(ffi_int_const_doc,
"Get the value of an integer constant.\n"
"\n"
"'ffi.integer_const(\"xxx\")' is equivalent to 'lib.xxx' if xxx names an\n"
"integer constant.  The point of this function is limited to use cases\n"
"where you have an 'ffi' object but not any associated 'lib' object.");

static PyObject *ffi_int_const(FFIObject *self, PyObject *args, PyObject *kwds)
{
    char *name;
    PyObject *x;
    static char *keywords[] = {"name", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", keywords, &name))
        return NULL;

    x = ffi_fetch_int_constant(self, name, 0);

    if (x == NULL && !PyErr_Occurred()) {
        PyErr_Format(PyExc_AttributeError,
                     "integer constant '%.200s' not found", name);
    }
    return x;
}

PyDoc_STRVAR(ffi_list_types_doc,
"Returns the user type names known to this FFI instance.\n"
"This returns a tuple containing three lists of names:\n"
"(typedef_names, names_of_structs, names_of_unions)");

static PyObject *ffi_list_types(FFIObject *self, PyObject *noargs)
{
    Py_ssize_t i, n1 = self->types_builder.ctx.num_typenames;
    Py_ssize_t n23 = self->types_builder.ctx.num_struct_unions;
    PyObject *o, *lst[3] = {NULL, NULL, NULL}, *result = NULL;

    lst[0] = PyList_New(n1);
    if (lst[0] == NULL)
        goto error;
    lst[1] = PyList_New(0);
    if (lst[1] == NULL)
        goto error;
    lst[2] = PyList_New(0);
    if (lst[2] == NULL)
        goto error;

    for (i = 0; i < n1; i++) {
        o = PyText_FromString(self->types_builder.ctx.typenames[i].name);
        if (o == NULL)
            goto error;
        PyList_SET_ITEM(lst[0], i, o);
    }

    for (i = 0; i < n23; i++) {
        const struct _cffi_struct_union_s *s;
        int err, index;

        s = &self->types_builder.ctx.struct_unions[i];
        if (s->name[0] == '$')
            continue;

        o = PyText_FromString(s->name);
        if (o == NULL)
            goto error;
        index = (s->flags & _CFFI_F_UNION) ? 2 : 1;
        err = PyList_Append(lst[index], o);
        Py_DECREF(o);
        if (err < 0)
            goto error;
    }
    result = PyTuple_Pack(3, lst[0], lst[1], lst[2]);
    /* fall-through */
 error:
    Py_XDECREF(lst[2]);
    Py_XDECREF(lst[1]);
    Py_XDECREF(lst[0]);
    return result;
}

PyDoc_STRVAR(ffi_memmove_doc,
"ffi.memmove(dest, src, n) copies n bytes of memory from src to dest.\n"
"\n"
"Like the C function memmove(), the memory areas may overlap;\n"
"apart from that it behaves like the C function memcpy().\n"
"\n"
"'src' can be any cdata ptr or array, or any Python buffer object.\n"
"'dest' can be any cdata ptr or array, or a writable Python buffer\n"
"object.  The size to copy, 'n', is always measured in bytes.\n"
"\n"
"Unlike other methods, this one supports all Python buffer including\n"
"byte strings and bytearrays---but it still does not support\n"
"non-contiguous buffers.");

#define ffi_memmove  b_memmove     /* ffi_memmove() => b_memmove()
                                      from _cffi_backend.c */

PyDoc_STRVAR(ffi_init_once_doc,
"init_once(function, tag): run function() once.  More precisely,\n"
"'function()' is called the first time we see a given 'tag'.\n"
"\n"
"The return value of function() is remembered and returned by the current\n"
"and all future init_once() with the same tag.  If init_once() is called\n"
"from multiple threads in parallel, all calls block until the execution\n"
"of function() is done.  If function() raises an exception, it is\n"
"propagated and nothing is cached.");

#if PY_MAJOR_VERSION < 3
/* PyCapsule_New is redefined to be PyCObject_FromVoidPtr in _cffi_backend,
   which gives 2.6 compatibility; but the destructor signature is different */
static void _free_init_once_lock(void *lock)
{
    PyThread_free_lock((PyThread_type_lock)lock);
}
#else
static void _free_init_once_lock(PyObject *capsule)
{
    PyThread_type_lock lock;
    lock = PyCapsule_GetPointer(capsule, "cffi_init_once_lock");
    if (lock != NULL)
        PyThread_free_lock(lock);
}
#endif

static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds)
{
    static char *keywords[] = {"func", "tag", NULL};
    PyObject *cache, *func, *tag, *tup, *res, *x, *lockobj;
    PyThread_type_lock lock;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", keywords, &func, &tag))
        return NULL;

    /* a lot of fun with reference counting and error checking
       in this function */

    /* atomically get or create a new dict (no GIL release) */
    cache = self->init_once_cache;
    if (cache == NULL) {
        cache = PyDict_New();
        if (cache == NULL)
            return NULL;
        self->init_once_cache = cache;
    }

    /* get the tuple from cache[tag], or make a new one: (False, lock) */
    tup = PyDict_GetItem(cache, tag);
    if (tup == NULL) {
        lock = PyThread_allocate_lock();
        if (lock == NULL)
            return NULL;
        x = PyCapsule_New(lock, "cffi_init_once_lock", _free_init_once_lock);
        if (x == NULL) {
            PyThread_free_lock(lock);
            return NULL;
        }
        tup = PyTuple_Pack(2, Py_False, x);
        Py_DECREF(x);
        if (tup == NULL)
            return NULL;
        x = tup;

        /* Possible corner case if 'tag' is an object overriding __eq__
           in pure Python: the GIL may be released when we are running it.
           We really need to call dict.setdefault(). */
        tup = PyObject_CallMethod(cache, "setdefault", "OO", tag, x);
        Py_DECREF(x);
        if (tup == NULL)
            return NULL;

        Py_DECREF(tup);   /* there is still a ref inside the dict */
    }

    res = PyTuple_GET_ITEM(tup, 1);
    Py_INCREF(res);

    if (PyTuple_GET_ITEM(tup, 0) == Py_True) {
        /* tup == (True, result): return the result. */
        return res;
    }

    /* tup == (False, lock) */
    lockobj = res;
    lock = (PyThread_type_lock)PyCapsule_GetPointer(lockobj,
                                                    "cffi_init_once_lock");
    if (lock == NULL) {
        Py_DECREF(lockobj);
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
    PyThread_acquire_lock(lock, WAIT_LOCK);
    Py_END_ALLOW_THREADS

    x = PyDict_GetItem(cache, tag);
    if (x != NULL && PyTuple_GET_ITEM(x, 0) == Py_True) {
        /* the real result was put in the dict while we were waiting
           for PyThread_acquire_lock() above */
        res = PyTuple_GET_ITEM(x, 1);
        Py_INCREF(res);
    }
    else {
        res = PyObject_CallFunction(func, "");
        if (res != NULL) {
            tup = PyTuple_Pack(2, Py_True, res);
            if (tup == NULL || PyDict_SetItem(cache, tag, tup) < 0) {
                Py_DECREF(res);
                res = NULL;
            }
            Py_XDECREF(tup);
        }
    }

    PyThread_release_lock(lock);
    Py_DECREF(lockobj);
    return res;
}

PyDoc_STRVAR(ffi_release_doc,
"Release now the resources held by a 'cdata' object from ffi.new(),\n"
"ffi.gc() or ffi.from_buffer().  The cdata object must not be used\n"
"afterwards.\n"
"\n"
"'ffi.release(cdata)' is equivalent to 'cdata.__exit__()'.\n"
"\n"
"Note that on CPython this method has no effect (so far) on objects\n"
"returned by ffi.new(), because the memory is allocated inline with the\n"
"cdata object and cannot be freed independently.  It might be fixed in\n"
"future releases of cffi.");

#define ffi_release  b_release     /* ffi_release() => b_release()
                                      from _cffi_backend.c */


#define METH_VKW  (METH_VARARGS | METH_KEYWORDS)
static PyMethodDef ffi_methods[] = {
 {"addressof",  (PyCFunction)ffi_addressof,  METH_VARARGS, ffi_addressof_doc},
 {"alignof",    (PyCFunction)ffi_alignof,    METH_O,       ffi_alignof_doc},
 {"def_extern", (PyCFunction)ffi_def_extern, METH_VKW,     ffi_def_extern_doc},
 {"callback",   (PyCFunction)ffi_callback,   METH_VKW,     ffi_callback_doc},
 {"cast",       (PyCFunction)ffi_cast,       METH_VARARGS, ffi_cast_doc},
 {"dlclose",    (PyCFunction)ffi_dlclose,    METH_VARARGS, ffi_dlclose_doc},
 {"dlopen",     (PyCFunction)ffi_dlopen,     METH_VARARGS, ffi_dlopen_doc},
 {"from_buffer",(PyCFunction)ffi_from_buffer,METH_VKW,     ffi_from_buffer_doc},
 {"from_handle",(PyCFunction)ffi_from_handle,METH_O,       ffi_from_handle_doc},
 {"gc",         (PyCFunction)ffi_gc,         METH_VKW,     ffi_gc_doc},
 {"getctype",   (PyCFunction)ffi_getctype,   METH_VKW,     ffi_getctype_doc},
#ifdef MS_WIN32
 {"getwinerror",(PyCFunction)ffi_getwinerror,METH_VKW,     ffi_getwinerror_doc},
#endif
 {"init_once",  (PyCFunction)ffi_init_once,  METH_VKW,     ffi_init_once_doc},
 {"integer_const",(PyCFunction)ffi_int_const,METH_VKW,     ffi_int_const_doc},
 {"list_types", (PyCFunction)ffi_list_types, METH_NOARGS,  ffi_list_types_doc},
 {"memmove",    (PyCFunction)ffi_memmove,    METH_VKW,     ffi_memmove_doc},
 {"new",        (PyCFunction)ffi_new,        METH_VKW,     ffi_new_doc},
{"new_allocator",(PyCFunction)ffi_new_allocator,METH_VKW,ffi_new_allocator_doc},
 {"new_handle", (PyCFunction)ffi_new_handle, METH_O,       ffi_new_handle_doc},
 {"offsetof",   (PyCFunction)ffi_offsetof,   METH_VARARGS, ffi_offsetof_doc},
 {"release",    (PyCFunction)ffi_release,    METH_O,       ffi_release_doc},
 {"sizeof",     (PyCFunction)ffi_sizeof,     METH_O,       ffi_sizeof_doc},
 {"string",     (PyCFunction)ffi_string,     METH_VKW,     ffi_string_doc},
 {"typeof",     (PyCFunction)ffi_typeof,     METH_O,       ffi_typeof_doc},
 {"unpack",     (PyCFunction)ffi_unpack,     METH_VKW,     ffi_unpack_doc},
 {NULL}
};

static PyGetSetDef ffi_getsets[] = {
    {"errno",  ffi_get_errno,  ffi_set_errno,  ffi_errno_doc},
    {NULL}
};

static PyTypeObject FFI_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_cffi_backend.FFI",
    sizeof(FFIObject),
    0,
    (destructor)ffi_dealloc,                    /* tp_dealloc */
    0,                                          /* tp_print */
    0,                                          /* tp_getattr */
    0,                                          /* tp_setattr */
    0,                                          /* tp_compare */
    0,                                          /* tp_repr */
    0,                                          /* tp_as_number */
    0,                                          /* tp_as_sequence */
    0,                                          /* tp_as_mapping */
    0,                                          /* tp_hash */
    0,                                          /* tp_call */
    0,                                          /* tp_str */
    PyObject_GenericGetAttr,                    /* tp_getattro */
    0,                                          /* tp_setattro */
    0,                                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
    0,                                          /* tp_doc */
    (traverseproc)ffi_traverse,                 /* tp_traverse */
    0,                                          /* tp_clear */
    0,                                          /* tp_richcompare */
    0,                                          /* tp_weaklistoffset */
    0,                                          /* tp_iter */
    0,                                          /* tp_iternext */
    ffi_methods,                                /* tp_methods */
    0,                                          /* tp_members */
    ffi_getsets,                                /* tp_getset */
    0,                                          /* tp_base */
    0,                                          /* tp_dict */
    0,                                          /* tp_descr_get */
    0,                                          /* tp_descr_set */
    0,                                          /* tp_dictoffset */
    ffiobj_init,                                /* tp_init */
    0,                                          /* tp_alloc */
    ffiobj_new,                                 /* tp_new */
    PyObject_GC_Del,                            /* tp_free */
};


static PyObject *
_fetch_external_struct_or_union(const struct _cffi_struct_union_s *s,
                                PyObject *included_ffis, int recursion)
{
    Py_ssize_t i;

    if (included_ffis == NULL)
        return NULL;

    if (recursion > 100) {
        PyErr_SetString(PyExc_RuntimeError,
                        "recursion overflow in ffi.include() delegations");
        return NULL;
    }

    for (i = 0; i < PyTuple_GET_SIZE(included_ffis); i++) {
        FFIObject *ffi1;
        const struct _cffi_struct_union_s *s1;
        int sindex;
        PyObject *x;

        ffi1 = (FFIObject *)PyTuple_GET_ITEM(included_ffis, i);
        sindex = search_in_struct_unions(&ffi1->types_builder.ctx, s->name,
                                         strlen(s->name));
        if (sindex < 0)  /* not found at all */
            continue;
        s1 = &ffi1->types_builder.ctx.struct_unions[sindex];
        if ((s1->flags & (_CFFI_F_EXTERNAL | _CFFI_F_UNION))
                == (s->flags & _CFFI_F_UNION)) {
            /* s1 is not external, and the same kind (struct or union) as s */
            return _realize_c_struct_or_union(&ffi1->types_builder, sindex);
        }
        /* not found, look more recursively */
        x = _fetch_external_struct_or_union(
                s, ffi1->types_builder.included_ffis, recursion + 1);
        if (x != NULL || PyErr_Occurred())
            return x;   /* either found, or got an error */
    }
    return NULL;   /* not found at all, leave without an error */
}