I would like to run some code on the Python version of Google App Engine that uses the built in File type. I’m looking for the easiest way to stop GAE from throwing errors due to illegal access. Has anyone already sub-classed or mocked File to read and write to memory rather than to the disk? I don’t need persistence, just the ability to simulate file access.
views:
81answers:
1
+5
A:
import __builtin__
import StringIO
class File(StringIO.StringIO):
def __init__(self, *a, **k): pass
__builtin__.file = __builtin__.open = File
You'll surely want finer-grained simulation, but this works as a very rough first cut.
Alex Martelli
2010-02-01 04:06:13
"class File(StringIO.StringIO):", no?
Kevin Little
2010-02-01 04:54:51
Oh yes, `class`, not `def` -- tx, editing to fix.
Alex Martelli
2010-02-01 05:17:41
Thanks. This works great from the Python interpreter. However in Google App Engine dev_server, whenever I run something like fout=open('output.txt','w'), I get an error such as: File "\google_appengine\google\appengine\tools\dev_appserver.py", line 1180, in __init__ raise IOError('invalid mode: %s' % mode)IOError: invalid mode: wI've tried a few different ways to run the code but I can't seem to override open. Any ideas?
Chris
2010-02-02 03:47:57
@Chris, consider you can't count on persistence of the code in memory: you have to repeat the "override" on every query you serve (if the SDK is overriding your override in `__builtin__`, try setting `.open` and `.file` in every *module* of interest -- `for n in themodules: sys.modules[n].open = File` etc).
Alex Martelli
2010-02-02 04:52:58
Rather than using StringIO and then adding all the missing attributes like closed, is it possible to take the same approach and create the new File class based on mmap?
Chris
2010-02-02 07:40:25
@Chris, it might be, but `mmap` is not supported on App Engine.
Alex Martelli
2010-02-02 15:35:18