]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
fix bug in example (should close file at all times)
authorGuido van Rossum <guido@python.org>
Fri, 7 Jul 1995 23:01:27 +0000 (23:01 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 7 Jul 1995 23:01:27 +0000 (23:01 +0000)
Doc/lib/libimp.tex
Doc/libimp.tex

index 2e72602b4e147fa0cae16c80706ee1fea0ffa79e..445681445e8a0d56486e0814cd0df5ccd61c7af3 100644 (file)
@@ -158,14 +158,17 @@ def __import__(name, globals=None, locals=None, fromlist=None):
     fp, pathname, (suffix, mode, type) = imp.find_module(name)
 
     # See what we got.
-    # Note that fp will be closed automatically when we return.
-    if type == imp.C_EXTENSION:
-        return imp.load_dynamic(name, pathname)
-    if type == imp.PY_SOURCE:
-        return imp.load_source(name, pathname, fp)
-    if type == imp.PY_COMPILED:
-        return imp.load_compiled(name, pathname, fp)
-
-    # Shouldn't get here at all.
-    raise ImportError, '%s: unknown module type (%d)' % (name, type)
+    try:
+        if type == imp.C_EXTENSION:
+            return imp.load_dynamic(name, pathname)
+        if type == imp.PY_SOURCE:
+            return imp.load_source(name, pathname, fp)
+        if type == imp.PY_COMPILED:
+            return imp.load_compiled(name, pathname, fp)
+
+        # Shouldn't get here at all.
+        raise ImportError, '%s: unknown module type (%d)' % (name, type)
+    finally:
+        # Since we may exit via an exception, close fp explicitly.
+        fp.close()
 \end{verbatim}
index 2e72602b4e147fa0cae16c80706ee1fea0ffa79e..445681445e8a0d56486e0814cd0df5ccd61c7af3 100644 (file)
@@ -158,14 +158,17 @@ def __import__(name, globals=None, locals=None, fromlist=None):
     fp, pathname, (suffix, mode, type) = imp.find_module(name)
 
     # See what we got.
-    # Note that fp will be closed automatically when we return.
-    if type == imp.C_EXTENSION:
-        return imp.load_dynamic(name, pathname)
-    if type == imp.PY_SOURCE:
-        return imp.load_source(name, pathname, fp)
-    if type == imp.PY_COMPILED:
-        return imp.load_compiled(name, pathname, fp)
-
-    # Shouldn't get here at all.
-    raise ImportError, '%s: unknown module type (%d)' % (name, type)
+    try:
+        if type == imp.C_EXTENSION:
+            return imp.load_dynamic(name, pathname)
+        if type == imp.PY_SOURCE:
+            return imp.load_source(name, pathname, fp)
+        if type == imp.PY_COMPILED:
+            return imp.load_compiled(name, pathname, fp)
+
+        # Shouldn't get here at all.
+        raise ImportError, '%s: unknown module type (%d)' % (name, type)
+    finally:
+        # Since we may exit via an exception, close fp explicitly.
+        fp.close()
 \end{verbatim}