[Python-Dev] Proposed patch to cgi.py for 2.1 -- please review!

Guido van Rossum guido@digicool.com
Thu, 12 Apr 2001 13:47:12 -0500


> > (It could break code if someone tries to use the fileno() on a field
> > object's file attribute.)
> 
> Switch to a real file when someone accesses the file attribute?

Hm.  I can do that, but I'm less happy about the resulting mess. :-(

Here's the patch.  I think I'get back to this post-2.1...

--Guido van Rossum (home page: http://www.python.org/~guido/)

Index: cgi.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/cgi.py,v
retrieving revision 1.63
diff -c -r1.63 cgi.py
*** cgi.py	2001/03/19 13:40:44	1.63
--- cgi.py	2001/04/12 16:45:50
***************
*** 509,515 ****
                  raise ValueError, 'Maximum content length exceeded'
          self.length = clen
  
!         self.list = self.file = None
          self.done = 0
          if ctype == 'application/x-www-form-urlencoded':
              self.read_urlencoded()
--- 509,515 ----
                  raise ValueError, 'Maximum content length exceeded'
          self.length = clen
  
!         self.list = self.__file = None
          self.done = 0
          if ctype == 'application/x-www-form-urlencoded':
              self.read_urlencoded()
***************
*** 524,531 ****
--- 524,537 ----
                  `self.name`, `self.filename`, `self.value`)
  
      def __getattr__(self, name):
+         if name == 'file':
+             self.file = self.__file_incarnate()
+             self.file.seek(0)
+             return self.file
          if name != 'value':
              raise AttributeError, name
+         if self.__file:
+             return self.__file.getvalue()
          if self.file:
              self.file.seek(0)
              value = self.file.read()
***************
*** 614,620 ****
              self.skip_lines()
          else:
              self.read_lines()
!         self.file.seek(0)
  
      bufsize = 8*1024            # I/O buffering size for copy to file
  
--- 620,627 ----
              self.skip_lines()
          else:
              self.read_lines()
!         if not self.__file:
!             self.file.seek(0)
  
      bufsize = 8*1024            # I/O buffering size for copy to file
  
***************
*** 633,644 ****
  
      def read_lines(self):
          """Internal: read lines until EOF or outerboundary."""
!         self.file = self.make_file('')
          if self.outerboundary:
              self.read_lines_to_outerboundary()
          else:
              self.read_lines_to_eof()
  
      def read_lines_to_eof(self):
          """Internal: read lines until EOF."""
          while 1:
--- 640,665 ----
  
      def read_lines(self):
          """Internal: read lines until EOF or outerboundary."""
!         self.__file = StringIO()
          if self.outerboundary:
              self.read_lines_to_outerboundary()
          else:
              self.read_lines_to_eof()
  
+     def __file_incarnate(self):
+         file = self.make_file('')
+         file.write(self.__file.getvalue())
+         self.__file = None
+         return file
+ 
+     def __write(self, line):
+         if self.__file is not None:
+             if self.__file.tell() + len(line) <= 1000:
+                 self.__file.write(line)
+                 return
+             self.file = self.__file_incarnate()
+         self.file.write(line)
+ 
      def read_lines_to_eof(self):
          """Internal: read lines until EOF."""
          while 1:
***************
*** 646,652 ****
              if not line:
                  self.done = -1
                  break
!             self.file.write(line)
  
      def read_lines_to_outerboundary(self):
          """Internal: read lines until outerboundary."""
--- 667,673 ----
              if not line:
                  self.done = -1
                  break
!             self.__write(line)
  
      def read_lines_to_outerboundary(self):
          """Internal: read lines until outerboundary."""
***************
*** 674,680 ****
                  line = line[:-1]
              else:
                  delim = ""
!             self.file.write(odelim + line)
  
      def skip_lines(self):
          """Internal: skip lines until outer boundary if defined."""
--- 695,701 ----
                  line = line[:-1]
              else:
                  delim = ""
!             self.__write(odelim + line)
  
      def skip_lines(self):
          """Internal: skip lines until outer boundary if defined."""