Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Call PyErr_NormalizeException for exceptions#1265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 17 commits intopythonnet:masterfromslide:normalize_exceptions
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
84197d4
Call PyErr_NormalizeException for exceptions
slideOct 15, 2020
eddd657
Add CHANGELOG entry
slideOct 15, 2020
a923e99
Add test
slideOct 15, 2020
9b3d14b
Localize call to PyErr_NormalizeException to the Format method.
slideOct 15, 2020
cc584dd
Merge branch 'master' into normalize_exceptions
slideNov 12, 2020
d1fe930
Merge branch 'master' into normalize_exceptions
slideNov 17, 2020
4229a38
Merge branch 'master' into normalize_exceptions
filmorNov 18, 2020
04c8b66
Merge branch 'master' into normalize_exceptions
slideNov 20, 2020
95940aa
Merge branch 'master' into normalize_exceptions
filmorDec 10, 2020
7a5ded5
Merge branch 'master' into normalize_exceptions
slideDec 10, 2020
092e961
incref before the call to PyErr_NormalizeException
slideNov 12, 2020
caa0922
Add test for __init__ that causes exception
slideDec 11, 2020
c28951f
Fix test for exception during normalization
slideDec 14, 2020
4654330
Merge branch 'master' into normalize_exceptions
slideDec 14, 2020
62a0b00
Merge branch 'master' into normalize_exceptions
slideDec 18, 2020
4ac9c48
Fixup test
slideDec 19, 2020
946aa1f
Merge branch 'normalize_exceptions' of github.com:slide/pythonnet int…
slideDec 19, 2020
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
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,6 +40,7 @@ details about the cause of the failure
- Indexers can now be used with interface objects
- Fixed a bug where indexers could not be used if they were inherited
- Made it possible to use `__len__` also on `ICollection<>` interface objects
- Fixed issue when calling PythonException.Format where another exception would be raise for unnormalized exceptions
- Made it possible to call `ToString`, `GetHashCode`, and `GetType` on inteface objects
- Fixed objects returned by enumerating `PyObject` being disposed too soon
- Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException
Expand Down
47 changes: 46 additions & 1 deletionsrc/embed_tests/TestPythonException.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,9 +86,54 @@ public void TestPythonExceptionFormatNoTraceback()
}
catch (PythonException ex)
{
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
Assert.AreEqual(ex.StackTrace, ex.Format());
}
}

[Test]
public void TestPythonExceptionFormatNormalized()
{
try
{
PythonEngine.Exec("a=b\n");
}
catch (PythonException ex)
{
Assert.AreEqual("Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\nNameError: name 'b' is not defined\n", ex.Format());
}
}

[Test]
public void TestPythonException_PyErr_NormalizeException()
{
using (var scope = Py.CreateScope())
{
scope.Exec(@"
class TestException(NameError):
def __init__(self, val):
super().__init__(val)
x = int(val)");
Assert.IsTrue(scope.TryGet("TestException", out PyObject type));

PyObject str = "dummy string".ToPython();
IntPtr typePtr = type.Handle;
IntPtr strPtr = str.Handle;
IntPtr tbPtr = Runtime.Runtime.None.Handle;
Runtime.Runtime.XIncref(typePtr);
Runtime.Runtime.XIncref(strPtr);
Runtime.Runtime.XIncref(tbPtr);
Runtime.Runtime.PyErr_NormalizeException(ref typePtr, ref strPtr, ref tbPtr);

using (PyObject typeObj = new PyObject(typePtr), strObj = new PyObject(strPtr), tbObj = new PyObject(tbPtr))
{
// the type returned from PyErr_NormalizeException should not be the same type since a new
// exception was raised by initializing the exception
Assert.AreNotEqual(type.Handle, typePtr);
// the message should now be the string from the throw exception during normalization
Assert.AreEqual("invalid literal for int() with base 10: 'dummy string'", strObj.ToString());
}
}
}
}
}
18 changes: 12 additions & 6 deletionssrc/runtime/pythonexception.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -160,12 +160,18 @@ public string Format()
{
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
{
Runtime.XIncref(_pyType);
Runtime.XIncref(_pyValue);
Runtime.XIncref(_pyTB);
using (PyObject pyType = new PyObject(_pyType))
using (PyObject pyValue = new PyObject(_pyValue))
using (PyObject pyTB = new PyObject(_pyTB))
IntPtr tb = _pyTB;
IntPtr type = _pyType;
IntPtr value = _pyValue;

Runtime.XIncref(type);
Runtime.XIncref(value);
Runtime.XIncref(tb);
Runtime.PyErr_NormalizeException(ref type, ref value, ref tb);

using (PyObject pyType = new PyObject(type))
using (PyObject pyValue = new PyObject(value))
using (PyObject pyTB = new PyObject(tb))
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
{
var buffer = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2035,7 +2035,7 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
internal static extern int PyErr_GivenExceptionMatches(IntPtr ob, IntPtr val);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void PyErr_NormalizeException(IntPtr ob, IntPtr val, IntPtr tb);
internal static extern void PyErr_NormalizeException(refIntPtr ob,refIntPtr val, ref IntPtr tb);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyErr_Occurred();
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp