Handle bad paths in `sys.path` by lostmsu · Pull Request #2383 · pythonnet/pythonnet · GitHub
Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
7 changes: 7 additions & 0 deletions src/runtime/AssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ static IEnumerable<string> FindAssemblyCandidates(string name)
}
else
{
int invalidCharIndex = head.IndexOfAny(Path.GetInvalidPathChars());
if (invalidCharIndex >= 0)
{
using var importWarning = Runtime.PyObject_GetAttrString(Exceptions.exceptions_module, "ImportWarning");
Exceptions.warn($"Path entry '{head}' has invalid char at position {invalidCharIndex}", importWarning.BorrowOrThrow());
continue;
}
path = Path.Combine(head, name);
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static void warn(string message, BorrowedReference exception, int stackle
}

using var warn = Runtime.PyObject_GetAttrString(warnings_module.obj, "warn");
Exceptions.ErrorCheck(warn.Borrow());
warn.BorrowOrThrow();

using var argsTemp = Runtime.PyTuple_New(3);
BorrowedReference args = argsTemp.BorrowOrThrow();
Expand All @@ -283,7 +283,7 @@ public static void warn(string message, BorrowedReference exception, int stackle
Runtime.PyTuple_SetItem(args, 2, level.StealOrThrow());

using var result = Runtime.PyObject_CallObject(warn.Borrow(), args);
Exceptions.ErrorCheck(result.Borrow());
result.BorrowOrThrow();
}

public static void warn(string message, BorrowedReference exception)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_module.py