Skip to content

Commit ac78d8d

Browse files
Niclas Borlinkarlicoss
authored andcommitted
Ensure input file is closed within load()
1 parent 349a61f commit ac78d8d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

orgparse/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ def load(path: Union[str, Path, TextIO], env: Optional[OrgEnv]=None) -> OrgNode:
131131
"""
132132
orgfile: TextIO
133133
if isinstance(path, (str, Path)):
134-
orgfile = codecs.open(str(path), encoding='utf8')
134+
# Use 'with' to close the file inside this function.
135+
with codecs.open(str(path), encoding='utf8') as orgfile:
136+
lines = (l.rstrip('\n') for l in orgfile.readlines())
135137
filename = str(path)
136138
else:
137139
orgfile = path
140+
lines = (l.rstrip('\n') for l in orgfile.readlines())
138141
filename = path.name if hasattr(path, 'name') else '<file-like>'
139-
return loadi((l.rstrip('\n') for l in orgfile.readlines()),
140-
filename=filename, env=env)
142+
return loadi(lines, filename=filename, env=env)
141143

142144

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

0 commit comments

Comments
 (0)