0

I have in the code I maintain:

app7z = dirs['mopy'].join('7z.exe').s # path to 7z.exe
command = '"%s" a "%s" -y -r "%s\\*"' % (app7z, dstFile.temp.s, srcDir.s)
ins = Popen(command, stdout=PIPE, startupinfo=startupinfo).stdout
#--Error checking and progress feedback
reCompressing = re.compile('Compressing\s+(.+)')
regMatch = reCompressing.match
reError = re.compile('Error: (.*)')
regErrMatch = reError.match
errorLine = []
for line in ins:
    maCompressing = regMatch(line)
    if len(errorLine) or regErrMatch(line):
        errorLine.append(line)
    if maCompressing:
        # update progress
result = ins.close() # THIS
if result:
    dstFile.temp.remove()
    raise StateError(_("%s: Compression failed:\n%s") % (dstFile.s, 
                       "\n".join(errorLine)))

(full code)

Does ins.close() return a non None value on failure ? My IDE (pycharm 3.4.2/4.5.2) warns me that it does not, but then not consistently.

I am on windows if this makes a difference - python 2.7.8

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

1 Answers1

0

What do you think, close can return? You probably want to use wait to get the exit code:

app7z = dirs['mopy'].join('7z.exe').s # path to 7z.exe
command = [app7z, 'a', dstFile.temp.s, "-y", "-r", os.path.join(src.Dir.s, '*')]
process = Popen(command, stdout=PIPE, startupinfo=startupinfo)
out = process.stdout
regMatch = re.compile('Compressing\s+(.+)').match
regErrMatch = re.compile('Error: (.*)').match
errorLine = []
for line in out:
    maCompressing = regMatch(line)
    if len(errorLine) or regErrMatch(line):
        errorLine.append(line)
    if maCompressing:
        # update progress
result = process.wait()
if result:
    dstFile.temp.remove()
    raise StateError(_("%s: Compression failed:\n%s") % (dstFile.s, 
                       "\n".join(errorLine)))
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • I did not write the code and I do not expect anything - whoever wrote that probably expected close to return a 'non zero' value for failure. Now would I need to wait ? Apparently the code reads `7z` output line by line and would know if an error occurred. Or is it better style to just wait anyway ? Also - would I need to still `close()` if I call wait ? – Mr_and_Mrs_D Jun 21 '15 at 17:04
  • Apparently wait can deadlock - would it be better to use communicate() - and then check err for non null value ? Also do I need to out.close() ? Thanks – Mr_and_Mrs_D Jun 22 '15 at 08:26
  • Ask a different question for those: http://stackoverflow.com/questions/30982217/python-popen-wait-vs-communicate-vs-calledprocesserror – Mr_and_Mrs_D Jun 22 '15 at 14:15