[Bf-committers] Patch: removing use of exec() and eval() from python source

Martin Poirier theeth at yahoo.com
Wed Oct 21 02:03:40 CEST 2009



--- On Tue, 10/20/09, Mathias Panzenböck <grosser.meister.morti at gmx.net> wrote:

> From: Mathias Panzenböck <grosser.meister.morti at gmx.net>
> Subject: Re: [Bf-committers] Patch: removing use of exec() and eval() from python source
> To: "bf-blender developers" <bf-committers at blender.org>
> Received: Tuesday, October 20, 2009, 7:27 PM
> On 10/21/2009 12:32 AM, Martin
> Poirier wrote:
> > 
> > --- On Tue, 10/20/09, Mathias Panzenböck <grosser.meister.morti at gmx.net>
> wrote:
> >
> >> It just occurred to me that it should be possible
> to
> >> write:
> >> exec("context.%s = self.value" % self.path)
> >>
> >> and:
> >> exec("context.%s = self.value_2 if
> context.%s!=self.value_2
> >> else self.value_1" %
> >> (self.path, self.path))
> >>
> >> Which prevents the unnecessary conversion to and
> from
> >> strings of the values.
> > 
> > The operator properties need to be defined with a
> specific type, evaluating the value means it can convert to
> whatever type is needed from a string property.
> > 
> >
> 
> So then the solution would be:
> exec("context.%s = float(self.value)" % self.path)
> 
> and:
> 
> exec("context.%s = str(self.value_2) if
> context.%s!=str(self.value_2) else
> str(self.value_1)" % (self.path, self.path))
> 
> or something like:
> 
> value_2 = str(self.value_2)
> exec("""\
> if context.%s != value_2:
>     context.%s = value_2
> else:
>     context.%s = str(self.value_1)
> """ % (self.path, self.path, self.path))
> 
> Which will allocate new objects *only* if the parameter
> isn't already of the
> right type and prevent precision loss and encoding problems
> etc.

self.value is always assured to be a string unless we have one operator per type, that's what I'm saying.

This is due to the rna property definition.

> Anyway, "evaluating the value means it can convert to
> whatever type is needed
> from a string property" is just nonsense when you use %d
> and %f format strings
> (as is done right now) because of:

All of them use - exec("[...] = %s" % value) - right now, so the string property is evaluate. (as it would do in your 3rd example earlier, except it doesn't need to convert to string since it's already assured to be one).

That works whether the property you are trying to change is a string, a float, an int, a boolean, ... as long as the string value provided evaluates to the correct type.

Just to be clear, I too find the use of exec perfectly distasteful (and eval only slightly so), I'm just trying to make sure the proposed code can do what the current one does.

Martin


      __________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now
http://ca.toolbar.yahoo.com.


More information about the Bf-committers mailing list