Hashlib MemoryError in Python 3.5 but not in 2.7
<p>I've been porting a set of Python 2.7 scripts to Python 3.5 so that I can use some libraries that aren't available in 2.7, but I'm getting MemoryError from this code that worked previously:</p>

<pre><code>import hashlib, functools
sha2h = hashlib.sha256()
with open('/path/to/any/file', 'rb') as f:
        [sha2h.update(chunk) for chunk in iter(functools.partial(f.read, 256), '')]
</code></pre>

<p>As far as I can tell, this is the proper way to get a SHA256 hash of a file. I can't seem to find anything about this issue. If it helps, here's the traceback when the above code is run from the shell:</p>

<pre><code>File "&lt;stdin&gt;", line 2, in &lt;module&gt;
File "&lt;stdin&gt;", line 2, in &lt;listcomp&gt;
</code></pre>

<p>Also tried replacing</p>

<pre><code>sha2h = hashlib.sha256()
</code></pre>

<p>with</p>

<pre><code>sha2h = hashlib.new('sha256')
</code></pre>

<p>to match the documentation for hashlib, but this yielded no change in outcome.</p>

<p>Any help or insight would be greatly appreciated!</p>

<p>On Python 3, you need to set <code>b''</code> instead of <code>''</code> as the sentinel value for the <code>iter</code> call:</p>

<pre><code>iter(functools.partial(f.read, 256), b'')
</code></pre>

<p>You also really shouldn't be using a list comprehension for side effects like this, but if you're porting existing code that does that, it's probably best to just leave that for now.</p>

python python-3.x hashlib functools