I'm working with python-gnupg to decrypt a file and the decrypted file content is very large so loading the entire contents into memory is not feasible.
I would like to short-circuit the write method in order to to manipulate the decrypted contents as it is written.
Here are some failed attempts:
import gpg
from StringIO import StringIO
# works but not feasible due to memory limitations
decrypted_data = gpg_client.decrypt_file(decrypted_data)
# works but no access to the buffer write method
gpg_client.decrypt_file(decrypted_data, output=buffer())
# fails with TypeError: coercing to Unicode: need string or buffer, instance found
class TestBuffer:
def __init__(self):
self.buffer = StringIO()
def write(self, data):
print('writing')
self.buffer.write(data)
gpg_client.decrypt_file(decrypted_data, output=TestBuffer())
Can anyone think of any other ideas that would allow me to create a file-like str or buffer object to output the data to?
selfindef write.stringio.StringIOor anio.StringIOthere? If the former, I don't see where any coercing to Unicode is happening, so you'll need to give us a minimal reproducible example that shows the code and what happens. If the latter… well, you should still give us a minimal reproducible example demonstrating the error, but in that case you're mixing and matching 3.x-styleiostack with the 2.x-style file-object stack, which is doable, but can get confusing.strorbufferobj to theoutputparam.