question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Brython not playing well with Opal.

See original GitHub issue

I am trying to get Opal (Ruby for JS) to play well with Brython.

But for some reason if Opal is loaded before Brython, and i then try to import datetime, i get an error.

If i remove the script that loads Opal, the error goes away again.

Minimal example that has the error:

<!doctype html>
<html>
  <head>
    <script src="https://cdn.opalrb.com/opal/1.0.0/opal.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.8.8/brython.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.8.8/brython_stdlib.js"></script>
  </head>
  <body onload="brython(1)">
    <script type="text/python">
    import datetime
    </script>
  </body>
</html>

The error is:

Javascript exception: TypeError: Cannot read property 'length' of undefined
    at Object.$B.$getattr (brython.js:6908)
    at _is_dst3982 (eval at exec_module (brython.js:9272), <anonymous>:428:91)
    at eval (eval at exec_module (brython.js:9272), <anonymous>:733:65)
    at exec_module (brython.js:9272)
    at t (brython.js:6570)
    at s (brython.js:6574)
    at Object.e.import_hooks (brython.js:13989)
    at $B.$__import__ (brython.js:9428)
    at Object.$B.$import (brython.js:9472)
    at eval (eval at exec_module (brython.js:9272), <anonymous>:16:8)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
PierreQuentelcommented, Apr 3, 2020

The latest commit changes an internal attribute $value used by Brython for instances of subclasses of int or float to a more specific $brython_value. Opal apparently sets the attribute $value for numbers, which caused the incompatibility.

2reactions
PierreQuentelcommented, Apr 2, 2020

@dkgof Interesting !

In Brython, the class of an object is usually determined by its attribute __class__ but, for a reason that I don’t remember, the attribute $class was used instead for a particular class (module), so this attribute $class was tested in the function that gets an object’s class.

Unfortunately, it seems that Opal sets an attribute $class to Javascript’s Date objects…

In the commit referenced above I have removed the use of $class in Brython, and your code now works. I leave the issue open for the moment, in case you find other incompatibilities.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Idea] Compile Python code with Brython statically · Issue #1416
I have looked at code and even run it with debugger, but I did not get how to hook importing module with Brython...
Read more >
Frequently asked questions - Brython documentation
Another reason why it is a not a good idea to precompile Brython is that the generated code is typically 10 times bigger...
Read more >
Limitations of the "file" protocol - Brython documentation
With this protocol, it is always possible to import the modules in the standard library if the file brython_stdlib.js is loaded in the...
Read more >
Testing and debugging - Brython documentation
Testing and debugging. Interactive test. The Brythons site, or its mirror available for download, include a console where you can test Python code....
Read more >
module browser.aio - Brython documentation
It replaces the asyncio module in CPython standard library, which cannot work in the browser context: it uses blocking functions such as run()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found