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

Campbell Barton ideasman42 at gmail.com
Wed Oct 21 02:34:53 CEST 2009


- rna strings can use index or string lookups so the getattr function
would need to support things like...
context.object['foo'].loc[0]

even if you extend getattr to do this Id much rather reuse the
existing animation code which attempts to do very fast lookups.
since a getattr that parses strings this complex is basically
re-implementing what we have in C.

exec is a bit dodgy but it serves a purpose well for now, added this
so I could quickly add in keybindings to adjust settings,

while its great having people look over the code and finding ways to
improve, this particular function isn't significant if you compare
with other parts of the api..

If you want to look into ways python use could be improved in ways
that benefit blender speed/api use see..
- dynamic_menu.py, subclasses menu's so new items can be added. Id be
happy to see this rewritten with nicer api access.
- bpy_rna.c - pyrna_func_call, is called many times while redrawing,
any improvements here would be good.
- autocomplete.py, used for console autocomplete, misses pythons
__builtins__ when searching the namespace, could be good to review
this code in general too.



On Wed, Oct 21, 2009 at 2:03 AM, Martin Poirier <theeth at yahoo.com> wrote:
>
>
> --- 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.
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at blender.org
> http://lists.blender.org/mailman/listinfo/bf-committers
>



-- 
- Campbell


More information about the Bf-committers mailing list