FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/pythonnet/src/runtime/constructorbinder.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
/
constructorbinder.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (85 loc) · 4.56 KB
Breadcrumbs
pythonnet
/
pythonnet
/
src
/
runtime
/
constructorbinder.cs
Copy path
File metadata and controls
96 lines (85 loc) · 4.56 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
// ==========================================================================
// 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
{
//========================================================================
// A ConstructorBinder encapsulates information about one or more managed
// constructors, and is responsible for selecting the right constructor
// given a set of Python arguments. This is slightly different than the
// standard MethodBinder because of a difference in invoking constructors
// using reflection (which is seems to be a CLR bug).
//========================================================================
internal
class
ConstructorBinder
:
MethodBinder
{
internal
ConstructorBinder
(
)
:
base
(
)
{
}
//====================================================================
// Constructors get invoked when an instance of a wrapped managed
// class or a subclass of a managed class is created. This differs
// from the MethodBinder implementation in that we return the raw
// result of the constructor rather than wrapping it as a Python
// object - the reason is that only the caller knows the correct
// Python type to use when wrapping the result (may be a subclass).
//====================================================================
internal
object
InvokeRaw
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
)
{
return
this
.
InvokeRaw
(
inst
,
args
,
kw
,
null
)
;
}
/// <summary>
/// Allows ctor selection to be limited to a single attempt at a
/// match by providing the MethodBase to use instead of searching
/// the entire MethodBinder.list (generic ArrayList)
/// </summary>
/// <param name="inst"> (possibly null) instance </param>
/// <param name="args"> PyObject* to the arg tuple </param>
/// <param name="kw"> PyObject* to the keyword args dict </param>
/// <param name="info"> The sole ContructorInfo to use or null </param>
/// <returns> The result of the constructor call with converted params </returns>
/// <remarks>
/// 2010-07-24 BC: I added the info parameter to the call to Bind()
/// Binding binding = this.Bind(inst, args, kw, info);
/// to take advantage of Bind()'s ability to use a single MethodBase (CI or MI).
/// </remarks>
internal
object
InvokeRaw
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
,
MethodBase
info
)
{
Binding
binding
=
this
.
Bind
(
inst
,
args
,
kw
,
info
)
;
Object
result
;
if
(
binding
==
null
)
{
// It is possible for __new__ to be invoked on construction
// of a Python subclass of a managed class, so args may
// reflect more args than are required to instantiate the
// class. So if we cant find a ctor that matches, we'll see
// if there is a default constructor and, if so, assume that
// any extra args are intended for the subclass' __init__.
IntPtr
eargs
=
Runtime
.
PyTuple_New
(
0
)
;
binding
=
this
.
Bind
(
inst
,
eargs
,
kw
)
;
Runtime
.
Decref
(
eargs
)
;
if
(
binding
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"no constructor matches given arguments"
)
;
return
null
;
}
}
// Fire the selected ctor and catch errors...
ConstructorInfo
ci
=
(
ConstructorInfo
)
binding
.
info
;
// Object construction is presumed to be non-blocking and fast
// enough that we shouldn't really need to release the GIL.
try
{
result
=
ci
.
Invoke
(
binding
.
args
)
;
}
catch
(
Exception
e
)
{
if
(
e
.
InnerException
!=
null
)
{
e
=
e
.
InnerException
;
}
Exceptions
.
SetError
(
e
)
;
return
null
;
}
return
result
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL