8000 Improved AttributeError when looking for a symbol that does not exist… · pythonnet/pythonnet@7980fc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7980fc4

Browse files
committed
Improved AttributeError when looking for a symbol that does not exist inside a .NET module.
1 parent 2449bd2 commit 7980fc4

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

src/python_tests_runner/PythonTestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class PythonTestRunner
1818
public void SetUp()
1919
{
2020
Python.Runtime.Runtime.PythonDLL =
21-
"/Library/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib";
21+
"C:\\Python37.2\\python37.dll";
2222
PythonEngine.Initialize();
2323
}
2424

src/runtime/Types/ClassDerived.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ public class PythonDerivedType
871871
internal static Dictionary<PyObject, List<PyTuple>> methodAssoc = new Dictionary<PyObject, List<PyTuple>>();
872872
public static void PushAttribute(PyObject obj)
873873
{
874+
using var _ = Py.GIL();
874875
var tp = new PyTuple(obj);
875876
attributesStack.Add(tp);
876877
}

src/runtime/Types/ModuleObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public static NewReference tp_getattro(BorrowedReference ob, BorrowedReference k
339339

340340
if (attr.IsNull())
341341
{
342-
Exceptions.SetError(Exceptions.AttributeError, name);
342+
Exceptions.SetError(Exceptions.AttributeError, $"name '{name}' is not defined in module '{self.moduleName}'.");
343343
return default;
344344
}
345345

src/testing/subclasstest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ public interface ISimpleInterface
130130
{
131131
bool Ok();
132132
}
133-
133+
public interface ISimpleInterface2
134+
{
135+
int Execute(CancellationToken token);
136+
}
134137
public class TestAttributeAttribute: Attribute
135138
{
136139
public int X { get; set; }
@@ -168,6 +171,10 @@ public static void TestObject(object obj)
168171
if (!si.Ok())
169172
throw new Exception();
170173

174+
}else if (obj is ISimpleInterface2 si2)
175+
{
176+
si2.Execute(CancellationToken.None);
177+
171178
}
172179
else
173180
{

tests/test_subclass.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
from System import (Console, Attribute, Double)
1111
from System.Diagnostics import (DebuggerDisplay, DebuggerDisplayAttribute, Debug)
1212
from System.ComponentModel import (Browsable, BrowsableAttribute)
13+
from System.Threading import (CancellationToken)
1314
import pytest
1415
from Python.Test import (IInterfaceTest, SubClassTest, EventArgsTest,
15-
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute, TestAttributeAttribute)
16+
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute, TestAttributeAttribute, ISimpleInterface2)
17+
import Python.Test
1618
from System.Collections.Generic import List
1719

1820

@@ -290,12 +292,26 @@ def Ok(self):
290292
class DualSubClass2(ISimpleInterface):
291293
def Ok(self):
292294
return True
295+
class DualSubClass3(ISimpleInterface2):
296+
def Execute(self, cancellationToken):
297+
return 0
298+
try:
299+
class DualSubClass4(Python.Test.ISimpleInterface3):
300+
def Execute(self, cancellationToken):
301+
return 0
302+
assert False # An exception should be thrown.
303+
except AttributeError as ae:
304+
assert ("not defined" in str(ae))
293305

294306
obj = DualSubClass()
295307
SimpleClass.TestObject(obj)
296308
obj = DualSubClass2()
297309
SimpleClass.TestObject(obj)
298310

311+
obj2 = DualSubClass3();
312+
SimpleClass.TestObject(obj2)
313+
#obj2.Execute(CancellationToken.None)
314+
299315
def test_class_with_attributes():
300316
import clr
301317
@clr.attribute(Browsable(False))

0 commit comments

Comments
 (0)
0