Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 201137a

Browse files
sdpythontonyroberts
authored andcommitted
fix an issue with Python 3.4, it does not crash anynore when throwing an exception
1 parent 14591b6 commit 201137a

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

pythonnet/src/runtime/exceptions.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,21 @@ internal unsafe static void ErrorOccurredCheck(IntPtr pointer) {
222222
///
223223
/// </remarks>
224224
internal static void SetupExceptionHack() {
225+
DebugUtil.Print("SetupExceptionHack");
225226
ns_exc = ClassManager.GetClass(typeof(Exception)).pyHandle;
226227
cache = new Hashtable();
227228

229+
string module_exception =
230+
#if(PYTHON34)
231+
"builtins"
232+
#else
233+
"exceptions"
234+
#endif
235+
;
236+
228237
string code =
229-
"import exceptions\n" +
230-
"class Exception(exceptions.Exception):\n" +
238+
"import {0}\n" +
239+
"class Exception({0}.Exception):\n" +
231240
" _class = None\n" +
232241
" _inner = None\n" +
233242
" \n" +
@@ -239,7 +248,7 @@ internal static void SetupExceptionHack() {
239248
" def __init__(self, *args, **kw):\n" +
240249
" inst = self.__class__._class(*args, **kw)\n" +
241250
" self.__dict__['_inner'] = inst\n" +
242-
" exceptions.Exception.__init__(self, *args, **kw)\n" +
251+
" {0}.Exception.__init__(self, *args, **kw)\n" +
243252
"\n" +
244253
" def __getattr__(self, name, _marker=[]):\n" +
245254
" inner = self.__dict__['_inner']\n" +
@@ -268,6 +277,8 @@ internal static void SetupExceptionHack() {
268277
" name = self.__class__.__name__\n" +
269278
" return '%s(\\'%s\\',)' % (name, msg) \n" +
270279
"\n";
280+
code = string.Format(code, module_exception);
281+
DebugUtil.Print(code);
271282

272283
IntPtr dict = Runtime.PyDict_New();
273284

pythonnet/src/runtime/interop.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static ObjectOffset() {
7979
}
8080

8181
public static int magic(IntPtr ob) {
82-
#if (PYTHON32 || PYTHON33 || PYTHON33)
82+
#if (PYTHON32 || PYTHON33 || PYTHON34)
8383
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
8484
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
8585
{
@@ -91,7 +91,7 @@ public static int magic(IntPtr ob) {
9191

9292
public static int DictOffset(IntPtr ob)
9393
{
94-
#if (PYTHON32 || PYTHON33 || PYTHON33)
94+
#if (PYTHON32 || PYTHON33 || PYTHON34)
9595
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
9696
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
9797
{
@@ -102,7 +102,7 @@ public static int DictOffset(IntPtr ob)
102102
}
103103

104104
public static int Size(IntPtr ob) {
105-
#if (PYTHON32 || PYTHON33 || PYTHON33)
105+
#if (PYTHON32 || PYTHON33 || PYTHON34)
106106
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
107107
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
108108
{

pythonnet/src/runtime/runtime.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,18 @@ internal static void Initialize() {
199199
// of the Python runtime that do not allow new-style classes to
200200
// be used as exceptions (Python versions 2.4 and lower).
201201

202-
#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34)
202+
#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 )
203203
wrap_exceptions = false;
204204
#else
205-
IntPtr m = PyImport_ImportModule("exceptions");
205+
206+
IntPtr m = PyImport_ImportModule(
207+
#if(PYTHON34)
208+
"builtins"
209+
#else
210+
"exceptions"
211+
#endif
212+
);
213+
206214
Exceptions.ErrorCheck(m);
207215
op = Runtime.PyObject_GetAttrString(m, "Exception");
208216
Exceptions.ErrorCheck(op);

0 commit comments

Comments
 (0)