Commits

Shu Zong Chen  committed 549f168

Initial FakeFile implementation

  • Participants
  • Parent commits 35337e4

Comments (0)

Files changed (2)

File fakeos/__init__.py

+from fakeos import os
+from fakeopen import open

File fakeos/fakefile.py

+"""
+A modified wrapper for StringIO that does a better job at pretending to be a file.
+It also persists, so you can open it again after you call close().
+"""
+
+import StringIO
+
+def punt_to_stringio(cls):
+	for method in ['flush', 'fileno', 'isatty', 'next', 'read', 'readline', 'readlines', 'xreadlines', 'seek', 'tell', 'truncate', 'write', 'writelines']:
+		def wrapped(self, method=method, *args, **kwargs):
+			f = getattr(self.fh, method)
+			return f(*args, **kwargs)
+		setattr(cls, method, wrapped)
+	return cls
+
+@punt_to_stringio
+class FakeFile(object):
+	def __init__(self, data=None):
+		if data is None:
+			data = "hello world"
+		self.fh = StringIO.StringIO(data)
+		self.open = True
+	
+	def close(self):
+		self.open = False
+	
+	def __iter__(self):
+		print "__iter__ called"
+		return iter(self.fh)