Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb112885

Browse files
authored
handle bad paths in sys.path (#2383)
fixes#2376
1 parent32051cb commitb112885

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ static IEnumerable<string> FindAssemblyCandidates(string name)
200200
}
201201
else
202202
{
203+
intinvalidCharIndex=head.IndexOfAny(Path.GetInvalidPathChars());
204+
if(invalidCharIndex>=0)
205+
{
206+
usingvarimportWarning=Runtime.PyObject_GetAttrString(Exceptions.exceptions_module,"ImportWarning");
207+
Exceptions.warn($"Path entry '{head}' has invalid char at position{invalidCharIndex}",importWarning.BorrowOrThrow());
208+
continue;
209+
}
203210
path=Path.Combine(head,name);
204211
}
205212

‎src/runtime/Exceptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public static void warn(string message, BorrowedReference exception, int stackle
270270
}
271271

272272
usingvarwarn=Runtime.PyObject_GetAttrString(warnings_module.obj,"warn");
273-
Exceptions.ErrorCheck(warn.Borrow());
273+
warn.BorrowOrThrow();
274274

275275
usingvarargsTemp=Runtime.PyTuple_New(3);
276276
BorrowedReferenceargs=argsTemp.BorrowOrThrow();
@@ -283,7 +283,7 @@ public static void warn(string message, BorrowedReference exception, int stackle
283283
Runtime.PyTuple_SetItem(args,2,level.StealOrThrow());
284284

285285
usingvarresult=Runtime.PyObject_CallObject(warn.Borrow(),args);
286-
Exceptions.ErrorCheck(result.Borrow());
286+
result.BorrowOrThrow();
287287
}
288288

289289
publicstaticvoidwarn(stringmessage,BorrowedReferenceexception)

‎tests/test_module.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,20 @@ def test_clr_add_reference():
344344
withpytest.raises(FileNotFoundException):
345345
AddReference("somethingtotallysilly")
346346

347+
348+
deftest_clr_add_reference_bad_path():
349+
importsys
350+
fromclrimportAddReference
351+
fromSystem.IOimportFileNotFoundException
352+
bad_path="hello\0world"
353+
sys.path.append(bad_path)
354+
try:
355+
withpytest.raises(FileNotFoundException):
356+
AddReference("test_clr_add_reference_bad_path")
357+
finally:
358+
sys.path.remove(bad_path)
359+
360+
347361
deftest_clr_get_clr_type():
348362
"""Test clr.GetClrType()."""
349363
fromclrimportGetClrType

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp