@@ -7,39 +7,30 @@ namespace Python.Runtime
7
7
{
8
8
public class PyModule : PyObject
9
9
{
10
- /// <summary>
11
- /// the variable dict of the module. Borrowed.
12
- /// </summary>
13
- internal readonly IntPtr variables ;
14
- internal BorrowedReference VarsRef => new BorrowedReference ( variables ) ;
15
-
16
- public PyModule ( string name = "" )
17
- : this ( Create ( name ?? throw new ArgumentNullException ( nameof ( name ) ) ) )
10
+ internal BorrowedReference variables => VarsRef ;
11
+ internal BorrowedReference VarsRef
18
12
{
13
+ get
14
+ {
15
+ var vars = Runtime . PyModule_GetDict ( Reference ) ;
16
+ PythonException . ThrowIfIsNull ( vars ) ;
17
+ return vars ;
18
+ }
19
19
}
20
20
21
- public PyModule ( string name , string ? fileName = null ) : this ( Create ( name , fileName ) ) { }
21
+ public PyModule ( string name = "" ) : this ( Create ( name ) )
22
+ {
23
+ InitializeBuiltins ( ) ;
24
+ }
22
25
23
- static StolenReference Create ( string name , string ? filename = null )
26
+ static StolenReference Create ( string name )
24
27
{
25
28
if ( name is null )
26
29
{
27
30
throw new ArgumentNullException ( nameof ( name ) ) ;
28
31
}
29
32
30
- NewReference op = Runtime . PyModule_New ( name ) ;
31
- PythonException . ThrowIfIsNull ( op ) ;
32
-
33
- if ( filename is not null )
34
- {
35
- BorrowedReference globals = Runtime . PyModule_GetDict ( op ) ;
36
- PythonException . ThrowIfIsNull ( globals ) ;
37
- using var pyFileName = filename . ToPython ( ) ;
38
- int rc = Runtime . PyDict_SetItemString ( globals , "__file__" , pyFileName . Reference ) ;
39
- PythonException . ThrowIfIsNotZero ( rc ) ;
40
- }
41
-
42
- return op . Steal ( ) ;
33
+ return Runtime . PyModule_New ( name ) . StealOrThrow ( ) ;
43
34
}
44
35
45
36
internal PyModule ( in StolenReference reference ) : base ( reference )
@@ -48,16 +39,17 @@ internal PyModule(in StolenReference reference) : base(reference)
48
39
{
49
40
throw new ArgumentException ( "object is not a module" ) ;
50
41
}
51
- //Refcount of the variables not increase
52
- variables = Runtime . PyModule_GetDict ( Reference ) . DangerousGetAddress ( ) ;
53
- PythonException . ThrowIfIsNull ( variables ) ;
42
+ }
54
43
44
+ private void InitializeBuiltins ( )
45
+ {
55
46
int res = Runtime . PyDict_SetItem (
56
47
VarsRef , PyIdentifier . __builtins__ ,
57
48
Runtime . PyEval_GetBuiltins ( )
58
49
) ;
59
50
PythonException . ThrowIfIsNotZero ( res ) ;
60
51
}
52
+
61
53
internal PyModule ( BorrowedReference reference ) : this ( new NewReference ( reference ) . Steal ( ) )
62
54
{
63
55
}
@@ -71,8 +63,7 @@ public static PyObject Import(string name)
71
63
if ( name is null ) throw new ArgumentNullException ( nameof ( name ) ) ;
72
64
73
65
NewReference op = Runtime . PyImport_ImportModule ( name ) ;
74
- PythonException . ThrowIfIsNull ( op ) ;
75
- return IsModule ( op ) ? new PyModule ( op . Steal ( ) ) : op . MoveToPyObject ( ) ;
66
+ return IsModule ( op . BorrowOrThrow ( ) ) ? new PyModule ( op . Steal ( ) ) : op . MoveToPyObject ( ) ;
76
67
}
77
68
78
69
/// <summary>
@@ -81,20 +72,17 @@ public static PyObject Import(string name)
81
72
public PyModule Reload ( )
82
73
{
83
74
NewReference op = Runtime . PyImport_ReloadModule ( this . Reference ) ;
84
- PythonException . ThrowIfIsNull ( op ) ;
85
- return new PyModule ( op . Steal ( ) ) ;
75
+ return new PyModule ( op . StealOrThrow ( ) ) ;
86
76
}
87
77
88
78
public static PyModule FromString ( string name , string code )
89
79
{
90
80
using NewReference c = Runtime . Py_CompileString ( code , "none" , ( int ) RunFlagType . File ) ;
91
- PythonException . ThrowIfIsNull ( c ) ;
92
- NewReference m = Runtime . PyImport_ExecCodeModule ( name , c ) ;
93
- PythonException . ThrowIfIsNull ( m ) ;
94
- return new PyModule ( m . Steal ( ) ) ;
81
+ NewReference m = Runtime . PyImport_ExecCodeModule ( name , c . BorrowOrThrow ( ) ) ;
82
+ return new PyModule ( m . StealOrThrow ( ) ) ;
95
83
}
96
84
97
- public void SetBuiltins ( PyDict builtins )
85
+ public PyModule SetBuiltins ( PyDict builtins )
98
86
{
99
87
if ( builtins == null || builtins . IsNone ( ) )
100
88
{
@@ -105,6 +93,7 @@ public void SetBuiltins(PyDict builtins)
105
93
PythonException . ThrowIfIsNull ( globals ) ;
106
94
int rc = Runtime . PyDict_SetItemString ( globals , "__builtins__" , builtins . Reference ) ;
107
95
PythonException . ThrowIfIsNotZero ( rc ) ;
96
+ return this ;
108
97
}
109
98
110
99
public static PyDict SysModules
@@ -157,7 +146,9 @@ public PyObject Import(string name, string? asname = null)
157
146
/// </summary>
158
147
public void Import ( PyModule module , string asname )
159
148
{
160
- this . SetPyValue ( asname , module . Handle ) ;
149
+ if ( module is null ) throw new ArgumentNullException ( nameof ( module ) ) ;
150
+ if ( asname is null ) throw new ArgumentNullException ( nameof ( asname ) ) ;
151
+ this . SetPyValue ( asname , module ) ;
161
152
}
162
153
163
154
/// <summary>
@@ -166,6 +157,8 @@ public void Import(PyModule module, string asname)
166
157
/// </summary>
167
158
public void Import ( PyObject module , string ? asname = null )
168
159
{
160
+ if ( module is null ) throw new ArgumentNullException ( nameof ( module ) ) ;
161
+
169
162
asname ??= module . GetAttr ( "__name__" ) . As < string > ( ) ;
170
163
Set ( asname , module ) ;
171
164
}
@@ -175,6 +168,8 @@ public void Import(PyObject module, string? asname = null)
175
168
/// </summary>
176
169
public void ImportAll ( PyModule module )
177
170
{
171
+ if ( module is null ) throw new ArgumentNullException ( nameof ( module ) ) ;
172
+
178
173
int result = Runtime . PyDict_Update ( VarsRef , module . VarsRef ) ;
179
174
if ( result < 0 )
180
175
{
@@ -206,6 +201,8 @@ public void ImportAll(PyObject module)
206
201
/// </summary>
207
202
public void ImportAll ( PyDict dict )
208
203
{
204
+ if ( dict is null ) throw new ArgumentNullException ( nameof ( dict ) ) ;
205
+
209
206
int result = Runtime . PyDict_Update ( VarsRef , dict . Reference ) ;
210
207
if ( result < 0 )
211
208
{
@@ -222,11 +219,13 @@ public void ImportAll(PyDict dict)
222
219
/// </remarks>
223
220
public PyObject Execute ( PyObject script , PyDict ? locals = null )
224
221
{
222
+ if ( script is null ) throw new ArgumentNullException ( nameof ( script ) ) ;
223
+
225
224
Check ( ) ;
226
- IntPtr _locals = locals == null ? variables : locals . obj ;
227
- IntPtr ptr = Runtime . PyEval_EvalCode ( script . Handle , variables , _locals ) ;
225
+ BorrowedReference _locals = locals == null ? variables : locals . obj ;
226
+ using var ptr = Runtime . PyEval_EvalCode ( script , variables , _locals ) ;
228
227
PythonException . ThrowIfIsNull ( ptr ) ;
229
- return new PyObject ( ptr ) ;
228
+ return ptr . MoveToPyObject ( ) ;
230
229
}
231
230
232
231
/// <summary>
@@ -254,6 +253,8 @@ public T Execute<T>(PyObject script, PyDict? locals = null)
254
253
/// </remarks>
255
254
public PyObject Eval ( string code , PyDict ? locals = null )
256
255
{
256
+ if ( code is null ) throw new ArgumentNullException ( nameof ( code ) ) ;
257
+
257
258
Check ( ) ;
258
259
BorrowedReference _locals = locals == null ? VarsRef : locals . Reference ;
259
260
@@ -285,11 +286,12 @@ public T Eval<T>(string code, PyDict? locals = null)
285
286
/// <remarks>
286
287
/// Exec a Python script and save its local variables in the current local variable dict.
287
288
/// </remarks>
288
- public void Exec ( string code , PyDict ? locals = null )
289
+ public PyModule Exec ( string code , PyDict ? locals = null )
289
290
{
290
291
Check ( ) ;
291
292
BorrowedReference _locals = locals == null ? VarsRef : locals . Reference ;
292
293
Exec ( code , VarsRef , _locals ) ;
294
+ return this ;
293
295
}
294
296
295
297
private void Exec ( string code , BorrowedReference _globals , BorrowedReference _locals )
@@ -307,16 +309,16 @@ private void Exec(string code, BorrowedReference _globals, BorrowedReference _lo
307
309
/// Add a new variable to the variables dict if it not exist
308
310
/// or update its value if the variable exists.
309
311
/// </remarks>
310
- public void Set ( string name , object value )
312
+ public PyModule Set ( string name , object value )
311
313
{
312
314
if ( name is null ) throw new ArgumentNullException ( nameof ( name ) ) ;
313
315
314
- IntPtr _value = Converter . ToPython ( value , value ? . GetType ( ) ) ;
315
- SetPyValue ( name , _value ) ;
316
- Runtime . XDecref ( _value ) ;
316
+ using var _value = Converter . ToPython ( value , value ? . GetType ( ) ?? typeof ( object ) ) ;
317
+ SetPyValue ( name , _value . Borrow ( ) ) ;
318
+ return this ;
317
319
}
318
320
319
- private void SetPyValue ( string name , IntPtr value )
321
+ private void SetPyValue ( string name , BorrowedReference value )
320
322
{
321
323
Check ( ) ;
322
324
using ( var pyKey = new PyString ( name ) )
@@ -335,7 +337,7 @@ private void SetPyValue(string name, IntPtr value)
335
337
/// <remarks>
336
338
/// Remove a variable from the variables dict.
337
339
/// </remarks>
338
- public void Remove ( string name )
340
+ public PyModule Remove ( string name )
339
341
{
340
342
if ( name is null ) throw new ArgumentNullException ( nameof ( name ) ) ;
341
343
@@ -348,6 +350,7 @@ public void Remove(string name)
348
350
throw PythonException . ThrowLastAsClrException ( ) ;
349
351
}
350
352
}
353
+ return this ;
351
354
}
352
355
353
356
/// <summary>
@@ -398,13 +401,8 @@ public bool TryGet(string name, out PyObject? value)
398
401
{
399
402
if ( Runtime . PyMapping_HasKey ( variables , pyKey . obj ) != 0 )
400
403
{
401
- IntPtr op = Runtime . PyObject_GetItem ( variables , pyKey . obj ) ;
402
- if ( op == IntPtr . Zero )
403
- {
404
- throw PythonException . ThrowLastAsClrException ( ) ;
405
- }
406
-
407
- value = new PyObject ( op ) ;
404
+ using var op = Runtime . PyObject_GetItem ( variables , pyKey . obj ) ;
405
+ value = new PyObject ( op . StealOrThrow ( ) ) ;
408
406
return true ;
409
407
}
410
408
else
@@ -467,7 +465,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
467
465
468
466
private void Check ( )
469
467
{
470
- if ( this . obj == IntPtr . Zero )
468
+ if ( this . rawPtr == IntPtr . Zero )
471
469
{
472
470
throw new ObjectDisposedException ( nameof ( PyModule ) ) ;
473
471
}
0 commit comments