<p dir="ltr"><br>
On Nov 27, 2013 2:11 PM, "Ned Batchelder" <<a href="mailto:ned@nedbatchelder.com">ned@nedbatchelder.com</a>> wrote:<br>
><br>
> On 11/27/13 2:40 PM, <a href="mailto:magnus.lycka@gmail.com">magnus.lycka@gmail.com</a> wrote:<br>
>><br>
>> So, in the case of "a.b + x" I'm really just interested in a and x, not b. So the (almost) whole story is that I do:<br>
>><br>
>>      # Find names not starting with ".", i.e a & b in "a.c + b"<br>
>>      abbr_expr = re.sub(r"\.\w+", "", expr)<br>
>>      names = compile(abbr_expr, '<string>', 'eval').co_names<br>
>>      # Python 2.6 returns '_[1]' in co_names for list comprehension. Bug?<br>
>>      names = [name for name in names if re.match(r'\w+$', name)]<br>
>><br>
>>      for name in names:<br>
>>          if name not in allowed_names:<br>
>>              raise NameError('Name: %s not permitted in expression: %s' % (name, expr))<br>
>><br>
><br>
> I don't know of a better way to determine the real names in the expression.  I doubt Python will insert a valid name into the namespace, since it doesn't want to step on real user names.  The simplest way to do that is to autogenerate invalid names, like "_[1]" (I wonder why it isn't "_[0]"?)</p>

<p dir="ltr">One possible alternative is to use the ast module to examine the parse tree of the expression instead of the generated code object. Hard to say whether that would be "better".<br>
</p>