FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/pymodule.cs at master · saroad2/pythonnet · GitHub
saroad2
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
pymodule.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (80 loc) · 3.31 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
pymodule.cs
Copy path
File metadata and controls
92 lines (80 loc) · 3.31 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
using
System
;
namespace
Python
.
Runtime
{
public
class
PyModule
:
PyScope
{
internal
PyModule
(
ref
NewReference
reference
)
:
base
(
ref
reference
,
PyScopeManager
.
Global
)
{
}
public
PyModule
(
PyObject
o
)
:
base
(
o
.
Reference
,
PyScopeManager
.
Global
)
{
}
public
PyModule
(
string
name
,
string
filename
=
null
)
:
this
(
Create
(
name
,
filename
)
)
{
}
/// <summary>
/// Given a module or package name, import the module and return the resulting object.
/// </summary>
/// <param name="name">Fully-qualified module or package name</param>
public
static
PyObject
Import
(
string
name
)
{
NewReference
op
=
Runtime
.
PyImport_ImportModule
(
name
)
;
PythonException
.
ThrowIfIsNull
(
op
)
;
return
IsModule
(
op
)
?
new
PyModule
(
ref
op
)
:
op
.
MoveToPyObject
(
)
;
}
/// <summary>
/// Reloads the module, and returns the updated object
/// </summary>
public
PyModule
Reload
(
)
{
NewReference
op
=
Runtime
.
PyImport_ReloadModule
(
this
.
Reference
)
;
PythonException
.
ThrowIfIsNull
(
op
)
;
return
new
PyModule
(
ref
op
)
;
}
public
static
PyModule
FromString
(
string
name
,
string
code
)
{
using
NewReference
c
=
Runtime
.
Py_CompileString
(
code
,
"none"
,
(
int
)
RunFlagType
.
File
)
;
PythonException
.
ThrowIfIsNull
(
c
)
;
NewReference
m
=
Runtime
.
PyImport_ExecCodeModule
(
name
,
c
)
;
PythonException
.
ThrowIfIsNull
(
m
)
;
return
new
PyModule
(
ref
m
)
;
}
private
static
PyModule
Create
(
string
name
,
string
filename
=
null
)
{
if
(
string
.
IsNullOrWhiteSpace
(
name
)
)
{
throw
new
ArgumentNullException
(
nameof
(
name
)
)
;
}
NewReference
op
=
Runtime
.
PyModule_New
(
name
)
;
PythonException
.
ThrowIfIsNull
(
op
)
;
if
(
filename
!=
null
)
{
BorrowedReference
globals
=
Runtime
.
PyModule_GetDict
(
op
)
;
PythonException
.
ThrowIfIsNull
(
globals
)
;
int
rc
=
Runtime
.
PyDict_SetItemString
(
globals
,
"__file__"
,
filename
.
ToPython
(
)
.
Reference
)
;
PythonException
.
ThrowIfIsNotZero
(
rc
)
;
}
return
new
PyModule
(
ref
op
)
;
}
public
void
SetBuiltins
(
PyDict
builtins
)
{
if
(
builtins
==
null
||
builtins
.
IsNone
(
)
)
{
throw
new
ArgumentNullException
(
nameof
(
builtins
)
)
;
}
BorrowedReference
globals
=
Runtime
.
PyModule_GetDict
(
this
.
Reference
)
;
PythonException
.
ThrowIfIsNull
(
globals
)
;
int
rc
=
Runtime
.
PyDict_SetItemString
(
globals
,
"__builtins__"
,
builtins
.
Reference
)
;
PythonException
.
ThrowIfIsNotZero
(
rc
)
;
}
public
static
PyDict
SysModules
{
get
{
BorrowedReference
sysModulesRef
=
Runtime
.
PyImport_GetModuleDict
(
)
;
PythonException
.
ThrowIfIsNull
(
sysModulesRef
)
;
return
new
PyDict
(
sysModulesRef
)
;
}
}
internal
static
bool
IsModule
(
BorrowedReference
reference
)
{
if
(
reference
==
null
)
return
false
;
BorrowedReference
type
=
Runtime
.
PyObject_TYPE
(
reference
)
;
return
Runtime
.
PyType_IsSubtype
(
type
,
Runtime
.
PyModuleType
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL