Changeset: 941a36dc11cb for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=941a36dc11cb Modified Files: testing/Mz.py.in Branch: default Log Message:
Use with clauses for subprocesses. diffs (truncated from 429 to 300 lines): diff --git a/testing/Mz.py.in b/testing/Mz.py.in --- a/testing/Mz.py.in +++ b/testing/Mz.py.in @@ -849,59 +849,59 @@ def GetBitsAndModsAndThreads(env) : if procdebug: print('GetBitsAndModsAndThreads: starting process "%s" (inpipe, outpipe, errpipe)\n' % '" "'.join(cmd)) setpgrp = True - proc = process.Popen(cmd, stdin=process.PIPE, stdout=process.PIPE, - stderr=process.PIPE, text=True) - proc.killed = False - proc.onechild = True - t = Timer(float(par['TIMEOUT']), killProc, args = [proc, proc.stderr, cmd]) - qOut = qErr = None - try: - t.start() - while True: - proc.poll() - if proc.returncode is not None: - break - if os.path.exists(os.path.join(dbpath, '.started')): - break - time.sleep(0.001) - if proc.returncode is None: - cmd = splitcommand(env['exe']['MAL_Client'][1]) + with process.Popen(cmd, stdin=process.PIPE, stdout=process.PIPE, + stderr=process.PIPE, text=True) as proc: + proc.killed = False + proc.onechild = True + t = Timer(float(par['TIMEOUT']), killProc, args = [proc, proc.stderr, cmd]) + qOut = qErr = None + try: + t.start() + while True: + proc.poll() + if proc.returncode is not None: + break + if os.path.exists(os.path.join(dbpath, '.started')): + break + time.sleep(0.001) + if proc.returncode is None: + cmd = splitcommand(env['exe']['MAL_Client'][1]) + if procdebug: + print('GetBitsAndModsAndThreads: starting process "%s" (inpipe, outpipe, errpipe)\n' % '" "'.join(cmd)) + with process.Popen(cmd, stdin=process.PIPE, stdout=process.PIPE, + stderr=process.PIPE, text=True) as clnt: + input = '''\ + c := mdb.modules(); + modsid := algebra.unique(c,nil:bat); + mods := algebra.projection(modsid,c); + s := "\\nModules: "; + sep := ""; + barrier (h:oid,t:str) := iterator.new(mods); + s := s + sep; + s := s + "\'"; + s := s + t; + s := s + "\'"; + sep := ","; + redo (h:oid,t:str) := iterator.next(mods); + exit h; + s := s + "\\n"; + io.printf(s); + ''' + ##module("NoModule"); + qOut, qErr = clnt.communicate(input=input) + proc.terminate() + sOut = proc.stdout.read() + sErr = proc.stderr.read() + proc.wait() + qOut = sOut + qOut + qErr = sErr + qErr + finally: + t.cancel() + if proc.returncode is None: + killProc(proc, proc.stderr, cmd) + proc.wait() if procdebug: - print('GetBitsAndModsAndThreads: starting process "%s" (inpipe, outpipe, errpipe)\n' % '" "'.join(cmd)) - clnt = process.Popen(cmd, stdin=process.PIPE, stdout=process.PIPE, - stderr=process.PIPE, text=True) - input = '''\ - c := mdb.modules(); - modsid := algebra.unique(c,nil:bat); - mods := algebra.projection(modsid,c); - s := "\\nModules: "; - sep := ""; - barrier (h:oid,t:str) := iterator.new(mods); - s := s + sep; - s := s + "\'"; - s := s + t; - s := s + "\'"; - sep := ","; - redo (h:oid,t:str) := iterator.next(mods); - exit h; - s := s + "\\n"; - io.printf(s); - ''' - ##module("NoModule"); - qOut, qErr = clnt.communicate(input=input) - proc.terminate() - sOut = proc.stdout.read() - sErr = proc.stderr.read() - proc.wait() - qOut = sOut + qOut - qErr = sErr + qErr - finally: - t.cancel() - if proc.returncode is None: - killProc(proc, proc.stderr, cmd) - proc.wait() - if procdebug: - print('GetBitsAndModsAndThreads: process exited "%s" (%s)\n' % ('" "'.join(cmd), proc.returncode)) + print('GetBitsAndModsAndThreads: process exited "%s" (%s)\n' % ('" "'.join(cmd), proc.returncode)) env['TST_MODS'] = [] env['TST_BITS'] = "" env['TST_INT128'] = "" @@ -1364,10 +1364,10 @@ def progress(count, total, test): def getkids(): # return a dictionary with process IDs as key and a list of child # processes as value - p = process.Popen(['ps', '-lu', os.getenv('USER')], - stdout=process.PIPE, stderr=process.PIPE, - text=True) - out, err = p.communicate() + with process.Popen(['ps', '-lu', os.getenv('USER')], + stdout=process.PIPE, stderr=process.PIPE, + text=True) as p: + out, err = p.communicate() if err: return {} lines = out.split('\n') @@ -1448,24 +1448,24 @@ def killProc(proc, outfile = None, cmd = else: cdb = None if cdb: - p = process.Popen([cdb, '-pv', '-p', str(proc.pid), - '-y', '%scache*;srv*http://msdl.microsoft.com/download/symbols' % sym, '-lines', '-c', '~*kP;!locks;q'], - stdout=process.PIPE, text=True) - out, err = p.communicate() + with process.Popen([cdb, '-pv', '-p', str(proc.pid), + '-y', '%scache*;srv*http://msdl.microsoft.com/download/symbols' % sym, '-lines', '-c', '~*kP;!locks;q'], + stdout=process.PIPE, text=True) as p: + out, err = p.communicate() else: out = '' else: try: - p = process.Popen(['pstack', str(proc.pid)], stdout=process.PIPE, - text=True) - try: - # pstack (gdb) sometimes hangs when trying to get the - # stack trace: kill it mercilessly if it does - t = Timer(60, reallyKill, args = [p]) - t.start() - except AttributeError: - t = None - out, err = p.communicate() + with process.Popen(['pstack', str(proc.pid)], stdout=process.PIPE, + text=True) as p: + try: + # pstack (gdb) sometimes hangs when trying to get the + # stack trace: kill it mercilessly if it does + t = Timer(60, reallyKill, args = [p]) + t.start() + except AttributeError: + t = None + out, err = p.communicate() if t is not None: t.cancel() except: @@ -1484,10 +1484,10 @@ def killProc(proc, outfile = None, cmd = elif os.name == 'nt': if procdebug: print('killProc: starting process "taskkill" "/F" "/T" "/PID" "%s"\n' % str(proc.pid)) - p = process.Popen(['taskkill','/F','/T','/PID',str(proc.pid)], - stdout=process.PIPE, stderr=process.PIPE, - text=True) - out, err = p.communicate() + with process.Popen(['taskkill','/F','/T','/PID',str(proc.pid)], + stdout=process.PIPE, stderr=process.PIPE, + text=True) as p: + out, err = p.communicate() if procdebug: print('killProc: process exited "taskkill" "/F" "/T" "/PID" "%s" (%s)\n' % (str(proc.pid), proc.returncode)) proc.kill() @@ -1628,25 +1628,25 @@ def RunIt(cmd, onechild, TestIn, TestOut if procdebug: print('RunIt: starting process "%s"\n' % '" "'.join(cmd)) setpgrp = True - proc = process.Popen(cmd, stdin=TestIn, stdout=TestOut, - stderr=TestErr, text=True) - proc.killed = False - proc.onechild = onechild - t = Timer(TimeOut, killProc, args = [proc, TestErr, cmd]) - try: - t.start() - # since both stdout and stderr are redirected to files, - # communicate will not return any useful data - proc.communicate(input = TestInput) - t.cancel() - if procdebug: - print('RunIt: process exited "%s" (%s)\n' % ('" "'.join(cmd), proc.returncode)) - except KeyboardInterrupt: - t.cancel() - killProc(proc, TestErr, cmd) - if procdebug: - print('RunIt: process killed "%s"\n' % '" "'.join(cmd)) - raise + with process.Popen(cmd, stdin=TestIn, stdout=TestOut, + stderr=TestErr, text=True) as proc: + proc.killed = False + proc.onechild = onechild + t = Timer(TimeOut, killProc, args = [proc, TestErr, cmd]) + try: + t.start() + # since both stdout and stderr are redirected to files, + # communicate will not return any useful data + proc.communicate(input = TestInput) + t.cancel() + if procdebug: + print('RunIt: process exited "%s" (%s)\n' % ('" "'.join(cmd), proc.returncode)) + except KeyboardInterrupt: + t.cancel() + killProc(proc, TestErr, cmd) + if procdebug: + print('RunIt: process killed "%s"\n' % '" "'.join(cmd)) + raise rc = returnCode(proc, TestErr) if rc == 'interrupt': raise KeyboardInterrupt @@ -1980,23 +1980,23 @@ def Check(command, input) : if procdebug: print('Check: starting process "%s" (inpipe,outpipe,errpipe)\n' % '" "'.join(command)) setpgrp = True - proc = process.Popen(command, stdin=process.PIPE, stdout=process.PIPE, - stderr=process.PIPE, text=True) - proc.killed = False - proc.onechild = True - t = Timer(float(par['TIMEOUT']), killProc, args = [proc]) - try: - t.start() - qOut, qErr = proc.communicate(input = input) - t.cancel() - if procdebug: - print('Check: process exited "%s" (%s)\n' % ('" "'.join(command), proc.returncode)) - except KeyboardInterrupt: - t.cancel() - killProc(proc) - if procdebug: - print('Check: process killed "%s"\n' % '" "'.join(command)) - raise + with process.Popen(command, stdin=process.PIPE, stdout=process.PIPE, + stderr=process.PIPE, text=True) as proc: + proc.killed = False + proc.onechild = True + t = Timer(float(par['TIMEOUT']), killProc, args = [proc]) + try: + t.start() + qOut, qErr = proc.communicate(input = input) + t.cancel() + if procdebug: + print('Check: process exited "%s" (%s)\n' % ('" "'.join(command), proc.returncode)) + except KeyboardInterrupt: + t.cancel() + killProc(proc) + if procdebug: + print('Check: process killed "%s"\n' % '" "'.join(command)) + raise qOut = qOut.split('\n') qErr = qErr.split('\n') failed = False @@ -2166,10 +2166,10 @@ if os.name == "nt": r = re.compile('^Microsoft Windows (.*)\[Version ([0-9]+\.[0-9]+)([^\[0-9].*)\]$') if procdebug: print('starting process "cmd" "/c" "ver" (inpipe,outpipe)\n') - proc = process.Popen('cmd /c ver', stdin=process.PIPE, - stdout=process.PIPE, stderr=process.PIPE, - text=True) - qOut, qErr = proc.communicate() + with process.Popen('cmd /c ver', stdin=process.PIPE, + stdout=process.PIPE, stderr=process.PIPE, + text=True) as proc: + qOut, qErr = proc.communicate() if procdebug: print('process exited "cmd" "/c" "ver" (%s)\n' % proc.returncode) for l in qOut.split('\n'): @@ -2319,19 +2319,19 @@ def main(argv) : r_noecho = '--no-echo' if CheckExec('R'): - proc = process.Popen(['R', '--version'], - stdout=process.PIPE, stderr=process.PIPE, - text=True) - r_out, r_err = proc.communicate() + with process.Popen(['R', '--version'], + stdout=process.PIPE, stderr=process.PIPE, + text=True) as proc: + r_out, r_err = proc.communicate() res = re.search(r'R version (?P<major>\d+)\.', r_out) if res is not None and int(res.group('major')) < 4: r_noecho = '--slave' _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list