[Bf-committers] Patch: removing use of exec() and eval() from python source
Mathias Panzenböck
grosser.meister.morti at gmx.net
Wed Oct 21 01:27:53 CEST 2009
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.
>
> Martin
>
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.
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:
>>> '%f' % '0.2'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float argument required, not str
-panzi
More information about the Bf-committers
mailing list