Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addresses active reported issues as reported by CodeQL #233

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modelscan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def scan(
with open(settings_file_path, encoding="utf-8") as sf:
settings = parse(sf.read()).unwrap()
click.echo(f"Detected settings file. Using {settings_file_path}. \n")
sf.close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The close should be redundant as the context manager should close it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Faisal, I have updated the PR, removed the redundant close & also ran make format to ensure formatting is corrected.

else:
click.echo(
f"No settings file detected at {settings_file_path}. Using defaults. \n"
Expand Down Expand Up @@ -178,17 +179,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
Loading