Skip to content

Commit

Permalink
Ensure input file is closed within load()
Browse files Browse the repository at this point in the history
  • Loading branch information
Niclas Borlin authored and karlicoss committed Oct 31, 2021
1 parent 349a61f commit ac78d8d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions orgparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ def load(path: Union[str, Path, TextIO], env: Optional[OrgEnv]=None) -> OrgNode:
"""
orgfile: TextIO
if isinstance(path, (str, Path)):
orgfile = codecs.open(str(path), encoding='utf8')
# Use 'with' to close the file inside this function.
with codecs.open(str(path), encoding='utf8') as orgfile:
lines = (l.rstrip('\n') for l in orgfile.readlines())
filename = str(path)
else:
orgfile = path
lines = (l.rstrip('\n') for l in orgfile.readlines())
filename = path.name if hasattr(path, 'name') else '<file-like>'
return loadi((l.rstrip('\n') for l in orgfile.readlines()),
filename=filename, env=env)
return loadi(lines, filename=filename, env=env)


def loads(string: str, filename: str='<string>', env: Optional[OrgEnv]=None) -> OrgNode:
Expand Down

0 comments on commit ac78d8d

Please sign in to comment.