Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
109 changes: 0 additions & 109 deletions src/runtime/AttributeErrorHint.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.55</Version>
<Version>2.0.56</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
7 changes: 0 additions & 7 deletions src/runtime/PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,6 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
}

ImportHook.UpdateCLRModuleDict();

// Set up the miss-only __getattr__ hook used to enrich AttributeError
// messages on reflected .NET types with member-name suggestions.
AttributeErrorHint.Initialize();
}

static BorrowedReference DefineModule(string name)
Expand Down Expand Up @@ -373,9 +369,6 @@ public static void Shutdown()
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;

ExecuteShutdownHandlers();

AttributeErrorHint.Shutdown();

// Remember to shut down the runtime.
Runtime.Shutdown();

Expand Down
4 changes: 0 additions & 4 deletions src/runtime/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@ internal static void InitializeClass(PyType type, ClassBase impl, Type clrType)

Runtime.PyType_Modified(type.Reference);

// Enrich AttributeError messages for missing attributes with member-name
// suggestions, via a miss-only __getattr__ hook (no hot-path cost).
AttributeErrorHint.Install(type.Reference);

//DebugUtil.DumpType(type);
}

Expand Down
73 changes: 11 additions & 62 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,20 @@ internal static void AppendAttributeErrorSuggestions(BorrowedReference ob, Borro
}

var name = Runtime.GetManagedString(key);
if (string.IsNullOrEmpty(name))
// Skip empty and dunder names: the latter are probed internally by CPython
// (e.g. __iter__, __len__) and are never user-facing typos worth helping with.
if (string.IsNullOrEmpty(name) || name.StartsWith("__", StringComparison.Ordinal))
{
return;
}

var hint = GetSuggestionHint(ob, name);
if (hint.Length == 0)
if (GetManagedObject(ob) is not CLRObject clrObj || clrObj.inst is null)
{
return;
}

var suggestions = GetSimilarMemberNames(clrObj.inst.GetType(), name);
if (suggestions.Count == 0)
{
return;
}
Expand All @@ -644,6 +651,7 @@ internal static void AppendAttributeErrorSuggestions(BorrowedReference ob, Borro
try
{
var baseMessage = GetErrorMessage(errValue.BorrowNullable(), name);
var hint = " Did you mean: " + string.Join(", ", suggestions.Select(s => $"'{s}'")) + "?";
Exceptions.SetError(Exceptions.AttributeError, baseMessage + hint);
}
finally
Expand All @@ -654,65 +662,6 @@ internal static void AppendAttributeErrorSuggestions(BorrowedReference ob, Borro
}
}

/// <summary>
/// Builds the full message for an <c>AttributeError</c> raised for a missing
/// attribute on a .NET object, including any "Did you mean ...?" hint. Used by
/// the miss-only <c>__getattr__</c> hook installed on reflected types (see
/// <see cref="AttributeErrorHint"/>), where the original error has already been
/// cleared, so the base message is reconstructed here.
/// </summary>
internal static string BuildMissingAttributeMessage(PyObject self, string name)
{
var typeName = "object";
try
{
using var pyType = self.GetPythonType();
typeName = pyType.Name;
}
catch
{
// fall back to the generic type name
}

var message = $"'{typeName}' object has no attribute '{name}'";
try
{
return message + GetSuggestionHint(self.Reference, name);
}
catch
{
// never let suggestion building turn into a different exception
return message;
}
}

/// <summary>
/// Returns " Did you mean: 'x', 'y'?" listing similarly-named members of the
/// managed object, or an empty string when there is nothing to suggest. Dunder
/// names are skipped: they are probed internally by CPython (e.g. __iter__,
/// __len__) and are never user-facing typos worth helping with.
/// </summary>
private static string GetSuggestionHint(BorrowedReference ob, string name)
{
if (string.IsNullOrEmpty(name) || name.StartsWith("__", StringComparison.Ordinal))
{
return string.Empty;
}

if (GetManagedObject(ob) is not CLRObject clrObj || clrObj.inst is null)
{
return string.Empty;
}

var suggestions = GetSimilarMemberNames(clrObj.inst.GetType(), name);
if (suggestions.Count == 0)
{
return string.Empty;
}

return " Did you mean: " + string.Join(", ", suggestions.Select(s => $"'{s}'")) + "?";
}

private static string GetErrorMessage(BorrowedReference value, string fallbackName)
{
if (value != null)
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/Types/ClassObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ public override void InitializeSlots(BorrowedReference pyType, SlotsHolder slots
protected virtual NewReference NewObjectToPython(object obj, BorrowedReference tp)
=> CLRObject.GetReference(obj, tp);

/// <summary>
/// Type __getattro__ implementation. Delegates to the generic CLR attribute
/// lookup, but enriches the AttributeError raised for a missing attribute with
/// suggestions of similarly-named members of the managed type.
/// </summary>
public static NewReference tp_getattro(BorrowedReference ob, BorrowedReference key)
{
var result = Runtime.PyObject_GenericGetAttr(ob, key);
if (result.IsNull())
{
AppendAttributeErrorSuggestions(ob, key);
}
return result;
}

private static NewReference NewEnum(Type type, BorrowedReference args, BorrowedReference tp)
{
nint argCount = Runtime.PyTuple_Size(args);
Expand Down
Loading