FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/RuntimeState.cs at noncopyableanalyzer · pythonnet/pythonnet · GitHub
pythonnet
pythonnet
Repository navigation
Code
Issues
147
(147)
Pull requests
11
(11)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
RuntimeState.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (60 loc) · 2 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
RuntimeState.cs
Copy path
File metadata and controls
67 lines (60 loc) · 2 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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
static
Python
.
Runtime
.
Runtime
;
namespace
Python
.
Runtime
{
class
RuntimeState
{
public
static
void
Save
(
)
{
if
(
!
PySys_GetObject
(
"initial_modules"
)
.
IsNull
)
{
throw
new
Exception
(
"Runtime State set already"
)
;
}
using
var
modules
=
PySet_New
(
default
)
;
int
res
=
PySys_SetObject
(
"initial_modules"
,
modules
.
Borrow
(
)
)
;
PythonException
.
ThrowIfIsNotZero
(
res
)
;
foreach
(
var
name
in
GetModuleNames
(
)
)
{
res
=
PySet_Add
(
modules
.
Borrow
(
)
,
new
BorrowedReference
(
name
)
)
;
PythonException
.
ThrowIfIsNotZero
(
res
)
;
}
}
public
static
void
Restore
(
)
{
RestoreModules
(
)
;
}
private
static
void
RestoreModules
(
)
{
var
intialModules
=
PySys_GetObject
(
"initial_modules"
)
;
Debug
.
Assert
(
!
intialModules
.
IsNull
)
;
var
modules
=
PyImport_GetModuleDict
(
)
;
foreach
(
var
nameRaw
in
GetModuleNames
(
)
)
{
var
name
=
new
BorrowedReference
(
nameRaw
)
;
if
(
PySet_Contains
(
intialModules
,
name
)
==
1
)
{
continue
;
}
if
(
PyDict_DelItem
(
modules
,
name
)
!=
0
)
{
PyErr_Print
(
)
;
}
}
}
public
static
IEnumerable
<
IntPtr
>
GetModuleNames
(
)
{
var
modules
=
PyImport_GetModuleDict
(
)
;
using
var
names
=
PyDict_Keys
(
modules
)
;
nint
length
=
PyList_Size
(
names
.
BorrowOrThrow
(
)
)
;
if
(
length
<
0
)
throw
PythonException
.
ThrowLastAsClrException
(
)
;
var
result
=
new
IntPtr
[
length
]
;
for
(
int
i
=
0
;
i
<
length
;
i
++
)
{
result
[
i
]
=
PyList_GetItem
(
names
.
Borrow
(
)
,
i
)
.
DangerousGetAddress
(
)
;
}
return
result
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL