Skip to content

Commit

Permalink
handle bad paths in sys.path
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed May 10, 2024
1 parent 32051cb commit 618e84a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Fixed

- Fixed RecursionError for reverse operators on C# operable types from python. See #2240
- Fixed probing for assemblies in `sys.path` failing when a path in `sys.path` has invalid characters. See #2376

## [3.0.3](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.3) - 2023-10-11

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/AssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ static IEnumerable<string> FindAssemblyCandidates(string name)
}
else
{
int invalidCharIndex = head.IndexOfAny(Path.GetInvalidPathChars());
if (invalidCharIndex >= 0)
{
Exceptions.warn($"Path entry '{head}' has invalid char at position {invalidCharIndex}", Exceptions.ValueError);
continue;
}
path = Path.Combine(head, name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ def test_clr_add_reference():
with pytest.raises(FileNotFoundException):
AddReference("somethingtotallysilly")


def test_clr_add_reference_bad_path():
import sys
from clr import AddReference
from System.IO import FileNotFoundException
bad_path = "hello\0world"
sys.path.append(bad_path)
try:
with pytest.raises(FileNotFoundException):
AddReference("test_clr_add_reference_bad_path")
finally:
sys.path.remove(bad_path)


def test_clr_get_clr_type():
"""Test clr.GetClrType()."""
from clr import GetClrType
Expand Down

0 comments on commit 618e84a

Please sign in to comment.