Skip to content

Commit

Permalink
give users some idea of why we might be unable to reflect .NET types …
Browse files Browse the repository at this point in the history
…to Python for them
  • Loading branch information
lostmsu committed Jul 1, 2024
1 parent 790b7c8 commit dfb46a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/runtime/InternalPythonnetException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Python.Runtime;

public class InternalPythonnetException : Exception
{
public InternalPythonnetException(string message, Exception innerException)
: base(message, innerException) { }
}
31 changes: 19 additions & 12 deletions src/runtime/Types/ReflectedClrType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@ public static ReflectedClrType GetOrCreate(Type type)
return pyType;
}

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

var impl = ClassManager.CreateClass(type);
var impl = ClassManager.CreateClass(type);

TypeManager.InitializeClassCore(type, pyType, impl);
TypeManager.InitializeClassCore(type, pyType, impl);

ClassManager.InitClassBase(type, impl, pyType);
ClassManager.InitClassBase(type, impl, pyType);

// Now we force initialize the Python type object to reflect the given
// managed type, filling the Python type slots with thunks that
// point to the managed methods providing the implementation.
TypeManager.InitializeClass(pyType, impl, type);
// Now we force initialize the Python type object to reflect the given
// managed type, filling the Python type slots with thunks that
// point to the managed methods providing the implementation.
TypeManager.InitializeClass(pyType, impl, type);
}
catch (Exception e)
{
throw new InternalPythonnetException($"Failed to create Python type for {type.FullName}", e);
}

return pyType;
}
Expand Down

0 comments on commit dfb46a6

Please sign in to comment.