FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/pytype.cs at master · saroad2/pythonnet · GitHub
saroad2
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
pytype.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
142 lines (115 loc) · 4.71 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
pytype.cs
Copy path
File metadata and controls
142 lines (115 loc) · 4.71 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
#nullable enable
using
System
;
using
System
.
Runtime
.
InteropServices
;
using
Python
.
Runtime
.
Native
;
namespace
Python
.
Runtime
{
[
Serializable
]
public
class
PyType
:
PyObject
{
/// <summary>Creates heap type object from the <paramref name="spec"/>.</summary>
public
PyType
(
TypeSpec
spec
,
PyTuple
?
bases
=
null
)
:
base
(
FromSpec
(
spec
,
bases
)
)
{
}
/// <summary>Wraps an existing type object.</summary>
public
PyType
(
PyObject
o
)
:
base
(
FromObject
(
o
)
)
{
}
internal
PyType
(
BorrowedReference
reference
)
:
base
(
reference
)
{
if
(
!
Runtime
.
PyType_Check
(
this
.
Handle
)
)
throw
new
ArgumentException
(
"object is not a type"
)
;
}
internal
PyType
(
StolenReference
reference
)
:
base
(
EnsureIsType
(
in
reference
)
)
{
}
internal
new
static
PyType
?
FromNullableReference
(
BorrowedReference
reference
)
=>
reference
==
null
?
null
:
new
PyType
(
new
NewReference
(
reference
)
.
Steal
(
)
)
;
internal
static
PyType
FromReference
(
BorrowedReference
reference
)
=>
FromNullableReference
(
reference
)
??
throw
new
ArgumentNullException
(
nameof
(
reference
)
)
;
public
string
Name
{
get
{
var
namePtr
=
new
StrPtr
{
RawPointer
=
Marshal
.
ReadIntPtr
(
Handle
,
TypeOffset
.
tp_name
)
,
}
;
return
namePtr
.
ToString
(
System
.
Text
.
Encoding
.
UTF8
)
!
;
}
}
/// <summary>Returns <c>true</c> when type is fully initialized</summary>
public
bool
IsReady
=>
Flags
.
HasFlag
(
TypeFlags
.
Ready
)
;
internal
TypeFlags
Flags
{
get
=>
(
TypeFlags
)
Util
.
ReadCLong
(
Handle
,
TypeOffset
.
tp_flags
)
;
set
=>
Util
.
WriteCLong
(
Handle
,
TypeOffset
.
tp_flags
,
(
long
)
value
)
;
}
/// <summary>Checks if specified object is a Python type.</summary>
public
static
bool
IsType
(
PyObject
value
)
{
if
(
value
is
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
return
Runtime
.
PyType_Check
(
value
.
obj
)
;
}
/// <summary>
/// Gets <see cref="PyType"/>, which represents the specified CLR type.
/// Must be called after the CLR type was mapped to its Python type.
/// </summary>
internal
static
PyType
Get
(
Type
clrType
)
{
if
(
clrType
==
null
)
{
throw
new
ArgumentNullException
(
nameof
(
clrType
)
)
;
}
ClassBase
pyClass
=
ClassManager
.
GetClass
(
clrType
)
;
return
new
PyType
(
pyClass
.
ObjectReference
)
;
}
internal
BorrowedReference
BaseReference
{
get
{
return
new
(
Marshal
.
ReadIntPtr
(
Handle
,
TypeOffset
.
tp_base
)
)
;
}
set
{
var
old
=
BaseReference
.
DangerousGetAddressOrNull
(
)
;
IntPtr
@new
=
value
.
DangerousGetAddress
(
)
;
Runtime
.
XIncref
(
@new
)
;
Marshal
.
WriteIntPtr
(
Handle
,
TypeOffset
.
tp_base
,
@new
)
;
Runtime
.
XDecref
(
old
)
;
}
}
internal
IntPtr
GetSlot
(
TypeSlotID
slot
)
{
IntPtr
result
=
Runtime
.
PyType_GetSlot
(
this
.
Reference
,
slot
)
;
return
Exceptions
.
ErrorCheckIfNull
(
result
)
;
}
private
static
IntPtr
EnsureIsType
(
in
StolenReference
reference
)
{
IntPtr
address
=
reference
.
DangerousGetAddressOrNull
(
)
;
if
(
address
==
IntPtr
.
Zero
)
throw
new
ArgumentNullException
(
nameof
(
reference
)
)
;
return
EnsureIsType
(
address
)
;
}
private
static
IntPtr
EnsureIsType
(
IntPtr
ob
)
=>
Runtime
.
PyType_Check
(
ob
)
?
ob
:
throw
new
ArgumentException
(
"object is not a type"
)
;
private
static
BorrowedReference
FromObject
(
PyObject
o
)
{
if
(
o
is
null
)
throw
new
ArgumentNullException
(
nameof
(
o
)
)
;
if
(
!
IsType
(
o
)
)
throw
new
ArgumentException
(
"object is not a type"
)
;
return
o
.
Reference
;
}
private
static
IntPtr
FromSpec
(
TypeSpec
spec
,
PyTuple
?
bases
=
null
)
{
if
(
spec
is
null
)
throw
new
ArgumentNullException
(
nameof
(
spec
)
)
;
if
(
(
spec
.
Flags
&
TypeFlags
.
HeapType
)
==
0
)
throw
new
NotSupportedException
(
"Only heap types are supported"
)
;
var
nativeSpec
=
new
NativeTypeSpec
(
spec
)
;
var
basesRef
=
bases
is
null
?
default
:
bases
.
Reference
;
var
result
=
Runtime
.
PyType_FromSpecWithBases
(
in
nativeSpec
,
basesRef
)
;
PythonException
.
ThrowIfIsNull
(
result
)
;
nativeSpec
.
Dispose
(
)
;
return
result
.
DangerousMoveToPointer
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL