Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e8ea7a6
1776 Inherit Generic Virtual Method Bug: Unit Test
rmadsen-ks Apr 27, 2022
691f207
1776 Inherit Generic Virtual Method Bug: Fix
rmadsen-ks Apr 28, 2022
e27ad8c
1774-ClassWithoutnamespace
rmadsen-ks Apr 29, 2022
f1fa696
better support for multiple inheritance
rmadsen-ks May 3, 2022
6f0b354
Fixed issue with protected constructors and classes with no constructor
rmadsen-ks May 3, 2022
3963621
Added supporr for class/property/method attributes.
rmadsen-ks May 5, 2022
4e0d003
- Expanded the way attributes amy be added
rmadsen-ks May 5, 2022
ea598a2
added support for creating abstract classes using a __clr_abstract__ …
rmadsen-ks May 6, 2022
d6abbb2
Improved AttributeError when looking for a symbol that does not exist…
rmadsen-ks May 24, 2022
2a84675
Added test to detect an issue with object construction.
rmadsen-ks Jun 28, 2022
172c642
got rid of a few deprecation warnings that pollute GitHub code review
lostmsu Jun 30, 2022
32d15eb
docs: Fix a few typos
timgates42 Jul 16, 2022
9eaf35f
Added support for marking properties with python types.
rmadsen-ks Sep 2, 2022
9ed94f6
Fixed issue calling base-base class implementation of methods.
rmadsen-ks Sep 2, 2022
da146d9
Merged with Pythonnet 3.0 RC.6.
rmadsen-ks Oct 28, 2022
b280b1b
Fixed bug related to converting number to a string
rmadsen-ks Oct 28, 2022
61c5d99
Added support for Python 3.11.
rmadsen-ks Oct 28, 2022
ed89819
Merge remote-tracking branch 'github/master'
rmadsen-ks Nov 14, 2023
56c8a39
Merge branch 'master' of github.com:pythonnet/pythonnet
rmadsen-ks Nov 14, 2023
e986114
Merge remote-tracking branch 'github/master'
rmadsen-ks Aug 1, 2024
be255ee
Merge branch 'master' into testbranch
filmor Dec 8, 2025
1dfa942
Merge branch 'master' into testbranch
filmor Apr 19, 2026
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
Added support for marking properties with python types.
Added support for specifying attributes on property-methods (eg. get/set attributes).
  • Loading branch information
rmadsen-ks committed Oct 26, 2022
commit 9eaf35ff4531a697f6513e50ebd4a44947b33f68
8 changes: 4 additions & 4 deletions src/runtime/PythonTypes/PyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class PyObject : DynamicObject, IDisposable, ISerializable
/// Trace stack for PyObject's construction
/// </summary>
public StackTrace Traceback { get; } = new StackTrace(1);
#endif
#endif

protected IntPtr rawPtr = IntPtr.Zero;
internal readonly int run = Runtime.GetRun();
Expand Down Expand Up @@ -165,7 +165,7 @@ public static PyObject FromManagedObject(object ob)
{
if (!Converter.ToManaged(obj, t, out var result, true))
{
throw new InvalidCastException("cannot convert object to target type",
throw new InvalidCastException($"Cannot convert object to target type '{t}'.",
PythonException.FetchCurrentOrNull(out _));
}
return result;
Expand Down Expand Up @@ -235,7 +235,7 @@ public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);

}

internal StolenReference Steal()
Expand Down Expand Up @@ -1314,7 +1314,7 @@ private bool TryCompare(PyObject arg, int op, out object @out)
}
return true;
}

public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object? result)
{
using var _ = Py.GIL();
Expand Down
27 changes: 27 additions & 0 deletions src/runtime/PythonTypes/PythonTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Python.Runtime;

/// <summary>
/// Marks a property type with a specific python type. Normally, properties has .NET types, but if the property has a python type,
/// that cannot be represented in the propert type info, so this attribute is used to mark the property with the corresponding python type.
/// </summary>
public class PythonTypeAttribute : Attribute
{
/// <summary> Type name. </summary>
public string TypeName { get; }

/// <summary> Importable module name. </summary>
public string Module { get; }

/// <summary>
/// Creates a new instance of PythonTypeAttribute.
/// </summary>
/// <param name="pyTypeModule"></param>
/// <param name="pyTypeName"></param>
public PythonTypeAttribute(string pyTypeModule, string pyTypeName)
{
TypeName = pyTypeName;
Module = pyTypeModule;
}
}
12 changes: 9 additions & 3 deletions src/runtime/Resources/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def add_attribute(self, *args, **kwargs):

class property(object):

def __init__(self, type, default):
def __init__(self, type, default = None):
import weakref
self._clr_property_type_ = type
self.default = default
Expand All @@ -70,9 +70,14 @@ def __init__(self, type, default):
self.fget = 1
self.fset = 1
def __get__(self, instance, owner):
if self.fget != 1:
return self.fget(instance)
v = self.values.get(instance, self.default)
return v
def __set__(self, instance, value):
if self.fset != 1:
self.fset(instance,value)
return
self.values[instance] = value
def add_attribute(self, *args, **kwargs):
lst = []
Expand All @@ -84,8 +89,9 @@ def add_attribute(self, *args, **kwargs):
self._clr_attributes_.extend(lst)
return self

def __call__(self, type, default):
self2 = self.__class__(self._clr_property_type_, type, default)
def __call__(self, func):
self2 = self.__class__(self._clr_property_type_, None)
self2.fget = func
self2._clr_attributes_ = self._clr_attributes_
return self2
class clrmethod(object):
Expand Down
52 changes: 45 additions & 7 deletions src/runtime/Types/ClassDerived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ static CustomAttributeBuilder AddAttribute(PyObject attr)
// named arguments and named properties or fields the same way.
var tp = new PyTuple(attr);
Type attribute = (Type) tp[0].AsManagedObject(typeof(object));
if (typeof(Attribute).IsAssignableFrom(attribute) == false)
{
throw new Exception("This type is not an attribute type.");
}

var args = (PyObject[]) tp[1].AsManagedObject(typeof(PyObject[]));
var dict = new PyDict(tp[2]);
Expand All @@ -168,9 +172,10 @@ static CustomAttributeBuilder AddAttribute(PyObject attr)
if (parameter.ParameterType.IsArray && null != parameter.GetCustomAttribute(typeof(ParamArrayAttribute)))
{
int cnt = args.Length - i;
var values = Array.CreateInstance(parameter.ParameterType.GetElementType(), cnt);
var elemType = parameter.ParameterType.GetElementType();
var values = Array.CreateInstance(elemType, cnt);
for(int j = 0; j < cnt; j++)
values.SetValue(args[i + j], j);
values.SetValue(args[i + j].AsManagedObject(typeof(object)), j);
paramValues.Add(values);
break;
}
Expand Down Expand Up @@ -750,28 +755,61 @@ private static void AddPythonProperty(string propertyName, PyObject func, TypeBu
MethodAttributes.SpecialName;

using var pyPropertyType = func.GetAttr("_clr_property_type_");
var propertyType = pyPropertyType.AsManagedObject(typeof(Type)) as Type;
var pyNativeType = new PyType(pyPropertyType);
Converter.ToManaged(pyPropertyType, typeof(Type), out var result, false);
var propertyType = result as Type;
string pyTypeName = null;
string pyTypeModule = null;
if (propertyType == null)
{
throw new ArgumentException("_clr_property_type must be a CLR type");
propertyType = typeof(PyObject);
pyTypeModule = pyNativeType.GetAttr("__module__").ToString();
//throw new ArgumentException("_clr_property_type must be a CLR type");
pyTypeName = pyNativeType.Name;
}

PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName,
PropertyAttributes.None,
propertyType,
null);
if (func.HasAttr("_clr_attributes_"))
if (func.HasAttr("_clr_attributes_"))
{
using (var attributes = new PyList(func.GetAttr("_clr_attributes_")))
{
foreach (var attr in attributes)
{
var builder = AddAttribute(attr);
if (builder == null) throw new Exception();
propertyBuilder.SetCustomAttribute(builder);
}
}
}

if (pyTypeName != null)
{
var cb = new CustomAttributeBuilder(typeof(PythonTypeAttribute).GetConstructors().First(),
new object[] {pyTypeModule, pyTypeName});
propertyBuilder.SetCustomAttribute(cb);
}

{
// load attribute set on functions
foreach (var fname in new[]{ "fget", "fset" })
{
using (var attributes = new PyList(func.GetAttr("_clr_attributes_")))
using var pyfget = func.GetAttr(fname);
if (methodAssoc.TryGetValue(pyfget, out var lst))
{
foreach (var attr in attributes)
foreach (var attr in lst)
{
var builder = AddAttribute(attr);
if (builder == null) throw new Exception();
propertyBuilder.SetCustomAttribute(builder);
}

methodAssoc.Remove(func);
}
}
}

if (func.HasAttr("fget"))
{
Expand Down