views:

81

answers:

1

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.

+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
"class File(StringIO.StringIO):", no?
Kevin Little
Oh yes, `class`, not `def` -- tx, editing to fix.
Alex Martelli
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
@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
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
@Chris, it might be, but `mmap` is not supported on App Engine.
Alex Martelli