Guest User

Untitled

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. Index: repy/restrictions.py
  2. ===================================================================
  3. --- repy/restrictions.py (revision 2298)
  4. +++ repy/restrictions.py (working copy)
  5. @@ -88,6 +88,7 @@
  6. known_calls = [ "canceltimer", "exitall", "file.close", "file.flush",
  7. "file.__init__", "file.next", "file.read", "file.readline",
  8. "file.readlines", "file.seek", "file.write", "file.writelines",
  9. + "file.truncate",
  10. "listdir", "removefile", "gethostbyname_ex", "getmyip", "open",
  11. "openconn", "recvmess", "sendmess", "settimer", "sleep",
  12. "socket.close", "socket.recv", "socket.send", "stopcomm",
  13. Index: repy/emulfile.py
  14. ===================================================================
  15. --- repy/emulfile.py (revision 2298)
  16. +++ repy/emulfile.py (working copy)
  17. @@ -95,8 +95,11 @@
  18. The mode (see open)
  19.  
  20. <Exceptions>
  21. - As with open, this may raise a number of errors
  22. + As with open, this may raise a number of errors. Additionally:
  23.  
  24. + TypeError if the mode is not a string.
  25. + ValueError if the modestring is invalid.
  26. +
  27. <Side Effects>
  28. Opens a file on disk, using a file descriptor. When opened with "w"
  29. it will truncate the existing file.
  30. @@ -105,12 +108,104 @@
  31. A file-like object
  32. """
  33.  
  34. - restrictions.assertisallowed('open',filename,mode)
  35. + if type(mode) is not str:
  36. + raise TypeError("Attempted to open file with invalid mode (must be a string).")
  37.  
  38. - return emulated_file(filename, mode)
  39. + # We just filter out 'b' / 't' in modestrings because emulated_file opens
  40. + # everything in binary mode for us.
  41. +
  42. + originalmode = mode
  43. +
  44. + if 'b' in mode:
  45. + mode = mode.replace('b','')
  46. +
  47. + if 't' in mode:
  48. + mode = mode.replace('t','')
  49. +
  50. + # Now we use our very safe, cross-platform open-like function and other
  51. + # file-object methods to emulate ANSI file modes.
  52. +
  53. + file_object = None
  54. + if mode == "r":
  55. + file_object = safe_open(filename, "r")
  56. +
  57. + elif mode == "r+":
  58. + file_object = safe_open(filename, "rw")
  59. +
  60. + elif mode == "w" or mode == "w+":
  61. + file_object = safe_open(filename, "rw", create=True)
  62. + file_object.truncate()
  63. +
  64. + elif mode == "a" or mode == "a+":
  65. + file_object = safe_open(filename, "rw", create=True)
  66. + file_object.seek(0, os.SEEK_END)
  67. +
  68. +
  69. + if file_object is None:
  70. + raise ValueError("Invalid mode ('%s') passed to open()." % originalmode)
  71. +
  72. + return file_object
  73. +
  74. +
  75. +
  76. +
  77. +def safe_open(filename, mode="r", create=False):
  78. + """
  79. + <Purpose>
  80. + Allows the user program to open a file safely. This function is not
  81. + meant to resemble the builtin "open".
  82. +
  83. + <Arguments>
  84. + filename:
  85. + The file that should be operated on
  86. + mode:
  87. + The mode:
  88. + "r": Open the file for reading.
  89. + "rw": Open the file for reading and writing.
  90. +
  91. + These are the only valid modes accepted by this version of
  92. + open(). Note: files opened with this function are always opened in
  93. + "binary" mode.
  94. + create:
  95. + If True, create the file if it doesn't exist already.
  96. +
  97. + <Exceptions>
  98. + As with open, this may raise a number of errors. Additionally:
  99. +
  100. + ValueError is raised if this is passed an invalid mode.
  101. +
  102. + <Side Effects>
  103. + Opens a file on disk, using a file descriptor.
  104. +
  105. + <Returns>
  106. + A file-like object
  107. + """
  108. +
  109. + # Only allow 'r' and 'rw'.
  110. +
  111. + actual_mode = None
  112. + if mode == "r":
  113. + actual_mode = "rb"
  114. + elif mode == "rw":
  115. + actual_mode = "r+b"
  116. +
  117. + if actual_mode is None:
  118. + raise ValueError("Valid modes for opening a file in repy are 'r' and 'rw'.")
  119.  
  120. + restrictions.assertisallowed('open', filename, actual_mode)
  121.  
  122. + if create and not os.path.exists(filename):
  123. + # Create a file by opening it in write mode and then closing it.
  124. + restrictions.assertisallowed('open', filename, "wb")
  125.  
  126. + create_file = emulated_file(filename, "wb")
  127. + create_file.close()
  128. +
  129. + return emulated_file(filename, actual_mode)
  130. +
  131. +
  132. +
  133. +
  134. # This keeps the state for the files (the actual objects, etc.)
  135. fileinfo = {}
  136.  
  137. @@ -318,5 +413,16 @@
  138. return retval
  139.  
  140.  
  141. + def truncate(self, size=None):
  142. + # Truncate a file to wherever. Needed to emulate "w" / "w+" C89 file modes.
  143. + myfilehandle = self.filehandle
  144. + restrictions.assertisallowed('file.truncate', size)
  145. +
  146. + if size is None:
  147. + return fileinfo[myfilehandle]['fobj'].truncate()
  148. + else:
  149. + return fileinfo[myfilehandle]['fobj'].truncate(size)
  150. +
  151. +
  152. # End of emulated_file class
Add Comment
Please, Sign In to add comment