Commit 773f1ec3 authored by Jason R. Coombs's avatar Jason R. Coombs

Rely on contextlib.closing for brevity.

parent 85a9ca5e
import sys import sys
import marshal import marshal
import contextlib
from distutils.version import StrictVersion from distutils.version import StrictVersion
from .py33compat import Bytecode from .py33compat import Bytecode
...@@ -81,6 +82,17 @@ class Require: ...@@ -81,6 +82,17 @@ class Require:
return self.version_ok(version) return self.version_ok(version)
def maybe_close(f):
@contextlib.contextmanager
def empty():
yield
return
if not f:
return empty()
return contextlib.closing(f)
def get_module_constant(module, symbol, default=-1, paths=None): def get_module_constant(module, symbol, default=-1, paths=None):
"""Find 'module' by searching 'paths', and extract 'symbol' """Find 'module' by searching 'paths', and extract 'symbol'
...@@ -94,7 +106,7 @@ def get_module_constant(module, symbol, default=-1, paths=None): ...@@ -94,7 +106,7 @@ def get_module_constant(module, symbol, default=-1, paths=None):
# Module doesn't exist # Module doesn't exist
return None return None
try: with maybe_close(f):
if kind == PY_COMPILED: if kind == PY_COMPILED:
f.read(8) # skip magic & date f.read(8) # skip magic & date
code = marshal.load(f) code = marshal.load(f)
...@@ -107,10 +119,6 @@ def get_module_constant(module, symbol, default=-1, paths=None): ...@@ -107,10 +119,6 @@ def get_module_constant(module, symbol, default=-1, paths=None):
imported = py27compat.get_module(module, paths, info) imported = py27compat.get_module(module, paths, info)
return getattr(imported, symbol, None) return getattr(imported, symbol, None)
finally:
if f:
f.close()
return extract_constant(code, symbol, default) return extract_constant(code, symbol, default)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment