forked from Embarcadero/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonEngine.pas
9470 lines (8513 loc) · 320 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 *)
(* *)
(* Dr. Dietmar Budelsky *)
(* dbudelsky@web.de *)
(* Germany *)
(* *)
(* Morgan Martinet *)
(* 4723 rue Brebeuf *)
(* H2J 3L2 MONTREAL (QC) *)
(* CANADA *)
(* e-mail: p4d@mmm-experts.com *)
(* *)
(* PyScripter *)
(* e-mail: pyscripter@gmail.com *)
(* *)
(* Project page: https://github.com/pyscripter/python4delphi *)
(**************************************************************************)
(* Functionality: Delphi Components that provide an interface to the *)
(* Python language (see python.txt for more infos on *)
(* Python itself). *)
(* *)
(**************************************************************************)
(* Contributors: *)
(* Grzegorz Makarewicz (mak@mikroplan.com.pl) *)
(* Andrew Robinson (andy@hps1.demon.co.uk) *)
(* Mark Watts(mark_watts@hotmail.com) *)
(* Olivier Deckmyn (olivier.deckmyn@mail.dotcom.fr) *)
(* Sigve Tjora (public@tjora.no) *)
(* Mark Derricutt (mark@talios.com) *)
(* Igor E. Poteryaev (jah@mail.ru) *)
(* Yuri Filimonov (fil65@mail.ru) *)
(* Stefan Hoffmeister (Stefan.Hoffmeister@Econos.de) *)
(* Michiel du Toit (micdutoit@hsbfn.com) - Lazarus Port *)
(* Chris Nicolai (nicolaitanes@gmail.com) *)
(* Andrey Gruzdev (andrey.gruzdev@gmail.com) *)
(**************************************************************************)
(* 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}
{$POINTERMATH ON}
unit PythonEngine;
{ TODO -oMMM : implement tp_as_buffer slot }
{ TODO -oMMM : implement Attribute descriptor and subclassing stuff }
{$IFNDEF FPC}
{$IFNDEF DELPHIXE2_OR_HIGHER}
Error! Delphi XE2 or higher is required!
{$ENDIF}
{$ENDIF}
{$IF defined(LINUX) or (defined(BSD) and not defined(DARWIN)) or defined(SOLARIS) or defined(HAIKU)}
{$define _so_files}
{$IFEND}
interface
uses
Types,
{$IFDEF MSWINDOWS}
Windows,
{$ELSE}
{$IFDEF FPC}
Dl,
DynLibs,
{$ELSE}
Posix.DLfcn,
Posix.Pthread,
{$ENDIF}
{$ENDIF}
Classes,
SysUtils,
SyncObjs,
Variants,
MethodCallBack;
{$IF not Defined(FPC) and (CompilerVersion >= 23)}
const
{$IF CompilerVersion >= 33}
pidSupportedPlatforms = pidAllPlatforms;
{$ELSE}
pidSupportedPlatforms = pidWin32 or pidWin64 or pidOSX32;
{$IFEND}
{$IFEND}
//#######################################################
//## ##
//## PYTHON specific constants ##
//## ##
//#######################################################
type
TPythonVersionProp = record
DllName : string;
RegVersion : string;
APIVersion : Integer;
end;
const
{$IFDEF MSWINDOWS}
PYTHON_KNOWN_VERSIONS: array[1..9] of TPythonVersionProp =
(
(DllName: 'python33.dll'; RegVersion: '3.3'; APIVersion: 1013),
(DllName: 'python34.dll'; RegVersion: '3.4'; APIVersion: 1013),
(DllName: 'python35.dll'; RegVersion: '3.5'; APIVersion: 1013),
(DllName: 'python36.dll'; RegVersion: '3.6'; APIVersion: 1013),
(DllName: 'python37.dll'; RegVersion: '3.7'; APIVersion: 1013),
(DllName: 'python38.dll'; RegVersion: '3.8'; APIVersion: 1013),
(DllName: 'python39.dll'; RegVersion: '3.9'; APIVersion: 1013),
(DllName: 'python310.dll'; RegVersion: '3.10'; APIVersion: 1013),
(DllName: 'python311.dll'; RegVersion: '3.11'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF _so_files}
PYTHON_KNOWN_VERSIONS: array[1..9] of TPythonVersionProp =
(
(DllName: 'libpython3.3m.so'; RegVersion: '3.3'; APIVersion: 1013),
(DllName: 'libpython3.4m.so'; RegVersion: '3.4'; APIVersion: 1013),
(DllName: 'libpython3.5m.so'; RegVersion: '3.5'; APIVersion: 1013),
(DllName: 'libpython3.6m.so'; RegVersion: '3.6'; APIVersion: 1013),
(DllName: 'libpython3.7m.so'; RegVersion: '3.7'; APIVersion: 1013),
(DllName: 'libpython3.8.so'; RegVersion: '3.8'; APIVersion: 1013),
(DllName: 'libpython3.9.so'; RegVersion: '3.9'; APIVersion: 1013),
(DllName: 'libpython3.10.so'; RegVersion: '3.10'; APIVersion: 1013),
(DllName: 'libpython3.11.so'; RegVersion: '3.11'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF DARWIN}
PYTHON_KNOWN_VERSIONS: array[1..9] of TPythonVersionProp =
(
(DllName: 'libpython3.3.dylib'; RegVersion: '3.3'; APIVersion: 1013),
(DllName: 'libpython3.4.dylib'; RegVersion: '3.4'; APIVersion: 1013),
(DllName: 'libpython3.5.dylib'; RegVersion: '3.5'; APIVersion: 1013),
(DllName: 'libpython3.6.dylib'; RegVersion: '3.6'; APIVersion: 1013),
(DllName: 'libpython3.7.dylib'; RegVersion: '3.7'; APIVersion: 1013),
(DllName: 'libpython3.8.dylib'; RegVersion: '3.8'; APIVersion: 1013),
(DllName: 'libpython3.9.dylib'; RegVersion: '3.9'; APIVersion: 1013),
(DllName: 'libpython3.10.dylib'; RegVersion: '3.10'; APIVersion: 1013),
(DllName: 'libpython3.11.dylib'; RegVersion: '3.11'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF ANDROID}
PYTHON_KNOWN_VERSIONS: array[5..9] of TPythonVersionProp =
(
(DllName: 'libpython3.7m.so'; RegVersion: '3.7'; APIVersion: 1013),
(DllName: 'libpython3.8.so'; RegVersion: '3.8'; APIVersion: 1013),
(DllName: 'libpython3.9.so'; RegVersion: '3.9'; APIVersion: 1013),
(DllName: 'libpython3.10.so'; RegVersion: '3.10'; APIVersion: 1013),
(DllName: 'libpython3.11.so'; RegVersion: '3.11'; APIVersion: 1013)
);
{$ENDIF}
COMPILED_FOR_PYTHON_VERSION_INDEX = High(PYTHON_KNOWN_VERSIONS);
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;
{$IFDEF CPUARM}
DEFAULT_CALLBACK_TYPE: TCallType = TCallType.ctARMSTD;
{$ELSE}
DEFAULT_CALLBACK_TYPE: TCallType = TCallType.ctCDECL;
{$ENDIF CPUARM}
type
// Delphi equivalent used by TPyObject
TRichComparisonOpcode = (pyLT, pyLE, pyEQ, pyNE, pyGT, pyGE);
// C long is 8 bytes in non-Windows 64-bit operating systems
// Same Delphi's LongInt but not fpc LongInt which is always 4 bytes
// Hence the following
{$IFDEF MSWINDOWS}
C_Long = Integer;
C_ULong = Cardinal;
{$ELSE}
C_Long = NativeInt;
C_ULong = NativeUInt;
{$ENDIF}
// wchar_t is 4 bytes on Linux/OS X/Android but 2 bytes on Windows
{$IFDEF POSIX}
PWCharT = PUCS4Char;
PPWCharT = ^PUCS4Char;
WCharTString = UCS4String;
{$ELSE}
PWCharT = PWideChar;
PPWCharT = PPWideChar;
WCharTString = UnicodeString;
{$ENDIF}
const
{
Type flags (tp_flags)
These flags are used to change expected features and behavior for a
particular type.
Arbitration of the flag bit positions will need to be coordinated among
all extension writers who publicly release their extensions (this will
be fewer than you might expect!).
Most flags were removed as of Python 3.0 to make room for new flags. (Some
flags are not for backwards compatibility but to indicate the presence of an
optional feature; these flags remain of course.)
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.
}
// Set if the type object is dynamically allocated
Py_TPFLAGS_HEAPTYPE = (C_ULong(1) shl 9);
// Set if the type allows subclassing
Py_TPFLAGS_BASETYPE = (C_ULong(1) shl 10);
// Set if the type is 'ready' -- fully initialized
Py_TPFLAGS_READY = (C_ULong(1) shl 12);
// Set while the type is being 'readied', to prevent recursive ready calls
Py_TPFLAGS_READYING = (C_ULong(1) shl 13);
// Objects support garbage collection (see objimp.h)
Py_TPFLAGS_HAVE_GC = (C_ULong(1) shl 14);
// Set if the type implements the vectorcall protocol (PEP 590) */
_Py_TPFLAGS_HAVE_VECTORCALL = (C_ULong(1) shl 11);
// Objects behave like an unbound method
Py_TPFLAGS_METHOD_DESCRIPTOR = (C_ULong(1) shl 17);
// Objects support type attribute cache
Py_TPFLAGS_HAVE_VERSION_TAG = (C_ULong(1) shl 18);
Py_TPFLAGS_VALID_VERSION_TAG = (C_ULong(1) shl 19);
// Type is abstract and cannot be instantiated
Py_TPFLAGS_IS_ABSTRACT = (C_ULong(1) shl 20);
// These flags are used to determine if a type is a subclass.
Py_TPFLAGS_LONG_SUBCLASS = (C_ULong(1) shl 24);
Py_TPFLAGS_LIST_SUBCLASS = (C_ULong(1) shl 25);
Py_TPFLAGS_TUPLE_SUBCLASS = (C_ULong(1) shl 26);
Py_TPFLAGS_BYTES_SUBCLASS = (C_ULong(1) shl 27);
Py_TPFLAGS_UNICODE_SUBCLASS = (C_ULong(1) shl 28);
Py_TPFLAGS_DICT_SUBCLASS = (C_ULong(1) shl 29);
Py_TPFLAGS_BASE_EXC_SUBCLASS = (C_ULong(1) shl 30);
Py_TPFLAGS_TYPE_SUBCLASS = (C_ULong(1) shl 31);
Py_TPFLAGS_DEFAULT = Py_TPFLAGS_BASETYPE or Py_TPFLAGS_HAVE_VERSION_TAG;
// See function PyType_HasFeature below for testing the flags.
// Delphi equivalent used by TPythonType
type
TPFlag = (tpfHeapType, tpfBaseType, tpfReady, tpfReadying, tpfHaveGC,
tpVectorCall, tpMethodDescriptor, tpHaveVersionTag,
tpValidVersionTag, tpIsAbstract, tpLongSubclass,
tpListSubClass, tpTupleSubclass, tpBytesSubclass,
tpBaseExcSubclass, tpTypeSubclass);
TPFlags = set of TPFlag;
const
TPFLAGS_DEFAULT = [tpfBaseType, tpHaveVersionTag];
//------- Python opcodes ----------//
const
single_input = 256;
file_input = 257;
eval_input = 258;
// UnicodeObject.h
const
// Return values of the PyUnicode_KIND() macro
{
PyUnicode_WCHAR_KIND is deprecated. Will be removed in Python 12.
String contains only wstr byte characters. This is only possible
when the string was created with a legacy API and _PyUnicode_Ready()
has not been called yet.
}
PyUnicode_WCHAR_KIND = 0;
PyUnicode_1BYTE_KIND = 1;
PyUnicode_2BYTE_KIND = 2;
PyUnicode_4BYTE_KIND = 4;
// 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;
PY_WRITE_RESTRICTED = 4;
RESTRICTED = (READ_RESTRICTED or PY_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
CR = #13;
LF = #10;
TAB = #09;
CRLF = CR+LF;
//#######################################################
//## ##
//## Python specific interface ##
//## ##
//#######################################################
type
PP_frozen = ^P_frozen;
P_frozen = ^_frozen;
PPyObject = ^PyObject;
PPPyObject = ^PPyObject;
PPPPyObject = ^PPPyObject;
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;
ssizeobjargproc = function( ob1 : PPyObject; i: NativeInt; ob2 : PPyObject):
integer; cdecl;
objobjargproc = function( ob1,ob2,ob3 : PPyObject): integer; cdecl;
pydestructor = procedure(ob: PPyObject); cdecl;
getattrfunc = function( ob1: PPyObject; name: PAnsiChar): PPyObject; cdecl;
setattrfunc = function( ob1: PPyObject; name: PAnsiChar; 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;
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 = {$IFDEF CPUX86}packed{$ENDIF} record
nb_add : binaryfunc;
nb_subtract : binaryfunc;
nb_multiply : binaryfunc;
nb_remainder : binaryfunc;
nb_divmod : binaryfunc;
nb_power : ternaryfunc;
nb_negative : unaryfunc;
nb_positive : unaryfunc;
nb_absolute : unaryfunc;
nb_bool : inquiry;
nb_invert : unaryfunc;
nb_lshift : binaryfunc;
nb_rshift : binaryfunc;
nb_and : binaryfunc;
nb_xor : binaryfunc;
nb_or : binaryfunc;
nb_int : unaryfunc;
nb_reserved : Pointer; // not used
nb_float : unaryfunc;
nb_inplace_add : binaryfunc;
nb_inplace_subtract : binaryfunc;
nb_inplace_multiply : 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;
nb_floor_divide : binaryfunc;
nb_true_divide : binaryfunc;
nb_inplace_floor_divide : binaryfunc;
nb_inplace_true_divide : binaryfunc;
nb_index : unaryfunc;
nb_matrix_multiply : binaryfunc; // new in python 3.5
nb_inplace_matrix_multiply : binaryfunc; // new in python 3.5
end;
PPyNumberMethods = ^PyNumberMethods;
PySequenceMethods = {$IFDEF CPUX86}packed{$ENDIF} record
sq_length : lenfunc;
sq_concat : binaryfunc;
sq_repeat : ssizeargfunc;
sq_item : ssizeargfunc;
was_sq_slice : Pointer; // empty slot in python 3.x
sq_ass_item : ssizeobjargproc;
was_sq_ass_slice : Pointer; // empty slot in python 3.x
sq_contains : objobjproc;
sq_inplace_concat : binaryfunc;
sq_inplace_repeat : ssizeargfunc;
end;
PPySequenceMethods = ^PySequenceMethods;
PyMappingMethods = {$IFDEF CPUX86}packed{$ENDIF} record
mp_length : lenfunc;
mp_subscript : binaryfunc;
mp_ass_subscript : objobjargproc;
end;
PPyMappingMethods = ^PyMappingMethods;
Py_complex = {$IFDEF CPUX86}packed{$ENDIF} record
real : double;
imag : double;
end;
PyObject = {$IFDEF CPUX86}packed{$ENDIF} record
ob_refcnt: NativeInt;
ob_type: PPyTypeObject;
end;
_frozen = {$IFDEF CPUX86}packed{$ENDIF} record
name : PAnsiChar;
code : PByte;
size : Integer;
end;
PySliceObject = {$IFDEF CPUX86}packed{$ENDIF} record
ob_refcnt: NativeInt;
ob_type: PPyTypeObject;
start, stop, step: PPyObject;
end;
PPyMethodDef = ^PyMethodDef;
PyMethodDef = {$IFDEF CPUX86}packed{$ENDIF} record
ml_name: PAnsiChar;
ml_meth: PyCFunction;
ml_flags: Integer;
ml_doc: PAnsiChar;
end;
// structmember.h
PPyMemberDef = ^PyMemberDef;
PyMemberDef = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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 = {$IFDEF CPUX86}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_vectorcall_offset: NativeInt;
tp_getattr: getattrfunc;
tp_setattr: setattrfunc;
tp_as_async: Pointer; // not implemented
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;
// Functions to access object as input/output buffer
tp_as_buffer: Pointer; // PPyBufferProcs - not implemented
// Flags to define presence of optional/expanded features
tp_flags: C_ULong;
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;
tp_del : PyDestructor;
tp_version_tag : Cardinal; // Type attribute cache version tag. Added in version 2.6
tp_finalize : PyDestructor;
tp_vectorcall : Pointer; // not implemented
//More spares
tp_xxx1 : NativeInt;
tp_xxx2 : NativeInt;
tp_xxx3 : NativeInt;
tp_xxx4 : NativeInt;
tp_xxx5 : NativeInt;
tp_xxx6 : NativeInt;
tp_xxx7 : NativeInt;
tp_xxx8 : NativeInt;
tp_xxx9 : NativeInt;
tp_xxx10 : NativeInt;
end;
// from pystate.h
// the structure of PyInterpreterState and PyThreadState is considered
// an implementation detail. It has been changing between python versions
// and there is no real use of accessing these structures directly.
PPyInterpreterState = Pointer;
PPyThreadState = Pointer;
// Parse tree node interface
PNode = ^node;
node = {$IFDEF CPUX86}packed{$ENDIF} record
n_type : smallint;
n_str : PAnsiChar;
n_lineno : integer;
n_col_offset: integer;
n_nchildren : integer;
n_child : PNode;
end;
PPyCompilerFlags = ^PyCompilerFlags;
PyCompilerFlags = {$IFDEF CPUX86}packed{$ENDIF} record
flags : integer;
cf_feature_version : integer; //added in Python 3.8
end;
// from datetime.h
{* Fields are packed into successive bytes, each viewed as unsigned and
* big-endian, unless otherwise noted:
*
* byte offset
* 0 year 2 bytes, 1-9999
* 2 month 1 byte, 1-12
* 3 day 1 byte, 1-31
* 4 hour 1 byte, 0-23
* 5 minute 1 byte, 0-59
* 6 second 1 byte, 0-59
* 7 usecond 3 bytes, 0-999999
* 10
*}
const
{ # of bytes for year, month, and day. }
_PyDateTime_DATE_DATASIZE = 4;
{ # of bytes for hour, minute, second, and usecond. }
_PyDateTime_TIME_DATASIZE = 6;
{ # of bytes for year, month, day, hour, minute, second, and usecond. }
_PyDateTime_DATETIME_DATASIZE = 10;
type
PyDateTime_Delta = {$IFDEF CPUX86}packed{$ENDIF} record
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : NativeInt; // -1 when unknown
days : Integer; // -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS
seconds : Integer; // 0 <= seconds < 24*3600 is invariant
microseconds: Integer; // 0 <= microseconds < 1000000 is invariant
end;
PPyDateTime_Delta = ^PyDateTime_Delta;
PyDateTime_TZInfo = {$IFDEF CPUX86}packed{$ENDIF} record // a pure abstract base clase
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
end;
PPyDateTime_TZInfo = ^PyDateTime_TZInfo;
{
/* The datetime and time types have hashcodes, and an optional tzinfo member,
* present if and only if hastzinfo is true.
*/
#define _PyTZINFO_HEAD \
PyObject_HEAD \
long hashcode; \
char hastzinfo; /* boolean flag */
}
{* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
* convenient to cast to, when getting at the hastzinfo member of objects
* starting with _PyTZINFO_HEAD.
*}
_PyDateTime_BaseTZInfo = {$IFDEF CPUX86}packed{$ENDIF} record
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
end;
_PPyDateTime_BaseTZInfo = ^_PyDateTime_BaseTZInfo;
{* All time objects are of PyDateTime_TimeType, but that can be allocated
* in two ways, with or without a tzinfo member. Without is the same as
* tzinfo == None, but consumes less memory. _PyDateTime_BaseTime is an
* internal struct used to allocate the right amount of space for the
* "without" case.
*}
{#define _PyDateTime_TIMEHEAD \
_PyTZINFO_HEAD \
unsigned char data[_PyDateTime_TIME_DATASIZE];
}
_PyDateTime_BaseTime = {$IFDEF CPUX86}packed{$ENDIF} record // hastzinfo false
// Start of _PyDateTime_TIMEHEAD
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
data : array[0..Pred(_PyDateTime_TIME_DATASIZE)] of Byte;
// End of _PyDateTime_TIMEHEAD
end;
_PPyDateTime_BaseTime = ^_PyDateTime_BaseTime;
PyDateTime_Time = {$IFDEF CPUX86}packed{$ENDIF} record // hastzinfo true
// Start of _PyDateTime_TIMEHEAD
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
data : array[0..Pred(_PyDateTime_TIME_DATASIZE)] of Byte;
// End of _PyDateTime_TIMEHEAD
tzinfo : PPyObject;
end;
PPyDateTime_Time = ^PyDateTime_Time;
{* All datetime objects are of PyDateTime_DateTimeType, but that can be
* allocated in two ways too, just like for time objects above. In addition,
* the plain date type is a base class for datetime, so it must also have
* a hastzinfo member (although it's unused there).
*}
PyDateTime_Date = {$IFDEF CPUX86}packed{$ENDIF} record
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
data : array[0..Pred(_PyDateTime_DATE_DATASIZE)] of Byte;
end;
PPyDateTime_Date = ^PyDateTime_Date;
{
#define _PyDateTime_DATETIMEHEAD \
_PyTZINFO_HEAD \
unsigned char data[_PyDateTime_DATETIME_DATASIZE];
}
_PyDateTime_BaseDateTime = {$IFDEF CPUX86}packed{$ENDIF} record // hastzinfo false
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
data : array[0..Pred(_PyDateTime_DATETIME_DATASIZE)] of Byte;
end;
_PPyDateTime_BaseDateTime = ^_PyDateTime_BaseDateTime;
PyDateTime_DateTime = {$IFDEF CPUX86}packed{$ENDIF} record // hastzinfo true
// Start of _PyDateTime_DATETIMEHEAD
// Start of _PyTZINFO_HEAD
// Start of the Head of an object
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
// End of the Head of an object
hashcode : Integer;
hastzinfo : Char; // boolean flag
// End of _PyTZINFO_HEAD
data : array[0..Pred(_PyDateTime_DATETIME_DATASIZE)] of Byte;
// End of _PyDateTime_DATETIMEHEAD
tzinfo : PPyObject;
end;
PPyDateTime_DateTime = ^PyDateTime_DateTime;
//#######################################################
//## ##
//## GIL state ##
//## ##
//#######################################################
const
PyGILState_LOCKED = 0;
PyGILState_UNLOCKED = 1;
type
PyGILState_STATE = type Integer; // (PyGILState_LOCKED, PyGILState_UNLOCKED);
//#######################################################
//## ##
//## New exception classes ##
//## ##
//#######################################################
// Components' exceptions
EDLLLoadError = class(Exception);
EDLLImportError = class(Exception)
public
WrongFunc : AnsiString;
ErrorCode : Integer;
end;
// Python's exceptions
EPythonError = class(Exception)
public
EName : string;
EValue : string;
end;
EPyExecError = class(EPythonError);
// Standard exception classes of Python
{ Hierarchy of Python exceptions, Python 2.3, copied from <INSTALL>\Python\exceptions.c
Exception\n\
|\n\
+-- SystemExit\n\
+-- StopIteration\n\
+-- StandardError\n\
| |\n\
| +-- KeyboardInterrupt\n\
| +-- ImportError\n\
| +-- EnvironmentError\n\
| | |\n\
| | +-- IOError\n\
| | +-- OSError\n\
| | |\n\
| | +-- WindowsError\n\
| | +-- VMSError\n\
| |\n\
| +-- EOFError\n\
| +-- RuntimeError\n\
| | |\n\
| | +-- NotImplementedError\n\
| |\n\
| +-- NameError\n\
| | |\n\
| | +-- UnboundLocalError\n\
| |\n\
| +-- AttributeError\n\
| +-- SyntaxError\n\
| | |\n\
| | +-- IndentationError\n\