FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/classobject.cs at refactor-qc-pythonnet5 · QuantConnect/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
QuantConnect
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
31
Star
29
Code
Issues
5
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
classobject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
167 lines (146 loc) · 5.78 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
classobject.cs
Copy path
File metadata and controls
167 lines (146 loc) · 5.78 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
using
System
.
Linq
;
using
System
;
using
System
.
Reflection
;
namespace
Python
.
Runtime
{
/// <summary>
/// Managed class that provides the implementation for reflected types.
/// Managed classes and value types are represented in Python by actual
/// Python type objects. Each of those type objects is associated with
/// an instance of ClassObject, which provides its implementation.
/// </summary>
[
Serializable
]
internal
class
ClassObject
:
ClassBase
{
internal
ConstructorBinder
binder
;
internal
int
NumCtors
=
0
;
internal
ClassObject
(
Type
tp
)
:
base
(
tp
)
{
var
_ctors
=
type
.
Value
.
GetConstructors
(
)
;
NumCtors
=
_ctors
.
Length
;
binder
=
new
ConstructorBinder
(
type
.
Value
)
;
foreach
(
ConstructorInfo
t
in
_ctors
)
{
binder
.
AddMethod
(
t
)
;
}
}
/// <summary>
/// Helper to get docstring from reflected constructor info.
/// </summary>
internal
NewReference
GetDocString
(
)
{
var
methods
=
binder
.
GetMethods
(
)
;
var
str
=
""
;
foreach
(
var
t
in
methods
)
{
if
(
str
.
Length
>
0
)
{
str
+=
Environment
.
NewLine
;
}
str
+=
t
.
MethodBase
.
ToString
(
)
;
}
return
NewReference
.
DangerousFromPointer
(
Runtime
.
PyString_FromString
(
str
)
)
;
}
/// <summary>
/// Implements __new__ for reflected classes and value types.
/// </summary>
public
static
IntPtr
tp_new
(
IntPtr
tp
,
IntPtr
args
,
IntPtr
kw
)
{
var
self
=
GetManagedObject
(
tp
)
as
ClassObject
;
// Sanity check: this ensures a graceful error if someone does
// something intentially wrong like use the managed metatype for
// a class that is not really derived from a managed class.
if
(
self
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
}
if
(
!
self
.
type
.
Valid
)
{
return
Exceptions
.
RaiseTypeError
(
self
.
type
.
DeletedMessage
)
;
}
Type
type
=
self
.
type
.
Value
;
// Primitive types do not have constructors, but they look like
// they do from Python. If the ClassObject represents one of the
// convertible primitive types, just convert the arg directly.
if
(
type
.
IsPrimitive
||
type
==
typeof
(
string
)
)
{
if
(
Runtime
.
PyTuple_Size
(
args
)
!=
1
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"no constructors match given arguments"
)
;
return
IntPtr
.
Zero
;
}
IntPtr
op
=
Runtime
.
PyTuple_GetItem
(
args
,
0
)
;
object
result
;
if
(
!
Converter
.
ToManaged
(
op
,
type
,
out
result
,
true
)
)
{
return
IntPtr
.
Zero
;
}
return
CLRObject
.
GetInstHandle
(
result
,
tp
)
;
}
if
(
type
.
IsAbstract
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"cannot instantiate abstract class"
)
;
return
IntPtr
.
Zero
;
}
if
(
type
.
IsEnum
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"cannot instantiate enumeration"
)
;
return
IntPtr
.
Zero
;
}
object
obj
=
self
.
binder
.
InvokeRaw
(
IntPtr
.
Zero
,
args
,
kw
)
;
if
(
obj
==
null
)
{
return
IntPtr
.
Zero
;
}
return
CLRObject
.
GetInstHandle
(
obj
,
tp
)
;
}
/// <summary>
/// Implementation of [] semantics for reflected types. This exists
/// both to implement the Array[int] syntax for creating arrays and
/// to support generic name overload resolution using [].
/// </summary>
public
override
IntPtr
type_subscript
(
IntPtr
idx
)
{
if
(
!
type
.
Valid
)
{
return
Exceptions
.
RaiseTypeError
(
type
.
DeletedMessage
)
;
}
// If this type is the Array type, the [<type>] means we need to
// construct and return an array type of the given element type.
if
(
type
.
Value
==
typeof
(
Array
)
)
{
if
(
Runtime
.
PyTuple_Check
(
idx
)
)
{
return
Exceptions
.
RaiseTypeError
(
"type expected"
)
;
}
var
c
=
GetManagedObject
(
idx
)
as
ClassBase
;
Type
t
=
c
!=
null
?
c
.
type
.
Value
:
Converter
.
GetTypeByAlias
(
idx
)
;
if
(
t
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"type expected"
)
;
}
Type
a
=
t
.
MakeArrayType
(
)
;
ClassBase
o
=
ClassManager
.
GetClass
(
a
)
;
Runtime
.
XIncref
(
o
.
pyHandle
)
;
return
o
.
pyHandle
;
}
// If there are generics in our namespace with the same base name
// as the current type, then [<type>] means the caller wants to
// bind the generic type matching the given type parameters.
Type
[
]
types
=
Runtime
.
PythonArgsToTypeArray
(
idx
)
;
if
(
types
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"type(s) expected"
)
;
}
Type
gtype
=
AssemblyManager
.
LookupTypes
(
$
"
{
type
.
Value
.
FullName
}
`
{
types
.
Length
}
"
)
.
FirstOrDefault
(
)
;
if
(
gtype
!=
null
)
{
var
g
=
ClassManager
.
GetClass
(
gtype
)
as
GenericType
;
return
g
.
type_subscript
(
idx
)
;
//Runtime.XIncref(g.pyHandle);
//return g.pyHandle;
}
return
Exceptions
.
RaiseTypeError
(
"unsubscriptable object"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL