brython-dev / brython

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

ajax.get with blocking=True doesn't block #1516

Closed ondrejj closed 4 years ago

ondrejj commented 4 years ago

I am testing your new version from git and failing with this error:

Traceback (most recent call last):
  File hra1/engine.py line 330, in preload_call
    preload(this())
  File hra1/engine.py line 158, in __call__
    for tileset in p.map_json["tilesets"]:
  File engine.py line 22, in __getattr__
    return self[item]
KeyError: map_json

Looks like this commit caused the problem: https://github.com/brython-dev/brython/commit/d242edfcae9b61900ceeb9abd9a40eed8885b6be

Not sure, what exactly is the problem, but here is part of my code:

class preload:
  def __init__(self, sounds=[]):
      # make list of sounds available globally                                  
      self.sounds = sounds
  def __call__(self, this):
      def json_loaded(req):
          p.map_json = JSON.parse(req.text)
      map_json_file = "map.json?%s" % time.time()
      ajax.get(map_json_file, blocking=True, oncomplete=json_loaded)
      this.load.tilemapTiledJSON("level1", map_json_file)
      for tileset in p.map_json["tilesets"]:

And looks like json_loaded is not called before last line, but there is an "ajax.get(...blocking=True), so this should be blocked until this function is complete.

Older versions works well.

PierreQuentel commented 4 years ago

This is strange, I can't reproduce the bug.

I have uploaded a version of the program at https://brython.info/tests/issue1516/, only modifying some links, and adding at line 148 of engine.py:

      def json_loaded(req):
          p.map_json = JSON.parse(req.text)
      map_json_file = "map.json?%s" % time.time()
      ajax.get(map_json_file, blocking=True, oncomplete=json_loaded)
      this.load.tilemapTiledJSON("level1", map_json_file)
      print(p.map_json["tilesets"]) # line added for debugging

The data actually shows in the browser console. The rest of the method fails, but that's because I didn't copy/paste the images and sounds.

ondrejj commented 4 years ago

I am sorry, looks like again my problem. brython_stdlib.js needs to be updated after install. I have to change my install script to do this automatically.

ondrejj commented 4 years ago

May be it would be useful to somehow detect this and write an error. For example save an build version at creation time and check it on load.