You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expect to be able to do the following:
#!/usr/bin/python
import fake_filesystem as fakefs
def main():
fs = fakefs.FakeFilesystem()
exec('open = fakefs.FakeFileOpen(fs)')
exec('file = fakefs.FakeFileOpen(fs)')
open(name='/foo', mode='w', buffering=1)
file(filename='/bar', mode='w', bufsize=1)
if __name__ == '__main__': main()
I expect to see NO output. Instead I see:
Traceback (most recent call last):
File "./test.py", line 13, in <module>
if __name__ == '__main__': main()
File "./test.py", line 10, in main
open(name='/foo', mode='w', buffering=1)
TypeError: __call__() got an unexpected keyword argument 'buffering'
I'm seeing this with the latest sources in Linux. However, this defect should
affect all operating systems considering that the Python open and file
documentation describe the standard named arguments as 'mode' and either
'buffering' or 'bufsize' (it's different for file and open). The current code
only supports named arguments 'flags' and 'bufsize'.
http://docs.python.org/library/functions.html#file
http://docs.python.org/library/functions.html#open
Here is my patch:
me@localhost:~/code/pyfakefs$ hg diff
diff -r 50ec7acd8b72 fake_filesystem.py
--- a/fake_filesystem.py Sat Jun 11 13:57:58 2011 -0400
+++ b/fake_filesystem.py Sat May 12 00:09:30 2012 -0700
@@ -2006,6 +2006,54 @@
return fakefile
+class FakeFileBuiltin(FakeFileOpen):
+ """Faked file() function replacement.
+
+ Returns FakeFile objects in a FakeFilesystem in place of the file() function.
+ """
+ def __call__(self, filename, mode='r', bufsize=-1):
+ """Returns a StringIO object with the contents of the target file object.
+
+ Args:
+ filename: path to target file
+ mode: additional file flags. All r/w/a r+/w+/a+ flags are supported.
+ 't', 'b', and 'U' are ignored, e.g., 'wb' is treated as 'w'.
+ bufsize: ignored. (Used for signature compliance with __builtin__.file)
+
+ Returns:
+ a StringIO object containing the contents of the target file
+
+ Raises:
+ IOError: if the target object is a directory, the path is invalid or
+ permission is denied.
+ """
+ return FakeFileOpen.__call__(self, filename, mode, bufsize)
+
+
+class FakeOpenBuiltin(FakeFileOpen):
+ """Faked open() function replacement.
+
+ Returns FakeFile objects in a FakeFilesystem in place of the open() function.
+ """
+ def __call__(self, name, mode='r', buffering=-1):
+ """Returns a StringIO object with the contents of the target file object.
+
+ Args:
+ name: path to target file
+ mode: additional file flags. All r/w/a r+/w+/a+ flags are supported.
+ 't', 'b', and 'U' are ignored, e.g., 'wb' is treated as 'w'.
+ buffering: ignored. (Used for signature compliance with __builtin__.open)
+
+ Returns:
+ a StringIO object containing the contents of the target file
+
+ Raises:
+ IOError: if the target object is a directory, the path is invalid or
+ permission is denied.
+ """
+ return FakeFileOpen.__call__(self, name, mode, buffering)
+
+
def _RunDoctest():
# pylint: disable-msg=C6204
import doctest
Here is a dumb test program that demonstrates the named arguments are now
correct:
#!/usr/bin/python
import fake_filesystem as fakefs
def main():
fs = fakefs.FakeFilesystem()
exec('open = fakefs.FakeOpenBuiltin(fs)')
exec('file = fakefs.FakeFileBuiltin(fs)')
open(name='/foo', mode='w', buffering=1)
file(filename='/bar', mode='w', bufsize=1)
if __name__ == '__main__': main()
I would have submitted this patch through mercurial, but the Code Contribution
page appears to be under construction.
Original issue reported on code.google.com by [email protected] on 12 May 2012 at 7:17
Original issue reported on code.google.com by
[email protected]
on 12 May 2012 at 7:17Attachments:
The text was updated successfully, but these errors were encountered: