Commits

Lucian Brănescu-Mihăilă  committed a1f9c72

Add get_bool and get_integer overrides on instance. get_bool calls obj.__bool__ always. get_integer returns the value if obj is an int, or otherwise calls __bool__.

  • Participants
  • Parent commits 611e9d4

Comments (0)

Files changed (4)

File objects/src/instance.winxed

         
         return string(id(self));
     }
+
+    function get_bool[vtable] () {
+        var bool = self.__bool__;
+
+        if(bool != null) return bool(self);
+
+        return true;
+    }
+
+    function get_integer[vtable] () {
+        var cls = self.__class__;
+        if(cls != null) {
+            var name = cls.__name__;
+
+            if(name != null && name == 'int')
+                return self.__value__;
+        }
+    
+        var bool = self.__bool__;
+        if(bool != null) return bool(self);
+    }
 }
 
 }

File objects/src/type.winxed

 namespace Python {
 
+function get_class_attr(cls, key, obj) {
+   for(var i in get_mro(cls)) {
+        if(exists i.__dict__[key]) {
+            var attr = i.__dict__[key];
+
+            // only for python objects
+            if(typeof(attr) == 'Python;instance')
+                if(exists attr.__dict__['__get__']) {
+                    return attr.__dict__['__get__'](attr, obj);
+                }
+
+            return attr;
+        }
+    }
+
+    return null;
+}
+
 function type() {
     var t = new Python.instance;
 
             else
                 sb.append_format("<class '%0.%1'>", obj.__module__, obj.__name__);
         else
-            return null; //TypeError
+            throw 'TypeError';
         
         return string(sb);
     };
         var cls = obj.__dict__['__class__'];
         var chain = get_mro(cls);
         
+        cry('trying descriptor in class attrs');    
         // first try descriptor among class attrs
         for(var i in chain) {
             if(exists i.__dict__[key]) {
                 if(typeof(attr) == 'Python;instance')
                     // if data-descriptor
                     if(exists attr.__dict__['__get__'] && exists attr.__dict__['__set__']) {
-                        cry('using data descriptor');
+                        //cry('using data descriptor');
                         return attr.__dict__['__get__'](attr, obj);
                     }
             }
         }
 
+        cry('trying on actual obj');
         // then attr on actual obj
         if(id(cls) == id(t)) { // if obj is a class
-            cry('obj is class');
+            //cry('obj is class');
+            
             for(var i in get_mro(obj)) {
                 if(exists i.__dict__[key]) {
-                    cry('getting from obj or parents');
+                    //cry('getting from obj or parents');
                     var attr = i.__dict__[key];
 
                     // only for python objects
                     return attr;
                 }
             }
+            
+            //var attr = get_class_attr(obj, key, obj);
+            //if(attr) return attr;
         } else {
-            cry('obj is not class');
+            //cry('obj is not class');
             if(exists obj.__dict__[key]) {
                 return obj.__dict__[key];
             }
         }
 
+        cry('trying on obj class');
         // then on the obj's class
+        
         for(var i in chain) {
             if(exists i.__dict__[key]) {
                 var attr = i.__dict__[key];
             }
         }
 
+        //var attr = get_class_attr(cls, key, obj);
+        //if(attr) return attr;
+
         throw 'AttributeError';
     };
 

File objects/t/instance.t

         self.assert.equal(i.f(1, 2), 3);
     }
 
-    function get_string() {
+    function get_string_override() {
         var i = new Python.instance;
         i.__repr__ = function(obj) {return '42';};
 
         self.assert.equal(string(i), '42');
     }
 
+    function get_bool_default() {
+        var i = new Python.instance;
+
+        self.assert.is_true(i ? 1 : 0);
+    }
+
+    function get_bool_from_int_default() {
+        var i = new Python.instance;
+
+        self.assert.is_true(i);
+    }
+
+    function get_bool_override() {
+        var i = new Python.instance;
+        i.__bool__ = function(obj) {return false;};
+
+        self.assert.is_false(i ? 1 : 0);
+    }
+
+    function get_bool_from_int_override() {
+        var i = new Python.instance;
+        i.__bool__ = function(obj) {return false;};
+
+        self.assert.is_false(i);
+    }
+
     function call_class() {
         var i = new Python.instance;
 

File objects/t/typeobject.t

     function object_new() {
         :(var t, var o) = boot();
         
-        var i = o.__new__(o);
+        var i = o();
 
         self.assert.same(i.__class__, o);
     }
         
         self.assert.same(i.__class__, o);
     }
-        
+ 
+    function instance_get() {
+        :(var t, var o) = boot();
+
+        var i = o();
+        i.bla = 2;
+
+        self.assert.same(i.bla, i.__dict__['bla']);
+    }
+       
     function instance_class_get() {
         :(var t, var o) = boot();
 
-        var i = o.__new__(o);
+        var i = o();
 
         o.bla = 42;
-
+        
         self.assert.same(i.bla, o.bla);
     }
 
     function instance_parent_get() {
         :(var t, var o) = boot();
 
-        var i = o.__new__(o);
+        var i = o();
 
         t.bla = 42;
 
     function instance_set_attr() {
         :(var t, var o) = boot();
 
-        var i = o.__new__(o);
+        var i = o();
         i.foo = 'bar';
 
         self.assert.equal(i.foo, 'bar');
     function non_data_descriptor() {
         :(var t, var o) = boot();
 
-        var i = o.__new__(o);
+        var i = o();
         i.__get__ = function(obj, attr) {return 42;};
         t.i = i;