Apr162008
Dude, where's my error? or How to make Python behave like PHP.
Filed under: pythonAs it turns out, in Python 2.4, eval ( ) was changed to allow any sort of mapping object (not just a plain Python dictionary) as the locals namespace. What this means is that we are free to pass in an object that can pretend it has all the variables in the world:
class Proxy ( dict ):
def __getattr__ ( self, attr ):
return Proxy ( )
def __getitem__ ( self, key ):
return Proxy ( )
def __call__ ( self, *args, **kw ):
return Proxy ( )
def __str__ ( self ):
return ''
eval ( 'x.y ( ).z [ 3 ]', { }, Proxy ( ) )
Despite the fact that none of the variables (x, y, z) exist in the code fragment we're evaluating, we get no exceptions. In fact we don't get a peep of output.
Clearly you'd need to flesh this class out a bit (like handling mathematical operations) and you'd probably want to actually have some values in it, so you'd need to define __setitem__ and __setattr__ behaviour, but that's left as an exercise for someone else.






My first thought is BLASPHEMY! And then I read on.