brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser
BSD 3-Clause "New" or "Revised" License
6.4k stars 512 forks source link

events() and unbind() behaves wrongly when using local functions #2512

Closed jibalamy closed 2 weeks ago

jibalamy commented 2 weeks ago

Hello,

I am encountering some unexpected behaviors with the events() and unbind() methods. Here is a short example:

<html>
<head>
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>

<body onload="brython(0)">
<div id="div"> TEST </div>

<script type="text/python">
from browser import document

def listener1(e = None): print("1")
def listener2(e = None): print("2")

document["div"].bind("click", listener1)
document["div"].bind("click", listener2)

print(listener1 in document["div"].events("click")) # print False, should be True
print(listener2 in document["div"].events("click")) # print False, should be True
document["div"].unbind("click", listener2)

</script>
</body></html>

The page print "False" two times in the console, while it should be True (the listener are those bound previously).

Then, if you click on the TEST div, it prints "2" in the console, while it should print "1". listener2 was removed, but actually, it is listener1 that is wrongly removed.

Thank you for providing Brython, Best regards, Jiba

PierreQuentel commented 2 weeks ago

Thanks for the report Jiba !