Hacker News new | ask | show | jobs
by amirouche 4765 days ago
This is not a translator but an interpreter, there is no compilation.

edit: except FUD, can you provide some points on which brython is failling, please ?

4 comments

Not that I have anything against Brython (which imo is a cool project, although I'm not so sure if it's practical yet), but since you asked for it, here's an example where it does differ from python:

  x = 0
  
  def impure():
      global x
      x += 1
      return x

  print(0 < impure() <= 1)
In python, impure() would only be evaluated once returning True, but Brython's chaining evaluates it twice, and so it returns False instead. Of course, this example isn't exactly something you'd usually see in actual code (heck, it's probably even bad practice in most situations) BUT the point here is that while most of python might be compatible with this, there exists some inconsistencies that causes one's previously working python code to fail (and these bugs would probably be very difficult to trace). Of course, Brython is still young and has time to hopefully fix these issues. (In fact, this is the second time I've tried this project. Back then they didn't even have the chaining feature working, so it's nice that they are making progress.)
Nifty example, but this is why we have integration test suites.

PyPy, jython, ironpython etc all had/have incompatibilities as the language evolves and as they work out the quirks in their respective runtimes.

This isn't compatible with cpython (afaik, none of the other runtimes have ever been COMPLETELY compatible, but this is much further off), but hell, it's pretty awesome.

As an aside, I'd shoot anyone who did that ^^ without a pretty amazing explaination.

Well... if brython is running the CPython integration tests to aim for proper compatibility then fair enough.

It is a cool project, whichever one of these python in javasript things is to become successful needs to aim for the same level of compatibility as say Jython or IronPython - yes it would take a long time, but this sort of thing is needed to be a useful implementation.

There's no support for metaclasses. type is missing entirely. nonlocal doesn't work. docstrings and some other double underscore names are just missing (__class__, __name__, etc.). Unicode literals don't work. dis obviously won't work since there isn't a bytecode compilation step. Scoping, like in the following code, behaves improperly:

  x = 1  
  def test():  
      print(x)  
      x = 2
I wonder if it's worth adding some failing tests to the project about these..
Things like this are why projects like pypy use CPython's test suite whereever the behavior is not CPython specific, and have contributed many tests back to CPython as well. These aren't minutia of the language, they are well understood and tested features that most moderately sized software projects are likely to rely on at some point.
That's not right, it is a translator/compiler. It simply translates a python code string into a JS code string, and then calls `eval()` on the JS code string. The `eval()` step can optionally be skipped so that you can precompile. This is much easier to implement than an interpreter anyway.
Yield support is buggy:

    def f():
        n = 0
        while n < 10:
          yield n
          n += 1
          print(n)
    g = f()
    print(next(g))
This prints 0, 1, ... 10, 0 instead of just 0