pythonpython-3.xsubprocessperforcechangelist

Subprocess.Popen process.communicate() function error


I tried this solution answer to solve my problem but when using the solution I got this error:

Traceback (most recent call last):
  File "version_build.py", line 125, in <module>
    if __name__=='__main__': main()
  File "version_build.py", line 113, in main
    change = p4change(p4)
  File "version_build.py", line 90, in p4change
    change = p.communicate(changespec)[0]
  File "C:\Python36\Lib\subprocess.py", line 843, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "C:\Python36\Lib\subprocess.py", line 1086, in _communicate
    self._stdin_write(input)
  File "C:\Python36\Lib\subprocess.py", line 781, in _stdin_write
    self.stdin.write(input)
TypeError: a bytes-like object is required, not 'str'

My main question is: Why am I getting this error, am I missing something from the solution given to the other question?

I am using python3. I have tried to use the str.encode() function, but when printing out the variable to the console, it is blank. b''

Here are the lines it is failing on (mainly the p.communicate line):

    changespec = 'change: new\n' + description
    
    p = subprocess.Popen([p4, 'change', '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    change = p.communicate(changespec)[0]
    return change

Solution

  • The "p4 change" command is probably failing because you haven't provided it a valid changespec (or you aren't logged in, or any number of other possible errors). To debug further you'll need to capture stderr from the command; there should be an error message that will explain exactly what's gone wrong.

    See the comments in the answer you linked about using the P4Python API. It's a much easier solution to this problem; among other things it will automatically surface errors as useful Python exceptions.