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

Skip to content

Commit 9f5db07

Browse files
committed
Make file handing in setup.py more robust by using context managers to properly
close files.
1 parent 2b40efd commit 9f5db07

1 file changed

Lines changed: 45 additions & 43 deletions

File tree

setup.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ def detect_modules(self):
423423
missing = []
424424

425425
config_h = sysconfig.get_config_h_filename()
426-
config_h_vars = sysconfig.parse_config_h(open(config_h))
426+
with open(config_h) as file:
427+
config_h_vars = sysconfig.parse_config_h(file)
427428

428429
platform = self.get_platform()
429430
srcdir = sysconfig.get_config_var('srcdir')
@@ -556,17 +557,17 @@ def detect_modules(self):
556557
os.makedirs(self.build_temp)
557558
ret = os.system("ldd %s > %s" % (do_readline, tmpfile))
558559
if ret >> 8 == 0:
559-
fp = open(tmpfile)
560-
for ln in fp:
561-
if 'curses' in ln:
562-
readline_termcap_library = re.sub(
563-
r'.*lib(n?cursesw?)\.so.*', r'\1', ln
564-
).rstrip()
565-
break
566-
if 'tinfo' in ln: # termcap interface split out from ncurses
567-
readline_termcap_library = 'tinfo'
568-
break
569-
fp.close()
560+
with open(tmpfile) as fp:
561+
for ln in fp:
562+
if 'curses' in ln:
563+
readline_termcap_library = re.sub(
564+
r'.*lib(n?cursesw?)\.so.*', r'\1', ln
565+
).rstrip()
566+
break
567+
# termcap interface split out from ncurses
568+
if 'tinfo' in ln:
569+
readline_termcap_library = 'tinfo'
570+
break
570571
os.unlink(tmpfile)
571572
# Issue 7384: If readline is already linked against curses,
572573
# use the same library for the readline and curses modules.
@@ -675,11 +676,11 @@ def detect_modules(self):
675676
if sys.platform == 'darwin' and is_macosx_sdk_path(name):
676677
name = os.path.join(macosx_sdk_root(), name[1:])
677678
try:
678-
incfile = open(name, 'r')
679-
for line in incfile:
680-
m = openssl_ver_re.match(line)
681-
if m:
682-
openssl_ver = eval(m.group(1))
679+
with open(name, 'r') as incfile:
680+
for line in incfile:
681+
m = openssl_ver_re.match(line)
682+
if m:
683+
openssl_ver = eval(m.group(1))
683684
except IOError as msg:
684685
print("IOError while reading opensshv.h:", msg)
685686
pass
@@ -825,7 +826,8 @@ class db_found(Exception): pass
825826

826827
if db_setup_debug: print("db: looking for db.h in", f)
827828
if os.path.exists(f):
828-
f = open(f, "rb").read()
829+
with open(f, 'rb') as file:
830+
f = file.read()
829831
m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
830832
if m:
831833
db_major = int(m.group(1))
@@ -945,7 +947,8 @@ class db_found(Exception): pass
945947

946948
if os.path.exists(f):
947949
if sqlite_setup_debug: print("sqlite: found %s"%f)
948-
incf = open(f).read()
950+
with open(f) as file:
951+
incf = file.read()
949952
m = re.search(
950953
r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
951954
if m:
@@ -1170,14 +1173,14 @@ class db_found(Exception): pass
11701173
zlib_h = zlib_inc[0] + '/zlib.h'
11711174
version = '"0.0.0"'
11721175
version_req = '"1.1.3"'
1173-
fp = open(zlib_h)
1174-
while 1:
1175-
line = fp.readline()
1176-
if not line:
1177-
break
1178-
if line.startswith('#define ZLIB_VERSION'):
1179-
version = line.split()[2]
1180-
break
1176+
with open(zlib_h) as fp:
1177+
while 1:
1178+
line = fp.readline()
1179+
if not line:
1180+
break
1181+
if line.startswith('#define ZLIB_VERSION'):
1182+
version = line.split()[2]
1183+
break
11811184
if version >= version_req:
11821185
if (self.compiler.find_library_file(lib_dirs, 'z')):
11831186
if sys.platform == "darwin":
@@ -1430,14 +1433,13 @@ def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
14301433
os.system("file %s/Tk.framework/Tk | grep 'for architecture' > %s"%(os.path.join(sysroot, F[1:]), tmpfile))
14311434
else:
14321435
os.system("file %s/Tk.framework/Tk | grep 'for architecture' > %s"%(F, tmpfile))
1433-
fp = open(tmpfile)
1434-
1435-
detected_archs = []
1436-
for ln in fp:
1437-
a = ln.split()[-1]
1438-
if a in archs:
1439-
detected_archs.append(ln.split()[-1])
1440-
fp.close()
1436+
1437+
with open(tmpfile) as fp:
1438+
detected_archs = []
1439+
for ln in fp:
1440+
a = ln.split()[-1]
1441+
if a in archs:
1442+
detected_archs.append(ln.split()[-1])
14411443
os.unlink(tmpfile)
14421444

14431445
for a in detected_archs:
@@ -1708,14 +1710,14 @@ def detect_ctypes(self, inc_dirs, lib_dirs):
17081710
ffi_inc = find_file('ffi.h', [], inc_dirs)
17091711
if ffi_inc is not None:
17101712
ffi_h = ffi_inc[0] + '/ffi.h'
1711-
fp = open(ffi_h)
1712-
while 1:
1713-
line = fp.readline()
1714-
if not line:
1715-
ffi_inc = None
1716-
break
1717-
if line.startswith('#define LIBFFI_H'):
1718-
break
1713+
with open(ffi_h) as fp:
1714+
while 1:
1715+
line = fp.readline()
1716+
if not line:
1717+
ffi_inc = None
1718+
break
1719+
if line.startswith('#define LIBFFI_H'):
1720+
break
17191721
ffi_lib = None
17201722
if ffi_inc is not None:
17211723
for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):

0 commit comments

Comments
 (0)