Hi Kees Bos,
Em 03-05-2012 14:05, Kees Bos escreveu:
On Thu, 2012-05-03 at 12:27 +0100, João Ventura wrote:
Trying again the following email, don't know if the list received it..
<rant>Who wrote translator_proto.py and the rest of the builtin's? And
what about comments on the code, so someone can understand what is
happening?</rant>
Quite a lot of people have contributed...
Yeah, just a rant.. Hard time knowing what variables are there to do
what.. Commented code would be important as it would be faster to
understand..
--------------------------------
Anyone,
in pyjs/builtin/_pyjs.js, in function $pyjs__class_instance(class_name,
module_name), does anyone knows what is the purpose of this piece of code?
if (cls_fn.__number__ !== null) {
instance = cls_fn.__new__.apply(null, [cls_fn, arguments[0]]);
args = arguments;
}
For what I've been able to understand, the function $pyjs__class_instance is
called when the class is being defined and also
when an object of a certain class is being created.
But if cls_fn.__number (as Luke said) is there to identify classes which are
instances of numbers, so this piece of code is saying something like "If you
are an instance of a subclass of a number, only your first
argument matters", correct?
But that is not true at all! If I do something like:
if (cls_fn.__number__ !== null&& 1==0) {
instance = cls_fn.__new__.apply(null, [cls_fn, arguments[0]]);
args = arguments;
}
this piece of code is never called and the arguments pass alright and
everything works fine again, as far as I could test..
Anyone knows something about this?!
I think that was for speed sake. The code in the else clause was
probably too slow if you'd created a lot of number instances.
Hum, yes, it seems so..
Btw, with this "&& 1==0" to ignore that if section, and defining an
__init__ with the same parameters as the __new__, I can instantiate an
object subclassing float, str or int.
So, by now if I have this:
class Float(float):
def __new__(cls, value=0.0, mult=1.0):
return float.__new__(cls, value*mult)
def is_positive(self):
if (self >= 0):
return True
return False
>>> f = Float(value=10.0, mult=2) # this works now!
20
>>> f.is_positive() # But this fails
Hello TypeError: $m.f.is_positive is not a function
>>> type(f)
function (value, radix) { if (typeof radix == "undefined") { radix =
arguments.callee.__args__[3][1]; } var v; if (typeof value.__int__ !=
"undefined") { return value.__int__(); } if (value.__number__) { if
(radix !== null) { throw $m.TypeError("int() can't convert non-string
with explicit base"); } v = value.valueOf(); if (v > 0) { v =
Math.floor(v); } else { v = Math.ceil(v); } } else if (typeof value ==
"string") { if (radix === null) { radix = 10; } value = value.lstrip();
switch (value[value.length - 1]) { case "l": case "L": v =
value.slice(0, value.length - 2); break; default: v = value; } if
(v.match($radix_regex[radix]) === null) { v = NaN; } else { v =
v.$$replace(" ", ""); v = parseInt(v, radix); } } else { throw
$m.TypeError("TypeError: int() argument must be a string or a number");
} if (isNaN(v) || !isFinite(v)) { throw $m.ValueError("invalid literal
for int() with base " + radix + ": '" + value + "'"); } return v; }
It seems that pyjs is severely broken when subclassing immutable types.
I'll give another example:
class String(str):
def __new__(cls, value=""):
return str.__new__(cls, value)
def __init__(self, value=""):
pass
def greet(self):
return "Hi %s" % (self)
>>> s = String(value="João Ventura")
João Ventura
>>> type(s)
class pyjslib.str
>>> s.greet()
Hello TypeError: $m.s.greet is not a function
In this case, it fails because pyjs converts s to a javascript string
object instead of an instance of "String". In CPython, that same code
returns:
>>> type(s)
<class '__main__.String'>
So it seems that many of the internals of pyjs (_pyjs.js, pyjslib.py,
maybe translator also) may have to be changed, possibly with the help of
developers who are more acquainted with the code.
This is very important for my projects, so it kinds of depend on the
future of this project.. Damn guys, why did you have to do this "hijack"
stunt now?! :S
Anyone wants to contribute?
Thanks,
João Ventura