@@ -86,9 +86,54 @@ public void TestPythonExceptionFormatNoTraceback()
86
86
}
87
87
catch ( PythonException ex )
88
88
{
89
- // ImportError/ModuleNotFoundError do not have a traceback when not running in a script
89
+ // ImportError/ModuleNotFoundError do not have a traceback when not running in a script
90
90
Assert . AreEqual ( ex . StackTrace , ex . Format ( ) ) ;
91
91
}
92
92
}
93
+
94
+ [ Test ]
95
+ public void TestPythonExceptionFormatNormalized ( )
96
+ {
97
+ try
98
+ {
99
+ PythonEngine . Exec ( "a=b\n " ) ;
100
+ }
101
+ catch ( PythonException ex )
102
+ {
103
+ Assert . AreEqual ( "Traceback (most recent call last):\n File \" <string>\" , line 1, in <module>\n NameError: name 'b' is not defined\n " , ex . Format ( ) ) ;
104
+ }
105
+ }
106
+
107
+ [ Test ]
108
+ public void TestPythonException_PyErr_NormalizeException ( )
109
+ {
110
+ using ( var scope = Py . CreateScope ( ) )
111
+ {
112
+ scope . Exec ( @"
113
+ class TestException(NameError):
114
+ def __init__(self, val):
115
+ super().__init__(val)
116
+ x = int(val)" ) ;
117
+ Assert . IsTrue ( scope . TryGet ( "TestException" , out PyObject type ) ) ;
118
+
119
+ PyObject str = "dummy string" . ToPython ( ) ;
120
+ IntPtr typePtr = type . Handle ;
121
+ IntPtr strPtr = str . Handle ;
122
+ IntPtr tbPtr = Runtime . Runtime . None . Handle ;
123
+ Runtime . Runtime . XIncref ( typePtr ) ;
124
+ Runtime . Runtime . XIncref ( strPtr ) ;
125
+ Runtime . Runtime . XIncref ( tbPtr ) ;
126
+ Runtime . Runtime . PyErr_NormalizeException ( ref typePtr , ref strPtr , ref tbPtr ) ;
127
+
128
+ using ( PyObject typeObj = new PyObject ( typePtr ) , strObj = new PyObject ( strPtr ) , tbObj = new PyObject ( tbPtr ) )
129
+ {
130
+ // the type returned from PyErr_NormalizeException should not be the same type since a new
131
+ // exception was raised by initializing the exception
132
+ Assert . AreNotEqual ( type . Handle , typePtr ) ;
133
+ // the message should now be the string from the throw exception during normalization
134
+ Assert . AreEqual ( "invalid literal for int() with base 10: 'dummy string'" , strObj . ToString ( ) ) ;
135
+ }
136
+ }
137
+ }
93
138
}
94
139
}
0 commit comments