Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
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
9 changes: 9 additions & 0 deletions src/python_tests_runner/PythonTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class PythonTestRunner
[OneTimeSetUp]
public void SetUp()
{
Python.Runtime.Runtime.PythonDLL =
"C:\\Python37.2\\python37.dll";
PythonEngine.Initialize();
}

Expand All @@ -36,6 +38,13 @@ static IEnumerable<string[]> PythonTestCases()
yield return new[] { "test_indexer", "test_boolean_indexer" };
yield return new[] { "test_delegate", "test_bool_delegate" };
yield return new[] { "test_subclass", "test_implement_interface_and_class" };
yield return new[] { "test_subclass", "test_virtual_generic_method" };
yield return new[] { "test_subclass", "test_interface_and_class_impl2" };
yield return new[] { "test_subclass", "test_class_with_attributes" };
yield return new[] { "test_subclass", "test_class_with_advanced_attribute" };
yield return new[] { "test_subclass", "test_more_subclasses" };
yield return new[] { "test_subclass", "test_more_subclasses2" };
yield return new[] { "test_subclass", "abstract_test" };
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/runtime/Native/TypeOffset311.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/PythonTypes/PyInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// Represents a Python integer object.
/// See the documentation at https://docs.python.org/3/c-api/long.html
/// </summary>
public partial class PyInt : PyNumber, IFormattable

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.10)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.7)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test ARM64

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.10)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.11)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.12)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.9)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.11)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.8)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.9)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.13)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.12)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.12)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.12)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.9)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.9)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.10)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.10)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.8)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.8)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.7)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.7)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.13)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.7)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.8)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.13)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.11)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 12 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.11)

'PyInt' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
internal PyInt(in StolenReference ptr) : base(ptr)
{
Expand Down Expand Up @@ -228,7 +228,8 @@
public string ToString(string format, IFormatProvider formatProvider)
{
using var _ = Py.GIL();
return ToBigInteger().ToString(format, formatProvider);
object val = Runtime.PyLong_AsLongLong(obj);

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.10)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.7)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test ARM64

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.10)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.11)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.12)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.9)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.11)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.8)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.12)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.12)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.9)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.9)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.8)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.7)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.7)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.13)

Converting null literal or possible null value to non-nullable type.

Check warning on line 231 in src/runtime/PythonTypes/PyInt.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x86, windows-latest, 3.11)

Converting null literal or possible null value to non-nullable type.
return val?.ToString() ?? ToBigInteger().ToString(format, formatProvider);
}

public override TypeCode GetTypeCode() => TypeCode.Int64;
Expand Down
6 changes: 3 additions & 3 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
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;
}
}
98 changes: 92 additions & 6 deletions src/runtime/Resources/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ def test(self):
string z = x.test; // calls into python and returns "x"
"""

def __init__(self, type_, fget=None, fset=None):
def __init__(self, type_, fget=None, fset=None, attributes = []):
self.__name__ = getattr(fget, "__name__", None)
self._clr_property_type_ = type_
self.fget = fget
self.fset = fset

self._clr_attributes_ = attributes
def __call__(self, fget):
return self.__class__(self._clr_property_type_,
self.__class__(self._clr_property_type_,
fget=fget,
fset=self.fset)
fset=self.fset,
attributes = self._clr_attributes_)


def setter(self, fset):
self.fset = fset
Expand All @@ -47,8 +49,51 @@ def __set__(self, instance, value):
if not self.fset:
raise AttributeError("%s is read-only" % self.__name__)
return self.fset.__get__(instance, None)(value)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

class property(object):

def __init__(self, type, default = None):
import weakref
self._clr_property_type_ = type
self.default = default
self.values = weakref.WeakKeyDictionary()
self._clr_attributes_ = []
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 = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

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):
"""
Method decorator for exposing python methods to .NET.
Expand All @@ -67,18 +112,59 @@ def test(self, x):
int z = x.test("hello"); // calls into python and returns len("hello")
"""

def __init__(self, return_type, arg_types, clrname=None, func=None):
def __init__(self, return_type = None, arg_types = [], clrname=None, func=None, **kwargs):
if return_type == None:
import System
return_type = System.Void
self.__name__ = getattr(func, "__name__", None)
self._clr_return_type_ = return_type
self._clr_arg_types_ = arg_types
self._clr_method_name_ = clrname or self.__name__
self.__func = func
if 'attributes' in kwargs:
self._clr_attributes_ = kwargs["attributes"]
else:
self._clr_attributes_ = []

def __call__(self, func):
return self.__class__(self._clr_return_type_,
self2 = self.__class__(self._clr_return_type_,
self._clr_arg_types_,
clrname=self._clr_method_name_,
func=func)
self2._clr_attributes_ = self._clr_attributes_
return self2

def __get__(self, instance, owner):
return self.__func.__get__(instance, owner)

def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

class attribute(object):

def __init__(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
import Python.Runtime
#todo: ensure that attributes only are pushed when @ is used.
self.attr = lst
for item in lst:
Python.Runtime.PythonDerivedType.PushAttribute(item)

def __call__(self, x):
import Python.Runtime
for item in self.attr:
if Python.Runtime.PythonDerivedType.AssocAttribute(item, x):
pass
return x
6 changes: 6 additions & 0 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,12 @@ internal static void SetNoSiteFlag()
return *Delegates.Py_NoSiteFlag;
});
}

internal static uint PyTuple_GetSize(BorrowedReference tuple)
{
IntPtr r = Delegates.PyTuple_Size(tuple);
return (uint)r.ToInt32();
}
}

internal class BadPythonDllException : MissingMethodException
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/StateSerialization/MaybeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ internal struct MaybeType : ISerializable
const string SerializationName = "n";
readonly string name;
readonly Type type;

public string DeletedMessage
{
get
Expand All @@ -38,6 +37,7 @@ public Type Value

public string Name => name;
public bool Valid => type != null;
public Type ValueOrNull => type;

public override string ToString()
{
Expand Down
Loading
Loading