@@ -1016,23 +1016,23 @@ def nauty_geng(self, options='', debug=False, immutable=False):
10161016
10171017 from sage .features .nauty import NautyExecutable
10181018 geng_path = NautyExecutable ("geng" ).absolute_filename ()
1019- sp = subprocess .Popen (shlex .quote (geng_path ) + " {0}" .format (options ), shell = True ,
1019+ with subprocess .Popen (shlex .quote (geng_path ) + " {0}" .format (options ), shell = True ,
10201020 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
10211021 stderr = subprocess .PIPE , close_fds = True ,
1022- encoding = 'latin-1' )
1023- msg = sp .stderr .readline ()
1024- if debug :
1025- yield msg
1026- elif msg .startswith ('>E' ):
1027- raise ValueError ('wrong format of parameter option' )
1028- gen = sp .stdout
1029- while True :
1030- try :
1031- s = next (gen )
1032- except StopIteration :
1033- # Exhausted list of graphs from nauty geng
1034- return
1035- yield graph .Graph (s [:- 1 ], format = 'graph6' , immutable = immutable )
1022+ encoding = 'latin-1' ) as sp :
1023+ msg = sp .stderr .readline ()
1024+ if debug :
1025+ yield msg
1026+ elif msg .startswith ('>E' ):
1027+ raise ValueError ('wrong format of parameter option' )
1028+ gen = sp .stdout
1029+ while True :
1030+ try :
1031+ s = next (gen )
1032+ except StopIteration :
1033+ # Exhausted list of graphs from nauty geng
1034+ return
1035+ yield graph .Graph (s [:- 1 ], format = 'graph6' , immutable = immutable )
10361036
10371037 def nauty_genbg (self , options = '' , debug = False , immutable = False ):
10381038 r"""
@@ -1203,41 +1203,41 @@ def nauty_genbg(self, options='', debug=False, immutable=False):
12031203
12041204 from sage .features .nauty import NautyExecutable
12051205 genbg_path = NautyExecutable ("genbgL" ).absolute_filename ()
1206- sp = subprocess .Popen (shlex .quote (genbg_path ) + " {0}" .format (options ), shell = True ,
1206+ with subprocess .Popen (shlex .quote (genbg_path ) + " {0}" .format (options ), shell = True ,
12071207 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
12081208 stderr = subprocess .PIPE , close_fds = True ,
1209- encoding = 'latin-1' )
1210- msg = sp .stderr .readline ()
1211- if debug :
1212- yield msg
1213- elif msg .startswith ('>E' ):
1214- raise ValueError ('wrong format of parameter options' )
1215-
1216- if msg .startswith ('>A' ):
1217- # We extract the partition of the vertices from the msg string
1218- for s in msg .split (' ' ):
1219- if s .startswith ('n=' ):
1220- from sage .rings .integer import Integer
1221- n1 , n2 = (Integer (t ) for t in s [2 :].split ('+' ) if t .isdigit ())
1222- partition = [set (range (n1 )), set (range (n1 , n1 + n2 ))]
1223- break
1209+ encoding = 'latin-1' ) as sp :
1210+ msg = sp .stderr .readline ()
1211+ if debug :
1212+ yield msg
1213+ elif msg .startswith ('>E' ):
1214+ raise ValueError ('wrong format of parameter options' )
1215+
1216+ if msg .startswith ('>A' ):
1217+ # We extract the partition of the vertices from the msg string
1218+ for s in msg .split (' ' ):
1219+ if s .startswith ('n=' ):
1220+ from sage .rings .integer import Integer
1221+ n1 , n2 = (Integer (t ) for t in s [2 :].split ('+' ) if t .isdigit ())
1222+ partition = [set (range (n1 )), set (range (n1 , n1 + n2 ))]
1223+ break
1224+ else :
1225+ # should never happen
1226+ raise ValueError ('unable to recover the partition' )
12241227 else :
1225- # should never happen
1226- raise ValueError ('unable to recover the partition' )
1227- else :
1228- # Either msg starts with >E or option -q has been given
1229- partition = None
1230-
1231- gen = sp .stdout
1232- from sage .graphs .bipartite_graph import BipartiteGraph
1233- while True :
1234- try :
1235- s = next (gen )
1236- except StopIteration :
1237- # Exhausted list of bipartite graphs from nauty genbgL
1238- return
1239- yield BipartiteGraph (s [:- 1 ], format = 'graph6' , partition = partition ,
1240- immutable = immutable )
1228+ # Either msg starts with >E or option -q has been given
1229+ partition = None
1230+
1231+ gen = sp .stdout
1232+ from sage .graphs .bipartite_graph import BipartiteGraph
1233+ while True :
1234+ try :
1235+ s = next (gen )
1236+ except StopIteration :
1237+ # Exhausted list of bipartite graphs from nauty genbgL
1238+ return
1239+ yield BipartiteGraph (s [:- 1 ], format = 'graph6' , partition = partition ,
1240+ immutable = immutable )
12411241
12421242 def nauty_genktreeg (self , options = '' , debug = False , immutable = False ):
12431243 r"""
@@ -1339,23 +1339,23 @@ def nauty_genktreeg(self, options='', debug=False, immutable=False):
13391339
13401340 from sage .features .nauty import NautyExecutable
13411341 geng_path = NautyExecutable ("genktreeg" ).absolute_filename ()
1342- sp = subprocess .Popen (shlex .quote (geng_path ) + " {0}" .format (options ), shell = True ,
1342+ with subprocess .Popen (shlex .quote (geng_path ) + " {0}" .format (options ), shell = True ,
13431343 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
13441344 stderr = subprocess .PIPE , close_fds = True ,
1345- encoding = 'latin-1' )
1346- msg = sp .stderr .readline ()
1347- if debug :
1348- yield msg
1349- elif msg .startswith ('>E' ):
1350- raise ValueError ('wrong format of parameter option' )
1351- gen = sp .stdout
1352- while True :
1353- try :
1354- s = next (gen )
1355- except StopIteration :
1356- # Exhausted list of graphs from nauty geng
1357- return
1358- yield graph .Graph (s [:- 1 ], format = 'graph6' , immutable = immutable )
1345+ encoding = 'latin-1' ) as sp :
1346+ msg = sp .stderr .readline ()
1347+ if debug :
1348+ yield msg
1349+ elif msg .startswith ('>E' ):
1350+ raise ValueError ('wrong format of parameter option' )
1351+ gen = sp .stdout
1352+ while True :
1353+ try :
1354+ s = next (gen )
1355+ except StopIteration :
1356+ # Exhausted list of graphs from nauty geng
1357+ return
1358+ yield graph .Graph (s [:- 1 ], format = 'graph6' , immutable = immutable )
13591359
13601360 def cospectral_graphs (self , vertices , matrix_function = None , graphs = None ,
13611361 immutable = False ):
@@ -1718,11 +1718,11 @@ def fullerenes(self, order, ipr=False, immutable=False):
17181718 command = shlex .quote (Buckygen ().absolute_filename ())
17191719 command += ' -' + ('I' if ipr else '' ) + 'd {0}d' .format (order )
17201720
1721- sp = subprocess .Popen (command , shell = True ,
1721+ with subprocess .Popen (command , shell = True ,
17221722 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
1723- stderr = subprocess .PIPE , close_fds = True )
1723+ stderr = subprocess .PIPE , close_fds = True ) as sp :
17241724
1725- yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
1725+ yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
17261726
17271727 def fusenes (self , hexagon_count , benzenoids = False , immutable = False ):
17281728 r"""
@@ -1806,11 +1806,11 @@ def fusenes(self, hexagon_count, benzenoids=False, immutable=False):
18061806 command = shlex .quote (Benzene ().absolute_filename ())
18071807 command += (' b' if benzenoids else '' ) + ' {0} p' .format (hexagon_count )
18081808
1809- sp = subprocess .Popen (command , shell = True ,
1809+ with subprocess .Popen (command , shell = True ,
18101810 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
1811- stderr = subprocess .PIPE , close_fds = True )
1811+ stderr = subprocess .PIPE , close_fds = True ) as sp :
18121812
1813- yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
1813+ yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
18141814
18151815 def plantri_gen (self , options = "" , immutable = False ):
18161816 r"""
@@ -1991,14 +1991,14 @@ def plantri_gen(self, options="", immutable=False):
19911991 import shlex
19921992 command = '{} {}' .format (shlex .quote (Plantri ().absolute_filename ()),
19931993 options )
1994- sp = subprocess .Popen (command , shell = True ,
1994+ with subprocess .Popen (command , shell = True ,
19951995 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
1996- stderr = subprocess .PIPE , close_fds = True )
1996+ stderr = subprocess .PIPE , close_fds = True ) as sp :
19971997
1998- try :
1999- yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
2000- except (TypeError , AssertionError ):
2001- raise AttributeError ("invalid options '{}'" .format (options ))
1998+ try :
1999+ yield from graphs ._read_planar_code (sp .stdout , immutable = immutable )
2000+ except (TypeError , AssertionError ):
2001+ raise AttributeError ("invalid options '{}'" .format (options ))
20022002
20032003 def planar_graphs (self , order , minimum_degree = None ,
20042004 minimum_connectivity = None ,
0 commit comments