Closed redradist closed 4 years ago
For BigInt
there is a problem : it's trivial to create an instance of the Javascript object (without including it in the javascript
module, it's in the global Javascript namespace, so available through window
):
Brython 3.8.9 on Netscape 5.0 (Windows)
>>> from browser import window
>>> window.BigInt(2)
<BigInt object>
>>>
but operations on these objects are not supported
>>> b2 = window.BigInt(2)
>>> b3 = window.BigInt(3)
>>> b2 + b3
Traceback (most recent call last):
File <string>, line 1, in <module>
TypeError: unsupported operand type(s) for +: 'JSObject' and 'JSObject'
>>>
because these objects don't support all the dunder methods (__add__, __mul__
, etc.). Implementing them is going to be less trivial, all the more so as operations between this type and Python numeric types should be supported (integers, floats, booleans...).
The most stairghtforward would be to implement javascript.BigInt
to return a Python integer : this way, interaction with Python types would be supported.
@PierreQuentel
For
BigInt
there is a problem : it's trivial to create an instance of the Javascript object (without including it in thejavascript
module, it's in the global Javascript namespace, so available throughwindow
):Brython 3.8.9 on Netscape 5.0 (Windows) >>> from browser import window >>> window.BigInt(2) <BigInt object> >>>
but operations on these objects are not supported
>>> b2 = window.BigInt(2) >>> b3 = window.BigInt(3) >>> b2 + b3 Traceback (most recent call last): File <string>, line 1, in <module> TypeError: unsupported operand type(s) for +: 'JSObject' and 'JSObject' >>>
because these objects don't support all the dunder methods (
__add__, __mul__
, etc.). Implementing them is going to be less trivial, all the more so as operations between this type and Python numeric types should be supported (integers, floats, booleans...).The most stairghtforward would be to implement
javascript.BigInt
to return a Python integer : this way, interaction with Python types would be supported.
Yea, seems like it better to return Python integer ... I read documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt seems like there is not many special method, it would also nice to provide
And what about Symbol
?
@redradist
The commit referenced above implements operations (addition, multiplication etc.) on instances created with window.BigInt
, as shown in the examples in the test file
b1 = window.BigInt('23456')
b2 = window.BigInt('78901')
assert b1 + b2 == window.BigInt(str(23456 + 78901))
assert b1 - b2 == window.BigInt(str(23456 - 78901))
assert b1 * b2 == window.BigInt(str(23456 * 78901))
assert b1 % b2 == window.BigInt(str(23456 % 78901))
b3 = window.BigInt('2')
assert b1 ** b3 == window.BigInt(str(23456 ** 2))
for num in [1, 4.7]:
try:
b1 + num
raise Exception("should have raised TypeError")
except TypeError:
pass
I have done nothing special for Symbol
, I can't imagine useful use cases for this in a Python script.
@PierreQuentel
@redradist The commit referenced above implements operations (addition, multiplication etc.) on instances created with
window.BigInt
, as shown in the examples in the test fileb1 = window.BigInt('23456') b2 = window.BigInt('78901') assert b1 + b2 == window.BigInt(str(23456 + 78901)) assert b1 - b2 == window.BigInt(str(23456 - 78901)) assert b1 * b2 == window.BigInt(str(23456 * 78901)) assert b1 % b2 == window.BigInt(str(23456 % 78901)) b3 = window.BigInt('2') assert b1 ** b3 == window.BigInt(str(23456 ** 2)) for num in [1, 4.7]: try: b1 + num raise Exception("should have raised TypeError") except TypeError: pass
I have done nothing special for
Symbol
, I can't imagine useful use cases for this in a Python script.
Thanks ;)
What do you meant by Symbol
? It does not work still ?
Yes, it works:
from browser import window
symbol = window.Symbol("test")
assert symbol.description == "test"
Yes, it works:
from browser import window symbol = window.Symbol("test") assert symbol.description == "test"
Cool !! Thanks, @PierreQuentel !!
In the following
Python
code standardJavaScript
primitive types (BigInt
andSymbol
) are missed:Below the Stacktraces for both this lines
Missed
BigInt
:Missed
Symbol
: