Skip to content

Commit

Permalink
Use relative imports when importing other modules in the same directory
Browse files Browse the repository at this point in the history
We were using absolute imports under the assumption that the /scripts
directory is in the path. This worked in normal use because every one of our
Python scripts either were in the /scripts directory, or added the /scripts
directory to the module search path in order to reference mbedtls_dev.
However, this broke things like
```
python3 -m unittest scripts/mbedtls_dev/psa_storage.py
```

Fix this by using relative imports.

Relative imports are only supposed to be used inside a package (Python
doesn't complain, but Pylint does). So make /scripts/mbedtls_dev a proper
package by creating __init__.py.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
  • Loading branch information
gilles-peskine-arm committed Oct 14, 2022
1 parent ca980c0 commit 239765a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions scripts/mbedtls_dev/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file needs to exist to make mbedtls_dev a package.
# Among other things, this allows modules in this directory to make
# relative impotrs.
2 changes: 1 addition & 1 deletion scripts/mbedtls_dev/crypto_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import re
from typing import FrozenSet, Iterable, List, Optional, Tuple

from mbedtls_dev.asymmetric_key_data import ASYMMETRIC_KEY_DATA
from .asymmetric_key_data import ASYMMETRIC_KEY_DATA


def short_expression(original: str, level: int = 0) -> str:
Expand Down
2 changes: 1 addition & 1 deletion scripts/mbedtls_dev/psa_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from typing import Dict, List, Optional, Set, Union
import unittest

from mbedtls_dev import c_build_helper
from . import c_build_helper


class Expr:
Expand Down
2 changes: 1 addition & 1 deletion scripts/mbedtls_dev/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import sys
from typing import Iterable, List, Optional

from mbedtls_dev import typing_util
from . import typing_util

def hex_string(data: bytes) -> str:
return '"' + binascii.hexlify(data).decode('ascii') + '"'
Expand Down
4 changes: 2 additions & 2 deletions scripts/mbedtls_dev/test_data_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Iterable, Iterator, List, Type, TypeVar

from mbedtls_dev import build_tree
from mbedtls_dev import test_case
from . import build_tree
from . import test_case

T = TypeVar('T') #pylint: disable=invalid-name

Expand Down

0 comments on commit 239765a

Please sign in to comment.