Skip to content

Commit 116d921

Browse files
committed
Fixed sonar complaints and improved coverage
1 parent c3bfd67 commit 116d921

4 files changed

Lines changed: 45 additions & 38 deletions

File tree

quantcrypt/internal/cli/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def find_command_modules() -> Generator[ModuleType, None, None]:
4040
for filepath in import_dir.rglob("*.py"):
4141
if filepath.name == "compile.py":
4242
required_packages = ["cffi", "setuptools", "yaml", "requests"]
43-
if not all(find_spec(pkg) is not None for pkg in required_packages):
43+
if not all(find_spec(pkg) is not None for pkg in required_packages): # pragma: no cover
4444
continue
4545
relative_path = filepath.resolve().relative_to(package_path)
4646
module_path = '.'.join(relative_path.with_suffix('').parts)

quantcrypt/internal/compiler.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,38 +110,42 @@ def include_directive(self) -> str:
110110
header_file = self.source_dir / "api.h"
111111
return f'#include "{header_file.as_posix()}"'
112112

113-
@property
114-
def compiler_args(self) -> list[str]: # pragma: no cover
115-
opsys = platform.system().lower()
116-
arch = platform.machine().lower()
117-
118-
if opsys not in ("windows", "linux", "darwin"):
119-
raise errors.UnsupportedPlatformError
120-
113+
def _windows_compiler_args(self) -> list[str]: # pragma: no cover
121114
extra_flags: list[str] = []
122-
unix_flags = [
115+
for flag in self.required_flags:
116+
extra_flags.append(f"/arch:{flag.upper()}")
117+
return ["/O2", "/MD", "/nologo", *extra_flags]
118+
119+
def _linux_compiler_args(self) -> list[str]: # pragma: no cover
120+
arch = platform.machine().lower()
121+
extra_flags = [
123122
"-fdata-sections", "-ffunction-sections",
124-
"-O3", "-flto", "-std=c99"
123+
"-O3", "-flto", "-std=c99", "-s"
125124
]
126-
if opsys == "windows" and arch in const.AMDArches:
125+
if arch in const.AMDArches:
127126
for flag in self.required_flags:
128-
extra_flags.append(f"/arch:{flag.upper()}")
129-
return ["/O2", "/MD", "/nologo", *extra_flags]
127+
extra_flags.append(f"-m{flag.lower()}")
128+
elif arch in const.ARMArches:
129+
march_flag = "-march=armv8.5-a"
130+
for flag in self.required_flags:
131+
march_flag += f"+{flag.lower()}"
132+
extra_flags.append(march_flag)
133+
return extra_flags
134+
135+
@staticmethod
136+
def _darwin_compiler_args() -> list[str]: # pragma: no cover
137+
return ["-fdata-sections", "-ffunction-sections", "-O3", "-flto", "-std=c99"]
138+
139+
@property
140+
def compiler_args(self) -> list[str]: # pragma: no cover
141+
opsys = platform.system().lower()
142+
if opsys == "windows":
143+
return self._windows_compiler_args()
130144
elif opsys == "linux":
131-
extra_flags.append("-s")
132-
if arch in const.AMDArches:
133-
for flag in self.required_flags:
134-
extra_flags.append(f"-m{flag.lower()}")
135-
elif arch in const.ARMArches:
136-
march_flag = "-march=armv8.5-a"
137-
for flag in self.required_flags:
138-
march_flag += f"+{flag.lower()}"
139-
extra_flags.append(march_flag)
145+
return self._linux_compiler_args()
140146
elif opsys == "darwin":
141-
pass
142-
143-
unix_flags.extend(extra_flags)
144-
return unix_flags
147+
return self._darwin_compiler_args()
148+
return list()
145149

146150
@property
147151
def linker_args(self) -> list[str]: # pragma: no cover

quantcrypt/internal/pqclean.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,17 @@ def read_algo_metadata(spec: const.AlgoSpec) -> PQAMetaData:
152152
return PQAMetaData(**data)
153153

154154

155-
def check_opsys_support(spf: PQASupportedPlatform) -> str | None:
155+
def check_opsys_support(spf: PQASupportedPlatform, variant: const.PQAVariant) -> str | None:
156+
os_name = ''
156157
for opsys in spf.operating_systems:
157-
if platform.system().lower() == opsys.lower():
158-
return opsys.lower()
159-
return None
158+
_os_name = opsys.lower()
159+
if platform.system().lower() == _os_name:
160+
os_name = _os_name
161+
if os_name:
162+
for x, y in const.ExcludedCombinations:
163+
if x == os_name and y == variant: # pragma: no cover
164+
return None
165+
return os_name or None
160166

161167

162168
def check_arch_support(impl: PQAImplementation) -> PQASupportedPlatform | None:
@@ -183,14 +189,11 @@ def check_platform_support(
183189
spf = check_arch_support(impl)
184190
if not spf:
185191
return None, None
186-
if spf.operating_systems:
187-
opsys = check_opsys_support(spf)
192+
elif spf.operating_systems:
193+
opsys = check_opsys_support(spf, variant)
188194
if not opsys:
189195
return None, None
190-
for x, y in const.ExcludedCombinations:
191-
if x == opsys and y == variant: # pragma: no cover
192-
return None, None
193-
if spf.required_flags: # pragma: no branch
196+
elif spf.required_flags: # pragma: no branch
194197
required_flags = spf.required_flags
195198

196199
pqclean_dir = find_pqclean_dir(src_must_exist=True)

tests/test_compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pathlib import Path
1313
from unittest.mock import patch
1414
from quantcrypt.internal import constants as const
15-
from quantcrypt.internal.compiler import Compiler
15+
from quantcrypt.compiler import Compiler
1616

1717

1818
def test_compiler_run(alt_tmp_path):

0 commit comments

Comments
 (0)