Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
5d6e5d5
use reference types instead of IntPtr where possible in runtime.cs
lostmsu Oct 17, 2021
2d33902
switched converter.cs to the new style references
lostmsu Oct 17, 2021
f8b761a
switched most of classbase.cs to the new style references (except cro…
lostmsu Oct 17, 2021
09d8e41
switched pyobject.cs to the new style references
lostmsu Oct 17, 2021
1b58cf4
mostly switched moduleobject.cs to the new style references
lostmsu Oct 17, 2021
c05c6ec
switched methodbinder.cs to the new style references
lostmsu Oct 17, 2021
d626f7e
partially switched classderived.cs to the new reference style
lostmsu Oct 17, 2021
ff60ec4
switched arrayobject.cs to the new style references
lostmsu Oct 17, 2021
0010fa0
switched delegatemanager.cs to the new style references
lostmsu Oct 17, 2021
178a359
partially switched metatype.cs to the new style references
lostmsu Oct 18, 2021
9764b25
switched typemanager.cs to the new style references
lostmsu Oct 18, 2021
11edcc3
switched pytype.cs to the new style references
lostmsu Oct 18, 2021
2e71874
mass enable nullable types
lostmsu Oct 18, 2021
3076040
fixed nullablity in arrayobject.cs
lostmsu Oct 18, 2021
56f3bd5
fixed nullability in assemblymanager.cs
lostmsu Oct 18, 2021
58cb0e6
switched classmanager.cs to the new style references
lostmsu Oct 18, 2021
2095b46
switched classobject.cs to the new style references
lostmsu Oct 18, 2021
f6b84da
partially switched managedtype.cs to the new style references
lostmsu Oct 18, 2021
ee65632
partially switched classmanager.cs to the new style references
lostmsu Oct 18, 2021
5266dc4
PyIdentifier public members to return borrowed references
lostmsu Oct 6, 2021
0bc3670
added nullability annotations to methodbinder.cs
lostmsu Oct 18, 2021
de9a8cb
switched methodbinding.cs and methodobject.cs to the new style refere…
lostmsu Oct 18, 2021
9195c30
switched overload.cs to the new style references
lostmsu Oct 18, 2021
590de7a
switched propertyobject.cs to the new style references
lostmsu Oct 18, 2021
7fa537a
switched delegateobject.cs to the new style references
lostmsu Oct 18, 2021
49124fc
switched module.cs to the new style references
lostmsu Oct 18, 2021
9db9b0b
nullability annotations for PyObject
lostmsu Oct 18, 2021
00fd17a
switched modulefunctionobject.cs to the new style references
lostmsu Oct 18, 2021
5798b41
minor refactorings
lostmsu Oct 18, 2021
ebdf7c5
partially switched moduleobject.cs and importhook.cs to the new style…
lostmsu Oct 18, 2021
5ad09e4
switched exceptions.cs to the new style references
lostmsu Oct 18, 2021
d1abd9a
switched interfaceobject.cs to the new style references
lostmsu Oct 18, 2021
43a862a
switched pythonexception.cs to the new style references
lostmsu Oct 18, 2021
1d80162
switched pytuple.cs to the new style references
lostmsu Oct 18, 2021
0241b38
switched eventobject.cs and eventbiding.cs to the new style references
lostmsu Oct 18, 2021
2ac952a
switched all PyObject derived classes to the new style references
lostmsu Oct 18, 2021
7adf98a
implemented non-confusing PyModule_AddObject
lostmsu Oct 18, 2021
2dd3f8f
switched pythonengine.cs to the new style references
lostmsu Oct 18, 2021
3b79019
switched fieldobject.cs and constructorbinding.cs to the new style re…
lostmsu Oct 18, 2021
9b990c1
switched finalizer.cs to the new style references
lostmsu Oct 19, 2021
027e529
switched debughelper.cs to the new style references
lostmsu Oct 19, 2021
4793818
switched converter extensions and sample codecs to the new style refe…
lostmsu Oct 19, 2021
0d60500
switched collection wrappers (from sample codec) to the new style ref…
lostmsu Oct 19, 2021
cf606a2
switched iterator.cs and indexer.cs to the new style references
lostmsu Oct 19, 2021
bb84c48
getting rid of a few minor warnings and compile errors
lostmsu Oct 19, 2021
e295679
switched to new references in some tests
lostmsu Oct 19, 2021
7a9e411
switched state serialization to new reference types (untested)
lostmsu Oct 19, 2021
9a9ed3b
minor error fixes
lostmsu Oct 19, 2021
581f695
assume remaning manual refcounting is not needed, because we use smar…
lostmsu Oct 19, 2021
07f1657
fixed new reference uses, that are not allowed in C#
lostmsu Oct 19, 2021
7deebd4
renamed parameter in tp_dealloc functions for clarity
lostmsu Oct 19, 2021
8619e77
allowed untested calls to PyObject_GC_Del and XDecref (3 in total)
lostmsu Oct 19, 2021
672aef6
fixed compile errors in TypeMethod (untested)
lostmsu Oct 19, 2021
c4909d4
workaround for analyzer not permitting copying a reference as the las…
lostmsu Oct 19, 2021
6fa2004
switched tests to match the new reference changes
lostmsu Oct 19, 2021
14949fb
fixed thunk loading for slots, that use new reference types
lostmsu Oct 19, 2021
0728e21
fixed PyObject_DelAttr load from DLL failing
lostmsu Oct 19, 2021
fe4c481
fixed uses of Marshal.Read/Marshal.Write overloads with first argumen…
lostmsu Oct 19, 2021
4346d41
fixed OnSerialized and OnDeserialized in PyObject not being typed cor…
lostmsu Oct 19, 2021
62e193a
fixed bad equality comparisons
lostmsu Oct 20, 2021
2fa8b9c
improved reliability of Clean and Dealloc implementations
lostmsu Oct 20, 2021
d6607b0
bad if condition
lostmsu Oct 20, 2021
6335d97
improved GetThunk reliability
lostmsu Oct 20, 2021
d649d6c
fixed circular dependency in Runtime PyMembers and InternString initi…
lostmsu Oct 20, 2021
d1bc193
tiny refactor
lostmsu Oct 20, 2021
32c4bb6
switched generictype.cs to the new style references
lostmsu Oct 20, 2021
a1427ac
increfs in OnSave are no longer necessary with the new references
lostmsu Oct 20, 2021
cd97a46
fixed MethodBinding failing for reference types
lostmsu Oct 20, 2021
05ecbcf
nullability annotation fix in MaybeMethodBase
lostmsu Oct 20, 2021
a3591b6
minor improvements
lostmsu Oct 20, 2021
7ed0c7a
WIP
lostmsu Oct 24, 2021
d6a853f
avoid generating and handling useless SerializationException when May…
lostmsu Oct 28, 2021
b0c25c1
finalizer does not attempt to finalize objects when runtime is shut down
lostmsu Oct 28, 2021
5ca474a
PyType Dict and MRO properties to assist debugging
lostmsu Oct 28, 2021
48078b3
WIP 2
lostmsu Oct 28, 2021
a624dd8
fixed PyObject disposal crashing when runtime is still finalizing
lostmsu Oct 29, 2021
e7ab071
arrays: use 64 bit indexing, and avoid first chance .NET exceptions o…
lostmsu Oct 29, 2021
cbe1dd2
refactored conditional ClassBase slot initialization
lostmsu Oct 29, 2021
d5f1c48
removed DisposePythonWrappersForClrTypes
lostmsu Oct 29, 2021
74d87c5
simplified outdated condition in ClassBase.tp_clear
lostmsu Oct 29, 2021
82d6c33
sprinkled a few DebuggerHidden to make debugging easier
lostmsu Oct 29, 2021
eeebcd7
fixed derived classes not inheriting slots correctly
lostmsu Oct 29, 2021
8ee8d3d
remove unused TypeManager._slotImpls
lostmsu Oct 29, 2021
1a4ada7
fixed TestRuntime not building in Release mode
lostmsu Oct 30, 2021
a610aa3
can't really clear managed references to Python objects from ManagedT…
lostmsu Oct 30, 2021
03f32cb
PythonException is serializable
lostmsu Oct 30, 2021
b1c9f5b
EventObject no longer used for static events. EventBinding is constru…
lostmsu Oct 30, 2021
cb4bb9a
use a special class to stub .NET types that no longer exist after a d…
lostmsu Nov 2, 2021
652f946
make EventHandlerCollection serializable
lostmsu Nov 2, 2021
84db670
fixed MaybeMemberInfo always failing for properties
lostmsu Nov 2, 2021
56fafe3
fixed construct_removed_class domain reload test case
lostmsu Nov 2, 2021
d33dcdd
domain reload test runner can run test by index
lostmsu Nov 2, 2021
b737e10
minor docs change
lostmsu Nov 2, 2021
d3e4fba
assert check in GetUnmanagedBaseType for null base
lostmsu Nov 2, 2021
e003e12
PythonEngine .Exec and .Eval no longer work with raw pointers
lostmsu Nov 9, 2021
d0a6f44
a few annotation to ease debugging
lostmsu Nov 9, 2021
e31f7ba
ensure Python types continue to exist when registered decoders for th…
lostmsu Nov 9, 2021
48c0dfc
GC-related WIP
lostmsu Nov 9, 2021
44d65d9
merge latest master
lostmsu Nov 23, 2021
cb58147
merge latest changes from upstream
lostmsu Nov 23, 2021
2fdbf0e
added TraceAlloc solution configuration
lostmsu Nov 24, 2021
a8ef06c
fixed sending PyObject across domain boundary
lostmsu Nov 24, 2021
7167229
fixed accidental premature disposal of Runtime.PyNone
lostmsu Nov 24, 2021
ab11fa2
made freeing GCHandles more robust
lostmsu Nov 24, 2021
7a4daeb
removed bad assert in generated constructor for derived classes
lostmsu Nov 24, 2021
e422367
fixed __pyobj__ access
lostmsu Nov 24, 2021
a74ea86
minor
lostmsu Nov 24, 2021
0325a8c
fixed Python derived types trying to double-free GCHandle when collec…
lostmsu Nov 24, 2021
85fab3b
reinstate collection assert on shutdown from Python
lostmsu Nov 24, 2021
932fce2
fixed crash when Python derived class instances survive past early sh…
lostmsu Nov 24, 2021
c2e207a
delay nulling GC handles of reflected instances until the last moment…
lostmsu Nov 24, 2021
c8f0f09
fixed assert in XDecref in case _Py_IsFinalizing is not present
lostmsu Nov 24, 2021
e269cf0
when initialized from Python, reset slots implemented in CLR: CLR mig…
lostmsu Nov 25, 2021
d7d5cb7
fixed minor warnings
lostmsu Nov 25, 2021
d6edace
fixed line endings in intern_.cs
lostmsu Nov 25, 2021
a86994f
use NonCopyableAnalyzer 0.7.0-m05
lostmsu Nov 25, 2021
2e040ea
Merge pull request #2 from losttech/ManagedType-DontKeepBorrowedRefs
lostmsu Dec 9, 2021
b614dba
merge latest master
lostmsu Dec 9, 2021
f09a48b
fixed MacOS bad assembly test by using PythonDLL (which is never a .N…
lostmsu Dec 9, 2021
3794fea
Merge branch 'master' into precise-API-references
lostmsu Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
switched module.cs to the new style references
  • Loading branch information
lostmsu committed Oct 18, 2021
commit 49124fc141a76dabb818466d393d94835463a3cf
106 changes: 52 additions & 54 deletions src/runtime/module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,30 @@ namespace Python.Runtime
{
public class PyModule : PyObject
{
/// <summary>
/// the variable dict of the module. Borrowed.
/// </summary>
internal readonly IntPtr variables;
internal BorrowedReference VarsRef => new BorrowedReference(variables);

public PyModule(string name = "")
: this(Create(name ?? throw new ArgumentNullException(nameof(name))))
internal BorrowedReference variables => VarsRef;
internal BorrowedReference VarsRef
{
get
{
var vars = Runtime.PyModule_GetDict(Reference);
PythonException.ThrowIfIsNull(vars);
return vars;
}
}

public PyModule(string name, string? fileName = null) : this(Create(name, fileName)) { }
public PyModule(string name = "") : this(Create(name))
{
InitializeBuiltins();
}

static StolenReference Create(string name, string? filename = null)
static StolenReference Create(string name)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}

NewReference op = Runtime.PyModule_New(name);
PythonException.ThrowIfIsNull(op);

if (filename is not null)
{
BorrowedReference globals = Runtime.PyModule_GetDict(op);
PythonException.ThrowIfIsNull(globals);
using var pyFileName = filename.ToPython();
int rc = Runtime.PyDict_SetItemString(globals, "__file__", pyFileName.Reference);
PythonException.ThrowIfIsNotZero(rc);
}

return op.Steal();
return Runtime.PyModule_New(name).StealOrThrow();
}

internal PyModule(in StolenReference reference) : base(reference)
Expand All @@ -48,16 +39,17 @@ internal PyModule(in StolenReference reference) : base(reference)
{
throw new ArgumentException("object is not a module");
}
//Refcount of the variables not increase
variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress();
PythonException.ThrowIfIsNull(variables);
}

private void InitializeBuiltins()
{
int res = Runtime.PyDict_SetItem(
VarsRef, PyIdentifier.__builtins__,
Runtime.PyEval_GetBuiltins()
);
PythonException.ThrowIfIsNotZero(res);
}

internal PyModule(BorrowedReference reference) : this(new NewReference(reference).Steal())
{
}
Expand All @@ -71,8 +63,7 @@ public static PyObject Import(string name)
if (name is null) throw new ArgumentNullException(nameof(name));

NewReference op = Runtime.PyImport_ImportModule(name);
PythonException.ThrowIfIsNull(op);
return IsModule(op) ? new PyModule(op.Steal()) : op.MoveToPyObject();
return IsModule(op.BorrowOrThrow()) ? new PyModule(op.Steal()) : op.MoveToPyObject();
}

/// <summary>
Expand All @@ -81,20 +72,17 @@ public static PyObject Import(string name)
public PyModule Reload()
{
NewReference op = Runtime.PyImport_ReloadModule(this.Reference);
PythonException.ThrowIfIsNull(op);
return new PyModule(op.Steal());
return new PyModule(op.StealOrThrow());
}

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(m.Steal());
NewReference m = Runtime.PyImport_ExecCodeModule(name, c.BorrowOrThrow());
return new PyModule(m.StealOrThrow());
}

public void SetBuiltins(PyDict builtins)
public PyModule SetBuiltins(PyDict builtins)
{
if (builtins == null || builtins.IsNone())
{
Expand All @@ -105,6 +93,7 @@ public void SetBuiltins(PyDict builtins)
PythonException.ThrowIfIsNull(globals);
int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", builtins.Reference);
PythonException.ThrowIfIsNotZero(rc);
return this;
}

public static PyDict SysModules
Expand Down Expand Up @@ -157,7 +146,9 @@ public PyObject Import(string name, string? asname = null)
/// </summary>
public void Import(PyModule module, string asname)
{
this.SetPyValue(asname, module.Handle);
if (module is null) throw new ArgumentNullException(nameof(module));
if (asname is null) throw new ArgumentNullException(nameof(asname));
this.SetPyValue(asname, module);
}

/// <summary>
Expand All @@ -166,6 +157,8 @@ public void Import(PyModule module, string asname)
/// </summary>
public void Import(PyObject module, string? asname = null)
{
if (module is null) throw new ArgumentNullException(nameof(module));

asname ??= module.GetAttr("__name__").As<string>();
Set(asname, module);
}
Expand All @@ -175,6 +168,8 @@ public void Import(PyObject module, string? asname = null)
/// </summary>
public void ImportAll(PyModule module)
{
if (module is null) throw new ArgumentNullException(nameof(module));

int result = Runtime.PyDict_Update(VarsRef, module.VarsRef);
if (result < 0)
{
Expand Down Expand Up @@ -206,6 +201,8 @@ public void ImportAll(PyObject module)
/// </summary>
public void ImportAll(PyDict dict)
{
if (dict is null) throw new ArgumentNullException(nameof(dict));

int result = Runtime.PyDict_Update(VarsRef, dict.Reference);
if (result < 0)
{
Expand All @@ -222,11 +219,13 @@ public void ImportAll(PyDict dict)
/// </remarks>
public PyObject Execute(PyObject script, PyDict? locals = null)
{
if (script is null) throw new ArgumentNullException(nameof(script));

Check();
IntPtr _locals = locals == null ? variables : locals.obj;
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, variables, _locals);
BorrowedReference _locals = locals == null ? variables : locals.obj;
using var ptr = Runtime.PyEval_EvalCode(script, variables, _locals);
PythonException.ThrowIfIsNull(ptr);
return new PyObject(ptr);
return ptr.MoveToPyObject();
}

/// <summary>
Expand Down Expand Up @@ -254,6 +253,8 @@ public T Execute<T>(PyObject script, PyDict? locals = null)
/// </remarks>
public PyObject Eval(string code, PyDict? locals = null)
{
if (code is null) throw new ArgumentNullException(nameof(code));

Check();
BorrowedReference _locals = locals == null ? VarsRef : locals.Reference;

Expand Down Expand Up @@ -285,11 +286,12 @@ public T Eval<T>(string code, PyDict? locals = null)
/// <remarks>
/// Exec a Python script and save its local variables in the current local variable dict.
/// </remarks>
public void Exec(string code, PyDict? locals = null)
public PyModule Exec(string code, PyDict? locals = null)
{
Check();
BorrowedReference _locals = locals == null ? VarsRef : locals.Reference;
Exec(code, VarsRef, _locals);
return this;
}

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

IntPtr _value = Converter.ToPython(value, value?.GetType());
SetPyValue(name, _value);
Runtime.XDecref(_value);
using var _value = Converter.ToPython(value, value?.GetType() ?? typeof(object));
SetPyValue(name, _value.Borrow());
return this;
}

private void SetPyValue(string name, IntPtr value)
private void SetPyValue(string name, BorrowedReference value)
{
Check();
using (var pyKey = new PyString(name))
Expand All @@ -335,7 +337,7 @@ private void SetPyValue(string name, IntPtr value)
/// <remarks>
/// Remove a variable from the variables dict.
/// </remarks>
public void Remove(string name)
public PyModule Remove(string name)
{
if (name is null) throw new ArgumentNullException(nameof(name));

Expand All @@ -348,6 +350,7 @@ public void Remove(string name)
throw PythonException.ThrowLastAsClrException();
}
}
return this;
}

/// <summary>
Expand Down Expand Up @@ -398,13 +401,8 @@ public bool TryGet(string name, out PyObject? value)
{
if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0)
{
IntPtr op = Runtime.PyObject_GetItem(variables, pyKey.obj);
if (op == IntPtr.Zero)
{
throw PythonException.ThrowLastAsClrException();
}

value = new PyObject(op);
using var op = Runtime.PyObject_GetItem(variables, pyKey.obj);
value = new PyObject(op.StealOrThrow());
return true;
}
else
Expand Down Expand Up @@ -467,7 +465,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)

private void Check()
{
if (this.obj == IntPtr.Zero)
if (this.rawPtr == IntPtr.Zero)
{
throw new ObjectDisposedException(nameof(PyModule));
}
Expand Down