FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/classbase.cs at master · sdpython/pythonnet · GitHub
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
sdpython
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
classbase.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
512 lines (458 loc) · 16.9 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
classbase.cs
Copy path
File metadata and controls
512 lines (458 loc) · 16.9 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
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Runtime
.
Serialization
;
namespace
Python
.
Runtime
{
/// <summary>
/// Base class for Python types that reflect managed types / classes.
/// Concrete subclasses include ClassObject and DelegateObject. This
/// class provides common attributes and common machinery for doing
/// class initialization (initialization of the class __dict__). The
/// concrete subclasses provide slot implementations appropriate for
/// each variety of reflected type.
/// </summary>
[
Serializable
]
internal
class
ClassBase
:
ManagedType
{
[
NonSerialized
]
internal
List
<
string
>
dotNetMembers
;
internal
Indexer
indexer
;
internal
Dictionary
<
int
,
MethodObject
>
richcompare
;
internal
MaybeType
type
;
internal
ClassBase
(
Type
tp
)
{
dotNetMembers
=
new
List
<
string
>
(
)
;
indexer
=
null
;
type
=
tp
;
}
internal
virtual
bool
CanSubclass
(
)
{
return
!
type
.
Value
.
IsEnum
;
}
public
readonly
static
Dictionary
<
string
,
int
>
CilToPyOpMap
=
new
Dictionary
<
string
,
int
>
{
[
"op_Equality"
]
=
Runtime
.
Py_EQ
,
[
"op_Inequality"
]
=
Runtime
.
Py_NE
,
[
"op_LessThanOrEqual"
]
=
Runtime
.
Py_LE
,
[
"op_GreaterThanOrEqual"
]
=
Runtime
.
Py_GE
,
[
"op_LessThan"
]
=
Runtime
.
Py_LT
,
[
"op_GreaterThan"
]
=
Runtime
.
Py_GT
,
}
;
/// <summary>
/// Default implementation of [] semantics for reflected types.
/// </summary>
public
virtual
IntPtr
type_subscript
(
IntPtr
idx
)
{
Type
[
]
types
=
Runtime
.
PythonArgsToTypeArray
(
idx
)
;
if
(
types
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"type(s) expected"
)
;
}
if
(
!
type
.
Valid
)
{
return
Exceptions
.
RaiseTypeError
(
type
.
DeletedMessage
)
;
}
Type
target
=
GenericUtil
.
GenericForType
(
type
.
Value
,
types
.
Length
)
;
if
(
target
!=
null
)
{
Type
t
=
target
.
MakeGenericType
(
types
)
;
ManagedType
c
=
ClassManager
.
GetClass
(
t
)
;
Runtime
.
XIncref
(
c
.
pyHandle
)
;
return
c
.
pyHandle
;
}
return
Exceptions
.
RaiseTypeError
(
$
"
{
type
.
Value
.
Namespace
}
.
{
type
.
Name
}
does not accept
{
types
.
Length
}
generic parameters"
)
;
}
/// <summary>
/// Standard comparison implementation for instances of reflected types.
/// </summary>
public
static
IntPtr
tp_richcompare
(
IntPtr
ob
,
IntPtr
other
,
int
op
)
{
CLRObject
co1
;
CLRObject
co2
;
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
var
cls
=
(
ClassBase
)
GetManagedObject
(
tp
)
;
// C# operator methods take precedence over IComparable.
// We first check if there's a comparison operator by looking up the richcompare table,
// otherwise fallback to checking if an IComparable interface is handled.
if
(
cls
.
richcompare
.
TryGetValue
(
op
,
out
var
methodObject
)
)
{
// Wrap the `other` argument of a binary comparison operator in a PyTuple.
IntPtr
args
=
Runtime
.
PyTuple_New
(
1
)
;
Runtime
.
XIncref
(
other
)
;
Runtime
.
PyTuple_SetItem
(
args
,
0
,
other
)
;
IntPtr
value
;
try
{
value
=
methodObject
.
Invoke
(
ob
,
args
,
IntPtr
.
Zero
)
;
}
finally
{
Runtime
.
XDecref
(
args
)
;
// Free args pytuple
}
return
value
;
}
switch
(
op
)
{
case
Runtime
.
Py_EQ
:
case
Runtime
.
Py_NE
:
IntPtr
pytrue
=
Runtime
.
PyTrue
;
IntPtr
pyfalse
=
Runtime
.
PyFalse
;
// swap true and false for NE
if
(
op
!=
Runtime
.
Py_EQ
)
{
pytrue
=
Runtime
.
PyFalse
;
pyfalse
=
Runtime
.
PyTrue
;
}
if
(
ob
==
other
)
{
Runtime
.
XIncref
(
pytrue
)
;
return
pytrue
;
}
co1
=
GetManagedObject
(
ob
)
as
CLRObject
;
co2
=
GetManagedObject
(
other
)
as
CLRObject
;
if
(
null
==
co2
)
{
Runtime
.
XIncref
(
pyfalse
)
;
return
pyfalse
;
}
object
o1
=
co1
.
inst
;
object
o2
=
co2
.
inst
;
if
(
Equals
(
o1
,
o2
)
)
{
Runtime
.
XIncref
(
pytrue
)
;
return
pytrue
;
}
Runtime
.
XIncref
(
pyfalse
)
;
return
pyfalse
;
case
Runtime
.
Py_LT
:
case
Runtime
.
Py_LE
:
case
Runtime
.
Py_GT
:
case
Runtime
.
Py_GE
:
co1
=
GetManagedObject
(
ob
)
as
CLRObject
;
co2
=
GetManagedObject
(
other
)
as
CLRObject
;
if
(
co1
==
null
||
co2
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"Cannot get managed object"
)
;
}
var
co1Comp
=
co1
.
inst
as
IComparable
;
if
(
co1Comp
==
null
)
{
Type
co1Type
=
co1
.
GetType
(
)
;
return
Exceptions
.
RaiseTypeError
(
$
"Cannot convert object of type
{
co1Type
}
to IComparable"
)
;
}
try
{
int
cmp
=
co1Comp
.
CompareTo
(
co2
.
inst
)
;
IntPtr
pyCmp
;
if
(
cmp
<
0
)
{
if
(
op
==
Runtime
.
Py_LT
||
op
==
Runtime
.
Py_LE
)
{
pyCmp
=
Runtime
.
PyTrue
;
}
else
{
pyCmp
=
Runtime
.
PyFalse
;
}
}
else
if
(
cmp
==
0
)
{
if
(
op
==
Runtime
.
Py_LE
||
op
==
Runtime
.
Py_GE
)
{
pyCmp
=
Runtime
.
PyTrue
;
}
else
{
pyCmp
=
Runtime
.
PyFalse
;
}
}
else
{
if
(
op
==
Runtime
.
Py_GE
||
op
==
Runtime
.
Py_GT
)
{
pyCmp
=
Runtime
.
PyTrue
;
}
else
{
pyCmp
=
Runtime
.
PyFalse
;
}
}
Runtime
.
XIncref
(
pyCmp
)
;
return
pyCmp
;
}
catch
(
ArgumentException
e
)
{
return
Exceptions
.
RaiseTypeError
(
e
.
Message
)
;
}
default
:
Runtime
.
XIncref
(
Runtime
.
PyNotImplemented
)
;
return
Runtime
.
PyNotImplemented
;
}
}
/// <summary>
/// Standard iteration support for instances of reflected types. This
/// allows natural iteration over objects that either are IEnumerable
/// or themselves support IEnumerator directly.
/// </summary>
public
static
IntPtr
tp_iter
(
IntPtr
ob
)
{
var
co
=
GetManagedObject
(
ob
)
as
CLRObject
;
if
(
co
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
}
var
e
=
co
.
inst
as
IEnumerable
;
IEnumerator
o
;
if
(
e
!=
null
)
{
o
=
e
.
GetEnumerator
(
)
;
}
else
{
o
=
co
.
inst
as
IEnumerator
;
if
(
o
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"iteration over non-sequence"
)
;
}
}
var
elemType
=
typeof
(
object
)
;
var
iterType
=
co
.
inst
.
GetType
(
)
;
foreach
(
var
ifc
in
iterType
.
GetInterfaces
(
)
)
{
if
(
ifc
.
IsGenericType
)
{
var
genTypeDef
=
ifc
.
GetGenericTypeDefinition
(
)
;
if
(
genTypeDef
==
typeof
(
IEnumerable
<
>
)
||
genTypeDef
==
typeof
(
IEnumerator
<
>
)
)
{
elemType
=
ifc
.
GetGenericArguments
(
)
[
0
]
;
break
;
}
}
}
return
new
Iterator
(
o
,
elemType
)
.
pyHandle
;
}
/// <summary>
/// Standard __hash__ implementation for instances of reflected types.
/// </summary>
public
static
IntPtr
tp_hash
(
IntPtr
ob
)
{
var
co
=
GetManagedObject
(
ob
)
as
CLRObject
;
if
(
co
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"unhashable type"
)
;
}
return
new
IntPtr
(
co
.
inst
.
GetHashCode
(
)
)
;
}
/// <summary>
/// Standard __str__ implementation for instances of reflected types.
/// </summary>
public
static
IntPtr
tp_str
(
IntPtr
ob
)
{
var
co
=
GetManagedObject
(
ob
)
as
CLRObject
;
if
(
co
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
}
try
{
return
Runtime
.
PyString_FromString
(
co
.
inst
.
ToString
(
)
)
;
}
catch
(
Exception
e
)
{
if
(
e
.
InnerException
!=
null
)
{
e
=
e
.
InnerException
;
}
Exceptions
.
SetError
(
e
)
;
return
IntPtr
.
Zero
;
}
}
public
static
IntPtr
tp_repr
(
IntPtr
ob
)
{
var
co
=
GetManagedObject
(
ob
)
as
CLRObject
;
if
(
co
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
}
try
{
//if __repr__ is defined, use it
var
instType
=
co
.
inst
.
GetType
(
)
;
System
.
Reflection
.
MethodInfo
methodInfo
=
instType
.
GetMethod
(
"__repr__"
)
;
if
(
methodInfo
!=
null
&&
methodInfo
.
IsPublic
)
{
var
reprString
=
methodInfo
.
Invoke
(
co
.
inst
,
null
)
as
string
;
return
Runtime
.
PyString_FromString
(
reprString
)
;
}
//otherwise use the standard object.__repr__(inst)
IntPtr
args
=
Runtime
.
PyTuple_New
(
1
)
;
Runtime
.
XIncref
(
ob
)
;
Runtime
.
PyTuple_SetItem
(
args
,
0
,
ob
)
;
IntPtr
reprFunc
=
Runtime
.
PyObject_GetAttr
(
Runtime
.
PyBaseObjectType
,
PyIdentifier
.
__repr__
)
;
var
output
=
Runtime
.
PyObject_Call
(
reprFunc
,
args
,
IntPtr
.
Zero
)
;
Runtime
.
XDecref
(
args
)
;
Runtime
.
XDecref
(
reprFunc
)
;
return
output
;
}
catch
(
Exception
e
)
{
if
(
e
.
InnerException
!=
null
)
{
e
=
e
.
InnerException
;
}
Exceptions
.
SetError
(
e
)
;
return
IntPtr
.
Zero
;
}
}
/// <summary>
/// Standard dealloc implementation for instances of reflected types.
/// </summary>
public
static
void
tp_dealloc
(
IntPtr
ob
)
{
ManagedType
self
=
GetManagedObject
(
ob
)
;
tp_clear
(
ob
)
;
Runtime
.
PyObject_GC_UnTrack
(
self
.
pyHandle
)
;
Runtime
.
PyObject_GC_Del
(
self
.
pyHandle
)
;
self
.
FreeGCHandle
(
)
;
}
public
static
int
tp_clear
(
IntPtr
ob
)
{
ManagedType
self
=
GetManagedObject
(
ob
)
;
if
(
!
self
.
IsTypeObject
(
)
)
{
ClearObjectDict
(
ob
)
;
}
self
.
tpHandle
=
IntPtr
.
Zero
;
return
0
;
}
protected
override
void
OnSave
(
InterDomainContext
context
)
{
base
.
OnSave
(
context
)
;
if
(
pyHandle
!=
tpHandle
)
{
IntPtr
dict
=
GetObjectDict
(
pyHandle
)
;
Runtime
.
XIncref
(
dict
)
;
context
.
Storage
.
AddValue
(
"dict"
,
dict
)
;
}
}
protected
override
void
OnLoad
(
InterDomainContext
context
)
{
base
.
OnLoad
(
context
)
;
if
(
pyHandle
!=
tpHandle
)
{
IntPtr
dict
=
context
.
Storage
.
GetValue
<
IntPtr
>
(
"dict"
)
;
SetObjectDict
(
pyHandle
,
dict
)
;
}
gcHandle
=
AllocGCHandle
(
)
;
Marshal
.
WriteIntPtr
(
pyHandle
,
TypeOffset
.
magic
(
)
,
(
IntPtr
)
gcHandle
)
;
}
/// <summary>
/// Implements __getitem__ for reflected classes and value types.
/// </summary>
public
static
IntPtr
mp_subscript
(
IntPtr
ob
,
IntPtr
idx
)
{
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
var
cls
=
(
ClassBase
)
GetManagedObject
(
tp
)
;
if
(
cls
.
indexer
==
null
||
!
cls
.
indexer
.
CanGet
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"unindexable object"
)
;
return
IntPtr
.
Zero
;
}
// Arg may be a tuple in the case of an indexer with multiple
// parameters. If so, use it directly, else make a new tuple
// with the index arg (method binders expect arg tuples).
IntPtr
args
=
idx
;
var
free
=
false
;
if
(
!
Runtime
.
PyTuple_Check
(
idx
)
)
{
args
=
Runtime
.
PyTuple_New
(
1
)
;
Runtime
.
XIncref
(
idx
)
;
Runtime
.
PyTuple_SetItem
(
args
,
0
,
idx
)
;
free
=
true
;
}
IntPtr
value
;
try
{
value
=
cls
.
indexer
.
GetItem
(
ob
,
args
)
;
}
finally
{
if
(
free
)
{
Runtime
.
XDecref
(
args
)
;
}
}
return
value
;
}
/// <summary>
/// Implements __setitem__ for reflected classes and value types.
/// </summary>
public
static
int
mp_ass_subscript
(
IntPtr
ob
,
IntPtr
idx
,
IntPtr
v
)
{
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
var
cls
=
(
ClassBase
)
GetManagedObject
(
tp
)
;
if
(
cls
.
indexer
==
null
||
!
cls
.
indexer
.
CanSet
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"object doesn't support item assignment"
)
;
return
-
1
;
}
// Arg may be a tuple in the case of an indexer with multiple
// parameters. If so, use it directly, else make a new tuple
// with the index arg (method binders expect arg tuples).
IntPtr
args
=
idx
;
var
free
=
false
;
if
(
!
Runtime
.
PyTuple_Check
(
idx
)
)
{
args
=
Runtime
.
PyTuple_New
(
1
)
;
Runtime
.
XIncref
(
idx
)
;
Runtime
.
PyTuple_SetItem
(
args
,
0
,
idx
)
;
free
=
true
;
}
// Get the args passed in.
var
i
=
Runtime
.
PyTuple_Size
(
args
)
;
IntPtr
defaultArgs
=
cls
.
indexer
.
GetDefaultArgs
(
args
)
;
var
numOfDefaultArgs
=
Runtime
.
PyTuple_Size
(
defaultArgs
)
;
var
temp
=
i
+
numOfDefaultArgs
;
IntPtr
real
=
Runtime
.
PyTuple_New
(
temp
+
1
)
;
for
(
var
n
=
0
;
n
<
i
;
n
++
)
{
IntPtr
item
=
Runtime
.
PyTuple_GetItem
(
args
,
n
)
;
Runtime
.
XIncref
(
item
)
;
Runtime
.
PyTuple_SetItem
(
real
,
n
,
item
)
;
}
// Add Default Args if needed
for
(
var
n
=
0
;
n
<
numOfDefaultArgs
;
n
++
)
{
IntPtr
item
=
Runtime
.
PyTuple_GetItem
(
defaultArgs
,
n
)
;
Runtime
.
XIncref
(
item
)
;
Runtime
.
PyTuple_SetItem
(
real
,
n
+
i
,
item
)
;
}
// no longer need defaultArgs
Runtime
.
XDecref
(
defaultArgs
)
;
i
=
temp
;
// Add value to argument list
Runtime
.
XIncref
(
v
)
;
Runtime
.
PyTuple_SetItem
(
real
,
i
,
v
)
;
try
{
cls
.
indexer
.
SetItem
(
ob
,
real
)
;
}
finally
{
Runtime
.
XDecref
(
real
)
;
if
(
free
)
{
Runtime
.
XDecref
(
args
)
;
}
}
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
return
-
1
;
}
return
0
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL