FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Types/GenericType.cs at v3.0.1 · 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
/
Types
/
GenericType.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
86 lines (75 loc) · 3.24 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Types
/
GenericType.cs
Copy path
File metadata and controls
86 lines (75 loc) · 3.24 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
using
System
;
using
System
.
Linq
;
namespace
Python
.
Runtime
{
/// <summary>
/// Implements reflected generic types. Note that the Python behavior
/// is the same for both generic type definitions and constructed open
/// generic types. Both are essentially factories for creating closed
/// types based on the required generic type parameters.
/// </summary>
[
Serializable
]
internal
class
GenericType
:
ClassBase
{
internal
GenericType
(
Type
tp
)
:
base
(
tp
)
{
}
/// <summary>
/// Implements __new__ for reflected generic types.
/// </summary>
public
static
NewReference
tp_new
(
BorrowedReference
tp
,
BorrowedReference
args
,
BorrowedReference
kw
)
{
var
self
=
(
GenericType
)
GetManagedObject
(
tp
)
!
;
if
(
!
self
.
type
.
Valid
)
{
return
Exceptions
.
RaiseTypeError
(
self
.
type
.
DeletedMessage
)
;
}
var
type
=
self
.
type
.
Value
;
if
(
type
.
IsInterface
&&
!
type
.
IsConstructedGenericType
)
{
var
nargs
=
Runtime
.
PyTuple_Size
(
args
)
;
if
(
nargs
==
1
)
{
var
instance
=
Runtime
.
PyTuple_GetItem
(
args
,
0
)
;
return
AsGenericInterface
(
instance
,
type
)
;
}
}
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"cannot instantiate an open generic type"
)
;
return
default
;
}
static
NewReference
AsGenericInterface
(
BorrowedReference
instance
,
Type
targetType
)
{
if
(
GetManagedObject
(
instance
)
is
not
CLRObject
obj
)
{
return
Exceptions
.
RaiseTypeError
(
"only .NET objects can be cast to .NET interfaces"
)
;
}
Type
[
]
supportedInterfaces
=
obj
.
inst
.
GetType
(
)
.
GetInterfaces
(
)
;
Type
[
]
constructedInterfaces
=
supportedInterfaces
.
Where
(
i
=>
i
.
IsConstructedGenericType
&&
i
.
GetGenericTypeDefinition
(
)
==
targetType
)
.
ToArray
(
)
;
if
(
constructedInterfaces
.
Length
==
1
)
{
BorrowedReference
pythonic
=
ClassManager
.
GetClass
(
constructedInterfaces
[
0
]
)
;
using
var
args
=
Runtime
.
PyTuple_New
(
1
)
;
Runtime
.
PyTuple_SetItem
(
args
.
Borrow
(
)
,
0
,
instance
)
;
return
Runtime
.
PyObject_CallObject
(
pythonic
,
args
.
Borrow
(
)
)
;
}
if
(
constructedInterfaces
.
Length
>
1
)
{
string
interfaces
=
string
.
Join
(
", "
,
constructedInterfaces
.
Select
(
TypeManager
.
GetPythonTypeName
)
)
;
return
Exceptions
.
RaiseTypeError
(
"Ambiguous cast to .NET interface. "
+
$
"Object implements:
{
interfaces
}
"
)
;
}
return
Exceptions
.
RaiseTypeError
(
"object does not implement "
+
TypeManager
.
GetPythonTypeName
(
targetType
)
)
;
}
/// <summary>
/// Implements __call__ for reflected generic types.
/// </summary>
public
static
NewReference
tp_call
(
BorrowedReference
ob
,
BorrowedReference
args
,
BorrowedReference
kw
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"object is not callable"
)
;
return
default
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL