Commits

Olemis Lang  committed 0131a5a

Brython #43 : Read-only properties. Limited support for descriptors __get__

  • Participants
  • Parent commits e498438
  • Branches t43_property

Comments (0)

Files changed (4)

+t43/t43_r418baddb9934_fix_py_classes_str.diff
+t43/t43_r418baddb9934_hex_oct.diff
+t43/t43_r418baddb9934_property.diff
 # Placed by Bitbucket

File t43/t43_r418baddb9934_fix_py_classes_str.diff

+# HG changeset patch
+# Parent ce4b5151548ef92f7addbdd4a09596f4eb008336
+Brython #43 : Fix __str__ for built-in types
+
+diff -r ce4b5151548e site/tests/index.html
+--- a/site/tests/index.html	Wed Apr 24 08:42:16 2013 +0200
++++ b/site/tests/index.html	Wed Apr 24 10:26:44 2013 -0500
+@@ -89,12 +89,13 @@
+     <option value="classes_tests.py">built-in classes</option>
+     <option value="test_classes.py">classes</option>
+     <option value="test_decorators.py">decorators</option>
++    <option value="test_dict.py">dicts</option>
+     <option value="dom_objects.py">DOM</option>
+     <option value="test_import.py">imports</option>
+     <option value="test_list_methods.py">lists</option>
++    <option value="test_print.py">print</option>
++    <option value="test_set.py">sets</option>
+     <option value="test_strings.py">strings</option>
+-    <option value="test_dict.py">dicts</option>
+-    <option value="test_set.py">sets</option>
+   </optgroup>
+   <optgroup label="Issues">
+     <option value="issues_gc.py">issues (GC)</option>
+diff -r ce4b5151548e site/tests/test_print.py
+--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
++++ b/site/tests/test_print.py	Wed Apr 24 10:26:44 2013 -0500
+@@ -0,0 +1,12 @@
++
++print(bool)
++print(dict)
++print(float)
++print(frozenset)
++print(int)
++print(object)
++print(set)
++print(tuple)
++print(list)
++print(Exception)
++
+diff -r ce4b5151548e src/py_classes.js
+--- a/src/py_classes.js	Wed Apr 24 08:42:16 2013 +0200
++++ b/src/py_classes.js	Wed Apr 24 10:26:44 2013 -0500
+@@ -481,7 +481,7 @@
+ float.__class__ = $type
+ float.__name__ = 'float'
+ float.__new__ = function(){return new $FloatClass(0.0)}
+-float.toString = function(){return "<class 'float'>"}
++float.toString = float.__str__ = function(){return "<class 'float'>"}
+ 
+ float.__hash__ = function() {
+     // http://cw.tactileint.com/++Floats/Ruby,JavaScript,Ruby
+@@ -696,7 +696,7 @@
+ int.__class__ = $type
+ int.__name__ = 'int'
+ int.__new__ = function(){return 0}
+-int.toString = function(){return "<class 'int'>"}
++int.toString = int.__str__ = function(){return "<class 'int'>"}
+ 
+ Number.prototype.__class__ = int
+ 
+@@ -980,7 +980,7 @@
+ }
+ object.__class__ = $type
+ object.__name__ = 'object'
+-object.__str__ = "<class 'object'>"
++object.toString = object.__str__ = function() { return "<class 'object'>" }
+ object.__hash__ = function () { 
+     __BRYTHON__.$py_next_hash+=1; 
+     return __BRYTHON__.$py_next_hash;

File t43/t43_r418baddb9934_hex_oct.diff

+# HG changeset patch
+# Parent 881897f34cfe2658a2f7b657a6867b1bcbfc15db
+Brython #43 : Wrong variable names in hex() and oct()
+
+diff -r 881897f34cfe site/tests/test_suite.py
+--- a/site/tests/test_suite.py	Wed Apr 24 10:26:44 2013 -0500
++++ b/site/tests/test_suite.py	Wed Apr 24 11:06:32 2013 -0500
+@@ -159,4 +159,11 @@
+ if not True:print('true!')
+ else:pass
+ 
++assert bin(12) == '0b1100'
++assert oct(12) == '0o14'
++assert hex(12) == '0xc'
++assert bin(-12) == '-0b1100'
++assert oct(-12) == '-0o14'
++assert hex(-12) == '-0xc'
++
+ print("passed all tests..")
+diff -r 881897f34cfe src/py_classes.js
+--- a/src/py_classes.js	Wed Apr 24 10:26:44 2013 -0500
++++ b/src/py_classes.js	Wed Apr 24 11:06:32 2013 -0500
+@@ -77,11 +77,18 @@
+      Exception('TypeError', 'Error, argument must be an integer or contains an __index__ function')
+      return
+   }
++  var prefix = "";
++  if (base == 8) { prefix = "0o" }
++  else if (base == 16) { prefix = '0x' }
++  else if (base == 2) { prefix = '0b' }
++  else {
++    // FIXME : Choose better prefix
++    prefix = ''
++  }
+   if (value >=0) { 
+-     return value.toString(base);
++     return prefix + value.toString(base);
+   } else {
+-    // todo:need to implement for negative values
+-    return null
++    return '-' + prefix + (-value).toString(base);
+   }
+ }
+ 
+@@ -653,7 +660,7 @@
+ 
+ //hex() (built in function)
+ function hex(x) {
+-   return $builtin_base_convert_helper(obj, 16)
++   return $builtin_base_convert_helper(x, 16)
+ }
+ 
+ //id() (built in function)
+@@ -990,7 +997,7 @@
+ 
+ // oct() (built in function)
+ function oct(x) {
+-   return $builtin_base_convert_helper(obj, 8)
++   return $builtin_base_convert_helper(x, 8)
+ }
+ 
+ function $open(){

File t43/t43_r418baddb9934_property.diff

+# HG changeset patch
+# Parent b48b863897098f31eb822fb12c0c08c951fc1dec
+Brython #43 : Property and descriptors
+
+diff -r b48b86389709 site/tests/test_classes.py
+--- a/site/tests/test_classes.py	Wed Apr 24 11:06:32 2013 -0500
++++ b/site/tests/test_classes.py	Wed Apr 24 22:21:14 2013 -0500
+@@ -24,7 +24,6 @@
+ assert obj.test1(2)=='test1test1'
+ assert obj.test2()=='test2'
+ 
+-#there is an error here, displayed on web console
+ assert obj.machin == 99
+ 
+ class stack(list):
+@@ -44,4 +43,24 @@
+     pass
+ assert str(bar())=='[]'
+ 
++# property and object descriptors
++
++class myclass:
++  def __init__(self):
++    self.a = 2
++
++  @property
++  def getx(self):
++      return self.a + 5
++
++assert myclass().getx == 7
++
++@property
++def gety(self):
++    return self.a + 9
++
++x.gety = gety
++
++assert x.gety is gety
++
+ print('passed all tests..')
+diff -r b48b86389709 src/py_classes.js
+--- a/src/py_classes.js	Wed Apr 24 11:06:32 2013 -0500
++++ b/src/py_classes.js	Wed Apr 24 22:21:14 2013 -0500
+@@ -1046,13 +1046,27 @@
+ function $prompt(text,fill){return prompt(text,fill || '')}
+ 
+ // property (built in function)
++function $PropertyClass() {
++    this.__class__ = "<class 'property'>"
++}
++
++$PropertyClass.prototype.__hash__ = object.__hash__
++$PropertyClass.prototype.toString = $PropertyClass.prototype.__str__ = function() {return "<property object at " + hex(this.__hash__()) + ">" }
++
+ function property(fget, fset, fdel, doc) {
+-   if (fget !== undefined) { return fget()}
+-   console.log(fset)
+-   console.log(fdel)
+-   throw NotImplementedError('property not implemented')
++    var p = new $PropertyClass()
++    p.__get__ = function(instance, factory) { return fget.__call__(instance) }; 
++    //p.__set__ = fdel; 
++    //p.__delete__ = fdel;
++    p.__doc__ = doc || "";
++    return p;
+ }
+ 
++property.__class__ = $type
++property.__name__ = 'property'
++property.toString = property.__str__ = function() { return "<class 'property'>" }
++property.__hash__ = object.__hash__
++
+ // range
+ function range(){
+     var $ns=$MakeArgs('range',arguments,[],{},'args',null)
+diff -r b48b86389709 src/py_utils.js
+--- a/src/py_utils.js	Wed Apr 24 11:06:32 2013 -0500
++++ b/src/py_utils.js	Wed Apr 24 22:21:14 2013 -0500
+@@ -267,7 +267,14 @@
+                 return "<bound method '"+attr+"' of "+obj.__class__.__name__+" object>"
+             }
+             return res
+-        }else{return obj[attr]}
++        }
++        else {
++            // FIXME: Improve descriptor access
++            res = obj[attr];
++            if (res.__get__ != undefined && typeof res.__get__==='function' && res === factory[attr])
++                res = res.__get__.apply(res, [obj, factory])
++            return res
++        }
+     }
+     if(factory[attr]!==undefined){
+         var res = factory[attr]
+@@ -287,6 +294,9 @@
+                 }
+             })(attr)
+         }
++        // FIXME: Improve descriptor access
++        if (res.__get__ !== undefined && typeof res.__get__==='function')
++            res = res.__get__.apply(res, [obj, factory])
+         return res
+     }else{ // inheritance
+         for(var i=0;i<factory.parents.length;i++){
+@@ -381,9 +391,12 @@
+     f.__str__ = function(){return "<class '"+class_name+"'>"}
+     for(var attr in factory){
+         f[attr]=factory[attr]
+-        f[attr].__str__ = (function(x){
+-            return function(){return "<function "+class_name+'.'+x+'>'}
+-            })(attr)
++        // FIXME: Improve
++        if (typeof f[attr] === 'function') {
++            f[attr].__str__ = (function(x){
++                return function(){return "<function "+class_name+'.'+x+'>'}
++                })(attr)
++        }
+     }
+     f.__getattr__ = function(attr){
+         if(f[attr]!==undefined){return f[attr]}