[mod_python] StringField patch for 3.3.10

Mike Looijmans nlv11281 at natlab.research.philips.com
Fri May 25 02:28:43 EDT 2007


Since I'm the one responsible for the StringField redesign in 3.3, I feel obliged to respond.

The suggested changes will break a lot more than they will fix. The strange implementation of the
__repr__ functions have been done that way to remain compatible with the existing code. The
StringField and Field classes (there used to be only one in mod_python 3.2) were made to be
compatible with the CGI version of fields.

The StringField can be made picklable by adding a __getstate__ and, if needed, a __setstate__
function, which will not break existing code. Probably, something like this will do (untested code):

...
     def __getstate__(self):
         return {'name': self.name, 'value': self.value}
...

That is a change that we could commit into the SVN archive i guess.

A Field instance (uploaded file with no size limit) should never be pickled - it contains a handle
to a file that will be deleted when the current request handler ends.

Graham's suggestion to translate the fields into something neutral, i suggest using a dictonary, is
simple enough, so I strongly advise taking that route.

--
Mike Looijmans
Philips Natlab / Topic Automation


Graham Dumpleton wrote:
> We can look at your changes, but the documentation does say about the
> Field class:
> 
> """This class is used internally by FieldStorage and is not meant to
> be instantiated by the user."""
> 
> In other words it is an implementation detail and you perhaps
> shouldn't be expecting it to be able to be pickled and unpickled, the
> later equating to instantiation by the user.
> 
> You would perhaps be better off using the public API to translate the
> form/fields into a neutral format and then doing the pickle on that.
> 
> BTW, I presume you meant 3.3.1 rather than 3.3.10.
> 
> Graham
> 
> On 25/05/07, Luca Montecchiani <l.montecchiani at teamsystem.com> wrote:
> 
>> Hi all,
>>
>> after a while I figure out all the problems withe the new
>> StringField objects and I've cooked a patch that works for me.
>> The patch try to fix the following problems:
>>
>> 1- StringField not pickable anymore with modpython 3.3.10
>> 2- StringField eval() problem with modpython 3.3.10
>> 3- avoid to put the entire file content on Field __repr__ method
>>
>> please let me know if this could be an acceptable way to address
>> all this compatibility problems.
>>
>> thanks in advance,
>> luca
>>
>> -- 
>> Luca Montecchiani
>> Software Di Base
>> TeamSystem S.p.a.
>> ------------------------------------------------------------------------------------------ 
>>
>> Informativa ai sensi del D. Lgs. 196-30/06/2003.
>> Il contenuto di questa e.mail e degli eventuali allegati, deve essere 
>> nella disponibilità
>> del solo destinatario.  Se ricevete per errore questa e-mail siete 
>> pregati di informarci
>> (rispedendola al mittente) e di provvedere alla sua rimozione.
>> Possono essere presenti informazioni riservate e non corrette 
>> (parzialmente o totalmente).
>> Le e-mail in partenza e in arrivo possono essere oggetto di monitoraggio
>> da parte di Teamsystem spa.  Del contenuto è responsabile il mittente 
>> della presente.
>> Chiunque venga in possesso non autorizzato di questa e-mail è vincolato
>> dalla Legge a non leggerne il contenuto, a non copiarla, a non 
>> diffonderla e a non usarla.
>> Informiamo che per l' esercizio dei diritti di cui all'art. 7 del 
>> d.lgs.196/2003 ci si può
>> rivolgere al Titolare del trattamento Teamsystem S.r.l. via Gagarin 
>> 205 61100 PESARO
>> per posta o fax, indicando sulla busta o sul foglio la dicitura 
>> "Inerente alla Privacy",
>> o inviando una e-mail all' indirizzo privacy at teamsystem.com .
>> ------------------------------------------------------------------------------------------ 
>>
>>
>> --- util.py.orig        Wed Nov 22 12:15:54 2006
>> +++ util.py     Thu May 24 23:06:42 2007
>> @@ -83,7 +83,12 @@
>>
>>      def __repr__(self):
>>          """Return printable representation."""
>> -        return "Field(%s, %s)" % (`self.name`, `self.value`)
>> +        if self.file:
>> +            # avoid to put the entire file content in the
>> +            # returned string !
>> +            return "Field(%s, %s)" % (`self.name`, `self.filename`)
>> +        else:
>> +            return "Field(%s, %s)" % (`self.name`, `self.value`)
>>
>>      def __getattr__(self, name):
>>          if name != 'value':
>> @@ -126,8 +131,8 @@
>>          return self.file
>>
>>      def __repr__(self):
>> -        """Return printable representation (to pass unit tests)."""
>> -        return "Field(%s, %s)" % (`self.name`, `self.value`)
>> +        # this is necessary if you want to eval a StringField
>> +        return self.value
>>
>>  class FieldList(list):
>>
>> @@ -330,15 +335,18 @@
>>              if disp_options.has_key("filename"):
>>                  field = Field(name)
>>                  field.filename = disp_options["filename"]
>> +                # file and headers attributes are only
>> +                # for Field objects because they will make
>> +                # a StringField unpickable
>> +                field.file = file
>> +                field.headers = headers
>>              else:
>>                  field = StringField(file.read())
>>                  field.name = name
>> -            field.file = file
>>              field.type = ctype
>>              field.type_options = type_options
>>              field.disposition = disp
>>              field.disposition_options = disp_options
>> -            field.headers = headers
>>              self.list.append(field)
>>
>>      def add_field(self, key, value):
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> 


-- 
Mike Looijmans
Philips Natlab / Topic Automation




More information about the Mod_python mailing list