FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/CallableObject.cs at net8.0 · 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
/
embed_tests
/
CallableObject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (76 loc) · 2.69 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
CallableObject.cs
Copy path
File metadata and controls
87 lines (76 loc) · 2.69 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
using
System
;
using
System
.
Collections
.
Generic
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
namespace
Python
.
EmbeddingTest
{
public
class
CallableObject
{
[
OneTimeSetUp
]
public
void
SetUp
(
)
{
PythonEngine
.
Initialize
(
)
;
using
var
locals
=
new
PyDict
(
)
;
PythonEngine
.
Exec
(
CallViaInheritance
.
BaseClassSource
,
locals
:
locals
)
;
CustomBaseTypeProvider
.
BaseClass
=
new
PyType
(
locals
[
CallViaInheritance
.
BaseClassName
]
)
;
PythonEngine
.
InteropConfiguration
.
PythonBaseTypeProviders
.
Add
(
new
CustomBaseTypeProvider
(
)
)
;
}
[
OneTimeTearDown
]
public
void
Dispose
(
)
{
PythonEngine
.
Shutdown
(
)
;
}
[
Test
]
public
void
CallMethodMakesObjectCallable
(
)
{
var
doubler
=
new
DerivedDoubler
(
)
;
dynamic
applyObjectTo21
=
PythonEngine
.
Eval
(
"lambda o: o(21)"
)
;
Assert
.
AreEqual
(
doubler
.
__call__
(
21
)
,
(
int
)
applyObjectTo21
(
doubler
.
ToPython
(
)
)
)
;
}
[
Test
]
public
void
CallMethodCanBeInheritedFromPython
(
)
{
var
callViaInheritance
=
new
CallViaInheritance
(
)
;
dynamic
applyObjectTo14
=
PythonEngine
.
Eval
(
"lambda o: o(14)"
)
;
Assert
.
AreEqual
(
callViaInheritance
.
Call
(
14
)
,
(
int
)
applyObjectTo14
(
callViaInheritance
.
ToPython
(
)
)
)
;
}
[
Test
]
public
void
CanOverwriteCall
(
)
{
var
callViaInheritance
=
new
CallViaInheritance
(
)
;
using
var
scope
=
Py
.
CreateScope
(
)
;
scope
.
Set
(
"o"
,
callViaInheritance
)
;
scope
.
Exec
(
"orig_call = o.Call"
)
;
scope
.
Exec
(
"o.Call = lambda a: orig_call(a*7)"
)
;
int
result
=
scope
.
Eval
<
int
>
(
"o.Call(5)"
)
;
Assert
.
AreEqual
(
105
,
result
)
;
}
class
Doubler
{
public
int
__call__
(
int
arg
)
=>
2
*
arg
;
}
class
DerivedDoubler
:
Doubler
{
}
class
CallViaInheritance
{
public
const
string
BaseClassName
=
"Forwarder"
;
public
static
readonly
string
BaseClassSource
=
$@
"
class MyCallableBase:
def __call__(self, val):
return self.Call(val)
class
{
BaseClassName
}
(MyCallableBase): pass
"
;
public
int
Call
(
int
arg
)
=>
3
*
arg
;
}
class
CustomBaseTypeProvider
:
IPythonBaseTypeProvider
{
internal
static
PyType
BaseClass
;
public
IEnumerable
<
PyType
>
GetBaseTypes
(
Type
type
,
IList
<
PyType
>
existingBases
)
{
Assert
.
Greater
(
BaseClass
.
Refcount
,
0
)
;
return
type
!=
typeof
(
CallViaInheritance
)
?
existingBases
:
new
[
]
{
BaseClass
}
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL