FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Runtime.cs at noncopyableanalyzer · pythonnet/pythonnet · GitHub
pythonnet/src/runtime/Runtime.cs at noncopyableanalyzer · pythonnet/pythonnet · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
pythonnet
pythonnet
Repository navigation
Code
Issues
147
(147)
Pull requests
11
(11)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
Runtime.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
1811 lines (1284 loc) · 72.2 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Runtime.cs
Copy path
File metadata and controls
1811 lines (1284 loc) · 72.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
System
.
Diagnostics
;
using
System
.
Diagnostics
.
Contracts
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Text
;
using
System
.
Threading
;
using
System
.
Collections
.
Generic
;
using
Python
.
Runtime
.
Native
;
using
System
.
Linq
;
using
static
System
.
FormattableString
;
namespace
Python
.
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>
public
unsafe
partial
class
Runtime
{
public
static
string
?
PythonDLL
{
get
=>
_PythonDll
;
set
{
if
(
_isInitialized
)
throw
new
InvalidOperationException
(
"This property must be set before runtime is initialized"
)
;
_PythonDll
=
value
;
}
}
static
string
?
_PythonDll
=
GetDefaultDllName
(
)
;
private
static
string
?
GetDefaultDllName
(
)
{
string
dll
=
Environment
.
GetEnvironmentVariable
(
"PYTHONNET_PYDLL"
)
;
if
(
dll
is
not
null
)
return
dll
;
string
verString
=
Environment
.
GetEnvironmentVariable
(
"PYTHONNET_PYVER"
)
;
if
(
!
Version
.
TryParse
(
verString
,
out
var
version
)
)
return
null
;
return
GetDefaultDllName
(
version
)
;
}
private
static
string
GetDefaultDllName
(
Version
version
)
{
string
prefix
=
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
)
?
""
:
"lib"
;
string
suffix
=
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
)
?
Invariant
(
$
"
{
version
.
Major
}
{
version
.
Minor
}
"
)
:
Invariant
(
$
"
{
version
.
Major
}
.
{
version
.
Minor
}
"
)
;
string
ext
=
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
)
?
".dll"
:
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
OSX
)
?
".dylib"
:
".so"
;
return
prefix
+
"python"
+
suffix
+
ext
;
}
private
static
bool
_isInitialized
=
false
;
internal
static
bool
IsInitialized
=>
_isInitialized
;
private
static
bool
_typesInitialized
=
false
;
internal
static
bool
TypeManagerInitialized
=>
_typesInitialized
;
internal
static
readonly
bool
Is32Bit
=
IntPtr
.
Size
==
4
;
// Available in newer .NET Core versions (>= 5) as IntPtr.MaxValue etc.
internal
static
readonly
long
IntPtrMaxValue
=
Is32Bit
?
Int32
.
MaxValue
:
Int64
.
MaxValue
;
internal
static
readonly
long
IntPtrMinValue
=
Is32Bit
?
Int32
.
MinValue
:
Int64
.
MinValue
;
internal
static
readonly
ulong
UIntPtrMaxValue
=
Is32Bit
?
UInt32
.
MaxValue
:
UInt64
.
MaxValue
;
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
internal
static
bool
IsWindows
=
Environment
.
OSVersion
.
Platform
==
PlatformID
.
Win32NT
;
internal
static
Version
InteropVersion
{
get
;
}
=
System
.
Reflection
.
Assembly
.
GetExecutingAssembly
(
)
.
GetName
(
)
.
Version
;
public
static
int
MainManagedThreadId
{
get
;
private
set
;
}
private
static
readonly
List
<
PyObject
>
_pyRefs
=
new
(
)
;
internal
static
Version
PyVersion
{
get
{
var
versionTuple
=
PySys_GetObject
(
"version_info"
)
;
var
major
=
Converter
.
ToInt32
(
PyTuple_GetItem
(
versionTuple
,
0
)
)
;
var
minor
=
Converter
.
ToInt32
(
PyTuple_GetItem
(
versionTuple
,
1
)
)
;
var
micro
=
Converter
.
ToInt32
(
PyTuple_GetItem
(
versionTuple
,
2
)
)
;
return
new
Version
(
major
,
minor
,
micro
)
;
}
}
const
string
RunSysPropName
=
"__pythonnet_run__"
;
static
int
run
=
0
;
internal
static
int
GetRun
(
)
{
int
runNumber
=
run
;
Debug
.
Assert
(
runNumber
>
0
,
"This must only be called after Runtime is initialized at least once"
)
;
return
runNumber
;
}
internal
static
bool
HostedInPython
;
internal
static
bool
ProcessIsTerminating
;
/// <summary>
/// Initialize the runtime...
/// </summary>
/// <remarks>Always call this method from the Main thread. After the
/// first call to this method, the main thread has acquired the GIL.</remarks>
internal
static
void
Initialize
(
bool
initSigs
=
false
)
{
if
(
_isInitialized
)
{
return
;
}
_isInitialized
=
true
;
bool
interpreterAlreadyInitialized
=
TryUsingDll
(
(
)
=>
Py_IsInitialized
(
)
!=
0
)
;
if
(
!
interpreterAlreadyInitialized
)
{
Py_InitializeEx
(
initSigs
?
1
:
0
)
;
NewRun
(
)
;
if
(
PyEval_ThreadsInitialized
(
)
==
0
)
{
PyEval_InitThreads
(
)
;
}
RuntimeState
.
Save
(
)
;
}
else
{
if
(
!
HostedInPython
)
{
PyGILState_Ensure
(
)
;
}
BorrowedReference
pyRun
=
PySys_GetObject
(
RunSysPropName
)
;
if
(
pyRun
!=
null
)
{
run
=
checked
(
(
int
)
PyLong_AsSignedSize_t
(
pyRun
)
)
;
}
else
{
NewRun
(
)
;
}
}
MainManagedThreadId
=
Thread
.
CurrentThread
.
ManagedThreadId
;
Finalizer
.
Initialize
(
)
;
InitPyMembers
(
)
;
ABI
.
Initialize
(
PyVersion
)
;
InternString
.
Initialize
(
)
;
GenericUtil
.
Reset
(
)
;
ClassManager
.
Reset
(
)
;
ClassDerivedObject
.
Reset
(
)
;
TypeManager
.
Initialize
(
)
;
CLRObject
.
creationBlocked
=
false
;
_typesInitialized
=
true
;
// Initialize modules that depend on the runtime class.
AssemblyManager
.
Initialize
(
)
;
OperatorMethod
.
Initialize
(
)
;
if
(
RuntimeData
.
HasStashData
(
)
)
{
RuntimeData
.
RestoreRuntimeData
(
)
;
}
else
{
PyCLRMetaType
=
MetaType
.
Initialize
(
)
;
ImportHook
.
Initialize
(
)
;
}
Exceptions
.
Initialize
(
)
;
// Need to add the runtime directory to sys.path so that we
// can find built-in assemblies like System.Data, et. al.
string
rtdir
=
RuntimeEnvironment
.
GetRuntimeDirectory
(
)
;
BorrowedReference
path
=
PySys_GetObject
(
"path"
)
;
using
var
item
=
PyString_FromString
(
rtdir
)
;
if
(
PySequence_Contains
(
path
,
item
.
Borrow
(
)
)
==
0
)
{
PyList_Append
(
path
,
item
.
Borrow
(
)
)
;
}
AssemblyManager
.
UpdatePath
(
)
;
clrInterop
=
GetModuleLazy
(
"clr.interop"
)
;
inspect
=
GetModuleLazy
(
"inspect"
)
;
hexCallable
=
new
(
(
)
=>
new
PyString
(
"%x"
)
.
GetAttr
(
"__mod__"
)
)
;
}
static
void
NewRun
(
)
{
run
++
;
using
var
pyRun
=
PyLong_FromLongLong
(
run
)
;
PySys_SetObject
(
RunSysPropName
,
pyRun
.
BorrowOrThrow
(
)
)
;
}
private
static
void
InitPyMembers
(
)
{
using
(
var
builtinsOwned
=
PyImport_ImportModule
(
"builtins"
)
)
{
var
builtins
=
builtinsOwned
.
Borrow
(
)
;
SetPyMember
(
out
PyNotImplemented
,
PyObject_GetAttrString
(
builtins
,
"NotImplemented"
)
.
StealNullable
(
)
)
;
SetPyMember
(
out
PyBaseObjectType
,
PyObject_GetAttrString
(
builtins
,
"object"
)
.
StealNullable
(
)
)
;
SetPyMember
(
out
_PyNone
,
PyObject_GetAttrString
(
builtins
,
"None"
)
.
StealNullable
(
)
)
;
SetPyMember
(
out
_PyTrue
,
PyObject_GetAttrString
(
builtins
,
"True"
)
.
StealNullable
(
)
)
;
SetPyMember
(
out
_PyFalse
,
PyObject_GetAttrString
(
builtins
,
"False"
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyBoolType
,
_PyTrue
!
)
;
SetPyMemberTypeOf
(
out
PyNoneType
,
_PyNone
!
)
;
SetPyMemberTypeOf
(
out
PyMethodType
,
PyObject_GetAttrString
(
builtins
,
"len"
)
.
StealNullable
(
)
)
;
// For some arcane reason, builtins.__dict__.__setitem__ is *not*
// a wrapper_descriptor, even though dict.__setitem__ is.
//
// object.__init__ seems safe, though.
SetPyMemberTypeOf
(
out
PyWrapperDescriptorType
,
PyObject_GetAttrString
(
PyBaseObjectType
,
"__init__"
)
.
StealNullable
(
)
)
;
SetPyMember
(
out
PySuper_Type
,
PyObject_GetAttrString
(
builtins
,
"super"
)
.
StealNullable
(
)
)
;
}
SetPyMemberTypeOf
(
out
PyStringType
,
PyString_FromString
(
"string"
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyUnicodeType
,
PyString_FromString
(
"unicode"
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyBytesType
,
EmptyPyBytes
(
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyTupleType
,
PyTuple_New
(
0
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyListType
,
PyList_New
(
0
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyDictType
,
PyDict_New
(
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyLongType
,
PyInt_FromInt32
(
0
)
.
StealNullable
(
)
)
;
SetPyMemberTypeOf
(
out
PyFloatType
,
PyFloat_FromDouble
(
0
)
.
StealNullable
(
)
)
;
_PyObject_NextNotImplemented
=
Get_PyObject_NextNotImplemented
(
)
;
{
using
var
sys
=
PyImport_ImportModule
(
"sys"
)
;
SetPyMemberTypeOf
(
out
PyModuleType
,
sys
.
StealNullable
(
)
)
;
}
}
private
static
NativeFunc
*
Get_PyObject_NextNotImplemented
(
)
{
using
var
pyType
=
SlotHelper
.
CreateObjectType
(
)
;
return
Util
.
ReadPtr
<
NativeFunc
>
(
pyType
.
Borrow
(
)
,
TypeOffset
.
tp_iternext
)
;
}
internal
static
void
Shutdown
(
)
{
if
(
Py_IsInitialized
(
)
==
0
||
!
_isInitialized
)
{
return
;
}
_isInitialized
=
false
;
var
state
=
PyGILState_Ensure
(
)
;
if
(
!
HostedInPython
&&
!
ProcessIsTerminating
)
{
// avoid saving dead objects
TryCollectingGarbage
(
runs
:
3
)
;
RuntimeData
.
Stash
(
)
;
}
AssemblyManager
.
Shutdown
(
)
;
OperatorMethod
.
Shutdown
(
)
;
ImportHook
.
Shutdown
(
)
;
ClearClrModules
(
)
;
RemoveClrRootModule
(
)
;
TryCollectingGarbage
(
MaxCollectRetriesOnShutdown
,
forceBreakLoops
:
true
,
obj
:
true
,
derived
:
false
,
buffer
:
false
)
;
CLRObject
.
creationBlocked
=
true
;
NullGCHandles
(
ExtensionType
.
loadedExtensions
)
;
ClassManager
.
RemoveClasses
(
)
;
TypeManager
.
RemoveTypes
(
)
;
_typesInitialized
=
false
;
MetaType
.
Release
(
)
;
PyCLRMetaType
.
Dispose
(
)
;
PyCLRMetaType
=
null
!
;
Exceptions
.
Shutdown
(
)
;
PythonEngine
.
InteropConfiguration
.
Dispose
(
)
;
DisposeLazyObject
(
clrInterop
)
;
DisposeLazyObject
(
inspect
)
;
DisposeLazyObject
(
hexCallable
)
;
PyObjectConversions
.
Reset
(
)
;
PyGC_Collect
(
)
;
bool
everythingSeemsCollected
=
TryCollectingGarbage
(
MaxCollectRetriesOnShutdown
)
;
Debug
.
Assert
(
everythingSeemsCollected
)
;
Finalizer
.
Shutdown
(
)
;
InternString
.
Shutdown
(
)
;
ResetPyMembers
(
)
;
if
(
!
HostedInPython
)
{
GC
.
Collect
(
)
;
GC
.
WaitForPendingFinalizers
(
)
;
PyGILState_Release
(
state
)
;
// Then release the GIL for good, if there is somehting to release
// Use the unchecked version as the checked version calls `abort()`
// if the current state is NULL.
if
(
_PyThreadState_UncheckedGet
(
)
!=
(
PyThreadState
*
)
0
)
{
PyEval_SaveThread
(
)
;
}
ExtensionType
.
loadedExtensions
.
Clear
(
)
;
CLRObject
.
reflectedObjects
.
Clear
(
)
;
}
else
{
PyGILState_Release
(
state
)
;
}
}
const
int
MaxCollectRetriesOnShutdown
=
20
;
internal
static
int
_collected
;
static
bool
TryCollectingGarbage
(
int
runs
,
bool
forceBreakLoops
,
bool
obj
=
true
,
bool
derived
=
true
,
bool
buffer
=
true
)
{
if
(
runs
<=
0
)
throw
new
ArgumentOutOfRangeException
(
nameof
(
runs
)
)
;
for
(
int
attempt
=
0
;
attempt
<
runs
;
attempt
++
)
{
Interlocked
.
Exchange
(
ref
_collected
,
0
)
;
nint
pyCollected
=
0
;
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
GC
.
Collect
(
)
;
GC
.
WaitForPendingFinalizers
(
)
;
pyCollected
+=
PyGC_Collect
(
)
;
pyCollected
+=
Finalizer
.
Instance
.
DisposeAll
(
disposeObj
:
obj
,
disposeDerived
:
derived
,
disposeBuffer
:
buffer
)
;
}
if
(
Volatile
.
Read
(
ref
_collected
)
==
0
&&
pyCollected
==
0
)
{
if
(
attempt
+
1
==
runs
)
return
true
;
}
else
if
(
forceBreakLoops
)
{
NullGCHandles
(
CLRObject
.
reflectedObjects
)
;
CLRObject
.
reflectedObjects
.
Clear
(
)
;
}
}
return
false
;
}
/// <summary>
/// Alternates .NET and Python GC runs in an attempt to collect all garbage
/// </summary>
/// <param name="runs">Total number of GC loops to run</param>
/// <returns><c>true</c> if a steady state was reached upon the requested number of tries (e.g. on the last try no objects were collected).</returns>
[
ForbidPythonThreads
]
public
static
bool
TryCollectingGarbage
(
int
runs
)
=>
TryCollectingGarbage
(
runs
,
forceBreakLoops
:
false
)
;
static
void
DisposeLazyObject
(
Lazy
<
PyObject
>
pyObject
)
{
if
(
pyObject
.
IsValueCreated
)
{
pyObject
.
Value
.
Dispose
(
)
;
}
}
private
static
Lazy
<
PyObject
>
GetModuleLazy
(
string
moduleName
)
=>
moduleName
is
null
?
throw
new
ArgumentNullException
(
nameof
(
moduleName
)
)
:
new
Lazy
<
PyObject
>
(
(
)
=>
PyModule
.
Import
(
moduleName
)
,
isThreadSafe
:
false
)
;
private
static
void
SetPyMember
(
out
PyObject
obj
,
StolenReference
value
)
{
// XXX: For current usages, value should not be null.
if
(
value
==
null
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
obj
=
new
PyObject
(
value
)
;
_pyRefs
.
Add
(
obj
)
;
}
private
static
void
SetPyMemberTypeOf
(
out
PyType
obj
,
PyObject
value
)
{
var
type
=
PyObject_Type
(
value
)
;
obj
=
new
PyType
(
type
.
StealOrThrow
(
)
,
prevalidated
:
true
)
;
_pyRefs
.
Add
(
obj
)
;
}
private
static
void
SetPyMemberTypeOf
(
out
PyObject
obj
,
StolenReference
value
)
{
if
(
value
==
null
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
var
@ref
=
new
BorrowedReference
(
value
.
Pointer
)
;
var
type
=
PyObject_Type
(
@ref
)
;
XDecref
(
value
.
AnalyzerWorkaround
(
)
)
;
SetPyMember
(
out
obj
,
type
.
StealNullable
(
)
)
;
}
private
static
void
ResetPyMembers
(
)
{
foreach
(
var
pyObj
in
_pyRefs
)
pyObj
.
Dispose
(
)
;
_pyRefs
.
Clear
(
)
;
}
private
static
void
ClearClrModules
(
)
{
var
modules
=
PyImport_GetModuleDict
(
)
;
using
var
items
=
PyDict_Items
(
modules
)
;
nint
length
=
PyList_Size
(
items
.
BorrowOrThrow
(
)
)
;
if
(
length
<
0
)
throw
PythonException
.
ThrowLastAsClrException
(
)
;
for
(
nint
i
=
0
;
i
<
length
;
i
++
)
{
var
item
=
PyList_GetItem
(
items
.
Borrow
(
)
,
i
)
;
var
name
=
PyTuple_GetItem
(
item
,
0
)
;
var
module
=
PyTuple_GetItem
(
item
,
1
)
;
if
(
ManagedType
.
IsInstanceOfManagedType
(
module
)
)
{
PyDict_DelItem
(
modules
,
name
)
;
}
}
}
private
static
void
RemoveClrRootModule
(
)
{
var
modules
=
PyImport_GetModuleDict
(
)
;
PyDictTryDelItem
(
modules
,
"clr"
)
;
PyDictTryDelItem
(
modules
,
"clr._extra"
)
;
}
private
static
void
PyDictTryDelItem
(
BorrowedReference
dict
,
string
key
)
{
if
(
PyDict_DelItemString
(
dict
,
key
)
==
0
)
{
return
;
}
if
(
!
PythonException
.
CurrentMatches
(
Exceptions
.
KeyError
)
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
PyErr_Clear
(
)
;
}
private
static
void
NullGCHandles
(
IEnumerable
<
IntPtr
>
objects
)
{
foreach
(
IntPtr
objWithGcHandle
in
objects
.
ToArray
(
)
)
{
var
@ref
=
new
BorrowedReference
(
objWithGcHandle
)
;
ManagedType
.
TryFreeGCHandle
(
@ref
)
;
}
}
#pragma warning disable
CS8618
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
// these objects are initialized in Initialize rather than in constructor
internal
static
PyObject
PyBaseObjectType
;
internal
static
PyObject
PyModuleType
;
internal
static
PyObject
PySuper_Type
;
internal
static
PyType
PyCLRMetaType
;
internal
static
PyObject
PyMethodType
;
internal
static
PyObject
PyWrapperDescriptorType
;
internal
static
PyObject
PyUnicodeType
;
internal
static
PyObject
PyStringType
;
internal
static
PyObject
PyTupleType
;
internal
static
PyObject
PyListType
;
internal
static
PyObject
PyDictType
;
internal
static
PyObject
PyLongType
;
internal
static
PyObject
PyFloatType
;
internal
static
PyType
PyBoolType
;
internal
static
PyType
PyNoneType
;
internal
static
BorrowedReference
PyTypeType
=>
new
(
Delegates
.
PyType_Type
)
;
internal
static
PyObject
PyBytesType
;
internal
static
NativeFunc
*
_PyObject_NextNotImplemented
;
internal
static
PyObject
PyNotImplemented
;
internal
const
int
Py_LT
=
0
;
internal
const
int
Py_LE
=
1
;
internal
const
int
Py_EQ
=
2
;
internal
const
int
Py_NE
=
3
;
internal
const
int
Py_GT
=
4
;
internal
const
int
Py_GE
=
5
;
internal
static
BorrowedReference
PyTrue
=>
_PyTrue
;
static
PyObject
_PyTrue
;
internal
static
BorrowedReference
PyFalse
=>
_PyFalse
;
static
PyObject
_PyFalse
;
internal
static
BorrowedReference
PyNone
=>
_PyNone
;
private
static
PyObject
_PyNone
;
private
static
Lazy
<
PyObject
>
inspect
;
internal
static
PyObject
InspectModule
=>
inspect
.
Value
;
private
static
Lazy
<
PyObject
>
clrInterop
;
internal
static
PyObject
InteropModule
=>
clrInterop
.
Value
;
private
static
Lazy
<
PyObject
>
hexCallable
;
internal
static
PyObject
HexCallable
=>
hexCallable
.
Value
;
#pragma warning restore
CS8618
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
internal
static
BorrowedReference
CLRMetaType
=>
PyCLRMetaType
;
public
static
PyObject
None
=>
new
(
_PyNone
)
;
/// <summary>
/// Check if any Python Exceptions occurred.
/// If any exist throw new PythonException.
/// </summary>
/// <remarks>
/// Can be used instead of `obj == IntPtr.Zero` for example.
/// </remarks>
internal
static
void
CheckExceptionOccurred
(
)
{
if
(
PyErr_Occurred
(
)
!=
null
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
internal
static
Type
[
]
?
PythonArgsToTypeArray
(
BorrowedReference
arg
)
{
return
PythonArgsToTypeArray
(
arg
,
false
)
;
}
internal
static
Type
[
]
?
PythonArgsToTypeArray
(
BorrowedReference
arg
,
bool
mangleObjects
)
{
// Given a PyObject * that is either a single type object or a
// tuple of (managed or unmanaged) type objects, return a Type[]
// containing the CLR Type objects that map to those types.
BorrowedReference
args
=
arg
;
NewReference
newArgs
=
default
;
if
(
!
PyTuple_Check
(
arg
)
)
{
newArgs
=
PyTuple_New
(
1
)
;
args
=
newArgs
.
Borrow
(
)
;
PyTuple_SetItem
(
args
,
0
,
arg
)
;
}
var
n
=
PyTuple_Size
(
args
)
;
var
types
=
new
Type
[
n
]
;
Type
?
t
=
null
;
for
(
var
i
=
0
;
i
<
n
;
i
++
)
{
BorrowedReference
op
=
PyTuple_GetItem
(
args
,
i
)
;
if
(
mangleObjects
&&
(
!
PyType_Check
(
op
)
)
)
{
op
=
PyObject_TYPE
(
op
)
;
}
ManagedType
?
mt
=
ManagedType
.
GetManagedObject
(
op
)
;
if
(
mt
is
ClassBase
b
)
{
var
_type
=
b
.
type
;
t
=
_type
.
Valid
?
_type
.
Value
:
null
;
}
else
if
(
mt
is
CLRObject
ob
)
{
var
inst
=
ob
.
inst
;
if
(
inst
is
Type
ty
)
{
t
=
ty
;
}
}
else
{
t
=
Converter
.
GetTypeByAlias
(
op
)
;
}
if
(
t
==
null
)
{
types
=
null
;
break
;
}
types
[
i
]
=
t
;
}
newArgs
.
Dispose
(
)
;
return
types
;
}
/// <summary>
/// Managed exports of the Python C API. Where appropriate, we do
/// some optimization to avoid managed <--> unmanaged transitions
/// (mostly for heavily used methods).
/// </summary>
[
Obsolete
(
"Use NewReference or PyObject constructor instead"
)
]
internal
static
unsafe
void
XIncref
(
BorrowedReference
op
)
{
Py_IncRef
(
op
)
;
return
;
}
internal
static
unsafe
void
XDecref
(
StolenReference
op
)
{
#if
DEBUG
Debug
.
Assert
(
op
==
null
||
Refcount
(
new
BorrowedReference
(
op
.
Pointer
)
)
>
0
)
;
Debug
.
Assert
(
_isInitialized
||
Py_IsInitialized
(
)
!=
0
||
_Py_IsFinalizing
(
)
!=
false
)
;
#endif
if
(
op
==
null
)
return
;
Py_DecRef
(
op
.
AnalyzerWorkaround
(
)
)
;
return
;
}
[
Pure
]
internal
static
unsafe
nint
Refcount
(
BorrowedReference
op
)
{
if
(
op
==
null
)
{
return
0
;
}
var
p
=
(
nint
*
)
(
op
.
DangerousGetAddress
(
)
+
ABI
.
RefCountOffset
)
;
return
*
p
;
}
[
Pure
]
internal
static
int
Refcount32
(
BorrowedReference
op
)
=>
checked
(
(
int
)
Refcount
(
op
)
)
;
internal
static
void
TryUsingDll
(
Action
op
)
=>
TryUsingDll
(
(
)
=>
{
op
(
)
;
return
0
;
}
)
;
/// <summary>
/// Call specified function, and handle PythonDLL-related failures.
/// </summary>
internal
static
T
TryUsingDll
<
T
>
(
Func
<
T
>
op
)
{
try
{
return
op
(
)
;
}
catch
(
TypeInitializationException
loadFailure
)
{
var
delegatesLoadFailure
=
loadFailure
;
// failure to load Delegates type might have been the cause
// of failure to load some higher-level type
while
(
delegatesLoadFailure
.
InnerException
is
TypeInitializationException
nested
)
{
delegatesLoadFailure
=
nested
;
}
if
(
delegatesLoadFailure
.
InnerException
is
BadPythonDllException
badDll
)
{
throw
badDll
;
}
throw
;
}
}
/// <summary>
/// Export of Macro Py_XIncRef. Use XIncref instead.
/// Limit this function usage for Testing and Py_Debug builds
/// </summary>
/// <param name="ob">PyObject Ptr</param>
internal
static
void
Py_IncRef
(
BorrowedReference
ob
)
=>
Delegates
.
Py_IncRef
(
ob
)
;
/// <summary>
/// Export of Macro Py_XDecRef. Use XDecref instead.
/// Limit this function usage for Testing and Py_Debug builds
/// </summary>
/// <param name="ob">PyObject Ptr</param>
internal
static
void
Py_DecRef
(
StolenReference
ob
)
=>
Delegates
.
Py_DecRef
(
ob
.
AnalyzerWorkaround
(
)
)
;
internal
static
void
Py_Initialize
(
)
=>
Delegates
.
Py_Initialize
(
)
;
internal
static
void
Py_InitializeEx
(
int
initsigs
)
=>
Delegates
.
Py_InitializeEx
(
initsigs
)
;
internal
static
int
Py_IsInitialized
(
)
=>
Delegates
.
Py_IsInitialized
(
)
;
internal
static
void
Py_Finalize
(
)
=>
Delegates
.
Py_Finalize
(
)
;
internal
static
PyThreadState
*
Py_NewInterpreter
(
)
=>
Delegates
.
Py_NewInterpreter
(
)
;
internal
static
void
Py_EndInterpreter
(
PyThreadState
*
threadState
)
=>
Delegates
.
Py_EndInterpreter
(
threadState
)
;
internal
static
PyThreadState
*
PyThreadState_New
(
PyInterpreterState
*
istate
)
=>
Delegates
.
PyThreadState_New
(
istate
)
;
internal
static
PyThreadState
*
PyThreadState_Get
(
)
=>
Delegates
.
PyThreadState_Get
(
)
;
internal
static
PyThreadState
*
_PyThreadState_UncheckedGet
(
)
=>
Delegates
.
_PyThreadState_UncheckedGet
(
)
;
internal
static
int
PyGILState_Check
(
)
=>
Delegates
.
PyGILState_Check
(
)
;
internal
static
PyGILState
PyGILState_Ensure
(
)
=>
Delegates
.
PyGILState_Ensure
(
)
;
internal
static
void
PyGILState_Release
(
PyGILState
gs
)
=>
Delegates
.
PyGILState_Release
(
gs
)
;
internal
static
PyThreadState
*
PyGILState_GetThisThreadState
(
)
=>
Delegates
.
PyGILState_GetThisThreadState
(
)
;
public
static
int
Py_Main
(
int
argc
,
string
[
]
argv
)
{
var
marshaler
=
StrArrayMarshaler
.
GetInstance
(
null
)
;
var
argvPtr
=
marshaler
.
MarshalManagedToNative
(
argv
)
;
try
{
return
Delegates
.
Py_Main
(
argc
,
argvPtr
)
;
}
finally
{
marshaler
.
CleanUpNativeData
(
argvPtr
)
;
}
}
internal
static
void
PyEval_InitThreads
(
)
=>
Delegates
.
PyEval_InitThreads
(
)
;
internal
static
int
PyEval_ThreadsInitialized
(
)
=>
Delegates
.
PyEval_ThreadsInitialized
(
)
;
internal
static
void
PyEval_AcquireLock
(
)
=>
Delegates
.
PyEval_AcquireLock
(
)
;
internal
static
void
PyEval_ReleaseLock
(
)
=>
Delegates
.
PyEval_ReleaseLock
(
)
;
internal
static
void
PyEval_AcquireThread
(
PyThreadState
*
tstate
)
=>
Delegates
.
PyEval_AcquireThread
(
tstate
)
;
internal
static
void
PyEval_ReleaseThread
(
PyThreadState
*
tstate
)
=>
Delegates
.
PyEval_ReleaseThread
(
tstate
)
;
internal
static
PyThreadState
*
PyEval_SaveThread
(
)
=>
Delegates
.
PyEval_SaveThread
(
)
;
internal
static
void
PyEval_RestoreThread
(
PyThreadState
*
tstate
)
=>
Delegates
.
PyEval_RestoreThread
(
tstate
)
;
internal
static
BorrowedReference
PyEval_GetBuiltins
(
)
=>
Delegates
.
PyEval_GetBuiltins
(
)
;
internal
static
BorrowedReference
PyEval_GetGlobals
(
)
=>
Delegates
.
PyEval_GetGlobals
(
)
;
internal
static
BorrowedReference
PyEval_GetLocals
(
)
=>
Delegates
.
PyEval_GetLocals
(
)
;
internal
static
IntPtr
Py_GetProgramName
(
)
=>
Delegates
.
Py_GetProgramName
(
)
;
internal
static
void
Py_SetProgramName
(
IntPtr
name
)
=>
Delegates
.
Py_SetProgramName
(
name
)
;
internal
static
IntPtr
Py_GetPythonHome
(
)
=>
Delegates
.
Py_GetPythonHome
(
)
;
internal
static
void
Py_SetPythonHome
(
IntPtr
home
)
=>
Delegates
.
Py_SetPythonHome
(
home
)
;
internal
static
IntPtr
Py_GetPath
(
)
=>
Delegates
.
Py_GetPath
(
)
;
internal
static
void
Py_SetPath
(
IntPtr
home
)
=>
Delegates
.
Py_SetPath
(
home
)
;
internal
static
IntPtr
Py_GetVersion
(
)
=>
Delegates
.
Py_GetVersion
(
)
;
internal
static
IntPtr
Py_GetPlatform
(
)
=>
Delegates
.
Py_GetPlatform
(
)
;
internal
static
IntPtr
Py_GetCopyright
(
)
=>
Delegates
.
Py_GetCopyright
(
)
;
internal
static
IntPtr
Py_GetCompiler
(
)
=>
Delegates
.
Py_GetCompiler
(
)
;
internal
static
IntPtr
Py_GetBuildInfo
(
)
=>
Delegates
.
Py_GetBuildInfo
(
)
;
const
PyCompilerFlags
Utf8String
=
PyCompilerFlags
.
IGNORE_COOKIE
|
PyCompilerFlags
.
SOURCE_IS_UTF8
;
internal
static
int
PyRun_SimpleString
(
string
code
)
{
using
var
codePtr
=
new
StrPtr
(
code
)
;
return
Delegates
.
PyRun_SimpleStringFlags
(
codePtr
,
Utf8String
)
;
}
internal
static
NewReference
PyRun_String
(
string
code
,
RunFlagType
st
,
BorrowedReference
globals
,
BorrowedReference
locals
)
{
using
var
codePtr
=
new
StrPtr
(
code
)
;
return
Delegates
.
PyRun_StringFlags
(
codePtr
,
st
,
globals
,
locals
,
Utf8String
)
;
}
internal
static
NewReference
PyEval_EvalCode
(
BorrowedReference
co
,
BorrowedReference
globals
,
BorrowedReference
locals
)
=>
Delegates
.
PyEval_EvalCode
(
co
,
globals
,
locals
)
;
/// <summary>
/// Return value: New reference.
/// This is a simplified interface to Py_CompileStringFlags() below, leaving flags set to NULL.
/// </summary>
internal
static
NewReference
Py_CompileString
(
string
str
,
string
file
,
int
start
)
{
using
var
strPtr
=
new
StrPtr
(
str
)
;
using
var
fileObj
=
new
PyString
(
file
)
;
return
Delegates
.
Py_CompileStringObject
(
strPtr
,
fileObj
,
start
,
Utf8String
,
-
1
)
;
}
internal
static
NewReference
PyImport_ExecCodeModule
(
string
name
,
BorrowedReference
code
)
{
using
var
namePtr
=
new
StrPtr
(
name
)
;
return
Delegates
.
PyImport_ExecCodeModule
(
namePtr
,
code
)
;
}
//====================================================================
// Python abstract object API
//====================================================================
/// <summary>
/// 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.
/// </summary>
internal
static
unsafe
BorrowedReference
PyObject_TYPE
(
BorrowedReference
op
)
{
IntPtr
address
=
op
.
DangerousGetAddressOrNull
(
)
;
if
(
address
==
IntPtr
.
Zero
)
{
return
BorrowedReference
.
Null
;
}
Debug
.
Assert
(
TypeOffset
.
ob_type
>
0
)
;
BorrowedReference
*
typePtr
=
(
BorrowedReference
*
)
(
address
+
TypeOffset
.
ob_type
)
;
return
*
typePtr
;
}
internal
static
NewReference
PyObject_Type
(
BorrowedReference
o
)
=>
Delegates
.
PyObject_Type
(
o
)
;
internal
static
string
PyObject_GetTypeName
(
BorrowedReference
op
)
{
Debug
.
Assert
(
TypeOffset
.
tp_name
>
0
)
;
Debug
.
Assert
(
op
!=
null
)
;
BorrowedReference
pyType
=
PyObject_TYPE
(
op
)
;
IntPtr
ppName
=
Util
.
ReadIntPtr
(
pyType
,
TypeOffset
.
tp_name
)
;
return
Marshal
.
PtrToStringAnsi
(
ppName
)
;
}
/// <summary>
/// Test whether the Python object is an iterable.
/// </summary>
internal
static
bool
PyObject_IsIterable
(
BorrowedReference
ob
)
{
var
ob_type
=
PyObject_TYPE
(
ob
)
;
return
Util
.
ReadIntPtr
(
ob_type
,
TypeOffset
.
tp_iter
)
!=
IntPtr
.
Zero
;
}
internal
static
int
PyObject_HasAttrString
(
BorrowedReference
pointer
,
string
name
)
{
using
var
namePtr
=
new
StrPtr
(
name
)
;
return
Delegates
.
PyObject_HasAttrString
(
pointer
,
namePtr
)
;
}
internal
static
NewReference
PyObject_GetAttrString
(
BorrowedReference
pointer
,
string
name
)
{
using
var
namePtr
=
new
StrPtr
(
name
)
;
return
Delegates
.
PyObject_GetAttrString
(
pointer
,
namePtr
)
;
}
internal
static
NewReference
PyObject_GetAttrString
(
BorrowedReference
pointer
,
StrPtr
name
)
=>
Delegates
.
PyObject_GetAttrString
(
pointer
,
name
)
;
internal
static
int
PyObject_DelAttr
(
BorrowedReference
@object
,
BorrowedReference
name
)
=>
Delegates
.
PyObject_SetAttr
(
@object
,
name
,
null
)
;
internal
static
int
PyObject_DelAttrString
(
BorrowedReference
@object
,
string
name
)
{
using
var
namePtr
=
new
StrPtr
(
name
)
;
return
Delegates
.
PyObject_SetAttrString
(
@object
,
namePtr
,
null
)
;
}
internal
static
int
PyObject_SetAttrString
(
BorrowedReference
@object
,
string
name
,
BorrowedReference
value
)
{
using
var
namePtr
=
new
StrPtr
(
name
)
;
return
Delegates
.
PyObject_SetAttrString
(
@object
,
namePtr
,
value
)
;
}
internal
static
int
PyObject_HasAttr
(
BorrowedReference
pointer
,
BorrowedReference
name
)
=>
Delegates
.
PyObject_HasAttr
(
pointer
,
name
)
;
internal
static
NewReference
PyObject_GetAttr
(
BorrowedReference
pointer
,
IntPtr
name
)
=>
Delegates
.
PyObject_GetAttr
(
pointer
,
new
BorrowedReference
(
name
)
)
;
internal
static
NewReference
PyObject_GetAttr
(
BorrowedReference
o
,
BorrowedReference
name
)
=>
Delegates
.
PyObject_GetAttr
(
o
,
name
)
;
internal
static
int
PyObject_SetAttr
(
BorrowedReference
o
,
BorrowedReference
name
,
BorrowedReference
value
)
=>
Delegates
.
PyObject_SetAttr
(
o
,
name
,
value
)
;
internal
static
NewReference
PyObject_GetItem
(
BorrowedReference
o
,
BorrowedReference
key
)
=>
Delegates
.
PyObject_GetItem
(
o
,
key
)
;
internal
static
int
PyObject_SetItem
(
BorrowedReference
o
,
BorrowedReference
key
,
BorrowedReference
value
)
=>
Delegates
.
PyObject_SetItem
(
o
,
key
,
value
)
;
internal
static
int
PyObject_DelItem
(
BorrowedReference
o
,
BorrowedReference
key
)
=>
Delegates
.
PyObject_DelItem
(
o
,
key
)
;
internal
static
NewReference
PyObject_GetIter
(
BorrowedReference
op
)
=>
Delegates
.
PyObject_GetIter
(
op
)
;
internal
static
NewReference
PyObject_Call
(
BorrowedReference
pointer
,
BorrowedReference
args
,
BorrowedReference
kw
)
=>
Delegates
.
PyObject_Call
(
pointer
,
args
,
kw
)
;
internal
static
NewReference
PyObject_CallObject
(
BorrowedReference
callable
,
BorrowedReference
args
)
=>
Delegates
.
PyObject_CallObject
(
callable
,
args
)
;
internal
static
IntPtr
PyObject_CallObject
(
IntPtr
pointer
,
IntPtr
args
)
=>
Delegates
.
PyObject_CallObject
(
new
BorrowedReference
(
pointer
)
,
new
BorrowedReference
(
args
)
)
.
DangerousMoveToPointerOrNull
(
)
;
internal
static
int
PyObject_RichCompareBool
(
BorrowedReference
value1
,
BorrowedReference
value2
,
int
opid
)
=>
Delegates
.
PyObject_RichCompareBool
(
value1
,
value2
,
opid
)
;
internal
static
int
PyObject_IsInstance
(
BorrowedReference
ob
,
BorrowedReference
type
)
=>
Delegates
.
PyObject_IsInstance
(
ob
,
type
)
;
internal
static
int
PyObject_IsSubclass
(
BorrowedReference
ob
,
BorrowedReference
type
)
=>
Delegates
.
PyObject_IsSubclass
(
ob
,
type
)
;
internal
static
void
PyObject_ClearWeakRefs
(
BorrowedReference
ob
)
=>
Delegates
.
PyObject_ClearWeakRefs
(
ob
)
;
internal
static
BorrowedReference
PyObject_GetWeakRefList
(
BorrowedReference
ob
)
{
Debug
.
Assert
(
ob
!=
null
)
;
var
type
=
PyObject_TYPE
(
ob
)
;
int
offset
=
Util
.
ReadInt32
(
type
,
TypeOffset
.
tp_weaklistoffset
)
;
if
(
offset
<=
0
)
return
BorrowedReference
.
Null
;
Debug
.
Assert
(
offset
>
0
)
;
return
Util
.
ReadRef
(
ob
,
offset
)
;
}
internal
static
int
PyCallable_Check
(
BorrowedReference
o
)
=>
Delegates
.
PyCallable_Check
(
o
)
;
internal
static
int
PyObject_IsTrue
(
IntPtr
pointer
)
=>
PyObject_IsTrue
(
new
BorrowedReference
(
pointer
)
)
;
internal
static
int
PyObject_IsTrue
(
BorrowedReference
pointer
)
=>
Delegates
.
PyObject_IsTrue
(
pointer
)
;
internal
static
int
PyObject_Not
(
BorrowedReference
o
)
=>
Delegates
.
PyObject_Not
(
o
)
;
internal
static
nint
PyObject_Size
(
BorrowedReference
pointer
)
=>
Delegates
.
PyObject_Size
(
pointer
)
;
internal
static
nint
PyObject_Hash
(
BorrowedReference
op
)
=>
Delegates
.
PyObject_Hash
(
op
)
;
internal
static
NewReference
PyObject_Repr
(
BorrowedReference
pointer
)
{
AssertNoErorSet
(
)
;
return
Delegates
.
PyObject_Repr
(
pointer
)
;
}
internal
static
NewReference
PyObject_Str
(
BorrowedReference
pointer
)
{
AssertNoErorSet
(
)
;
return
Delegates
.
PyObject_Str
(
pointer
)
;
}
[
Conditional
(
"DEBUG"
)
]
internal
static
void
AssertNoErorSet
(
)
{
if
(
Exceptions
.
ErrorOccurred
(
)
)
throw
new
InvalidOperationException
(
"Can't call with exception set"
,
PythonException
.
FetchCurrent
(
)
)
;
}
internal
static
NewReference
PyObject_Dir
(
BorrowedReference
pointer
)
=>
Delegates
.
PyObject_Dir
(
pointer
)
;
internal
static
void
_Py_NewReference
(
BorrowedReference
ob
)
{
if
(
Delegates
.
_Py_NewReference
!=
null
)
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL