1

I am trying to calculate an md5 hash of a file from stdin using Python 3

Here is the error message returned. I can't see why it doesn't return the md5 hash. Any help appreciated.

$./pymd5.py < tmp.pdf
Traceback (most recent call last):
  File "./pymd5.py", line 29, in <module>
    main()
  File "./pymd5.py", line 25, in main
    print(m.hexdigest())
TypeError: 'str' does not support the buffer interface
$ 

The code:

#!/usr/local/bin/python3.2

import sys
import hashlib

BUFSIZE = 4096

def make_streams_binary():
    sys.stdin  = sys.stdin.detach()
    sys.stdout = sys.stdout.detach()

def main():
    make_streams_binary()
    m = hashlib.md5()
    while True:
        data = sys.stdin.read(BUFSIZE)
        if not data:
            break
        m.update(data)

    print(m.hexdigest())

if __name__ == "__main__":
    main()
Duke Dougal
  • 24,359
  • 31
  • 91
  • 123
  • possible duplicate of [TypeError: 'str' does not support the buffer interface](http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface) – agf Apr 18 '12 at 04:12
  • I'm sorry that doesn't really explain what the problem is. – Duke Dougal Apr 18 '12 at 04:13

1 Answers1

5

When you do

sys.stdout = sys.stdout.detach()

It removes the ability to print normally at the terminal on Python 3, because you get a buffer instead of one wrapped for encoding and decoding. Before you print, you should do:

sys.stdout = sys._stdout

To get the original stdout back.

agf
  • 171,228
  • 44
  • 289
  • 238