Skip to content

Commit 1373cbc

Browse files
committed
handle bad paths in sys.path
fixes pythonnet#2376
1 parent 32051cb commit 1373cbc

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2222
### Fixed
2323

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

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

src/runtime/AssemblyManager.cs

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ static IEnumerable<string> FindAssemblyCandidates(string name)
200200
}
201201
else
202202
{
203+
int invalidCharIndex = head.IndexOfAny(Path.GetInvalidPathChars());
204+
if (invalidCharIndex >= 0)
205+
{
206+
Exceptions.warn($"Path entry '{head}' has invalid char at position {invalidCharIndex}", Exceptions.ValueError);
207+
continue;
208+
}
203209
path = Path.Combine(head, name);
204210
}
205211

tests/test_module.py

+14
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,20 @@ def test_clr_add_reference():
344344
with pytest.raises(FileNotFoundException):
345345
AddReference("somethingtotallysilly")
346346

347+
348+
def test_clr_add_reference_bad_path():
349+
import sys
350+
from clr import AddReference
351+
from System.IO import FileNotFoundException
352+
bad_path = "hello\0world"
353+
sys.path.append(bad_path)
354+
try:
355+
with pytest.raises(FileNotFoundException):
356+
AddReference("test_clr_add_reference_bad_path")
357+
finally:
358+
sys.path.remove(bad_path)
359+
360+
347361
def test_clr_get_clr_type():
348362
"""Test clr.GetClrType()."""
349363
from clr import GetClrType

0 commit comments

Comments
 (0)