fix: file open leaks (#3067)

This commit is contained in:
Nicolò Boschi 2024-07-30 14:45:42 +02:00 • committed by GitHub
commit 747a1848d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 54 deletions

View file

@ -22,7 +22,8 @@ class JsonAgentComponent(LCAgentComponent):
def build_agent(self) -> AgentExecutor: def build_agent(self) -> AgentExecutor:
if self.path.endswith("yaml") or self.path.endswith("yml"): if self.path.endswith("yaml") or self.path.endswith("yml"):
yaml_dict = yaml.load(open(self.path, "r"), Loader=yaml.FullLoader) with open(self.path, "r") as file:
yaml_dict = yaml.load(file, Loader=yaml.FullLoader)
spec = JsonSpec(dict_=yaml_dict) spec = JsonSpec(dict_=yaml_dict)
else: else:
spec = JsonSpec.from_file(Path(self.path)) spec = JsonSpec.from_file(Path(self.path))

View file

@ -24,7 +24,8 @@ class OpenAPIAgentComponent(LCAgentComponent):
def build_agent(self) -> AgentExecutor: def build_agent(self) -> AgentExecutor:
if self.path.endswith("yaml") or self.path.endswith("yml"): if self.path.endswith("yaml") or self.path.endswith("yml"):
yaml_dict = yaml.load(open(self.path, "r"), Loader=yaml.FullLoader) with open(self.path, "r") as file:
yaml_dict = yaml.load(file, Loader=yaml.FullLoader)
spec = JsonSpec(dict_=yaml_dict) spec = JsonSpec(dict_=yaml_dict)
else: else:
spec = JsonSpec.from_file(Path(self.path)) spec = JsonSpec.from_file(Path(self.path))

View file

@ -20,7 +20,8 @@ def upload(file_path, host, flow_id):
""" """
try: try:
url = f"{host}/api/v1/upload/{flow_id}" url = f"{host}/api/v1/upload/{flow_id}"
response = httpx.post(url, files={"file": open(file_path, "rb")}) with open(file_path, "rb") as file:
response = httpx.post(url, files={"file": file})
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
else: else:

View file

@ -169,7 +169,7 @@ class DatabaseService(Service):
# which is a buffer # which is a buffer
# I don't want to output anything # I don't want to output anything
# subprocess.DEVNULL is an int # subprocess.DEVNULL is an int
buffer = open(self.script_location / "alembic.log", "w") with open(self.script_location / "alembic.log", "w") as buffer:
alembic_cfg = Config(stdout=buffer) alembic_cfg = Config(stdout=buffer)
# alembic_cfg.attributes["connection"] = session # alembic_cfg.attributes["connection"] = session
alembic_cfg.set_main_option("script_location", str(self.script_location)) alembic_cfg.set_main_option("script_location", str(self.script_location))