summaryrefslogtreecommitdiff
path: root/sandbox/docpy-writer/input/tutorial.txt
blob: 6d2a8abd59f4bedb553641531ac72f51867c529b (plain)
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
ctypes tutorial
+++++++++++++++

This tutorial describes version 0.9.9 of ``ctypes``. 
Since older versions are quite common, I'll mention major differences
when needed.

Loading dynamic link libraries
------------------------------

``ctypes`` exports the ``cdll``, and on Windows also ``windll`` and
``oledll`` objects to load dynamic link libraries.

You load libraries by accessing them as attributes of these objects.
``cdll`` loads libraries which export functions using the standard
``cdecl`` calling convention, while ``windll`` libraries call
functions using the ``stdcall`` calling convention. ``oledll`` also
uses the ``stdcall`` calling convention, and assumes the functions
return a Windows ``HRESULT`` error code. The error code is used to
automatically raise ``WindowsError`` Python exceptions when the
function call fails.

Here are some examples for Windows, note that ``msvcrt`` is the MS
standard C library containing most standard C functions, and uses the
cdecl calling convention::

      >>> from ctypes import *
      >>> print windll.kernel32
      <WinDLL 'kernel32', handle 7c800000 at ...>
      >>> print cdll.msvcrt
      <CDLL 'msvcrt', handle 77be0000 at ...>

In principle the same way should work on Linux, but most of the time
it seems required to specify the search path in this way. So this
example shows also how to load libraries by specifying their
filename::

       >>> from ctypes import *
       >>> libc = cdll.load("/lib/libc.so.6") # doctest: +SKIP
       <CDLL '/lib/libc.so.6', handle 40018c28 at 4019978c>
       >>>

**Note:** in older versions, the ``LoadLibrary`` method should be used
instead.

This tutorial uses windows in its examples, however, functions from
the standard C library like ``strchr`` and ``printf`` should also work
on Linux and other systems.


Accessing functions from loaded dlls
------------------------------------

Functions are accessed as attributes of dll objects::

      >>> from ctypes import *
      >>> print cdll.msvcrt.printf
      <_FuncPtr object at 0x...>
      >>> print windll.kernel32.GetModuleHandleA
      <_FuncPtr object at 0x...>
      >>> print windll.kernel32.MyOwnFunction
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "ctypes.py", line 239, in __getattr__
          func = _StdcallFuncPtr(name, self)
      AttributeError: function 'MyOwnFunction' not found


Note that win32 system dlls like ``kernel32`` and ``user32`` often
export ANSI as well as UNICODE versions of a function. The UNICODE
version is exported with an ``W`` appended to the name, while the ANSI
version is exported with an ``A`` appended to the name. The win32
``GetModuleHandle`` function, which returns a *module handle* for a
given module name, has the following C prototype, and a macro is used
to expose one of them as ``GetModuleHandle`` depending on whether
UNICODE is defined or not::

      /* ANSI version */
      HMODULE GetModuleHandleA(LPCSTR lpModuleName);
      /* UNICODE version */
      HMODULE GetModuleHandleW(LPCWSTR lpModuleName);

``windll`` does not try to select one of them by magic, you must
access the version you need by specifying ``GetModuleHandleA`` or
``GetModuleHandleW`` explicitely, and then call it with normal strings
or unicode strings respectively.

Sometimes, dlls export functions with names which aren't valid Python
identifiers, like ``"??2@YAPAXI@Z"``. In this case you have to use
``getattr`` to retrieve the function (XXX Better example?)::

      >>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")
      <_FuncPtr object at 0x...>
      >>>


Calling functions
-----------------

You can call these functions like any other Python callable. This
example uses the ``time()`` function, which returns system time in
seconds since the UNIX epoch, and the ``GetModuleHandleA()`` function,
which returns a win32 module handle.

This example calls both functions with a NULL pointer (``None`` should
be used as the NULL pointer)::

      >>> from ctypes import *
      >>> print cdll.msvcrt.time(None)
      1143...
      >>> print hex(windll.kernel32.GetModuleHandleA(None))
      0x1d000000

``ctypes`` tries to protect you from calling functions with the wrong
number of arguments.  Unfortunately this only works on Windows.  It
does this by examining the stack after the function returns::

      >>> windll.kernel32.GetModuleHandleA()
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      ValueError: Procedure probably called with not enough arguments (4 bytes missing)
      >>> windll.kernel32.GetModuleHandleA(0, 0)
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      ValueError: Procedure probably called with too many arguments (4 bytes in excess)
      >>>

On Windows, ``ctypes`` uses win32 structured exception handling to
prevent crashes from general protection faults when functions are
called with invalid argument values::

      >>> windll.kernel32.GetModuleHandleA(32)
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      WindowsError: exception: access violation reading 0x00000020
      >>>

There are, however, enough ways to crash Python with ``ctypes``, so
you should be careful anyway.

Python integers, strings and unicode strings are the only objects that
can directly be used as parameters in these function calls.

Before we move on calling functions with other parameter types, we
have to learn more about ``ctypes`` data types.

Simple data types
-----------------

``ctypes`` defines a number of primitive C compatible data types :

    +---------------+-----------------------+-----------+
    |ctypes type    |C type                 |Python type|
    +===============+=======================+===========+
    |``c_char``     |``char``               |character  |
    +---------------+-----------------------+-----------+
    |``c_byte``     |``char``               |integer    |
    +---------------+-----------------------+-----------+
    |``c_ubyte``    |``unsigned char``      |integer    |
    +---------------+-----------------------+-----------+
    |``c_short``    |``short``              |integer    |
    +---------------+-----------------------+-----------+
    |``c_ushort``   |``unsigned short``     |integer    |
    +---------------+-----------------------+-----------+
    |``c_int``      |``int``                |integer    |
    +---------------+-----------------------+-----------+
    |``c_uint``     |``unsigned int``       |integer    |
    +---------------+-----------------------+-----------+
    |``c_long``     |``long``               |integer    |
    +---------------+-----------------------+-----------+
    |``c_ulong``    |``unsigned long``      |long       |
    +---------------+-----------------------+-----------+
    |``c_longlong`` |``__int64`` or         |long       |
    |               |``long long``          |           |
    +---------------+-----------------------+-----------+
    |``c_ulonglong``|``unsigned __int64`` or|long       |
    |               |``unsigned long long`` |           |
    +---------------+-----------------------+-----------+
    |``c_float``    |``float``              |float      |
    +---------------+-----------------------+-----------+
    |``c_double``   |``double``             |float      |
    +---------------+-----------------------+-----------+
    |``c_char_p``   |``char *``             |string or  |
    |               |(NUL terminated)       |``None``   |
    +---------------+-----------------------+-----------+
    |``c_wchar_p``  |``wchar_t *``          |unicode or |
    |               |(NUL terminated)       |``None``   |
    +---------------+-----------------------+-----------+
    |``c_void_p``   |``void *``             |integer or |
    |               |                       |``None``   |
    +---------------+-----------------------+-----------+



All these types can be created by calling them with an optional
initializer of the correct type and value::

      >>> c_int()
      c_long(0)
      >>> c_char_p("Hello, World")
      c_char_p('Hello, World')
      >>> c_ushort(-3)
      c_ushort(65533)
      >>>
    
Since these types are mutable, their value can also be changed
afterwards::

      >>> i = c_int(42)
      >>> print i
      c_long(42)
      >>> print i.value
      42
      >>> i.value = -99
      >>> print i.value
      -99
      >>>

Assigning a new value to instances of the pointer types ``c_char_p``,
``c_wchar_p``, and ``c_void_p`` changes the *memory location* they
point to, *not the contents* of the memory block (of course not,
because Python strings are immutable)::

      >>> s = "Hello, World"
      >>> c_s = c_char_p(s)
      >>> print c_s
      c_char_p('Hello, World')
      >>> c_s.value = "Hi, there"
      >>> print c_s
      c_char_p('Hi, there')
      >>> print s                 # first string is unchanged
      Hello, World

You should be careful, however, not to pass them to functions
expecting pointers to mutable memory. If you need mutable memory
blocks, ctypes has a ``create_string_buffer`` function which creates
these in various ways.  The current memory block contents can be
accessed (or changed) with the ``raw`` property, if you want to access
it as NUL terminated string, use the ``string`` property::

      >>> from ctypes import *
      >>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
      >>> print sizeof(p), repr(p.raw)
      3 '\x00\x00\x00'
      >>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
      >>> print sizeof(p), repr(p.raw)
      6 'Hello\x00'
      >>> print repr(p.value)
      'Hello'
      >>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
      >>> print sizeof(p), repr(p.raw)
      10 'Hello\x00\x00\x00\x00\x00'
      >>> p.value = "Hi"      
      >>> print sizeof(p), repr(p.raw)
      10 'Hi\x00lo\x00\x00\x00\x00\x00'
      >>>

The ``create_string_buffer`` function replaces the ``c_buffer``
function (which is still available as an alias), as well as the
``c_string`` function from earlier ctypes releases.  To create a
mutable memory block containing unicode characters of the C type
``wchar_t`` use the ``create_unicode_buffer`` function.

Calling functions, continued
----------------------------

Note that printf prints to the real standard output channel, *not* to
``sys.stdout``, so these examples will only work at the console
prompt, not from within *IDLE* or *PythonWin*::

      >>> from ctypes import *; printf = cdll.msvcrt.printf
      >>> printf("Hello, %s\n", "World!") # doctest: +SKIP
      Hello, World!
      14
      >>> printf("Hello, %S", u"World!") # doctest: +SKIP
      Hello, World!
      14
      >>> printf("%d bottles of beer\n", 42) # doctest: +SKIP
      42 bottles of beer
      19
      >>> printf("%f bottles of beer\n", 42.5) # doctest: +SKIP
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      ArgumentError: argument 2: exceptions.TypeError: Don't know how to convert parameter 2
      >>>

As has been mentioned before, all Python types except integers,
strings, and unicode strings have to be wrapped in their corresponding
``ctypes`` type, so that they can be converted to the required C data
type::

      >>> from ctypes import *
      >>> printf = cdll.msvcrt.printf
      >>> printf("An int %d, a double %f\n", 1234, c_double(3.14)) # doctest: +SKIP
      Integer 1234, double 3.1400001049
      34
      >>>

Calling functions with your own custom data types
-------------------------------------------------

You can also customize ``ctypes`` argument conversion to allow
instances of your own classes be used as function arguments.
``ctypes`` looks for an ``_as_parameter_`` attribute and uses this as
the function argument. Of course, it must be one of integer, string,
or unicode::

      >>> class Bottles(object):
      ...     def __init__(self, number):
      ...         self._as_parameter_ = number
      ...
      >>> bottles = Bottles(42)
      >>> from ctypes import *
      >>> printf = cdll.msvcrt.printf
      >>> printf("%d bottles of beer\n", bottles) # doctest: +SKIP
      42 bottles of beer
      19
      >>>

If you don't want to store the instance's data in the
``_as_parameter_`` instance variable, you could define a ``property``
which makes the data avaiblable.

Specifying the required argument types (function prototypes)
------------------------------------------------------------

It is possible to specify the required argument types of functions
exported from DLLs by setting the ``argtypes`` attribute.

``argtypes`` must be a sequence of C data types (the ``printf``
function is probably not a good example here, because it takes a
variable number and different types of parameters depending on the
format string, on the other hand this is quite handy to experiment
with this feature)::

      >>> from ctypes import *
      >>> printf = cdll.msvcrt.printf
      >>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
      >>> printf("String '%s', Int %d, Double %f\n", "Hi", 10, 2.2) # doctest: +SKIP
      String 'Hi', Int 10, Double 2.200000

Specifying a format protects against incompatible argument types (just
as a prototype for a C function), and tries to convert the arguments
to valid types::

      >>> printf("%d %d %d", 1, 2, 3) # doctest: +SKIP
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      TypeError: string expected instead of int instance
      >>> printf("%s %d %f", "X", 2, 3) # doctest: +SKIP
      X 2 3.00000012
      >>>

If you have defined your own classes which you pass to function calls,
you have to implement a ``from_param`` class method for them to be
able to use them in the ``argtypes`` sequence. The ``from_param``
class method receives the Python object passed to the function call,
it should do a typecheck or whatever is needed to make sure this
object is acceptable, and then return the object itself, it's
``_as_parameter_`` attribute, or whatever you want to pass as the C
function argument in this case. Again, the result should be an
integer, string, unicode, a ``ctypes`` instance, or something having
the ``_as_parameter_`` attribute.


Return types
------------

By default functions are assumed to return integers.  Other return
types can be specified by setting the ``restype`` attribute of the
function object.

Here is a more advanced example, it uses the strchr function, which
expects a string pointer and a char, and returns a pointer to a
string::

      >>> from ctypes import *
      >>> strchr = cdll.msvcrt.strchr
      >>> strchr("abcdef", ord("d")) # doctest: +SKIP
      8059983
      >>> strchr.restype = c_char_p # c_char_p is a pointer to a string
      >>> strchr("abcdef", ord("d"))
      'def'
      >>> print strchr("abcdef", ord("x"))
      None
      >>>

If you want to avoid the ``ord("x")`` calls above, you can set the
``argtypes`` attribute, and the second argument will be converted from
a single character Python string into a C char::

      >>> from ctypes import *
      >>> msvcrt = cdll.msvcrt
      >>> msvcrt.strchr.restype = c_char_p
      >>> msvcrt.strchr.argtypes = [c_char_p, c_char]
      >>> msvcrt.strchr("abcdef", "d")
      'def'
      >>> msvcrt.strchr("abcdef", "def")
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      ArgumentError: argument 2: exceptions.TypeError: one character string expected
      >>> print msvcrt.strchr("abcdef", "x")
      None
      >>> msvcrt.strchr("abcdef", "d")
      'def'
      >>>
      
XXX Mention the ``errcheck`` protocol...


You can also use a callable Python object (a function or a class for
example) as the ``restype`` attribute.  It will be called with the
``integer`` the C function returns, and the result of this call will
be used as the result of your function call. This is useful to check
for error return values and automatically raise an exception::

      >>> from ctypes import *
      >>> GetModuleHandle = windll.kernel32.GetModuleHandleA
      >>> def ValidHandle(value):
      ...     if value == 0:
      ...         raise WinError()
      ...     return value
      ...
      >>>
      >>> GetModuleHandle.restype = ValidHandle
      >>> GetModuleHandle(None)
      486539264
      >>> GetModuleHandle("something silly") # doctest: +IGNORE_EXCEPTION_DETAIL
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "<stdin>", line 3, in ValidHandle
      WindowsError: [Errno 126] The specified module could not be found.
      >>>

``WinError`` is a function which will call Windows ``FormatMessage()``
api to get the string representation of an error code, and *returns*
an exception.  ``WinError`` takes an optional error code parameter, if
no one is used, it calls ``GetLastError()`` to retrieve it.

Passing pointers (or: passing parameters by reference)
------------------------------------------------------

Sometimes a C api function expects a *pointer* to a data type as
parameter, probably to write into the corresponding location, or if
the data is too large to be passed by value. This is also known as
*passing parameters by reference*.

``ctypes`` exports the ``byref`` function which is used to pass
parameters by reference.  The same effect can be achieved with the
``pointer`` function, although ``pointer`` does a lot more work since
it constructs a real pointer object, so it is faster to use ``byref``
if you don't need the pointer object in Python itself::

      >>> from ctypes import *
      >>> msvcrt = cdll.msvcrt
      >>> i = c_int()
      >>> f = c_float()
      >>> s = create_string_buffer('\000' * 32)
      >>> print i.value, f.value, repr(s.value)
      0 0.0 ''
      >>> msvcrt.sscanf("1 3.14 Hello", "%d %f %s",
      ...               byref(i), byref(f), s)
      3
      >>> print i.value, f.value, repr(s.value)
      1 3.1400001049 'Hello'
      >>>

Structures and Unions
---------------------

Structures and unions must derive from the ``Structure`` and ``Union``
base classes which are defined in the ``ctypes`` module. Each subclass
must define a ``_fields_`` attribute.  ``_fields_`` must be a list of
*2-tuples*, containing a *field name* and a *field type*.

The field type must be a ``ctypes`` type like ``c_int``, or any other
derived ``ctypes`` type: structure, union, array, pointer.

Here is a simple example of a POINT structure, which contains two
integers named ``x`` and ``y``, and also shows how to initialize a
structure in the constructor::

      >>> from ctypes import *
      >>> class POINT(Structure):
      ...     _fields_ = [("x", c_int),
      ...                 ("y", c_int)]
      ...
      >>> point = POINT(10, 20)
      >>> print point.x, point.y
      10 20
      >>> point = POINT(y=5)
      >>> print point.x, point.y
      0 5
      >>> POINT(1, 2, 3)
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      ValueError: too many initializers
      >>>

You can, however, build much more complicated structures. Structures
can itself contain other structures by using a structure as a field
type.

Here is a RECT structure which contains two POINTs named ``upperleft``
and ``lowerright`` ::

      >>> class RECT(Structure):
      ...     _fields_ = [("upperleft", POINT),
      ...                 ("lowerright", POINT)]
      ...
      >>> rc = RECT(point)
      >>> print rc.upperleft.x, rc.upperleft.y
      0 5
      >>> print rc.lowerright.x, rc.lowerright.y
      0 0
      >>>

Nested structures can also be initialized in the constructor in
several ways::

      >>> r = RECT(POINT(1, 2), POINT(3, 4))
      >>> r = RECT((1, 2), (3, 4))

XXX Fields descriptors can be retrieved from the *class*, they
have readonly ``size`` and ``offset`` attributes describing the size
in bytes and the offset of this field from the beginning of the
internal memory buffer::

      >>> print POINT.x.size, POINT.x.offset
      4 0
      >>> print POINT.y.size, POINT.y.offset
      4 4
      >>>

By default, Structure and Union fields are aligned in the same way the
C compiler does it. It is possible to override this behaviour be
specifying a ``_pack_`` class attribute in the subclass
definition. This must be set to a positive integer and specifies the
maximum alignment for the fields. This is what ``#pragma pack(n)``
also does in MSVC.

**New in version 0.6.2**: Structures and unions can also be passed *by
value* to function calls.

Arrays
------

Arrays are sequences, containing a fixed number of instances of the
same type.

The recommended way to create array types is by multiplying a data
type with a positive integer::

      TenPointsArrayType = POINT * 10

Here is an example of an somewhat artifical data type, a structure
containing 4 POINTs among other stuff::

      >>> from ctypes import *
      >>> class POINT(Structure):
      ...    _fields_ = ("x", c_int), ("y", c_int)
      ...
      >>> class MyStruct(Structure):
      ...    _fields_ = [("a", c_int),
      ...                ("b", c_float),
      ...                ("point_array", POINT * 4)]
      >>>
      >>> print len(MyStruct().point_array)
      4


Instances are created in the usual way, by calling the class::

      arr = TenPointsArrayType()
      for pt in arr:
          print pt.x, pt.y

The above code print a series of ``0 0`` lines, because the array
contents is initialized to zeros.

Initializers of the correct type can also be specified::

      >>> from ctypes import *
      >>> TenIntegers = c_int * 10
      >>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      >>> print ii
      <c_long_Array_10 object at 0x...>
      >>> for i in ii: print i,
      ...
      1 2 3 4 5 6 7 8 9 10
      >>>

Pointers
--------

Pointer instances are created by calling the ``pointer`` function on a
``ctypes`` type::

       >>> from ctypes import *
       >>> i = c_int(42)
       >>> pi = pointer(i)
       >>>

XXX XXX Not correct: use indexing, not the contents atribute

Pointer instances have a ``contents`` attribute which returns the
ctypes' type pointed to, the ``c_int(42)`` in the above case::

       >>> pi.contents
       c_long(42)
       >>>

Assigning another ``c_int`` instance to the pointer's contents
attribute would cause the pointer to point to the memory location
where this is stored::

       >>> pi.contents = c_int(99)
       >>> pi.contents
       c_long(99)
       >>>

Pointer instances can also be indexed with integers::

       >>> pi[0]
       99
       >>>

XXX What is this???
Assigning to an integer index changes the pointed to value::

       >>> i2 = pi[0]
       >>> i2
       99
       >>> pi[0] = 22
       >>> i2
       99
       >>>

It is also possible to use indexes different from 0, but you must know
what you're doing when you use this: You access or change arbitrary
memory locations when you do this. Generally you only use this feature
if you receive a pointer from a C function, and you *know* that the
pointer actually points to an array instead of a single item.

Pointer classes/types
---------------------

Behind the scenes, the ``pointer`` function does more than simply
create pointer instances, it has to create pointer *types* first.
This is done with the ``POINTER`` function, which accepts any
``ctypes`` type, and returns a new type::

       >>> from ctypes import *
       >>> PI = POINTER(c_int)
       >>> PI
       <class 'ctypes.LP_c_long'>
       >>> PI(42)
       Traceback (most recent call last):
         File "<stdin>", line 1, in ?
       TypeError: expected c_long instead of int
       >>> PI(c_int(42))
       <ctypes.LP_c_long object at 0x...>
       >>>

Incomplete Types
----------------

**Note:** This code seems to work in older versions, but the past
convention was to use the now deprecated ``SetPointerType`` function.
If you have an older version and the following example does not work
for you, try looking up that function.

*Incomplete Types* are structures, unions or arrays whose members are
not yet specified. In the ``ctypes`` context, you can create types
representing pointers to these incomplete types by passing their name
(as a string) to the POINTER function, and complete the result
subclass later.

Consider this example (C-code)::

       struct cell;

       struct {
           char *name;
           struct cell *next;
       } cell;

The straightforward translation into ctypes code would be this, but it
does not work::

       >>> class cell(Structure):
       ...     _fields_ = [("name", c_char_p),
       ...                 ("next", POINTER(cell))]
       ...
       Traceback (most recent call last):
         File "<stdin>", line 1, in ?
         File "<stdin>", line 2, in cell
       NameError: name 'cell' is not defined
       >>>

because the new ``class cell`` is not available in the class statement
itself.

We can do it by creating an *incomplete type*. Just leave out the
``_fields_`` declaration and fill it up later::

       >>> from ctypes import *
       >>> class cell(Structure):
       ...     pass
       ...
       >>> cell._fields_ = [("name", c_char_p),
       ...                  ("next", POINTER(cell))]
       >>>

Lets try it. We create two instances of ``cell``, and let them point
to each other, and finally follow the pointer chain a few times::

       >>> c1 = cell()
       >>> c1.name = "foo"
       >>> c2 = cell()
       >>> c2.name = "bar"
       >>> c1.next = pointer(c2)
       >>> c2.next = pointer(c1)
       >>> p = c1
       >>> for i in range(8):
       ...     print p.name,
       ...     p = p.next[0]
       ...
       foo bar foo bar foo bar foo bar
       >>>    


Callback functions
------------------

(This example is too long, I should have used a shorter array)

``ctypes`` allows to create C callable function pointers from Python
callables. These are sometimes called *callback functions*.

First, you must create a class for the callback function, the class
knows the calling convention, the result type the function has to
return, and the number and types of the arguments this function will
receive.

``ctypes`` provides the CFUNCTYPE factory function to create types for
callback functions using the normal cdecl calling convention, and, on
Windows, the WINFUNCTYPE factory function to create types for callback
functions using the stdcall calling convention.

Both of these factory functions are called with the result type as
first argument, and the callback functions expected argument types as
the remaining arguments.

I will present an example here which uses the standard C library's
``qsort`` function, this is used to sort items with the help of a
callback function. ``qsort`` will be used to sort an array of
integers::
    
       >>> from ctypes import *
       >>> IntArray5 = c_int * 5
       >>> ia = IntArray5(5, 1, 7, 33, 99)
       >>> qsort = cdll.msvcrt.qsort
       >>>

``qsort`` must be called with a pointer to the data to sort, the
number of items in the data array, the size of one item, and the sort
function, which is the callback. The callback function will then be
called with two pointers to items, and it must return a negative
integer if the first item is smaller than the second, a 0 if they are
equal, and a positive integer else.

So our callback function receives pointers to integers, and must
return an integer. First we create the ``type`` for the callback
function::

       >>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
       >>>

For the first implementation of the callback function, we simply print
the arguments we get, and return 0 (incremental development)::

       >>> def py_cmp_func(a, b):
       ...     print "py_cmp_func", a, b
       ...     return 0
       ...
       >>>

Create the C callable function::

       >>> cmp_func = CMPFUNC(py_cmp_func)
       >>>

And we're ready to go::

       >>> qsort(ia, len(ia), sizeof(c_int), cmp_func)
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       py_cmp_func <ctypes.LP_c_long object at 0x00C70AD0> <ctypes.LP_c_long object at 0x00C70B20>
       -1
       >>>

We know how to access the contents of a pointer, so lets redefine our callback::

    >>> def py_cmp_func(a, b):
    ...     print "py_cmp_func", a[0], b[0]
    ...     return 0
    ...
    >>> cmp_func = CMPFUNC(py_cmp_func)
    >>> qsort(ia, len(ia), sizeof(c_int), cmp_func)
    py_cmp_func 7 1
    py_cmp_func 33 1
    py_cmp_func 99 1
    py_cmp_func 5 1
    py_cmp_func 7 5
    py_cmp_func 33 5
    py_cmp_func 99 5
    py_cmp_func 7 99
    py_cmp_func 33 99
    py_cmp_func 7 33
    -1
    >>>

Ah, we're nearly done! Last refinements::

    >>> def py_cmp_func(a, b):
    ...     print "py_cmp_func", a[0], b[0]
    ...     return a[0] - b[0]
    ...
    >>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func))
    py_cmp_func 33 7
    py_cmp_func 99 33
    py_cmp_func 5 99
    py_cmp_func 1 99
    py_cmp_func 33 7
    py_cmp_func 1 33
    py_cmp_func 5 33
    py_cmp_func 5 7
    py_cmp_func 1 7
    py_cmp_func 5 1
    -1
    >>>

So, is our array sorted now::

    >>> for i in ia: print i,
    ...
    1 5 7 33 99
    >>>

Yep, it worked!

**Important note for callback functions:**

Make sure you keep references to CFUNCTYPE objects as long as they are
used from C code. ctypes doesn't, and if you don't, they may be
garbage collected, crashing your program when a callback is made.

Accessing values exported from dlls
-----------------------------------

Sometimes, a dll not only exports functions, it also exports
values. Examples in the Python dll itself are the ``Py_OptimizeFlag``,
an integer set to 0, 1, or 2, depending on the ``-O`` or ``-OO`` flag
given on startup.

Starting with version 0.6.1, ``ctypes`` can access values like this
with the ``in_dll`` class methods of the types. The following examples
only work on Windows::

      >>> from ctypes import *
      >>> opt_flag = c_int.in_dll(pythonapi, "Py_OptimizeFlag")
      >>> print opt_flag
      c_long(0)
      >>>

If the interpreter would have been started with ``-O``, the sample
would have printed ``c_long(1)``, or ``c_long(2)`` if ``-OO`` would have
been specified.

A somewhat extended example which also demontrates the use of pointers
accesses the ``PyImport_FrozenModules`` pointer exported by Python.

Quoting the Python docs: *This pointer is initialized to point to an
array of ``struct _frozen`` records, terminated by one whose members
are all NULL or zero. When a frozen module is imported, it is searched
in this table. Third-party code could play tricks with this to provide
a dynamically created collection of frozen modules.*

So manipulating this pointer could even prove useful. To restrict the
example size, we show only how this table can be read with
``ctypes``::

      >>> from ctypes import *
      >>>
      >>> class struct_frozen(Structure):
      ...     _fields_ = [("name", c_char_p),
      ...                 ("code", POINTER(c_ubyte)),
      ...                 ("size", c_int)]
      ...
      >>>

XXX Should use pythonapi instead!!!
We have ``loaded`` the Python dll and defined the ``struct _frozen``
data type, so we can get the pointer to the table::

      >>> FrozenTable = POINTER(struct_frozen)
      >>> table = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules")
      >>>

Since ``table`` is a ``pointer`` to the ``struct_frozen`` records, we
can iterate over it, we just have to make sure that our loop
terminates, because pointers have no size. Sooner or later it would
probably crash with an access violation or whatever, so it's better to
break out of the loop when we hit the NULL entry::

      >>> for item in table:
      ...    print item.name, item.size
      ...    if item.name is None:
      ...        break
      ...
      __hello__ 104
      __phello__ -104
      __phello__.spam 104
      None 0
      >>>

The fact that standard Python has a frozen module and a frozen package
(indicated by the negative size member) is not wellknown, AFAIK it is
used for testing. Try it out with ``import __hello__`` for example.

XXX Describe how to access the ``code`` member fields, which contain
the byte code for the modules.

Surprises
---------

There are some corners in ``ctypes`` where you may be expect something
else than what actually happens.

Consider the following example::

      >>> from ctypes import *
      >>> class POINT(Structure):
      ...     _fields_ = ("x", c_int), ("y", c_int)
      ...
      >>> class RECT(Structure):
      ...     _fields_ = ("a", POINT), ("b", POINT)
      ...
      >>> p1 = POINT(1, 2)
      >>> p2 = POINT(3, 4)
      >>> rc = RECT(p1, p2)
      >>> print rc.a.x, rc.a.y, rc.b.x, rc.b.y
      1 2 3 4
      >>> # now swap the two points
      >>> rc.a, rc.b = rc.b, rc.a
      >>> print rc.a.x, rc.a.y, rc.b.x, rc.b.y
      3 4 3 4

Hm. We certainly expected the last statement to print ``3 4 1 2``.
What happended? Here are the steps of the ``rc.a, rc.b = rc.b, rc.a``
line above::

      >>> temp0, temp1 = rc.b, rc.a
      >>> rc.a = temp0
      >>> rc.b = temp1

Note that ``temp0`` and ``temp1`` are objects still using the internal
buffer of the ``rc`` object above. So executing ``rc.a = temp0``
copies the buffer contents of ``temp0`` into ``rc`` 's buffer.  This,
in turn, changes the contents of ``temp1``. So, the last assignment
``rc.b = temp1``, doesn't have the expected effect.

Keep in mind that retrieving subobjects from Structure, Unions, and
Arrays doesn't *copy* the subobject, it does more retrieve a wrapper
object accessing the root-object's underlying buffer.


Bugs, ToDo and non-implemented things
-------------------------------------

XXX Wrong
    Bitfields are not implemented.

    Enumeration types are not implemented. You can do it easily
    yourself, using ``c_int`` as the base class.

    ``long double`` is not implemented.

.. no longer true:  You cannot pass structures to functions as arguments, and you
   cannot set them as return type (only pointers).

.. no longer true?   Callback functions implemented in Python can *only* return integers.

..
  Local Variables:
  compile-command: "make_html"
  End: