Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6ac380

Browse files
committed
fix bug in example (should close file at all times)
1 parent 6b686e9 commit d6ac380

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

Doc/lib/libimp.tex

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ \subsection{Examples}
158158
fp, pathname, (suffix, mode, type) = imp.find_module(name)
159159
160160
# See what we got.
161-
# Note that fp will be closed automatically when we return.
162-
if type == imp.C_EXTENSION:
163-
return imp.load_dynamic(name, pathname)
164-
if type == imp.PY_SOURCE:
165-
return imp.load_source(name, pathname, fp)
166-
if type == imp.PY_COMPILED:
167-
return imp.load_compiled(name, pathname, fp)
168-
169-
# Shouldn't get here at all.
170-
raise ImportError, '%s: unknown module type (%d)' % (name, type)
161+
try:
162+
if type == imp.C_EXTENSION:
163+
return imp.load_dynamic(name, pathname)
164+
if type == imp.PY_SOURCE:
165+
return imp.load_source(name, pathname, fp)
166+
if type == imp.PY_COMPILED:
167+
return imp.load_compiled(name, pathname, fp)
168+
169+
# Shouldn't get here at all.
170+
raise ImportError, '%s: unknown module type (%d)' % (name, type)
171+
finally:
172+
# Since we may exit via an exception, close fp explicitly.
173+
fp.close()
171174
\end{verbatim}

Doc/libimp.tex

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ \subsection{Examples}
158158
fp, pathname, (suffix, mode, type) = imp.find_module(name)
159159
160160
# See what we got.
161-
# Note that fp will be closed automatically when we return.
162-
if type == imp.C_EXTENSION:
163-
return imp.load_dynamic(name, pathname)
164-
if type == imp.PY_SOURCE:
165-
return imp.load_source(name, pathname, fp)
166-
if type == imp.PY_COMPILED:
167-
return imp.load_compiled(name, pathname, fp)
168-
169-
# Shouldn't get here at all.
170-
raise ImportError, '%s: unknown module type (%d)' % (name, type)
161+
try:
162+
if type == imp.C_EXTENSION:
163+
return imp.load_dynamic(name, pathname)
164+
if type == imp.PY_SOURCE:
165+
return imp.load_source(name, pathname, fp)
166+
if type == imp.PY_COMPILED:
167+
return imp.load_compiled(name, pathname, fp)
168+
169+
# Shouldn't get here at all.
170+
raise ImportError, '%s: unknown module type (%d)' % (name, type)
171+
finally:
172+
# Since we may exit via an exception, close fp explicitly.
173+
fp.close()
171174
\end{verbatim}

0 commit comments

Comments
 (0)