-
Notifications
You must be signed in to change notification settings - Fork 8
/
PythonEngine.pas
9729 lines (8765 loc) · 331 KB
/
PythonEngine.pas
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
(**************************************************************************)
(* *)
(* Module: Unit 'PythonEngine' Copyright (c) 1997 *)
(* *)
(* Version: 3.0 Dr. Dietmar Budelsky *)
(* Sub-Version: 0.33 [email protected] *)
(* Germany *)
(* *)
(* Morgan Martinet *)
(* 4723 rue Brebeuf *)
(* H2J 3L2 MONTREAL (QC) *)
(* CANADA *)
(* e-mail: [email protected] *)
(* *)
(* look at the project page at: http://python4Delphi.googlecode.com/ *)
(**************************************************************************)
(* Functionality: Delphi Components that provide an interface to the *)
(* Python language (see python.txt for more infos on *)
(* Python itself). *)
(* *)
(**************************************************************************)
(* Contributors: *)
(* Grzegorz Makarewicz ([email protected]) *)
(* Andrew Robinson ([email protected]) *)
(* Mark Watts([email protected]) *)
(* Olivier Deckmyn ([email protected]) *)
(* Sigve Tjora ([email protected]) *)
(* Mark Derricutt ([email protected]) *)
(* Igor E. Poteryaev ([email protected]) *)
(* Yuri Filimonov ([email protected]) *)
(* Stefan Hoffmeister ([email protected]) *)
(* Michiel du Toit ([email protected]) - Lazarus Port *)
(* Chris Nicolai ([email protected]) *)
(* Kiriakos Vlahos ([email protected]) *)
(* Andrey Gruzdev ([email protected]) *)
(**************************************************************************)
(* This source code is distributed with no WARRANTY, for no reason or use.*)
(* Everyone is allowed to use and change this code free for his own tasks *)
(* and projects, as long as this header and its copyright text is intact. *)
(* For changed versions of this code, which are public distributed the *)
(* following additional conditions have to be fullfilled: *)
(* 1) The header has to contain a comment on the change and the author of *)
(* it. *)
(* 2) A copy of the changed source has to be sent to the above E-Mail *)
(* address or my then valid address, if this is possible to the *)
(* author. *)
(* The second condition has the target to maintain an up to date central *)
(* version of the component. If this condition is not acceptable for *)
(* confidential or legal reasons, everyone is free to derive a component *)
(* or to generate a diff file to my or other original sources. *)
(* Dr. Dietmar Budelsky, 1997-11-17 *)
(**************************************************************************)
{$I Definition.Inc}
unit PythonEngine;
{ TODO -oMMM : implement tp_as_buffer slot }
{ TODO -oMMM : implement Attribute descriptor and subclassing stuff }
{$IFNDEF FPC}
{$IFNDEF DELPHI7_OR_HIGHER}
Error! Delphi 7 or higher is required!
{$ENDIF}
{$ENDIF}
interface
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
{$IFDEF UNIX}
Types,
{$ENDIF}
{$IFNDEF FPC}
{$IFDEF LINUX}
Libc, // kylix needs this. fpc doesn't
{$ENDIF}
{$ENDIF}
Classes,
SysUtils,
SyncObjs,
Variants,
{$IFDEF DELPHI2005_OR_HIGHER}
{$IFNDEF UNICODE}
WideStrings,
{$ENDIF}
{$ELSE}
TinyWideStrings,
{$ENDIF}
{$IFDEF FPC}
dynlibs,
{$ENDIF}
MethodCallBack;
//#######################################################
//## ##
//## PYTHON specific constants ##
//## ##
//#######################################################
type
{$IFNDEF UNICODE}
UnicodeString = WideString;
TUnicodeStringList = TWideStringList;
{$ELSE}
TUnicodeStringList = TStringList;
{$ENDIF}
{$IFNDEF FPC}
{$IF CompilerVersion < 21}
NativeInt = integer;
NativeUInt = Cardinal;
{$IFEND}
PNativeInt = ^NativeInt;
{$ELSE}
{$IF DEFINED(FPC_FULLVERSION) and (FPC_FULLVERSION >= 20500)}
{$ELSE}
NativeInt = integer;
NativeUInt = Cardinal;
{$IFEND}
PNativeInt = ^NativeInt;
{$ENDIF}
TPythonVersionProp = packed record
DllName : String;
RegVersion : String;
APIVersion : Integer;
CanUseLatest : Boolean;
end;
const
{$IFDEF MSWINDOWS}
PYTHON_KNOWN_VERSIONS: array[1..9] of TPythonVersionProp =
( (DllName: 'python23.dll'; RegVersion: '2.3'; APIVersion: 1012; CanUseLatest: True),
(DllName: 'python24.dll'; RegVersion: '2.4'; APIVersion: 1012; CanUseLatest: True),
(DllName: 'python25.dll'; RegVersion: '2.5'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python26.dll'; RegVersion: '2.6'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python27.dll'; RegVersion: '2.7'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python30.dll'; RegVersion: '3.0'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python31.dll'; RegVersion: '3.1'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python32.dll'; RegVersion: '3.2'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'python33.dll'; RegVersion: '3.3'; APIVersion: 1013; CanUseLatest: True) );
{$ENDIF}
{$IFDEF UNIX}
PYTHON_KNOWN_VERSIONS: array[1..9] of TPythonVersionProp =
( (DllName: 'libpython2.3.so'; RegVersion: '2.3'; APIVersion: 1012; CanUseLatest: True),
(DllName: 'libpython2.4.so'; RegVersion: '2.4'; APIVersion: 1012; CanUseLatest: True),
(DllName: 'libpython2.5.so'; RegVersion: '2.5'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython2.6.so'; RegVersion: '2.6'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython2.7.so'; RegVersion: '2.7'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython3.0.so'; RegVersion: '3.0'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython3.1.so'; RegVersion: '3.1'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython3.2.so'; RegVersion: '3.2'; APIVersion: 1013; CanUseLatest: True),
(DllName: 'libpython3.3.so'; RegVersion: '3.3'; APIVersion: 1013; CanUseLatest: True) );
{$ENDIF}
{$IFDEF PYTHON23}
COMPILED_FOR_PYTHON_VERSION_INDEX = 1;
{$ENDIF}
{$IFDEF PYTHON24}
COMPILED_FOR_PYTHON_VERSION_INDEX = 2;
{$ENDIF}
{$IFDEF PYTHON25}
COMPILED_FOR_PYTHON_VERSION_INDEX = 3;
{$ENDIF}
{$IFDEF PYTHON26}
COMPILED_FOR_PYTHON_VERSION_INDEX = 4;
{$ENDIF}
{$IFDEF PYTHON27}
COMPILED_FOR_PYTHON_VERSION_INDEX = 5;
{$ENDIF}
{$IFDEF PYTHON30}
COMPILED_FOR_PYTHON_VERSION_INDEX = 6;
{$ENDIF}
{$IFDEF PYTHON31}
COMPILED_FOR_PYTHON_VERSION_INDEX = 7;
{$ENDIF}
{$IFDEF PYTHON32}
COMPILED_FOR_PYTHON_VERSION_INDEX = 8;
{$ENDIF}
{$IFDEF PYTHON33}
COMPILED_FOR_PYTHON_VERSION_INDEX = 9;
{$ENDIF}
PYT_METHOD_BUFFER_INCREASE = 10;
PYT_MEMBER_BUFFER_INCREASE = 10;
PYT_GETSET_BUFFER_INCREASE = 10;
METH_VARARGS = $0001;
METH_KEYWORDS = $0002;
// Masks for the co_flags field of PyCodeObject
CO_OPTIMIZED = $0001;
CO_NEWLOCALS = $0002;
CO_VARARGS = $0004;
CO_VARKEYWORDS = $0008;
// Rich comparison opcodes introduced in version 2.1
Py_LT = 0;
Py_LE = 1;
Py_EQ = 2;
Py_NE = 3;
Py_GT = 4;
Py_GE = 5;
type
// Delphi equivalent used by TPyObject
TRichComparisonOpcode = (pyLT, pyLE, pyEQ, pyNE, pyGT, pyGE);
const
{Type flags (tp_flags) introduced in version 2.0
These flags are used to extend the type structure in a backwards-compatible
fashion. Extensions can use the flags to indicate (and test) when a given
type structure contains a new feature. The Python core will use these when
introducing new functionality between major revisions (to avoid mid-version
changes in the PYTHON_API_VERSION).
Arbitration of the flag bit positions will need to be coordinated among
all extension writers who publically release their extensions (this will
be fewer than you might expect!)..
Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
given type object has a specified feature.
}
// PyBufferProcs contains bf_getcharbuffer
Py_TPFLAGS_HAVE_GETCHARBUFFER = (1 shl 0);
// PySequenceMethods contains sq_contains
Py_TPFLAGS_HAVE_SEQUENCE_IN = (1 shl 1);
// Objects which participate in garbage collection (see objimp.h)
Py_TPFLAGS_GC = (1 shl 2);
// PySequenceMethods and PyNumberMethods contain in-place operators
Py_TPFLAGS_HAVE_INPLACEOPS = (1 shl 3);
// PyNumberMethods do their own coercion */
Py_TPFLAGS_CHECKTYPES = (1 shl 4);
Py_TPFLAGS_HAVE_RICHCOMPARE = (1 shl 5);
// Objects which are weakly referencable if their tp_weaklistoffset is >0
// XXX Should this have the same value as Py_TPFLAGS_HAVE_RICHCOMPARE?
// These both indicate a feature that appeared in the same alpha release.
Py_TPFLAGS_HAVE_WEAKREFS = (1 shl 6);
// tp_iter is defined
Py_TPFLAGS_HAVE_ITER = (1 shl 7);
// New members introduced by Python 2.2 exist
Py_TPFLAGS_HAVE_CLASS = (1 shl 8);
// Set if the type object is dynamically allocated
Py_TPFLAGS_HEAPTYPE = (1 shl 9);
// Set if the type allows subclassing
Py_TPFLAGS_BASETYPE = (1 shl 10);
// Set if the type is 'ready' -- fully initialized
Py_TPFLAGS_READY = (1 shl 12);
// Set while the type is being 'readied', to prevent recursive ready calls
Py_TPFLAGS_READYING = (1 shl 13);
// Objects support garbage collection (see objimp.h)
Py_TPFLAGS_HAVE_GC = (1 shl 14);
Py_TPFLAGS_DEFAULT = Py_TPFLAGS_HAVE_GETCHARBUFFER
or Py_TPFLAGS_HAVE_SEQUENCE_IN
or Py_TPFLAGS_HAVE_INPLACEOPS
or Py_TPFLAGS_HAVE_RICHCOMPARE
or Py_TPFLAGS_HAVE_WEAKREFS
or Py_TPFLAGS_HAVE_ITER
or Py_TPFLAGS_HAVE_CLASS
or Py_TPFLAGS_BASETYPE
;
// See function PyType_HasFeature below for testing the flags.
// Delphi equivalent used by TPythonType
type
TPFlag = (tpfHaveGetCharBuffer, tpfHaveSequenceIn, tpfGC, tpfHaveInplaceOps,
tpfCheckTypes, tpfHaveRichCompare, tpfHaveWeakRefs
,tpfHaveIter, tpfHaveClass, tpfHeapType, tpfBaseType, tpfReady, tpfReadying, tpfHaveGC
);
TPFlags = set of TPFlag;
const
TPFLAGS_DEFAULT = [tpfHaveGetCharBuffer, tpfHaveSequenceIn, tpfHaveInplaceOps,
tpfHaveRichCompare, tpfHaveWeakRefs, tpfHaveIter,
tpfHaveClass, tpfBaseType
];
//------- Python opcodes ----------//
Const
single_input = 256;
file_input = 257;
eval_input = 258;
p4d_funcdef = 259;
p4d_parameters = 260;
p4d_varargslist = 261;
p4d_fpdef = 262;
p4d_fplist = 263;
p4d_stmt = 264;
p4d_simple_stmt = 265;
p4d_small_stmt = 266;
p4d_expr_stmt = 267;
p4d_augassign = 268;
p4d_print_stmt = 269;
p4d_del_stmt = 270;
p4d_pass_stmt = 271;
p4d_flow_stmt = 272;
p4d_break_stmt = 273;
p4d_continue_stmt = 274;
p4d_return_stmt = 275;
p4d_raise_stmt = 276;
p4d_import_stmt = 277;
p4d_import_as_name = 278;
p4d_dotted_as_name = 279;
p4d_dotted_name = 280;
p4d_global_stmt = 281;
p4d_exec_stmt = 282;
p4d_assert_stmt = 283;
p4d_compound_stmt = 284;
p4d_if_stmt = 285;
p4d_while_stmt = 286;
p4d_for_stmt = 287;
p4d_try_stmt = 288;
p4d_except_clause = 289;
p4d_suite = 290;
p4d_test = 291;
p4d_and_test = 291;
p4d_not_test = 293;
p4d_comparison = 294;
p4d_comp_op = 295;
p4d_expr = 296;
p4d_xor_expr = 297;
p4d_and_expr = 298;
p4d_shift_expr = 299;
p4d_arith_expr = 300;
p4d_term = 301;
p4d_factor = 302;
p4d_power = 303;
p4d_atom = 304;
p4d_listmaker = 305;
p4d_lambdef = 306;
p4d_trailer = 307;
p4d_subscriptlist = 308;
p4d_subscript = 309;
p4d_sliceop = 310;
p4d_exprlist = 311;
p4d_testlist = 312;
p4d_dictmaker = 313;
p4d_classdef = 314;
p4d_arglist = 315;
p4d_argument = 316;
p4d_list_iter = 317;
p4d_list_for = 318;
p4d_list_if = 319;
// structmember.h
const
//* Types */
T_SHORT = 0;
T_INT = 1;
T_LONG = 2;
T_FLOAT = 3;
T_DOUBLE = 4;
T_STRING = 5;
T_OBJECT = 6;
//* XXX the ordering here is weird for binary compatibility */
T_CHAR = 7; //* 1-character string */
T_BYTE = 8; //* 8-bit signed int */
//* unsigned variants: */
T_UBYTE = 9;
T_USHORT = 10;
T_UINT = 11;
T_ULONG = 12;
//* Added by Jack: strings contained in the structure */
T_STRING_INPLACE= 13;
T_OBJECT_EX = 16;{* Like T_OBJECT, but raises AttributeError
when the value is NULL, instead of
converting to None. *}
//* Flags */
READONLY = 1;
RO = READONLY; //* Shorthand */
READ_RESTRICTED = 2;
WRITE_RESTRICTED = 4;
RESTRICTED = (READ_RESTRICTED or WRITE_RESTRICTED);
type
TPyMemberType = (mtShort, mtInt, mtLong, mtFloat, mtDouble, mtString, mtObject,
mtChar, mtByte, mtUByte, mtUShort, mtUInt, mtULong,
mtStringInplace, mtObjectEx);
TPyMemberFlag = (mfDefault, mfReadOnly, mfReadRestricted, mfWriteRestricted, mfRestricted);
//#######################################################
//## ##
//## Non-Python specific constants ##
//## ##
//#######################################################
const
ErrInit = -300;
CR = #13;
LF = #10;
TAB = #09;
CRLF = CR+LF;
//#######################################################
//## ##
//## Global declarations, nothing Python specific ##
//## ##
//#######################################################
type
TPAnsiChar = array[0..16000] of PAnsiChar;
TPWideChar = array[0..16000] of PWideChar;
PPAnsiChar = ^TPAnsiChar;
PPWideChar = ^TPWideChar;
PInt = ^Integer;
PDouble = ^Double;
PFloat = ^Real;
PLong = ^LongInt;
PShort = ^ShortInt;
//#######################################################
//## ##
//## Python specific interface ##
//## ##
//#######################################################
type
PP_frozen = ^P_frozen;
P_frozen = ^_frozen;
PPyObject = ^PyObject;
PPPyObject = ^PPyObject;
PPPPyObject = ^PPPyObject;
PPyIntObject = ^PyIntObject;
PPyTypeObject = ^PyTypeObject;
PPySliceObject = ^PySliceObject;
AtExitProc = procedure;
PyCFunction = function( self, args:PPyObject): PPyObject; cdecl;
PyCFunctionWithKW = function( self, args, keywords:PPyObject): PPyObject; cdecl;
unaryfunc = function( ob1 : PPyObject): PPyObject; cdecl;
binaryfunc = function( ob1,ob2 : PPyObject): PPyObject; cdecl;
ternaryfunc = function( ob1,ob2,ob3 : PPyObject): PPyObject; cdecl;
inquiry = function( ob1 : PPyObject): integer; cdecl;
lenfunc = function( ob1 : PPyObject): NativeInt; cdecl;
coercion = function( ob1,ob2 : PPPyObject): integer; cdecl;
ssizeargfunc = function( ob1 : PPyObject; i: NativeInt): PPyObject; cdecl;
ssizessizeargfunc = function( ob1 : PPyObject; i1, i2: NativeInt):
PPyObject; cdecl;
ssizeobjargproc = function( ob1 : PPyObject; i: NativeInt; ob2 : PPyObject):
integer; cdecl;
ssizessizeobjargproc = function( ob1: PPyObject; i1, i2: NativeInt;
ob2: PPyObject): integer; cdecl;
objobjargproc = function( ob1,ob2,ob3 : PPyObject): integer; cdecl;
pydestructor = procedure(ob: PPyObject); cdecl;
printfunc = function( ob: PPyObject; var f: file; i: integer): integer; cdecl;
getattrfunc = function( ob1: PPyObject; name: PAnsiChar): PPyObject; cdecl;
setattrfunc = function( ob1: PPyObject; name: PAnsiChar; ob2: PPyObject): integer; cdecl;
cmpfunc = function( ob1,ob2: PPyObject): integer; cdecl;
reprfunc = function( ob: PPyObject): PPyObject; cdecl;
hashfunc = function( ob: PPyObject): NativeInt; cdecl; // !! in 2.x it is still a LongInt
getattrofunc = function( ob1,ob2: PPyObject): PPyObject; cdecl;
setattrofunc = function( ob1,ob2,ob3: PPyObject): integer; cdecl;
/// jah 29-sep-2000 : updated for python 2.0
/// added from object.h
getreadbufferproc = function ( ob1: PPyObject; i: NativeInt; ptr: Pointer): NativeInt; cdecl;
getwritebufferproc= function ( ob1: PPyObject; i: NativeInt; ptr: Pointer): NativeInt; cdecl;
getsegcountproc = function ( ob1: PPyObject; i: NativeInt): NativeInt; cdecl;
getcharbufferproc = function ( ob1: PPyObject; i: NativeInt; const pstr: PAnsiChar): NativeInt; cdecl;
objobjproc = function ( ob1, ob2: PPyObject): integer; cdecl;
visitproc = function ( ob1: PPyObject; ptr: Pointer): integer; cdecl;
traverseproc = function ( ob1: PPyObject; proc: visitproc; ptr: Pointer): integer; cdecl;
richcmpfunc = function ( ob1, ob2 : PPyObject; i : Integer) : PPyObject; cdecl;
getiterfunc = function ( ob1 : PPyObject) : PPyObject; cdecl;
iternextfunc = function ( ob1 : PPyObject) : PPyObject; cdecl;
descrgetfunc = function ( ob1, ob2, ob3 : PPyObject) : PPyObject; cdecl;
descrsetfunc = function ( ob1, ob2, ob3 : PPyObject) : Integer; cdecl;
initproc = function ( self, args, kwds : PPyObject) : Integer; cdecl;
newfunc = function ( subtype: PPyTypeObject; args, kwds : PPyObject) : PPyObject; cdecl;
allocfunc = function ( self: PPyTypeObject; nitems : NativeInt) : PPyObject; cdecl;
PyNumberMethods = {$IFNDEF CPUX64}packed{$ENDIF} record
nb_add : binaryfunc;
nb_substract : binaryfunc;
nb_multiply : binaryfunc;
nb_divide : binaryfunc;
nb_remainder : binaryfunc;
nb_divmod : binaryfunc;
nb_power : ternaryfunc;
nb_negative : unaryfunc;
nb_positive : unaryfunc;
nb_absolute : unaryfunc;
nb_nonzero : inquiry;
nb_invert : unaryfunc;
nb_lshift : binaryfunc;
nb_rshift : binaryfunc;
nb_and : binaryfunc;
nb_xor : binaryfunc;
nb_or : binaryfunc;
nb_coerce : coercion;
nb_int : unaryfunc;
nb_long : unaryfunc;
nb_float : unaryfunc;
nb_oct : unaryfunc;
nb_hex : unaryfunc;
/// jah 29-sep-2000 : updated for python 2.0
/// added from .h
nb_inplace_add : binaryfunc;
nb_inplace_subtract : binaryfunc;
nb_inplace_multiply : binaryfunc;
nb_inplace_divide : binaryfunc;
nb_inplace_remainder : binaryfunc;
nb_inplace_power : ternaryfunc;
nb_inplace_lshift : binaryfunc;
nb_inplace_rshift : binaryfunc;
nb_inplace_and : binaryfunc;
nb_inplace_xor : binaryfunc;
nb_inplace_or : binaryfunc;
// Added in release 2.2
// The following require the Py_TPFLAGS_HAVE_CLASS flag
nb_floor_divide : binaryfunc;
nb_true_divide : binaryfunc;
nb_inplace_floor_divide : binaryfunc;
nb_inplace_true_divide : binaryfunc;
end;
PPyNumberMethods = ^PyNumberMethods;
PySequenceMethods = {$IFNDEF CPUX64}packed{$ENDIF} record
sq_length : lenfunc;
sq_concat : binaryfunc;
sq_repeat : ssizeargfunc;
sq_item : ssizeargfunc;
sq_slice : ssizessizeargfunc;
sq_ass_item : ssizeobjargproc;
sq_ass_slice : ssizessizeobjargproc;
/// jah 29-sep-2000 : updated for python 2.0
/// added from .h
sq_contains : objobjproc;
sq_inplace_concat : binaryfunc;
sq_inplace_repeat : ssizeargfunc;
end;
PPySequenceMethods = ^PySequenceMethods;
PyMappingMethods = {$IFNDEF CPUX64}packed{$ENDIF} record
mp_length : lenfunc;
mp_subscript : binaryfunc;
mp_ass_subscript : objobjargproc;
end;
PPyMappingMethods = ^PyMappingMethods;
/// jah 29-sep-2000 : updated for python 2.0
/// added from .h
PyBufferProcs = {$IFNDEF CPUX64}packed{$ENDIF} record
bf_getreadbuffer : getreadbufferproc;
bf_getwritebuffer : getwritebufferproc;
bf_getsegcount : getsegcountproc;
bf_getcharbuffer : getcharbufferproc;
end;
PPyBufferProcs = ^PyBufferProcs;
Py_complex = {$IFNDEF CPUX64}packed{$ENDIF} record
real : double;
imag : double;
end;
PyObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt: NativeInt;
ob_type: PPyTypeObject;
end;
PyIntObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
ob_ival : LongInt;
end;
_frozen = {$IFNDEF CPUX64}packed{$ENDIF} record
name : PAnsiChar;
code : PByte;
size : Integer;
end;
PySliceObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt: NativeInt;
ob_type: PPyTypeObject;
start, stop, step: PPyObject;
end;
PPyMethodDef = ^PyMethodDef;
PyMethodDef = {$IFNDEF CPUX64}packed{$ENDIF} record
ml_name: PAnsiChar;
ml_meth: PyCFunction;
ml_flags: Integer;
ml_doc: PAnsiChar;
end;
// structmember.h
PPyMemberDef = ^PyMemberDef;
PyMemberDef = {$IFNDEF CPUX64}packed{$ENDIF} record
name : PAnsiChar;
_type : integer;
offset : NativeInt;
flags : integer;
doc : PAnsiChar;
end;
// descrobject.h
// Descriptors
getter = function ( obj : PPyObject; context : Pointer) : PPyObject; cdecl;
setter = function ( obj, value : PPyObject; context : Pointer) : integer; cdecl;
PPyGetSetDef = ^PyGetSetDef;
PyGetSetDef = {$IFNDEF CPUX64}packed{$ENDIF} record
name : PAnsiChar;
get : getter;
_set : setter;
doc : PAnsiChar;
closure : Pointer;
end;
wrapperfunc = function (self, args: PPyObject; wrapped : Pointer) : PPyObject; cdecl;
pwrapperbase = ^wrapperbase;
wrapperbase = {$IFNDEF CPUX64}packed{$ENDIF} record
name : PAnsiChar;
wrapper : wrapperfunc;
doc : PAnsiChar;
end;
// Various kinds of descriptor objects
{#define PyDescr_COMMON \
PyObject_HEAD \
PyTypeObject *d_type; \
PyObject *d_name
}
PPyDescrObject = ^PyDescrObject;
PyDescrObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
d_type : PPyTypeObject;
d_name : PPyObject;
end;
PPyMethodDescrObject = ^PyMethodDescrObject;
PyMethodDescrObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of PyDescr_COMMON
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
d_type : PPyTypeObject;
d_name : PPyObject;
// End of PyDescr_COMMON
d_method : PPyMethodDef;
end;
PPyMemberDescrObject = ^PyMemberDescrObject;
PyMemberDescrObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of PyDescr_COMMON
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
d_type : PPyTypeObject;
d_name : PPyObject;
// End of PyDescr_COMMON
d_member : PPyMemberDef;
end;
PPyGetSetDescrObject = ^PyGetSetDescrObject;
PyGetSetDescrObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of PyDescr_COMMON
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
d_type : PPyTypeObject;
d_name : PPyObject;
// End of PyDescr_COMMON
d_getset : PPyGetSetDef;
end;
PPyWrapperDescrObject = ^PyWrapperDescrObject;
PyWrapperDescrObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of PyDescr_COMMON
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
d_type : PPyTypeObject;
d_name : PPyObject;
// End of PyDescr_COMMON
d_base : pwrapperbase;
d_wrapped : Pointer; // This can be any function pointer
end;
PPyModuleDef_Base = ^PyModuleDef_Base;
PyModuleDef_Base = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
m_init : function( ) : PPyObject; cdecl;
m_index : NativeInt;
m_copy : PPyObject;
end;
PPyModuleDef = ^PyModuleDef;
PyModuleDef = {$IFNDEF CPUX64}packed{$ENDIF} record
m_base : PyModuleDef_Base;
m_name : PAnsiChar;
m_doc : PAnsiChar;
m_size : NativeInt;
m_methods : PPyMethodDef;
m_reload : inquiry;
m_traverse : traverseproc;
m_clear : inquiry;
m_free : inquiry;
end;
// object.h
PyTypeObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt: NativeInt;
ob_type: PPyTypeObject;
ob_size: NativeInt; // Number of items in variable part
tp_name: PAnsiChar; // For printing
tp_basicsize, tp_itemsize: NativeInt; // For allocation
// Methods to implement standard operations
tp_dealloc: pydestructor;
tp_print: printfunc;
tp_getattr: getattrfunc;
tp_setattr: setattrfunc;
tp_compare: cmpfunc;
tp_repr: reprfunc;
// Method suites for standard classes
tp_as_number: PPyNumberMethods;
tp_as_sequence: PPySequenceMethods;
tp_as_mapping: PPyMappingMethods;
// More standard operations (here for binary compatibility)
tp_hash: hashfunc;
tp_call: ternaryfunc;
tp_str: reprfunc;
tp_getattro: getattrofunc;
tp_setattro: setattrofunc;
/// jah 29-sep-2000 : updated for python 2.0
// Functions to access object as input/output buffer
tp_as_buffer: PPyBufferProcs;
// Flags to define presence of optional/expanded features
tp_flags: LongInt;
tp_doc: PAnsiChar; // Documentation string
// call function for all accessible objects
tp_traverse: traverseproc;
// delete references to contained objects
tp_clear: inquiry;
// rich comparisons
tp_richcompare: richcmpfunc;
// weak reference enabler
tp_weaklistoffset: NativeInt;
// Iterators
tp_iter : getiterfunc;
tp_iternext : iternextfunc;
// Attribute descriptor and subclassing stuff
tp_methods : PPyMethodDef;
tp_members : PPyMemberDef;
tp_getset : PPyGetSetDef;
tp_base : PPyTypeObject;
tp_dict : PPyObject;
tp_descr_get : descrgetfunc;
tp_descr_set : descrsetfunc;
tp_dictoffset : NativeInt;
tp_init : initproc;
tp_alloc : allocfunc;
tp_new : newfunc;
tp_free : pydestructor; // Low-level free-memory routine
tp_is_gc : inquiry; // For PyObject_IS_GC
tp_bases : PPyObject;
tp_mro : PPyObject; // method resolution order
tp_cache : PPyObject;
tp_subclasses : PPyObject;
tp_weaklist : PPyObject;
//More spares
tp_xxx7 : NativeInt;
tp_xxx8 : LongInt;
end;
PPyMethodChain = ^PyMethodChain;
PyMethodChain = {$IFNDEF CPUX64}packed{$ENDIF} record
methods: PPyMethodDef;
link: PPyMethodChain;
end;
PPyClassObject = ^PyClassObject;
PyClassObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
cl_bases : PPyObject; // A tuple of class objects
cl_dict : PPyObject; // A dictionary
cl_name : PPyObject; // A string
// The following three are functions or NULL
cl_getattr : PPyObject;
cl_setattr : PPyObject;
cl_delattr : PPyObject;
end;
PPyInstanceObject = ^PyInstanceObject;
PyInstanceObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
in_class : PPyClassObject; // The class object
in_dict : PPyObject; // A dictionary
end;
{ Instance method objects are used for two purposes:
(a) as bound instance methods (returned by instancename.methodname)
(b) as unbound methods (returned by ClassName.methodname)
In case (b), im_self is NULL
}
PPyMethodObject = ^PyMethodObject;
PyMethodObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
im_func : PPyObject; // The function implementing the method
im_self : PPyObject; // The instance it is bound to, or NULL
im_class : PPyObject; // The class that defined the method
end;
// Bytecode object, compile.h
PPyCodeObject = ^PyCodeObject;
PyCodeObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
co_argcount : Integer; // #arguments, except *args
co_nlocals : Integer; // #local variables
co_stacksize : Integer; // #entries needed for evaluation stack
co_flags : Integer; // CO_..., see below
co_code : PPyObject; // instruction opcodes (it hides a PyStringObject)
co_consts : PPyObject; // list (constants used)
co_names : PPyObject; // list of strings (names used)
co_varnames : PPyObject; // tuple of strings (local variable names)
co_freevars : PPyObject; // tuple of strings (free variable names)
co_cellvars : PPyObject; // tuple of strings (cell variable names)
// The rest doesn't count for hash/cmp
co_filename : PPyObject; // string (where it was loaded from)
co_name : PPyObject; // string (name, for reference)
co_firstlineno : Integer; // first source line number
co_lnotab : PPyObject; // string (encoding addr<->lineno mapping)
end;
// from pystate.h
PPyInterpreterState = ^PyInterpreterState;
PPyThreadState = ^PyThreadState;
PPyFrameObject = ^PyFrameObject;
// Interpreter environments
PyInterpreterState = {$IFNDEF CPUX64}packed{$ENDIF} record
next : PPyInterpreterState;
tstate_head : PPyThreadState;
modules : PPyObject;
sysdict : PPyObject;
builtins : PPyObject;
checkinterval : integer;
end;
// Thread specific information
PyThreadState = {$IFNDEF CPUX64}packed{$ENDIF} record
next : PPyThreadState;
interp : PPyInterpreterState;
frame : PPyFrameObject;
recursion_depth: integer;
ticker : integer;
tracing : integer;
sys_profilefn : Pointer; // c-functions for profile/trace
sys_tracefn : Pointer;
sys_profilefunc: PPyObject;
sys_tracefunc : PPyObject;
curexc_type : PPyObject;
curexc_value : PPyObject;
curexc_traceback: PPyObject;
exc_type : PPyObject;
exc_value : PPyObject;
exc_traceback : PPyObject;
dict : PPyObject;
tick_counter :Integer;
gilstate_counter :Integer;
async_exc :PPyObject; { Asynchronous exception to raise }
thread_id :LongInt; { Thread id where this tstate was created }
{ XXX signal handlers should also be here }
end;
// from frameobject.h
PPyTryBlock = ^PyTryBlock;
PyTryBlock = {$IFNDEF CPUX64}packed{$ENDIF} record
b_type : Integer; // what kind of block this is
b_handler : Integer; // where to jump to find handler
b_level : Integer; // value stack level to pop to
end;
CO_MAXBLOCKS = 0..19;
PyFrameObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the VAR_HEAD of an object.
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
ob_size : NativeInt; // Number of items in variable part
// End of the Head of an object
f_back : PPyFrameObject; // previous frame, or NULL
f_code : PPyCodeObject; // code segment
f_builtins : PPyObject; // builtin symbol table (PyDictObject)
f_globals : PPyObject; // global symbol table (PyDictObject)
f_locals : PPyObject; // local symbol table (PyDictObject)
f_valuestack : PPPyObject; // points after the last local
(* Next free slot in f_valuestack. Frame creation sets to f_valuestack.
Frame evaluation usually NULLs it, but a frame that yields sets it
to the current stack top. *)
f_stacktop : PPPyObject;
f_trace : PPyObject; // Trace function
f_exc_type, f_exc_value, f_exc_traceback: PPyObject;
f_tstate : PPyThreadState;
f_lasti : Integer; // Last instruction if called
f_lineno : Integer; // Current line number
f_iblock : Integer; // index in f_blockstack
f_blockstack : array[CO_MAXBLOCKS] of PyTryBlock; // for try and loop blocks
f_localsplus : array[0..0] of PPyObject; // locals+stack, dynamically sized
end;
// From traceback.c
PPyTraceBackObject = ^PyTraceBackObject;
PyTraceBackObject = {$IFNDEF CPUX64}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
tb_next : PPyTraceBackObject;
tb_frame : PPyFrameObject;
tb_lasti : Integer;
tb_lineno : Integer;
end;
// Parse tree node interface