Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 171

Show
Ignore:
Timestamp:
02/26/06 04:02:17
Author:
fumanchu
Message:

Added codewalk support for dict creation (BUILD_MAP, DUP_TOP, ROT_THREE, and STORE_SUBSCR).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/codewalk.py

    r170 r171  
    687687 
    688688 
     689class MapStackObject(dict): 
     690     
     691    def __add__(self, other): 
     692        if isinstance(other, basestring): 
     693            return repr(self) + other 
     694        return dict.__add__(self, other) 
     695     
     696    def __repr__(self): 
     697        return "{%s}" % ", ".join([": ".join((k, v)) 
     698                                   for k, v in self.iteritems()]) 
     699 
     700 
    689701class LambdaDecompiler(Visitor): 
    690702    """LambdaDecompiler(obj=lambda function or func_code). 
     
    773785                self.debug("newtarget:", self.last_target_ip, target) 
    774786     
     787    def visit_BUILD_LIST(self, lo, hi): 
     788        terms = ", ".join([self.stack.pop() for i in range(lo + hi << 8)]) 
     789        self.stack.append("[%s]" % terms) 
     790     
     791    def visit_BUILD_MAP(self, lo, hi): 
     792        # We're actually going to put a non-string object on the stack here, 
     793        # with the expectation that the next bytecodes will populate it. 
     794        self.stack.append(MapStackObject()) 
     795     
    775796    def visit_BUILD_TUPLE(self, lo, hi): 
    776797        terms = ", ".join([self.stack.pop() for i in range(lo + hi << 8)]) 
    777798        self.stack.append("(%s)" % terms) 
    778      
    779     def visit_BUILD_LIST(self, lo, hi): 
    780         terms = ", ".join([self.stack.pop() for i in range(lo + hi << 8)]) 
    781         self.stack.append("[%s]" % terms) 
    782799     
    783800    def visit_CALL_FUNCTION(self, lo, hi): 
     
    807824        self.stack.append(term1 + " " + op + " " + term2) 
    808825        self.debug(op) 
     826     
     827    def visit_DUP_TOP(self): 
     828        self.stack.append(self.stack[-1]) 
    809829     
    810830    def visit_JUMP_IF_FALSE(self, lo, hi): 
     
    855875        self.stack.pop() 
    856876     
     877    def visit_ROT_THREE(self): 
     878        v = self.stack.pop() 
     879        k = self.stack.pop() 
     880        x = self.stack.pop() 
     881        self.stack.extend([k, x, v]) 
     882     
    857883    def visit_SLICE_PLUS_0(self): 
    858884        arg = self.stack.pop() 
     
    873899        del self.stack[-3:] 
    874900        self.stack.append("%s[%s:%s]" % args) 
     901     
     902    def visit_STORE_SUBSCR(self): 
     903        v = self.stack.pop() 
     904        x = self.stack.pop() 
     905        k = self.stack.pop() 
     906        x[k] = v 
    875907     
    876908    def visit_UNARY_CONVERT(self): 
  • trunk/test/test_logic.py

    r170 r171  
    4949        e = logic.Expression(lambda x: x.Name in ['George', 'John']) 
    5050        self.assertEqual(repr(e), lx + "x.Name in ['George', 'John'])") 
     51         
     52        # Test varying return values. 
     53        e = logic.Expression(lambda x: x + 1) 
     54        self.assertEqual(repr(e), lx + "x + 1)") 
     55        e = logic.Expression(lambda x: x.Val + 3) 
     56        self.assertEqual(repr(e), lx + "x.Val + 3)") 
     57        e = logic.Expression(lambda x, y: x ** y) 
     58        self.assertEqual(repr(e), "logic.Expression(lambda x, y: x ** y)") 
     59        e = logic.Expression(lambda x: {'a': 1, 'b': 2}) 
     60        self.assertEqual(repr(e), lx + "{'a': 1, 'b': 2})") 
     61        e = logic.Expression(lambda x: {'a': 1, 'b': 2}[x.Name[0]]) 
     62        self.assertEqual(repr(e), lx + "{'a': 1, 'b': 2}[x.Name[0]])") 
    5163     
    5264    def test_pickling(self):