Commit 511d753c by Xavier Francisco Committed by GitHub

Ensure list access is performed after the length check (#2265)

parent bf331a54
...@@ -34,13 +34,12 @@ class PackageInclude(Include): ...@@ -34,13 +34,12 @@ class PackageInclude(Include):
return self.check_elements() return self.check_elements()
def check_elements(self): # type: () -> PackageInclude def check_elements(self): # type: () -> PackageInclude
root = self._elements[0]
if not self._elements: if not self._elements:
raise ValueError( raise ValueError(
"{} does not contain any element".format(self._base / self._include) "{} does not contain any element".format(self._base / self._include)
) )
root = self._elements[0]
if len(self._elements) > 1: if len(self._elements) > 1:
# Probably glob # Probably glob
self._is_package = True self._is_package = True
......
...@@ -41,3 +41,12 @@ def test_package_include_with_no_python_files_in_dir(): ...@@ -41,3 +41,12 @@ def test_package_include_with_no_python_files_in_dir():
PackageInclude(base=with_includes, include="not_a_python_pkg") PackageInclude(base=with_includes, include="not_a_python_pkg")
assert str(e.value) == "not_a_python_pkg is not a package." assert str(e.value) == "not_a_python_pkg is not a package."
def test_package_include_with_non_existent_directory():
with pytest.raises(ValueError) as e:
PackageInclude(base=with_includes, include="not_a_dir")
err_str = str(with_includes / "not_a_dir") + " does not contain any element"
assert str(e.value) == err_str
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment