Skip to content

Commit

Permalink
addresses active reported issues as reported by CodeQL (#233)
Browse files Browse the repository at this point in the history
Co-authored-by: Trevor Gevers <[email protected]>
  • Loading branch information
tlgevers and Trevor Gevers authored Dec 10, 2024
1 parent 76c569c commit 45ed304
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
4 changes: 4 additions & 0 deletions modelscan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,21 @@ def create_settings(force: bool, location: Optional[str]) -> None:
if force:
with open(settings_path, mode="w", encoding="utf-8") as settings_file:
settings_file.write(SettingsUtils.get_default_settings_as_toml())
settings_file.close()
else:
logger.warning(
"%s file already exists. Please use `--force` flag if you intend to overwrite it.",
settings_path,
)

except FileNotFoundError:
with open(settings_path, mode="w", encoding="utf-8") as settings_file:
settings_file.write(SettingsUtils.get_default_settings_as_toml())
settings_file.close()


def main() -> None:
result = 0
try:
result = cli.main(standalone_mode=False)

Expand Down
1 change: 1 addition & 0 deletions modelscan/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(
source: Union[Path, str],
scanner: str = "",
) -> None:
super().__init__(scanner)
self.module = module
self.operator = operator
self.source = source
Expand Down
10 changes: 6 additions & 4 deletions modelscan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class ModelDataEmpty(ValueError):
class Model:
_source: Path
_stream: Optional[IO[bytes]]
_source_file_used: bool
_should_close_stream: bool # Flag to control closing of file
_context: Dict[str, Any]

def __init__(self, source: Union[str, Path], stream: Optional[IO[bytes]] = None):
self._source = Path(source)
self._stream = stream
self._source_file_used = False
self._should_close_stream = stream is None # Only close if opened
self._context = {"formats": []}

def set_context(self, key: str, value: Any) -> None:
Expand All @@ -29,14 +29,16 @@ def open(self) -> "Model":
return self

self._stream = open(self._source, "rb")
self._source_file_used = True
self._should_close_stream = True

return self

def close(self) -> None:
# Only close the stream if we opened a file (not for IO[bytes] objects passed in)
if self._stream and self._source_file_used:
if self._stream and self._should_close_stream:
self._stream.close()
self._stream = None # Avoid double-closing
self._should_close_stream = False # Reset the flag

def __enter__(self) -> "Model":
return self.open()
Expand Down
1 change: 0 additions & 1 deletion modelscan/skip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from enum import Enum

from modelscan.settings import Property

Expand Down
2 changes: 1 addition & 1 deletion modelscan/tools/picklescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np

from modelscan.error import ModelScanError, PickleGenopsError
from modelscan.error import PickleGenopsError
from modelscan.skip import ModelScanSkipped, SkipCategories
from modelscan.issues import Issue, IssueCode, IssueSeverity, OperatorIssueDetails
from modelscan.scanners.scan import ScanResults
Expand Down

0 comments on commit 45ed304

Please sign in to comment.