Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: repy/restrictions.py
- ===================================================================
- --- repy/restrictions.py (revision 2298)
- +++ repy/restrictions.py (working copy)
- @@ -88,6 +88,7 @@
- known_calls = [ "canceltimer", "exitall", "file.close", "file.flush",
- "file.__init__", "file.next", "file.read", "file.readline",
- "file.readlines", "file.seek", "file.write", "file.writelines",
- + "file.truncate",
- "listdir", "removefile", "gethostbyname_ex", "getmyip", "open",
- "openconn", "recvmess", "sendmess", "settimer", "sleep",
- "socket.close", "socket.recv", "socket.send", "stopcomm",
- Index: repy/emulfile.py
- ===================================================================
- --- repy/emulfile.py (revision 2298)
- +++ repy/emulfile.py (working copy)
- @@ -95,8 +95,11 @@
- The mode (see open)
- <Exceptions>
- - As with open, this may raise a number of errors
- + As with open, this may raise a number of errors. Additionally:
- + TypeError if the mode is not a string.
- + ValueError if the modestring is invalid.
- +
- <Side Effects>
- Opens a file on disk, using a file descriptor. When opened with "w"
- it will truncate the existing file.
- @@ -105,12 +108,104 @@
- A file-like object
- """
- - restrictions.assertisallowed('open',filename,mode)
- + if type(mode) is not str:
- + raise TypeError("Attempted to open file with invalid mode (must be a string).")
- - return emulated_file(filename, mode)
- + # We just filter out 'b' / 't' in modestrings because emulated_file opens
- + # everything in binary mode for us.
- +
- + originalmode = mode
- +
- + if 'b' in mode:
- + mode = mode.replace('b','')
- +
- + if 't' in mode:
- + mode = mode.replace('t','')
- +
- + # Now we use our very safe, cross-platform open-like function and other
- + # file-object methods to emulate ANSI file modes.
- +
- + file_object = None
- + if mode == "r":
- + file_object = safe_open(filename, "r")
- +
- + elif mode == "r+":
- + file_object = safe_open(filename, "rw")
- +
- + elif mode == "w" or mode == "w+":
- + file_object = safe_open(filename, "rw", create=True)
- + file_object.truncate()
- +
- + elif mode == "a" or mode == "a+":
- + file_object = safe_open(filename, "rw", create=True)
- + file_object.seek(0, os.SEEK_END)
- +
- +
- + if file_object is None:
- + raise ValueError("Invalid mode ('%s') passed to open()." % originalmode)
- +
- + return file_object
- +
- +
- +
- +
- +def safe_open(filename, mode="r", create=False):
- + """
- + <Purpose>
- + Allows the user program to open a file safely. This function is not
- + meant to resemble the builtin "open".
- +
- + <Arguments>
- + filename:
- + The file that should be operated on
- + mode:
- + The mode:
- + "r": Open the file for reading.
- + "rw": Open the file for reading and writing.
- +
- + These are the only valid modes accepted by this version of
- + open(). Note: files opened with this function are always opened in
- + "binary" mode.
- + create:
- + If True, create the file if it doesn't exist already.
- +
- + <Exceptions>
- + As with open, this may raise a number of errors. Additionally:
- +
- + ValueError is raised if this is passed an invalid mode.
- +
- + <Side Effects>
- + Opens a file on disk, using a file descriptor.
- +
- + <Returns>
- + A file-like object
- + """
- +
- + # Only allow 'r' and 'rw'.
- +
- + actual_mode = None
- + if mode == "r":
- + actual_mode = "rb"
- + elif mode == "rw":
- + actual_mode = "r+b"
- +
- + if actual_mode is None:
- + raise ValueError("Valid modes for opening a file in repy are 'r' and 'rw'.")
- + restrictions.assertisallowed('open', filename, actual_mode)
- + if create and not os.path.exists(filename):
- + # Create a file by opening it in write mode and then closing it.
- + restrictions.assertisallowed('open', filename, "wb")
- + create_file = emulated_file(filename, "wb")
- + create_file.close()
- +
- + return emulated_file(filename, actual_mode)
- +
- +
- +
- +
- # This keeps the state for the files (the actual objects, etc.)
- fileinfo = {}
- @@ -318,5 +413,16 @@
- return retval
- + def truncate(self, size=None):
- + # Truncate a file to wherever. Needed to emulate "w" / "w+" C89 file modes.
- + myfilehandle = self.filehandle
- + restrictions.assertisallowed('file.truncate', size)
- +
- + if size is None:
- + return fileinfo[myfilehandle]['fobj'].truncate()
- + else:
- + return fileinfo[myfilehandle]['fobj'].truncate(size)
- +
- +
- # End of emulated_file class
Add Comment
Please, Sign In to add comment