FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/pyimport.cs at master · AdsTable/pythonnet · GitHub
AdsTable
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
pyimport.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
109 lines (97 loc) · 3.16 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
pyimport.cs
Copy path
File metadata and controls
109 lines (97 loc) · 3.16 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
using
System
;
using
System
.
IO
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
namespace
Python
.
EmbeddingTest
{
/// <summary>
/// Test Import unittests and regressions
/// </summary>
/// <remarks>
/// Keeping in old-style SetUp/TearDown due to required SetUp.
/// The required directory structure was added to .\pythonnet\src\embed_tests\fixtures\ directory:
/// + PyImportTest/
/// | - __init__.py
/// | + test/
/// | | - __init__.py
/// | | - one.py
/// </remarks>
public
class
PyImportTest
{
private
IntPtr
_gs
;
[
SetUp
]
public
void
SetUp
(
)
{
PythonEngine
.
Initialize
(
)
;
_gs
=
PythonEngine
.
AcquireLock
(
)
;
/* Append the tests directory to sys.path
* using reflection to circumvent the private
* modifiers placed on most Runtime methods. */
string
testPath
=
Path
.
Combine
(
TestContext
.
CurrentContext
.
TestDirectory
,
"fixtures"
)
;
TestContext
.
Out
.
WriteLine
(
testPath
)
;
IntPtr
str
=
Runtime
.
Runtime
.
PyString_FromString
(
testPath
)
;
Assert
.
IsFalse
(
str
==
IntPtr
.
Zero
)
;
BorrowedReference
path
=
Runtime
.
Runtime
.
PySys_GetObject
(
"path"
)
;
Assert
.
IsFalse
(
path
.
IsNull
)
;
Runtime
.
Runtime
.
PyList_Append
(
path
,
str
)
;
Runtime
.
Runtime
.
XDecref
(
str
)
;
}
[
TearDown
]
public
void
Dispose
(
)
{
PythonEngine
.
ReleaseLock
(
_gs
)
;
PythonEngine
.
Shutdown
(
)
;
}
/// <summary>
/// Test subdirectory import
/// </summary>
[
Test
]
public
void
TestDottedName
(
)
{
var
module
=
PyModule
.
Import
(
"PyImportTest.test.one"
)
;
Assert
.
IsNotNull
(
module
)
;
}
/// <summary>
/// Tests that sys.args is set. If it wasn't exception would be raised.
/// </summary>
[
Test
]
public
void
TestSysArgsImportException
(
)
{
var
module
=
PyModule
.
Import
(
"PyImportTest.sysargv"
)
;
Assert
.
IsNotNull
(
module
)
;
}
/// <summary>
/// Test Global Variable casting. GH#420
/// </summary>
[
Test
]
public
void
TestCastGlobalVar
(
)
{
dynamic
foo
=
Py
.
Import
(
"PyImportTest.cast_global_var"
)
;
Assert
.
AreEqual
(
"1"
,
foo
.
FOO
.
ToString
(
)
)
;
Assert
.
AreEqual
(
"1"
,
foo
.
test_foo
(
)
.
ToString
(
)
)
;
foo
.
FOO
=
2
;
Assert
.
AreEqual
(
"2"
,
foo
.
FOO
.
ToString
(
)
)
;
Assert
.
AreEqual
(
"2"
,
foo
.
test_foo
(
)
.
ToString
(
)
)
;
}
[
Test
]
public
void
BadAssembly
(
)
{
string
path
;
if
(
Python
.
Runtime
.
Runtime
.
IsWindows
)
{
path
=
@"C:\Windows\System32\kernel32.dll"
;
}
else
{
Assert
.
Pass
(
"TODO: add bad assembly location for other platforms"
)
;
return
;
}
string
code
=
$@
"
import clr
clr.AddReference('
{
path
}
')
"
;
var
error
=
Assert
.
Throws
<
PythonException
>
(
(
)
=>
PythonEngine
.
Exec
(
code
)
)
;
Assert
.
AreEqual
(
nameof
(
FileLoadException
)
,
error
.
PythonTypeName
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL