FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/arrayobject.cs at update/NetCore21 · losttech/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
losttech
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
arrayobject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
250 lines (205 loc) · 7.12 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
arrayobject.cs
Copy path
File metadata and controls
250 lines (205 loc) · 7.12 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
using
System
;
using
System
.
Collections
;
namespace
Python
.
Runtime
{
/// <summary>
/// Implements a Python type for managed arrays. This type is essentially
/// the same as a ClassObject, except that it provides sequence semantics
/// to support natural array usage (indexing) from Python.
/// </summary>
[
Serializable
]
internal
class
ArrayObject
:
ClassBase
{
internal
ArrayObject
(
Type
tp
)
:
base
(
tp
)
{
}
internal
override
bool
CanSubclass
(
)
{
return
false
;
}
public
static
IntPtr
tp_new
(
IntPtr
tp
,
IntPtr
args
,
IntPtr
kw
)
{
var
self
=
GetManagedObject
(
tp
)
as
ArrayObject
;
if
(
Runtime
.
PyTuple_Size
(
args
)
!=
1
)
{
return
Exceptions
.
RaiseTypeError
(
"array expects 1 argument"
)
;
}
IntPtr
op
=
Runtime
.
PyTuple_GetItem
(
args
,
0
)
;
object
result
;
if
(
!
Converter
.
ToManaged
(
op
,
self
.
type
,
out
result
,
true
)
)
{
return
IntPtr
.
Zero
;
}
return
CLRObject
.
GetInstHandle
(
result
,
tp
)
;
}
/// <summary>
/// Implements __getitem__ for array types.
/// </summary>
public
new
static
IntPtr
mp_subscript
(
IntPtr
ob
,
IntPtr
idx
)
{
var
obj
=
(
CLRObject
)
GetManagedObject
(
ob
)
;
var
arrObj
=
(
ArrayObject
)
GetManagedObjectType
(
ob
)
;
var
items
=
obj
.
inst
as
Array
;
Type
itemType
=
arrObj
.
type
.
GetElementType
(
)
;
int
rank
=
items
.
Rank
;
int
index
;
object
value
;
// Note that CLR 1.0 only supports int indexes - methods to
// support long indices were introduced in 1.1. We could
// support long indices automatically, but given that long
// indices are not backward compatible and a relative edge
// case, we won't bother for now.
// Single-dimensional arrays are the most common case and are
// cheaper to deal with than multi-dimensional, so check first.
if
(
rank
==
1
)
{
index
=
Runtime
.
PyInt_AsLong
(
idx
)
;
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid index value"
)
;
}
if
(
index
<
0
)
{
index
=
items
.
Length
+
index
;
}
try
{
value
=
items
.
GetValue
(
index
)
;
}
catch
(
IndexOutOfRangeException
)
{
Exceptions
.
SetError
(
Exceptions
.
IndexError
,
"array index out of range"
)
;
return
IntPtr
.
Zero
;
}
return
Converter
.
ToPython
(
value
,
itemType
)
;
}
// Multi-dimensional arrays can be indexed a la: list[1, 2, 3].
if
(
!
Runtime
.
PyTuple_Check
(
idx
)
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"invalid index value"
)
;
return
IntPtr
.
Zero
;
}
var
count
=
Runtime
.
PyTuple_Size
(
idx
)
;
var
args
=
new
int
[
count
]
;
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
IntPtr
op
=
Runtime
.
PyTuple_GetItem
(
idx
,
i
)
;
index
=
Runtime
.
PyInt_AsLong
(
op
)
;
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid index value"
)
;
}
if
(
index
<
0
)
{
index
=
items
.
GetLength
(
i
)
+
index
;
}
args
.
SetValue
(
index
,
i
)
;
}
try
{
value
=
items
.
GetValue
(
args
)
;
}
catch
(
IndexOutOfRangeException
)
{
Exceptions
.
SetError
(
Exceptions
.
IndexError
,
"array index out of range"
)
;
return
IntPtr
.
Zero
;
}
return
Converter
.
ToPython
(
value
,
itemType
)
;
}
/// <summary>
/// Implements __setitem__ for array types.
/// </summary>
public
static
new
int
mp_ass_subscript
(
IntPtr
ob
,
IntPtr
idx
,
IntPtr
v
)
{
var
obj
=
(
CLRObject
)
GetManagedObject
(
ob
)
;
var
items
=
obj
.
inst
as
Array
;
Type
itemType
=
obj
.
inst
.
GetType
(
)
.
GetElementType
(
)
;
int
rank
=
items
.
Rank
;
int
index
;
object
value
;
if
(
items
.
IsReadOnly
)
{
Exceptions
.
RaiseTypeError
(
"array is read-only"
)
;
return
-
1
;
}
if
(
!
Converter
.
ToManaged
(
v
,
itemType
,
out
value
,
true
)
)
{
return
-
1
;
}
if
(
rank
==
1
)
{
index
=
Runtime
.
PyInt_AsLong
(
idx
)
;
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
Exceptions
.
RaiseTypeError
(
"invalid index value"
)
;
return
-
1
;
}
if
(
index
<
0
)
{
index
=
items
.
Length
+
index
;
}
try
{
items
.
SetValue
(
value
,
index
)
;
}
catch
(
IndexOutOfRangeException
)
{
Exceptions
.
SetError
(
Exceptions
.
IndexError
,
"array index out of range"
)
;
return
-
1
;
}
return
0
;
}
if
(
!
Runtime
.
PyTuple_Check
(
idx
)
)
{
Exceptions
.
RaiseTypeError
(
"invalid index value"
)
;
return
-
1
;
}
var
count
=
Runtime
.
PyTuple_Size
(
idx
)
;
var
args
=
new
int
[
count
]
;
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
IntPtr
op
=
Runtime
.
PyTuple_GetItem
(
idx
,
i
)
;
index
=
Runtime
.
PyInt_AsLong
(
op
)
;
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
Exceptions
.
RaiseTypeError
(
"invalid index value"
)
;
return
-
1
;
}
if
(
index
<
0
)
{
index
=
items
.
GetLength
(
i
)
+
index
;
}
args
.
SetValue
(
index
,
i
)
;
}
try
{
items
.
SetValue
(
value
,
args
)
;
}
catch
(
IndexOutOfRangeException
)
{
Exceptions
.
SetError
(
Exceptions
.
IndexError
,
"array index out of range"
)
;
return
-
1
;
}
return
0
;
}
/// <summary>
/// Implements __contains__ for array types.
/// </summary>
public
static
int
sq_contains
(
IntPtr
ob
,
IntPtr
v
)
{
var
obj
=
(
CLRObject
)
GetManagedObject
(
ob
)
;
Type
itemType
=
obj
.
inst
.
GetType
(
)
.
GetElementType
(
)
;
var
items
=
obj
.
inst
as
IList
;
object
value
;
if
(
!
Converter
.
ToManaged
(
v
,
itemType
,
out
value
,
false
)
)
{
return
0
;
}
if
(
items
.
Contains
(
value
)
)
{
return
1
;
}
return
0
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL