Skip to content

Commit 49124fc

Browse files
committed
switched module.cs to the new style references
1 parent 7fa537a commit 49124fc

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

src/runtime/module.cs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,30 @@ namespace Python.Runtime
77
{
88
public class PyModule : PyObject
99
{
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
1812
{
13+
get
14+
{
15+
var vars = Runtime.PyModule_GetDict(Reference);
16+
PythonException.ThrowIfIsNull(vars);
17+
return vars;
18+
}
1919
}
2020

21-
public PyModule(string name, string? fileName = null) : this(Create(name, fileName)) { }
21+
public PyModule(string name = "") : this(Create(name))
22+
{
23+
InitializeBuiltins();
24+
}
2225

23-
static StolenReference Create(string name, string? filename = null)
26+
static StolenReference Create(string name)
2427
{
2528
if (name is null)
2629
{
2730
throw new ArgumentNullException(nameof(name));
2831
}
2932

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();
4334
}
4435

4536
internal PyModule(in StolenReference reference) : base(reference)
@@ -48,16 +39,17 @@ internal PyModule(in StolenReference reference) : base(reference)
4839
{
4940
throw new ArgumentException("object is not a module");
5041
}
51-
//Refcount of the variables not increase
52-
variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress();
53-
PythonException.ThrowIfIsNull(variables);
42+
}
5443

44+
private void InitializeBuiltins()
45+
{
5546
int res = Runtime.PyDict_SetItem(
5647
VarsRef, PyIdentifier.__builtins__,
5748
Runtime.PyEval_GetBuiltins()
5849
);
5950
PythonException.ThrowIfIsNotZero(res);
6051
}
52+
6153
internal PyModule(BorrowedReference reference) : this(new NewReference(reference).Steal())
6254
{
6355
}
@@ -71,8 +63,7 @@ public static PyObject Import(string name)
7163
if (name is null) throw new ArgumentNullException(nameof(name));
7264

7365
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();
7667
}
7768

7869
/// <summary>
@@ -81,20 +72,17 @@ public static PyObject Import(string name)
8172
public PyModule Reload()
8273
{
8374
NewReference op = Runtime.PyImport_ReloadModule(this.Reference);
84-
PythonException.ThrowIfIsNull(op);
85-
return new PyModule(op.Steal());
75+
return new PyModule(op.StealOrThrow());
8676
}
8777

8878
public static PyModule FromString(string name, string code)
8979
{
9080
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());
9583
}
9684

97-
public void SetBuiltins(PyDict builtins)
85+
public PyModule SetBuiltins(PyDict builtins)
9886
{
9987
if (builtins == null || builtins.IsNone())
10088
{
@@ -105,6 +93,7 @@ public void SetBuiltins(PyDict builtins)
10593
PythonException.ThrowIfIsNull(globals);
10694
int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", builtins.Reference);
10795
PythonException.ThrowIfIsNotZero(rc);
96+
return this;
10897
}
10998

11099
public static PyDict SysModules
@@ -157,7 +146,9 @@ public PyObject Import(string name, string? asname = null)
157146
/// </summary>
158147
public void Import(PyModule module, string asname)
159148
{
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);
161152
}
162153

163154
/// <summary>
@@ -166,6 +157,8 @@ public void Import(PyModule module, string asname)
166157
/// </summary>
167158
public void Import(PyObject module, string? asname = null)
168159
{
160+
if (module is null) throw new ArgumentNullException(nameof(module));
161+
169162
asname ??= module.GetAttr("__name__").As<string>();
170163
Set(asname, module);
171164
}
@@ -175,6 +168,8 @@ public void Import(PyObject module, string? asname = null)
175168
/// </summary>
176169
public void ImportAll(PyModule module)
177170
{
171+
if (module is null) throw new ArgumentNullException(nameof(module));
172+
178173
int result = Runtime.PyDict_Update(VarsRef, module.VarsRef);
179174
if (result < 0)
180175
{
@@ -206,6 +201,8 @@ public void ImportAll(PyObject module)
206201
/// </summary>
207202
public void ImportAll(PyDict dict)
208203
{
204+
if (dict is null) throw new ArgumentNullException(nameof(dict));
205+
209206
int result = Runtime.PyDict_Update(VarsRef, dict.Reference);
210207
if (result < 0)
211208
{
@@ -222,11 +219,13 @@ public void ImportAll(PyDict dict)
222219
/// </remarks>
223220
public PyObject Execute(PyObject script, PyDict? locals = null)
224221
{
222+
if (script is null) throw new ArgumentNullException(nameof(script));
223+
225224
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);
228227
PythonException.ThrowIfIsNull(ptr);
229-
return new PyObject(ptr);
228+
return ptr.MoveToPyObject();
230229
}
231230

232231
/// <summary>
@@ -254,6 +253,8 @@ public T Execute<T>(PyObject script, PyDict? locals = null)
254253
/// </remarks>
255254
public PyObject Eval(string code, PyDict? locals = null)
256255
{
256+
if (code is null) throw new ArgumentNullException(nameof(code));
257+
257258
Check();
258259
BorrowedReference _locals = locals == null ? VarsRef : locals.Reference;
259260

@@ -285,11 +286,12 @@ public T Eval<T>(string code, PyDict? locals = null)
285286
/// <remarks>
286287
/// Exec a Python script and save its local variables in the current local variable dict.
287288
/// </remarks>
288-
public void Exec(string code, PyDict? locals = null)
289+
public PyModule Exec(string code, PyDict? locals = null)
289290
{
290291
Check();
291292
BorrowedReference _locals = locals == null ? VarsRef : locals.Reference;
292293
Exec(code, VarsRef, _locals);
294+
return this;
293295
}
294296

295297
private void Exec(string code, BorrowedReference _globals, BorrowedReference _locals)
@@ -307,16 +309,16 @@ private void Exec(string code, BorrowedReference _globals, BorrowedReference _lo
307309
/// Add a new variable to the variables dict if it not exist
308310
/// or update its value if the variable exists.
309311
/// </remarks>
310-
public void Set(string name, object value)
312+
public PyModule Set(string name, object value)
311313
{
312314
if (name is null) throw new ArgumentNullException(nameof(name));
313315

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;
317319
}
318320

319-
private void SetPyValue(string name, IntPtr value)
321+
private void SetPyValue(string name, BorrowedReference value)
320322
{
321323
Check();
322324
using (var pyKey = new PyString(name))
@@ -335,7 +337,7 @@ private void SetPyValue(string name, IntPtr value)
335337
/// <remarks>
336338
/// Remove a variable from the variables dict.
337339
/// </remarks>
338-
public void Remove(string name)
340+
public PyModule Remove(string name)
339341
{
340342
if (name is null) throw new ArgumentNullException(nameof(name));
341343

@@ -348,6 +350,7 @@ public void Remove(string name)
348350
throw PythonException.ThrowLastAsClrException();
349351
}
350352
}
353+
return this;
351354
}
352355

353356
/// <summary>
@@ -398,13 +401,8 @@ public bool TryGet(string name, out PyObject? value)
398401
{
399402
if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0)
400403
{
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());
408406
return true;
409407
}
410408
else
@@ -467,7 +465,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
467465

468466
private void Check()
469467
{
470-
if (this.obj == IntPtr.Zero)
468+
if (this.rawPtr == IntPtr.Zero)
471469
{
472470
throw new ObjectDisposedException(nameof(PyModule));
473471
}

0 commit comments

Comments
 (0)