Skip to content

Commit df42f77

Browse files
committed
cleanup subprocess in nistlat (tested win & mac)
1 parent e94d513 commit df42f77

File tree

1 file changed

+21
-81
lines changed

1 file changed

+21
-81
lines changed

GSASII/nistlat.py

Lines changed: 21 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,13 @@ def ReduceCell(center, cellin, mode=0, deltaV=0, output=None):
147147
os.remove('NIST10')
148148
# import shutil
149149
# print(shutil.which(nistlattice))
150-
p = subprocess.Popen([nistlattice],encoding='UTF-8',
150+
with subprocess.Popen([nistlattice],encoding='UTF-8',
151151
stdin=subprocess.PIPE,
152152
stdout=subprocess.PIPE,
153-
stderr=subprocess.PIPE)
154-
p.stdin.write(inp)
155-
p.stdin.close()
156-
# read output and parse
157-
err = p.stderr.read()
158-
cellout = p.stdout.readlines()
159-
p.terminate()
160-
try:
161-
p.wait(1)
162-
except TimeoutExpired:
163-
print('timeout on wait')
164-
p.kill()
165-
try:
166-
p.wait(2)
167-
except TimeoutExpired:
168-
pass
169-
p.stdout.close()
170-
p.stderr.close()
171-
153+
stderr=subprocess.PIPE) as p:
154+
o,err = p.communicate(input=inp)
155+
156+
cellout = o.split('\n')
172157
celldict['input'] = (cellin,center,setting)
173158
celldict['output'] = []
174159
d = 1
@@ -222,34 +207,21 @@ def ConvCell(redcell):
222207
if os.path.exists('NIST10'): # cleanup
223208
print("Removing old NIST10 file")
224209
os.remove('NIST10')
225-
p = subprocess.Popen([convcell],encoding='UTF-8',
210+
with subprocess.Popen([convcell],encoding='UTF-8',
226211
stdin=subprocess.PIPE,
227212
stdout=subprocess.PIPE,
228-
stderr=subprocess.PIPE)
229-
p.stdin.write(inp)
230-
p.stdin.close()
231-
# read output and parse
232-
err = p.stderr.read()
233-
out = p.stdout.readlines()
234-
p.terminate()
235-
try:
236-
p.wait(1)
237-
except TimeoutExpired:
238-
print('timeout on wait')
239-
p.kill()
240-
try:
241-
p.wait(2)
242-
except TimeoutExpired:
243-
pass
244-
p.stdout.close()
245-
p.stderr.close()
213+
stderr=subprocess.PIPE) as p:
214+
o,err = p.communicate(input=inp)
215+
216+
out = o.split('\n')
246217
if debug and err:
247218
print(f'ConvCell err = {err}')
248219
line = '?'
249220
linenum = 0
250221
cell = []
251222
center = ' '
252223
setting = ' '
224+
mat = ''
253225
try:
254226
while out:
255227
line=out.pop(0)
@@ -328,30 +300,14 @@ def CompareCell(cell1, center1, cell2, center2, tolerance=3*[0.2]+3*[1],
328300
if os.path.exists('NIST10'): # cleanup
329301
print("Removing old NIST10 file")
330302
os.remove('NIST10')
331-
p = subprocess.Popen([nistlattice],encoding='UTF-8',
303+
with subprocess.Popen([nistlattice],encoding='UTF-8',
332304
stdin=subprocess.PIPE,
333305
stdout=subprocess.PIPE,
334-
stderr=subprocess.PIPE)
335-
p.stdin.write(inp)
336-
p.stdin.close()
337-
err = p.stderr.read()
338-
lines = p.stdout.readlines()
339-
line = '?'
306+
stderr=subprocess.PIPE) as p:
307+
o,err = p.communicate(input=inp)
308+
309+
lines = o.split('\n')
340310
fp = None
341-
if output: fp = open(output,'w')
342-
# read output and parse
343-
p.terminate()
344-
try:
345-
p.wait(1)
346-
except TimeoutExpired:
347-
print('timeout on wait')
348-
p.kill()
349-
try:
350-
p.wait(2)
351-
except TimeoutExpired:
352-
pass
353-
p.stdout.close()
354-
p.stderr.close()
355311
if fp:
356312
for line in lines: _emulateLP(line,fp)
357313
fp.close()
@@ -460,29 +416,13 @@ def CellSymSearch(cellin, center, tolerance=3*[0.2]+3*[1], mode=0,
460416
if os.path.exists('NIST10'): # cleanup
461417
print("Removing old NIST10 file")
462418
os.remove('NIST10')
463-
p = subprocess.Popen([nistlattice],encoding='UTF-8',
419+
with subprocess.Popen([nistlattice],encoding='UTF-8',
464420
stdin=subprocess.PIPE,
465421
stdout=subprocess.PIPE,
466-
stderr=subprocess.PIPE)
467-
p.stdin.write(inp)
468-
p.stdin.close()
469-
# read output and parse
470-
err = p.stderr.read()
471-
lines = p.stdout.readlines()
472-
473-
p.terminate()
474-
try:
475-
p.wait(1)
476-
except TimeoutExpired:
477-
print('timeout on wait')
478-
p.kill()
479-
try:
480-
p.wait(2)
481-
except TimeoutExpired:
482-
pass
483-
p.stdout.close()
484-
p.stderr.close()
485-
422+
stderr=subprocess.PIPE) as p:
423+
o,err = p.communicate(input=inp)
424+
425+
lines = o.split('\n')
486426
d = 1
487427
fp = None
488428
if output: fp = open(output,'w')

0 commit comments

Comments
 (0)