Skip to content

Commit 3f6276d

Browse files
Harden get_module_part() against "." (#2202) (#2203)
(cherry picked from commit bd78ab0) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent bbf9ce4 commit 3f6276d

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ Release date: TBA
1919

2020

2121

22+
What's New in astroid 2.15.6?
23+
=============================
24+
Release date: 2023-05-14
25+
26+
* Harden ``get_module_part()`` against ``"."``.
27+
28+
Closes pylint-dev/pylint#8749
29+
30+
2231
What's New in astroid 2.15.5?
2332
=============================
2433
Release date: 2023-05-14

astroid/modutils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ def get_module_part(dotted_name: str, context_file: str | None = None) -> str:
433433
), "explicit relative import, but no context_file?"
434434
path = [] # prevent resolving the import non-relatively
435435
starti = 1
436-
while parts[starti] == "": # for all further dots: change context
436+
# for all further dots: change context
437+
while starti < len(parts) and parts[starti] == "":
437438
starti += 1
438439
assert (
439440
context_file is not None

tests/test_modutils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def test_get_module_part_exception(self) -> None:
147147
ImportError, modutils.get_module_part, "unknown.module", modutils.__file__
148148
)
149149

150+
def test_get_module_part_only_dot(self) -> None:
151+
self.assertEqual(modutils.get_module_part(".", modutils.__file__), ".")
152+
150153

151154
class ModPathFromFileTest(unittest.TestCase):
152155
"""Given an absolute file path return the python module's path as a list."""

0 commit comments

Comments
 (0)