Skip to content

Commit c60d250

Browse files
committed
give users some idea of why we might be unable to reflect .NET types to Python for them
1 parent d02b3bf commit c60d250

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Python.Runtime;
4+
5+
public class InternalPythonnetException : Exception
6+
{
7+
public InternalPythonnetException(string message, Exception innerException)
8+
: base(message, innerException) { }
9+
}

src/runtime/Types/ReflectedClrType.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,29 @@ public static ReflectedClrType GetOrCreate(Type type)
3030
return pyType;
3131
}
3232

33-
// Ensure, that matching Python type exists first.
34-
// It is required for self-referential classes
35-
// (e.g. with members, that refer to the same class)
36-
pyType = AllocateClass(type);
37-
ClassManager.cache.Add(type, pyType);
33+
try
34+
{
35+
// Ensure, that matching Python type exists first.
36+
// It is required for self-referential classes
37+
// (e.g. with members, that refer to the same class)
38+
pyType = AllocateClass(type);
39+
ClassManager.cache.Add(type, pyType);
3840

39-
var impl = ClassManager.CreateClass(type);
41+
var impl = ClassManager.CreateClass(type);
4042

41-
TypeManager.InitializeClassCore(type, pyType, impl);
43+
TypeManager.InitializeClassCore(type, pyType, impl);
4244

43-
ClassManager.InitClassBase(type, impl, pyType);
45+
ClassManager.InitClassBase(type, impl, pyType);
4446

45-
// Now we force initialize the Python type object to reflect the given
46-
// managed type, filling the Python type slots with thunks that
47-
// point to the managed methods providing the implementation.
48-
TypeManager.InitializeClass(pyType, impl, type);
47+
// Now we force initialize the Python type object to reflect the given
48+
// managed type, filling the Python type slots with thunks that
49+
// point to the managed methods providing the implementation.
50+
TypeManager.InitializeClass(pyType, impl, type);
51+
}
52+
catch (Exception e)
53+
{
54+
throw new InternalPythonnetException($"Failed to create Python type for {type.FullName}", e);
55+
}
4956

5057
return pyType;
5158
}

0 commit comments

Comments
 (0)