FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/PythonTypes/PyObject.cs at v3.1.0 · pythonnet/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pythonnet
/
pythonnet
Public
Notifications
You must be signed in to change notification settings
Fork
780
Star
5.5k
Code
Issues
153
Pull requests
12
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
PythonTypes
/
PyObject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
1577 lines (1371 loc) · 54.2 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
PythonTypes
/
PyObject.cs
Copy path
File metadata and controls
1577 lines (1371 loc) · 54.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
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Dynamic
;
using
System
.
Linq
;
using
System
.
Linq
.
Expressions
;
using
System
.
Runtime
.
Serialization
;
using
System
.
Threading
;
namespace
Python
.
Runtime
{
/// <summary>
/// Represents a generic Python object. The methods of this class are
/// generally equivalent to the Python "abstract object API". See
/// PY2: https://docs.python.org/2/c-api/object.html
/// PY3: https://docs.python.org/3/c-api/object.html
/// for details.
/// </summary>
[
Serializable
]
[
DebuggerDisplay
(
"{"
+
nameof
(
DebuggerDisplay
)
+
",nq}"
)
]
public
partial
class
PyObject
:
DynamicObject
,
IDisposable
,
ISerializable
{
#if
TRACE_ALLOC
/// <summary>
/// Trace stack for PyObject's construction
/// </summary>
public
StackTrace
Traceback
{
get
;
}
=
new
StackTrace
(
1
)
;
#endif
protected
IntPtr
rawPtr
=
IntPtr
.
Zero
;
internal
readonly
int
run
=
Runtime
.
GetRun
(
)
;
internal
BorrowedReference
obj
=>
new
(
rawPtr
)
;
public
static
PyObject
None
=>
new
(
Runtime
.
PyNone
)
;
internal
BorrowedReference
Reference
=>
new
(
rawPtr
)
;
/// <summary>
/// PyObject Constructor
/// </summary>
/// <remarks>
/// Creates a new PyObject from an IntPtr object reference. Note that
/// the PyObject instance assumes ownership of the object reference
/// and the reference will be DECREFed when the PyObject is garbage
/// collected or explicitly disposed.
/// </remarks>
[
Obsolete
]
internal
PyObject
(
IntPtr
ptr
)
{
if
(
ptr
==
IntPtr
.
Zero
)
throw
new
ArgumentNullException
(
nameof
(
ptr
)
)
;
rawPtr
=
ptr
;
Finalizer
.
Instance
.
ThrottledCollect
(
)
;
}
[
Obsolete
(
"for testing purposes only"
)
]
internal
PyObject
(
IntPtr
ptr
,
bool
skipCollect
)
{
if
(
ptr
==
IntPtr
.
Zero
)
throw
new
ArgumentNullException
(
nameof
(
ptr
)
)
;
rawPtr
=
ptr
;
if
(
!
skipCollect
)
Finalizer
.
Instance
.
ThrottledCollect
(
)
;
}
/// <summary>
/// Creates new <see cref="PyObject"/> pointing to the same object as
/// the <paramref name="reference"/>. Increments refcount, allowing <see cref="PyObject"/>
/// to have ownership over its own reference.
/// </summary>
internal
PyObject
(
BorrowedReference
reference
)
{
if
(
reference
.
IsNull
)
throw
new
ArgumentNullException
(
nameof
(
reference
)
)
;
rawPtr
=
new
NewReference
(
reference
)
.
DangerousMoveToPointer
(
)
;
Finalizer
.
Instance
.
ThrottledCollect
(
)
;
}
internal
PyObject
(
BorrowedReference
reference
,
bool
skipCollect
)
{
if
(
reference
.
IsNull
)
throw
new
ArgumentNullException
(
nameof
(
reference
)
)
;
rawPtr
=
new
NewReference
(
reference
)
.
DangerousMoveToPointer
(
)
;
if
(
!
skipCollect
)
Finalizer
.
Instance
.
ThrottledCollect
(
)
;
}
internal
PyObject
(
in
StolenReference
reference
)
{
if
(
reference
==
null
)
throw
new
ArgumentNullException
(
nameof
(
reference
)
)
;
rawPtr
=
reference
.
DangerousGetAddressOrNull
(
)
;
Finalizer
.
Instance
.
ThrottledCollect
(
)
;
}
/// <summary>
/// Create a new PyObject instance of this object, bumping the reference
/// count.
/// </summary>
public
PyObject
NewReference
(
)
=>
new
(
this
)
;
// Ensure that encapsulated Python object is decref'ed appropriately
// when the managed wrapper is garbage-collected.
~
PyObject
(
)
{
if
(
!
IsDisposed
)
{
#if
TRACE_ALLOC
CheckRun
(
)
;
#endif
Interlocked
.
Increment
(
ref
Runtime
.
_collected
)
;
Finalizer
.
Instance
.
AddFinalizedObject
(
ref
rawPtr
,
run
#if
TRACE_ALLOC
,
Traceback
#endif
)
;
}
Dispose
(
false
)
;
}
/// <summary>
/// Gets the native handle of the underlying Python object. This
/// value is generally for internal use by the PythonNet runtime.
/// </summary>
[
Obsolete
]
public
IntPtr
Handle
{
get
{
return
rawPtr
;
}
}
/// <summary>
/// Gets raw Python proxy for this object (bypasses all conversions,
/// except <c>null</c> <==> <c>None</c>)
/// </summary>
/// <remarks>
/// Given an arbitrary managed object, return a Python instance that
/// reflects the managed object.
/// </remarks>
public
static
PyObject
FromManagedObject
(
object
ob
)
{
// Special case: if ob is null, we return None.
if
(
ob
==
null
)
{
return
new
PyObject
(
Runtime
.
PyNone
)
;
}
return
CLRObject
.
GetReference
(
ob
)
.
MoveToPyObject
(
)
;
}
/// <summary>
/// Creates new <see cref="PyObject"/> from a nullable reference.
/// When <paramref name="reference"/> is <c>null</c>, <c>null</c> is returned.
/// </summary>
internal
static
PyObject
?
FromNullableReference
(
BorrowedReference
reference
)
=>
reference
.
IsNull
?
null
:
new
PyObject
(
reference
)
;
/// <summary>
/// AsManagedObject Method
/// </summary>
/// <remarks>
/// Return a managed object of the given type, based on the
/// value of the Python object.
/// </remarks>
public
object
?
AsManagedObject
(
Type
t
)
{
if
(
!
Converter
.
ToManaged
(
obj
,
t
,
out
var
result
,
true
)
)
{
throw
new
InvalidCastException
(
"cannot convert object to target type"
,
PythonException
.
FetchCurrentOrNull
(
out
_
)
)
;
}
return
result
;
}
/// <summary>
/// Return a managed object of the given type, based on the
/// value of the Python object.
/// </summary>
public
T
As
<
T
>
(
)
=>
(
T
)
this
.
AsManagedObject
(
typeof
(
T
)
)
!
;
internal
bool
IsDisposed
=>
rawPtr
==
IntPtr
.
Zero
;
void
CheckDisposed
(
)
{
if
(
IsDisposed
)
throw
new
ObjectDisposedException
(
nameof
(
PyObject
)
)
;
}
protected
virtual
void
Dispose
(
bool
disposing
)
{
if
(
IsDisposed
)
{
return
;
}
if
(
Runtime
.
Py_IsInitialized
(
)
==
0
&&
Runtime
.
_Py_IsFinalizing
(
)
!=
true
)
{
throw
new
InvalidOperationException
(
"Python runtime must be initialized"
)
;
}
nint
refcount
=
Runtime
.
Refcount
(
this
.
obj
)
;
Debug
.
Assert
(
refcount
>
0
,
"Object refcount is 0 or less"
)
;
if
(
refcount
==
1
)
{
Runtime
.
PyErr_Fetch
(
out
var
errType
,
out
var
errVal
,
out
var
traceback
)
;
try
{
Runtime
.
XDecref
(
StolenReference
.
Take
(
ref
rawPtr
)
)
;
Runtime
.
CheckExceptionOccurred
(
)
;
}
finally
{
// Python requires finalizers to preserve exception:
// https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
Runtime
.
PyErr_Restore
(
errType
.
StealNullable
(
)
,
errVal
.
StealNullable
(
)
,
traceback
.
StealNullable
(
)
)
;
}
}
else
{
Runtime
.
XDecref
(
StolenReference
.
Take
(
ref
rawPtr
)
)
;
}
this
.
rawPtr
=
IntPtr
.
Zero
;
}
/// <summary>
/// The Dispose method provides a way to explicitly release the
/// Python object represented by a PyObject instance. It is a good
/// idea to call Dispose on PyObjects that wrap resources that are
/// limited or need strict lifetime control. Otherwise, references
/// to Python objects will not be released until a managed garbage
/// collection occurs.
/// </summary>
public
void
Dispose
(
)
{
GC
.
SuppressFinalize
(
this
)
;
Dispose
(
true
)
;
}
internal
StolenReference
Steal
(
)
{
GC
.
SuppressFinalize
(
this
)
;
return
StolenReference
.
Take
(
ref
this
.
rawPtr
)
;
}
[
Obsolete
(
"Test use only"
)
]
internal
void
Leak
(
)
{
Debug
.
Assert
(
!
IsDisposed
)
;
GC
.
SuppressFinalize
(
this
)
;
rawPtr
=
IntPtr
.
Zero
;
}
internal
IntPtr
DangerousGetAddressOrNull
(
)
=>
rawPtr
;
internal
void
CheckRun
(
)
{
if
(
run
!=
Runtime
.
GetRun
(
)
)
throw
new
RuntimeShutdownException
(
rawPtr
)
;
}
internal
BorrowedReference
GetPythonTypeReference
(
)
=>
Runtime
.
PyObject_TYPE
(
obj
)
;
/// <summary>
/// GetPythonType Method
/// </summary>
/// <remarks>
/// Returns the Python type of the object. This method is equivalent
/// to the Python expression: type(object).
/// </remarks>
public
PyType
GetPythonType
(
)
{
var
tp
=
Runtime
.
PyObject_Type
(
Reference
)
;
return
new
PyType
(
tp
.
StealOrThrow
(
)
,
prevalidated
:
true
)
;
}
/// <summary>
/// TypeCheck Method
/// </summary>
/// <remarks>
/// Returns true if the object o is of type typeOrClass or a subtype
/// of typeOrClass.
/// </remarks>
public
bool
TypeCheck
(
PyType
typeOrClass
)
{
if
(
typeOrClass
==
null
)
throw
new
ArgumentNullException
(
nameof
(
typeOrClass
)
)
;
return
Runtime
.
PyObject_TypeCheck
(
obj
,
typeOrClass
.
obj
)
;
}
internal
PyType
PyType
=>
this
.
GetPythonType
(
)
;
/// <summary>
/// HasAttr Method
/// </summary>
/// <remarks>
/// Returns true if the object has an attribute with the given name.
/// </remarks>
public
bool
HasAttr
(
string
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
return
Runtime
.
PyObject_HasAttrString
(
Reference
,
name
)
!=
0
;
}
/// <summary>
/// HasAttr Method
/// </summary>
/// <remarks>
/// Returns true if the object has an attribute with the given name,
/// where name is a PyObject wrapping a string or unicode object.
/// </remarks>
public
bool
HasAttr
(
PyObject
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
return
Runtime
.
PyObject_HasAttr
(
Reference
,
name
.
Reference
)
!=
0
;
}
/// <summary>
/// GetAttr Method
/// </summary>
/// <remarks>
/// Returns the named attribute of the Python object, or raises a
/// PythonException if the attribute access fails.
/// </remarks>
public
PyObject
GetAttr
(
string
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
using
var
op
=
Runtime
.
PyObject_GetAttrString
(
obj
,
name
)
;
return
new
PyObject
(
op
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// Returns the named attribute of the Python object, or the given
/// default object if the attribute access throws AttributeError.
/// </summary>
/// <remarks>
/// This method ignores any AttrubiteError(s), even ones
/// not raised due to missing requested attribute.
///
/// For example, if attribute getter calls other Python code, and
/// that code happens to cause AttributeError elsewhere, it will be ignored
/// and <paramref name="_default"/> value will be returned instead.
/// </remarks>
/// <param name="name">Name of the attribute.</param>
/// <param name="_default">The object to return on AttributeError.</param>
[
Obsolete
(
"See remarks"
)
]
public
PyObject
GetAttr
(
string
name
,
PyObject
_default
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
using
var
op
=
Runtime
.
PyObject_GetAttrString
(
obj
,
name
)
;
if
(
op
.
IsNull
(
)
)
{
if
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
AttributeError
)
)
{
Runtime
.
PyErr_Clear
(
)
;
return
_default
;
}
else
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
return
new
PyObject
(
op
.
Steal
(
)
)
;
}
/// <summary>
/// GetAttr Method
/// </summary>
/// <remarks>
/// Returns the named attribute of the Python object or raises a
/// PythonException if the attribute access fails. The name argument
/// is a PyObject wrapping a Python string or unicode object.
/// </remarks>
public
PyObject
GetAttr
(
PyObject
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
using
var
op
=
Runtime
.
PyObject_GetAttr
(
obj
,
name
.
obj
)
;
return
new
PyObject
(
op
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// Returns the named attribute of the Python object, or the given
/// default object if the attribute access throws AttributeError.
/// </summary>
/// <remarks>
/// This method ignores any AttrubiteError(s), even ones
/// not raised due to missing requested attribute.
///
/// For example, if attribute getter calls other Python code, and
/// that code happens to cause AttributeError elsewhere, it will be ignored
/// and <paramref name="_default"/> value will be returned instead.
/// </remarks>
/// <param name="name">Name of the attribute. Must be of Python type 'str'.</param>
/// <param name="_default">The object to return on AttributeError.</param>
[
Obsolete
(
"See remarks"
)
]
public
PyObject
GetAttr
(
PyObject
name
,
PyObject
_default
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
using
var
op
=
Runtime
.
PyObject_GetAttr
(
obj
,
name
.
obj
)
;
if
(
op
.
IsNull
(
)
)
{
if
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
AttributeError
)
)
{
Runtime
.
PyErr_Clear
(
)
;
return
_default
;
}
else
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
return
new
PyObject
(
op
.
Steal
(
)
)
;
}
/// <summary>
/// SetAttr Method
/// </summary>
/// <remarks>
/// Set an attribute of the object with the given name and value. This
/// method throws a PythonException if the attribute set fails.
/// </remarks>
public
void
SetAttr
(
string
name
,
PyObject
value
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
int
r
=
Runtime
.
PyObject_SetAttrString
(
obj
,
name
,
value
.
obj
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// SetAttr Method
/// </summary>
/// <remarks>
/// Set an attribute of the object with the given name and value,
/// where the name is a Python string or unicode object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public
void
SetAttr
(
PyObject
name
,
PyObject
value
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
int
r
=
Runtime
.
PyObject_SetAttr
(
obj
,
name
.
obj
,
value
.
obj
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// DelAttr Method
/// </summary>
/// <remarks>
/// Delete the named attribute of the Python object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public
void
DelAttr
(
string
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
int
r
=
Runtime
.
PyObject_DelAttrString
(
obj
,
name
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// DelAttr Method
/// </summary>
/// <remarks>
/// Delete the named attribute of the Python object, where name is a
/// PyObject wrapping a Python string or unicode object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public
void
DelAttr
(
PyObject
name
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
int
r
=
Runtime
.
PyObject_DelAttr
(
obj
,
name
.
obj
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// GetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given object index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public
virtual
PyObject
GetItem
(
PyObject
key
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
using
var
op
=
Runtime
.
PyObject_GetItem
(
obj
,
key
.
obj
)
;
if
(
op
.
IsNull
(
)
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
return
new
PyObject
(
op
.
Steal
(
)
)
;
}
/// <summary>
/// GetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given string index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public
virtual
PyObject
GetItem
(
string
key
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
using
var
pyKey
=
new
PyString
(
key
)
;
return
GetItem
(
pyKey
)
;
}
/// <summary>
/// GetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given numeric index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public
virtual
PyObject
GetItem
(
int
index
)
{
using
var
key
=
new
PyInt
(
index
)
;
return
GetItem
(
key
)
;
}
/// <summary>
/// SetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given object index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public
virtual
void
SetItem
(
PyObject
key
,
PyObject
value
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
int
r
=
Runtime
.
PyObject_SetItem
(
obj
,
key
.
obj
,
value
.
obj
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// SetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given string index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public
virtual
void
SetItem
(
string
key
,
PyObject
value
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
using
var
pyKey
=
new
PyString
(
key
)
;
SetItem
(
pyKey
,
value
)
;
}
/// <summary>
/// SetItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given numeric index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public
virtual
void
SetItem
(
int
index
,
PyObject
value
)
{
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
using
var
pyindex
=
new
PyInt
(
index
)
;
SetItem
(
pyindex
,
value
)
;
}
/// <summary>
/// DelItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given object index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public
virtual
void
DelItem
(
PyObject
key
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
int
r
=
Runtime
.
PyObject_DelItem
(
obj
,
key
.
obj
)
;
if
(
r
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// DelItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given string index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public
virtual
void
DelItem
(
string
key
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
nameof
(
key
)
)
;
using
var
pyKey
=
new
PyString
(
key
)
;
DelItem
(
pyKey
)
;
}
/// <summary>
/// DelItem Method
/// </summary>
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given numeric index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public
virtual
void
DelItem
(
int
index
)
{
using
var
pyindex
=
new
PyInt
(
index
)
;
DelItem
(
pyindex
)
;
}
/// <summary>
/// Returns the length for objects that support the Python sequence
/// protocol.
/// </summary>
public
virtual
long
Length
(
)
{
var
s
=
Runtime
.
PyObject_Size
(
Reference
)
;
if
(
s
<
0
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
return
s
;
}
/// <summary>
/// String Indexer
/// </summary>
/// <remarks>
/// Provides a shorthand for the string versions of the GetItem and
/// SetItem methods.
/// </remarks>
public
virtual
PyObject
this
[
string
key
]
{
get
{
return
GetItem
(
key
)
;
}
set
{
SetItem
(
key
,
value
)
;
}
}
/// <summary>
/// PyObject Indexer
/// </summary>
/// <remarks>
/// Provides a shorthand for the object versions of the GetItem and
/// SetItem methods.
/// </remarks>
public
virtual
PyObject
this
[
PyObject
key
]
{
get
{
return
GetItem
(
key
)
;
}
set
{
SetItem
(
key
,
value
)
;
}
}
/// <summary>
/// Numeric Indexer
/// </summary>
/// <remarks>
/// Provides a shorthand for the numeric versions of the GetItem and
/// SetItem methods.
/// </remarks>
public
virtual
PyObject
this
[
int
index
]
{
get
{
return
GetItem
(
index
)
;
}
set
{
SetItem
(
index
,
value
)
;
}
}
/// <summary>
/// Return a new (Python) iterator for the object. This is equivalent
/// to the Python expression "iter(object)".
/// </summary>
/// <exception cref="PythonException">Thrown if the object can not be iterated.</exception>
public
PyIter
GetIterator
(
)
=>
PyIter
.
GetIter
(
this
)
;
/// <summary>
/// Invoke Method
/// </summary>
/// <remarks>
/// Invoke the callable object with the given arguments, passed as a
/// PyObject[]. A PythonException is raised if the invocation fails.
/// </remarks>
public
PyObject
Invoke
(
params
PyObject
[
]
args
)
{
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
if
(
args
.
Contains
(
null
)
)
throw
new
ArgumentNullException
(
)
;
var
t
=
new
PyTuple
(
args
)
;
using
var
r
=
Runtime
.
PyObject_Call
(
obj
,
t
.
obj
,
null
)
;
t
.
Dispose
(
)
;
return
new
PyObject
(
r
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// Invoke Method
/// </summary>
/// <remarks>
/// Invoke the callable object with the given arguments, passed as a
/// Python tuple. A PythonException is raised if the invocation fails.
/// </remarks>
public
PyObject
Invoke
(
PyTuple
args
)
{
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
using
var
r
=
Runtime
.
PyObject_Call
(
obj
,
args
.
obj
,
null
)
;
return
new
PyObject
(
r
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// Invoke Method
/// </summary>
/// <remarks>
/// Invoke the callable object with the given positional and keyword
/// arguments. A PythonException is raised if the invocation fails.
/// </remarks>
public
PyObject
Invoke
(
PyObject
[
]
args
,
PyDict
?
kw
)
{
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
if
(
args
.
Contains
(
null
)
)
throw
new
ArgumentNullException
(
)
;
using
var
t
=
new
PyTuple
(
args
)
;
using
var
r
=
Runtime
.
PyObject_Call
(
obj
,
t
.
obj
,
kw
is
null
?
null
:
kw
.
obj
)
;
return
new
PyObject
(
r
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// Invoke Method
/// </summary>
/// <remarks>
/// Invoke the callable object with the given positional and keyword
/// arguments. A PythonException is raised if the invocation fails.
/// </remarks>
public
PyObject
Invoke
(
PyTuple
args
,
PyDict
?
kw
)
{
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
using
var
r
=
Runtime
.
PyObject_Call
(
obj
,
args
.
obj
,
kw
is
null
?
null
:
kw
.
obj
)
;
return
new
PyObject
(
r
.
StealOrThrow
(
)
)
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
string
name
,
params
PyObject
[
]
args
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
if
(
args
.
Contains
(
null
)
)
throw
new
ArgumentNullException
(
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
string
name
,
PyTuple
args
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
PyObject
name
,
params
PyObject
[
]
args
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
if
(
args
.
Contains
(
null
)
)
throw
new
ArgumentNullException
(
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
PyObject
name
,
PyTuple
args
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
string
name
,
PyObject
[
]
args
,
PyDict
?
kw
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
if
(
args
.
Contains
(
null
)
)
throw
new
ArgumentNullException
(
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
,
kw
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public
PyObject
InvokeMethod
(
string
name
,
PyTuple
args
,
PyDict
?
kw
)
{
if
(
name
==
null
)
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
if
(
args
==
null
)
throw
new
ArgumentNullException
(
nameof
(
args
)
)
;
PyObject
method
=
GetAttr
(
name
)
;
PyObject
result
=
method
.
Invoke
(
args
,
kw
)
;
method
.
Dispose
(
)
;
return
result
;
}
/// <summary>
/// IsInstance Method
/// </summary>
/// <remarks>
/// Return true if the object is an instance of the given Python type
/// or class. This method always succeeds.
/// </remarks>
public
bool
IsInstance
(
PyObject
typeOrClass
)
{
if
(
typeOrClass
==
null
)
throw
new
ArgumentNullException
(
nameof
(
typeOrClass
)
)
;
int
r
=
Runtime
.
PyObject_IsInstance
(
obj
,
typeOrClass
.
obj
)
;
if
(
r
<
0
)
{
Runtime
.
PyErr_Clear
(
)
;
return
false
;
}
return
r
!=
0
;
}
/// <summary>
/// Return <c>true</c> if the object is identical to or derived from the
/// given Python type or class. This method always succeeds.
/// </summary>
public
bool
IsSubclass
(
PyObject
typeOrClass
)
{
if
(
typeOrClass
==
null
)
throw
new
ArgumentNullException
(
nameof
(
typeOrClass
)
)
;
return
IsSubclass
(
typeOrClass
.
Reference
)
;
}
internal
bool
IsSubclass
(
BorrowedReference
typeOrClass
)
{
if
(
typeOrClass
.
IsNull
)
throw
new
ArgumentNullException
(
nameof
(
typeOrClass
)
)
;
int
r
=
Runtime
.
PyObject_IsSubclass
(
Reference
,
typeOrClass
)
;
if
(
r
<
0
)
{
Runtime
.
PyErr_Clear
(
)
;
return
false
;
}
return
r
!=
0
;
}
/// <summary>
/// IsCallable Method
/// </summary>
/// <remarks>
/// Returns true if the object is a callable object. This method
/// always succeeds.
/// </remarks>
public
bool
IsCallable
(
)
{
return
Runtime
.
PyCallable_Check
(
obj
)
!=
0
;
}
/// <summary>
/// IsIterable Method
/// </summary>
/// <remarks>
/// Returns true if the object is iterable object. This method
/// always succeeds.
/// </remarks>
public
bool
IsIterable
(
)
{
return
Runtime
.
PyObject_IsIterable
(
obj
)
;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL