forked from lmbelo/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonEngine.pas
More file actions
11139 lines (10065 loc) · 385 KB
/
PythonEngine.pas
File metadata and controls
11139 lines (10065 loc) · 385 KB
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..12] of TPythonVersionProp =
(
(DllName: 'python27.dll'; RegVersion: '2.7'; APIVersion: 1013),
(DllName: 'python32.dll'; RegVersion: '3.2'; APIVersion: 1013),
(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),
(DllName: 'python312.dll'; RegVersion: '3.12'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF _so_files}
PYTHON_KNOWN_VERSIONS: array[1..12] of TPythonVersionProp =
(
(DllName: 'libpython2.7.so'; RegVersion: '2.7'; APIVersion: 1013),
(DllName: 'libpython3.2m.so'; RegVersion: '3.2'; APIVersion: 1013),
(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),
(DllName: 'libpython3.12.so'; RegVersion: '3.12'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF DARWIN}
PYTHON_KNOWN_VERSIONS: array[1..12] of TPythonVersionProp =
(
(DllName: 'libpython2.7.dylib'; RegVersion: '2.7'; APIVersion: 1013),
(DllName: 'libpython3.2.dylib'; RegVersion: '3.2'; APIVersion: 1013),
(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),
(DllName: 'libpython3.12.dylib'; RegVersion: '3.12'; APIVersion: 1013)
);
{$ENDIF}
{$IFDEF ANDROID}
PYTHON_KNOWN_VERSIONS: array[5..10] 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),
(DllName: 'libpython3.12.so'; RegVersion: '3.12'; 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;
METH_CLASS = $0010;
METH_STATIC = $0020;
// 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}
Py_ssize_t = NativeInt;
const
{Type flags (tp_flags) introduced in version 2.0
From cp2:
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).
Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
From cp3:
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.
}
//From cp2:
// PyBufferProcs contains bf_getcharbuffer
Py_TPFLAGS_HAVE_GETCHARBUFFER = (C_ULong(1) shl 0);
// PySequenceMethods contains sq_contains
Py_TPFLAGS_HAVE_SEQUENCE_IN = (C_ULong(1) shl 1);
// Objects which participate in garbage collection (see objimp.h)
Py_TPFLAGS_GC = (C_ULong(1) shl 2);
// PySequenceMethods and PyNumberMethods contain in-place operators
Py_TPFLAGS_HAVE_INPLACEOPS = (C_ULong(1) shl 3);
// PyNumberMethods do their own coercion */
Py_TPFLAGS_CHECKTYPES = (C_ULong(1) shl 4);
Py_TPFLAGS_HAVE_RICHCOMPARE = (C_ULong(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 = (C_ULong(1) shl 6);
// tp_iter is defined
Py_TPFLAGS_HAVE_ITER = (C_ULong(1) shl 7);
// New members introduced by Python 2.2 exist
Py_TPFLAGS_HAVE_CLASS = (C_ULong(1) shl 8);
//From cp3:
// 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_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
//From both
or Py_TPFLAGS_BASETYPE
//From cp3
or Py_TPFLAGS_HAVE_VERSION_TAG
;
// See function PyType_HasFeature below for testing the flags.
// Delphi equivalent used by TPythonType
type
TPFlag = (//From cp2
tpfHaveGetCharBuffer, tpfHaveSequenceIn, tpfGC, tpfHaveInplaceOps,
tpfCheckTypes, tpfHaveRichCompare, tpfHaveWeakRefs ,tpfHaveIter, tpfHaveClass,
//From cp3
tpfHeapType, tpfBaseType, tpfReady, tpfReadying, tpfHaveGC,
tpVectorCall, tpMethodDescriptor, tpHaveVersionTag,
tpValidVersionTag, tpIsAbstract, tpLongSubclass,
tpListSubClass, tpTupleSubclass, tpBytesSubclass,
tpBaseExcSubclass, tpTypeSubclass);
TPFlags = set of TPFlag;
const
TPFLAGS_DEFAULT_CP2 = [tpfHaveSequenceIn, tpfHaveInplaceOps,
tpfHaveRichCompare, tpfHaveIter,
tpfHaveClass, tpfBaseType];
TPFLAGS_DEFAULT_CP3 = [tpfBaseType, tpHaveVersionTag];
//------- Python opcodes ----------//
const
single_input = 256;
file_input = 257;
eval_input = 258;
//From cp2:
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;
//From cp3:
// 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
//From cp2:
ErrInit = -300;
//From cp3:
CR = #13;
LF = #10;
TAB = #09;
CRLF = CR+LF;
//#######################################################
//## ##
//## Python specific interface ##
//## ##
//#######################################################
type
//From cp2:
PPyIntObject = ^PyIntObject;
//From cp3:
PP_frozen = ^P_frozen;
P_frozen = ^_frozen;
PPyObject = ^PyObject;
PPPyObject = ^PPyObject;
PPPPyObject = ^PPPyObject;
PPyTypeObject = ^PyTypeObject;
PPySliceObject = ^PySliceObject;
//From cp2:
ssizessizeargfunc = function( ob1 : PPyObject; i1, i2: NativeInt): PPyObject; cdecl;
ssizessizeobjargproc = function( ob1: PPyObject; i1, i2: NativeInt; ob2: PPyObject): integer; cdecl;
printfunc = function( ob: PPyObject; var f: file; i: integer): integer; cdecl;
cmpfunc = function( ob1,ob2: PPyObject): integer; cdecl;
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;
//From cp3:
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;
//From cp2:
PyNumberMethodsCP2 = {$IFDEF CPUX86}packed{$ENDIF} record
nb_add : binaryfunc;
nb_subtract : 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;
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;
nb_floor_divide : binaryfunc;
nb_true_divide : binaryfunc;
nb_inplace_floor_divide : binaryfunc;
nb_inplace_true_divide : binaryfunc;
nb_index : unaryfunc;
end;
PPyNumberMethodsCP2 = ^PyNumberMethodsCP2;
PyNumberMethodsCP3 = {$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;
PPyNumberMethodsCP3 = ^PyNumberMethodsCP3;
PPyNumberMethods = pointer; //Switch between cp2 and cp3
PySequenceMethods = {$IFDEF CPUX86}packed{$ENDIF} record
sq_length : lenfunc;
sq_concat : binaryfunc;
sq_repeat : ssizeargfunc;
sq_item : ssizeargfunc;
sq_slice : ssizessizeargfunc; // empty slot in python 3.x
sq_ass_item : ssizeobjargproc;
sq_ass_slice : ssizessizeobjargproc; // 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;
//From cp2:
// PyBufferProcs has changed drastiacally in Python 3.0 but since
// it is currently not implemented it does not matter
PyBufferProcs = {$IFDEF CPUX86}packed{$ENDIF} record
bf_getreadbuffer : getreadbufferproc;
bf_getwritebuffer : getwritebufferproc;
bf_getsegcount : getsegcountproc;
bf_getcharbuffer : getcharbufferproc;
end;
PPyBufferProcs = ^PyBufferProcs;
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;
//From cp2:
PyIntObject = {$IFNDEF CPUX64}packed{$ENDIF} record
ob_refcnt : NativeInt;
ob_type : PPyTypeObject;
ob_ival : LongInt;
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;
//From cp2:
tp_print: printfunc; // not available and replaced in Python 3.x
tp_compare: cmpfunc; // not available and replaced in Python 3.x
// Method suites for standard classes
tp_as_number: PPyNumberMethods; //DECIDE BETWEEN CP2 and CP3
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: PPyBufferProcs; //From cp2
// 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 cp2:
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