forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.cs
More file actions
executable file
·1357 lines (1059 loc) · 47.4 KB
/
runtime.cs
File metadata and controls
executable file
·1357 lines (1059 loc) · 47.4 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
// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Python.Runtime {
[SuppressUnmanagedCodeSecurityAttribute()]
public class Runtime {
/// <summary>
/// Encapsulates the low-level Python C API. Note that it is
/// the responsibility of the caller to have acquired the GIL
/// before calling any of these methods.
/// </summary>
internal const string dll = "python24";
internal static bool wrap_exceptions;
internal static bool is32bit;
internal static void Initialize() {
is32bit = IntPtr.Size == 4;
Runtime.Py_Initialize();
Runtime.PyEval_InitThreads();
IntPtr dict = Runtime.PyImport_GetModuleDict();
IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__");
PyBaseObjectType = Runtime.PyObject_GetAttrString(op, "object");
PyModuleType = Runtime.PyObject_Type(op);
PyNone = Runtime.PyObject_GetAttrString(op, "None");
PyTrue = Runtime.PyObject_GetAttrString(op, "True");
PyFalse = Runtime.PyObject_GetAttrString(op, "False");
PyBoolType = Runtime.PyObject_Type(PyTrue);
PyNoneType = Runtime.PyObject_Type(PyNone);
PyTypeType = Runtime.PyObject_Type(PyNoneType);
op = Runtime.PyObject_GetAttrString(dict, "keys");
PyMethodType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyString_FromString("string");
PyStringType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyUnicode_FromString("unicode");
PyUnicodeType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyTuple_New(0);
PyTupleType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyList_New(0);
PyListType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyDict_New();
PyDictType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyInt_FromInt32(0);
PyIntType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyLong_FromLong(0);
PyLongType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
op = Runtime.PyFloat_FromDouble(0);
PyFloatType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
IntPtr s = Runtime.PyString_FromString("_temp");
IntPtr d = Runtime.PyDict_New();
IntPtr c = Runtime.PyClass_New(IntPtr.Zero, d, s);
PyClassType = Runtime.PyObject_Type(c);
IntPtr i = Runtime.PyInstance_New(c, IntPtr.Zero, IntPtr.Zero);
PyInstanceType = Runtime.PyObject_Type(i);
Runtime.Decref(s);
Runtime.Decref(i);
Runtime.Decref(c);
Runtime.Decref(d);
Error = new IntPtr(-1);
// Determine whether we need to wrap exceptions for versions of
// of the Python runtime that do not allow new-style classes to
// be used as exceptions (Python versions 2.4 and lower).
IntPtr m = PyImport_ImportModule("exceptions");
op = Runtime.PyObject_GetAttrString(m, "Exception");
if (Runtime.PyObject_TYPE(op) == PyClassType) {
wrap_exceptions = true;
}
Runtime.Decref(op);
Runtime.Decref(m);
//wrap_exceptions = false;
// Initialize modules that depend on the runtime class.
AssemblyManager.Initialize();
PyCLRMetaType = MetaType.Initialize();
Exceptions.Initialize();
ImportHook.Initialize();
}
internal static void Shutdown() {
AssemblyManager.Shutdown();
Exceptions.Shutdown();
ImportHook.Shutdown();
Py_Finalize();
}
internal static IntPtr Py_file_input = (IntPtr)257;
internal static IntPtr PyBaseObjectType;
internal static IntPtr PyModuleType;
internal static IntPtr PyClassType;
internal static IntPtr PyInstanceType;
internal static IntPtr PyCLRMetaType;
internal static IntPtr PyMethodType;
internal static IntPtr PyUnicodeType;
internal static IntPtr PyStringType;
internal static IntPtr PyTupleType;
internal static IntPtr PyListType;
internal static IntPtr PyDictType;
internal static IntPtr PyIntType;
internal static IntPtr PyLongType;
internal static IntPtr PyFloatType;
internal static IntPtr PyBoolType;
internal static IntPtr PyNoneType;
internal static IntPtr PyTypeType;
internal static IntPtr PyTrue;
internal static IntPtr PyFalse;
internal static IntPtr PyNone;
internal static IntPtr Error;
internal static IntPtr GetBoundArgTuple(IntPtr obj, IntPtr args) {
if (Runtime.PyObject_TYPE(args) != Runtime.PyTupleType) {
Exceptions.SetError(Exceptions.TypeError, "tuple expected");
return IntPtr.Zero;
}
int size = Runtime.PyTuple_Size(args);
IntPtr items = Runtime.PyTuple_New(size + 1);
Runtime.PyTuple_SetItem(items, 0, obj);
Runtime.Incref(obj);
for (int i = 0; i < size; i++) {
IntPtr item = Runtime.PyTuple_GetItem(args, i);
Runtime.Incref(item);
Runtime.PyTuple_SetItem(items, i + 1, item);
}
return items;
}
internal static IntPtr ExtendTuple(IntPtr t, params IntPtr[] args) {
int size = Runtime.PyTuple_Size(t);
int add = args.Length;
int total = size + add;
IntPtr item;
IntPtr items = Runtime.PyTuple_New(size + add);
for (int i = 0; i < size; i++) {
item = Runtime.PyTuple_GetItem(t, i);
Runtime.Incref(item);
Runtime.PyTuple_SetItem(items, i, item);
}
for (int n = 0; n < add; n++) {
item = args[n];
Runtime.Incref(item);
Runtime.PyTuple_SetItem(items, size + n, item);
}
return items;
}
//===================================================================
// Managed exports of the Python C API. Where appropriate, we do
// some optimization to avoid managed <--> unmanaged transitions
// (mostly for heavily used methods).
//===================================================================
internal unsafe static void Incref(IntPtr op) {
void *p = (void *)op;
if ((void *)0 != p) {
if (is32bit) { (*(int *)p)++; }
else { (*(long *)p)++; }
}
}
internal unsafe static void Decref(IntPtr op) {
void *p = (void *)op;
if ((void *)0 != p) {
if (is32bit) { --(*(int *)p); }
else { --(*(long *)p); }
if ((*(int *)p) == 0) {
void *t = is32bit ? (void *)(*((uint *)p + 1)) :
(void *)(*((ulong *)p + 1));
void *f = is32bit ? (void *)(*((uint *)t + 6)) :
(void *)(*((ulong *)t + 6));
if ((void *)0 == f) {
return;
}
NativeCall.Impl.Void_Call_1(new IntPtr(f), op);
return;
}
}
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
Py_Initialize();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
Py_IsInitialized();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
Py_Finalize();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
Py_NewInterpreter();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
Py_EndInterpreter(IntPtr threadState);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyThreadState_New(IntPtr istate);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyThreadState_Get();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyThread_get_key_value(IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyThread_get_thread_ident();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyThread_set_key_value(IntPtr key, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyThreadState_Swap(IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyGILState_Ensure();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyGILState_Release(IntPtr gs);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyGILState_GetThisThreadState();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
public unsafe static extern int
Py_Main(int argc, string[] argv);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_InitThreads();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_AcquireLock();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_ReleaseLock();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_AcquireThread(IntPtr tstate);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_ReleaseThread(IntPtr tstate);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyEval_SaveThread();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
PyEval_RestoreThread(IntPtr tstate);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyEval_GetBuiltins();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyEval_GetGlobals();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyEval_GetLocals();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetProgramName();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
Py_SetProgramName(string name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetPythonHome();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern void
Py_SetPythonHome(string home);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetVersion();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetPlatform();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetCopyright();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetCompiler();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern string
Py_GetBuildInfo();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyRun_SimpleString(string code);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
Py_CompileString(string code, string file, IntPtr tok);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyImport_ExecCodeModule(string name, IntPtr code);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyCFunction_New(IntPtr ml, IntPtr self);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyCFunction_NewEx(IntPtr ml, IntPtr self, IntPtr mod);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyCFunction_Call(IntPtr func, IntPtr args, IntPtr kw);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyClass_New(IntPtr bases, IntPtr dict, IntPtr name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyInstance_New(IntPtr cls, IntPtr args, IntPtr kw);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyInstance_NewRaw(IntPtr cls, IntPtr dict);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyMethod_New(IntPtr func, IntPtr self, IntPtr cls);
//====================================================================
// Python abstract object API
//====================================================================
// A macro-like method to get the type of a Python object. This is
// designed to be lean and mean in IL & avoid managed <-> unmanaged
// transitions. Note that this does not incref the type object.
internal unsafe static IntPtr
PyObject_TYPE(IntPtr op) {
void *p = (void *)op;
if ((void *)0 == p) {
return IntPtr.Zero;
}
if (is32bit) {
return new IntPtr((void *)(*((uint *)p + 1)));
}
else {
return new IntPtr((void *)(*((ulong *)p + 1)));
}
}
// Managed version of the standard Python C API PyObject_Type call.
// This version avoids a managed <-> unmanaged transition. This one
// does incref the returned type object.
internal unsafe static IntPtr
PyObject_Type(IntPtr op) {
IntPtr tp = PyObject_TYPE(op);
Runtime.Incref(tp);
return tp;
}
internal static string PyObject_GetTypeName(IntPtr op) {
IntPtr pyType = Marshal.ReadIntPtr(op, IntPtr.Size);
IntPtr ppName = Marshal.ReadIntPtr(pyType, (3 * IntPtr.Size));
return Marshal.PtrToStringAnsi(ppName);
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_HasAttrString(IntPtr pointer, string name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_GetAttrString(IntPtr pointer, string name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_SetAttrString(IntPtr pointer, string name, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_HasAttr(IntPtr pointer, IntPtr name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_GetAttr(IntPtr pointer, IntPtr name);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_SetAttr(IntPtr pointer, IntPtr name, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_GetItem(IntPtr pointer, IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_SetItem(IntPtr pointer, IntPtr key, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_DelItem(IntPtr pointer, IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_GetIter(IntPtr op);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_CallObject(IntPtr pointer, IntPtr args);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_Compare(IntPtr value1, IntPtr value2);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_IsInstance(IntPtr ob, IntPtr type);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_IsSubclass(IntPtr ob, IntPtr type);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyCallable_Check(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_IsTrue(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyObject_Size(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_Hash(IntPtr op);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_Repr(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_Str(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyObject_Dir(IntPtr pointer);
//====================================================================
// Python number API
//====================================================================
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Int(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Long(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Float(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern bool
PyNumber_Check(IntPtr ob);
internal static bool PyInt_Check(IntPtr ob) {
return PyObject_TypeCheck(ob, Runtime.PyIntType);
}
internal static bool PyBool_Check(IntPtr ob) {
return PyObject_TypeCheck(ob, Runtime.PyBoolType);
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
private unsafe static extern IntPtr
PyInt_FromLong(IntPtr value);
internal static IntPtr PyInt_FromInt32(int value) {
IntPtr v = new IntPtr(value);
return PyInt_FromLong(v);
}
internal static IntPtr PyInt_FromInt64(long value) {
IntPtr v = new IntPtr(value);
return PyInt_FromLong(v);
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyInt_AsLong(IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyInt_FromString(string value, IntPtr end, int radix);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyInt_GetMax();
internal static bool PyLong_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyLongType;
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromLong(long value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromUnsignedLong(uint value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromDouble(double value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromLongLong(long value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromUnsignedLongLong(ulong value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyLong_FromString(string value, IntPtr end, int radix);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyLong_AsLong(IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern uint
PyLong_AsUnsignedLong(IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern long
PyLong_AsLongLong(IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern ulong
PyLong_AsUnsignedLongLong(IntPtr value);
internal static bool PyFloat_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyFloatType;
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyFloat_FromDouble(double value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyFloat_FromString(IntPtr value, IntPtr junk);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern double
PyFloat_AsDouble(IntPtr ob);
//====================================================================
// Python sequence API
//====================================================================
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern bool
PySequence_Check(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_GetItem(IntPtr pointer, int index);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_SetItem(IntPtr pointer, int index, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_DelItem(IntPtr pointer, int index);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_GetSlice(IntPtr pointer, int i1, int i2);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_SetSlice(IntPtr pointer, int i1, int i2, IntPtr v);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_DelSlice(IntPtr pointer, int i1, int i2);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_Size(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_Contains(IntPtr pointer, IntPtr item);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_Concat(IntPtr pointer, IntPtr other);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_Repeat(IntPtr pointer, int count);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_Index(IntPtr pointer, IntPtr item);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PySequence_Count(IntPtr pointer, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_Tuple(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PySequence_List(IntPtr pointer);
//====================================================================
// Python string API
//====================================================================
internal static bool IsStringType(IntPtr op) {
IntPtr t = PyObject_TYPE(op);
return (t == PyStringType) || (t == PyUnicodeType);
}
internal static bool PyString_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyStringType;
}
internal static IntPtr PyString_FromString(string value) {
return PyString_FromStringAndSize(value, value.Length);
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyString_FromStringAndSize(string value, int size);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyString_AsString",
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyString_AS_STRING(IntPtr op);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyString_Size(IntPtr pointer);
internal static bool PyUnicode_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyUnicodeType;
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_FromObject",
ExactSpelling=true, CharSet=CharSet.Unicode)]
internal unsafe static extern IntPtr
PyUnicode_FromObject(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_FromEncodedObject",
ExactSpelling=true, CharSet=CharSet.Unicode)]
internal unsafe static extern IntPtr
PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_FromUnicode",
ExactSpelling=true, CharSet=CharSet.Unicode)]
internal unsafe static extern IntPtr
PyUnicode_FromUnicode(string s, int size);
internal static IntPtr PyUnicode_FromString(string s) {
return PyUnicode_FromUnicode(s, (s.Length));
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_GetSize",
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyUnicode_GetSize(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_AsUnicode",
ExactSpelling=true)]
internal unsafe static extern char *
PyUnicode_AsUnicode(IntPtr ob);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_AsUnicode",
ExactSpelling=true, CharSet=CharSet.Unicode)]
internal unsafe static extern IntPtr
PyUnicode_AS_UNICODE(IntPtr op);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
EntryPoint="PyUnicodeUCS2_FromOrdinal",
ExactSpelling=true, CharSet=CharSet.Unicode)]
internal unsafe static extern IntPtr
PyUnicode_FromOrdinal(int c);
internal unsafe static string GetManagedString(IntPtr op) {
IntPtr type = PyObject_TYPE(op);
if (type == Runtime.PyStringType) {
return Marshal.PtrToStringAnsi(
PyString_AS_STRING(op),
Runtime.PyString_Size(op)
);
}
if (type == Runtime.PyUnicodeType) {
char *p = Runtime.PyUnicode_AsUnicode(op);
int size = Runtime.PyUnicode_GetSize(op);
return new String(p, 0, size);
}
return null;
}
//====================================================================
// Python dictionary API
//====================================================================
internal static bool PyDict_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyDictType;
}
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDict_New();
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDictProxy_New(IntPtr dict);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDict_GetItem(IntPtr pointer, IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDict_GetItemString(IntPtr pointer, string key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyDict_SetItem(IntPtr pointer, IntPtr key, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyDict_SetItemString(IntPtr pointer, string key, IntPtr value);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyDict_DelItem(IntPtr pointer, IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern int
PyMapping_HasKey(IntPtr pointer, IntPtr key);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDict_Keys(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyDict_Values(IntPtr pointer);
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,