FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/pythonnet/src/runtime/generictype.cs at develop · davidanthoff/pythonnet · GitHub
davidanthoff
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
pythonnet
/
src
/
runtime
/
generictype.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
100 lines (81 loc) · 4.02 KB
Breadcrumbs
pythonnet
/
pythonnet
/
src
/
runtime
/
generictype.cs
Copy path
File metadata and controls
100 lines (81 loc) · 4.02 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
// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using
System
;
using
System
.
Reflection
;
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>
internal
class
GenericType
:
ClassBase
{
internal
GenericType
(
Type
tp
)
:
base
(
tp
)
{
}
//====================================================================
// Implements __new__ for reflected generic types.
//====================================================================
public
static
IntPtr
tp_new
(
IntPtr
tp
,
IntPtr
args
,
IntPtr
kw
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"cannot instantiate an open generic type"
)
;
return
IntPtr
.
Zero
;
}
//====================================================================
// Implements __call__ for reflected generic types.
//====================================================================
public
static
IntPtr
tp_call
(
IntPtr
ob
,
IntPtr
args
,
IntPtr
kw
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"object is not callable"
)
;
return
IntPtr
.
Zero
;
}
//====================================================================
// Implements subscript syntax for reflected generic types. A closed
// type is created by binding the generic type via subscript syntax:
// inst = List[str]()
//====================================================================
public
override
IntPtr
type_subscript
(
IntPtr
idx
)
{
Type
[
]
types
=
Runtime
.
PythonArgsToTypeArray
(
idx
)
;
if
(
types
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"type(s) expected"
)
;
}
if
(
!
this
.
type
.
IsGenericTypeDefinition
)
{
return
Exceptions
.
RaiseTypeError
(
"type is not a generic type definition"
)
;
}
// This is a little tricky, because an instance of GenericType
// may represent either a specific generic type, or act as an
// alias for one or more generic types with the same base name.
if
(
this
.
type
.
ContainsGenericParameters
)
{
Type
[
]
args
=
this
.
type
.
GetGenericArguments
(
)
;
Type
target
=
null
;
if
(
args
.
Length
==
types
.
Length
)
{
target
=
this
.
type
;
}
else
{
foreach
(
Type
t
in
GenericUtil
.
GenericsForType
(
this
.
type
)
)
{
if
(
t
.
GetGenericArguments
(
)
.
Length
==
types
.
Length
)
{
target
=
t
;
break
;
}
}
}
if
(
target
!=
null
)
{
Type
t
=
target
.
MakeGenericType
(
types
)
;
ManagedType
c
=
(
ManagedType
)
ClassManager
.
GetClass
(
t
)
;
Runtime
.
Incref
(
c
.
pyHandle
)
;
return
c
.
pyHandle
;
}
return
Exceptions
.
RaiseTypeError
(
"no type matches params"
)
;
}
return
Exceptions
.
RaiseTypeError
(
"unsubscriptable object"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL