beeware / batavia

A JavaScript implementation of the Python virtual machine.
http://pybee.org/batavia
Other
1.39k stars 424 forks source link

Confused END_FINALLY: TypeError: undefined is not a valid argument for 'instanceof' #246

Closed jstoebel closed 8 years ago

jstoebel commented 8 years ago

Working on String.__ge__ I encountered this error when running tests. Would it be appropriate to take a crack at this? Any words of wisdom/hints?

Traceback (most recent call last):

  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield

  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()

  File "/Users/stoebelj/Dropbox/batavia/tests/utils.py", line 846, in func
    substitutions=SAMPLE_SUBSTITUTIONS

  File "/Users/stoebelj/Dropbox/batavia/tests/utils.py", line 893, in assertBinaryOperation
    substitutions=substitutions

  File "/Users/stoebelj/Dropbox/batavia/tests/utils.py", line 496, in assertCodeExecution
    self.assertEqual(js_out, py_out, context)

  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 797, in assertEqual
    assertion_func(first, second, msg=msg)

  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 1170, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))

  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 642, in fail
    raise self.failureException(msg)

AssertionError: '>>> [50 chars]= y\nConfused END_FINALLY: TypeError: undefine[79 chars]')\n' != '>>> [50 chars]= y\n<class \'TypeError\'> : unorderable types[739 chars]\n\n'
  >>> x = ""
  >>> y = bytearray(b"hello world")
  >>> x >= y
- Confused END_FINALLY: TypeError: undefined is not a valid argument for 'instanceof' (evaluating 'obj instanceof type')
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
+ >>> x = "3"
+ >>> y = bytearray(b"hello world")
+ >>> x >= y
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
+ >>> x = "This is another string"
+ >>> y = bytearray(b"hello world")
+ >>> x >= y
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
+ >>> x = "Mÿ hôvèrçràft îß fûłl öf éêlś"
+ >>> y = bytearray(b"hello world")
+ >>> x >= y
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
+ >>> x = "One arg: %s"
+ >>> y = bytearray(b"hello world")
+ >>> x >= y
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
+ >>> x = "Three args: %s | %s | %s"
+ >>> y = bytearray(b"hello world")
+ >>> x >= y
+ <class 'TypeError'> : unorderable types: str() >= bytearray()
+ 
 : Global context: Error running x >= y
freakboy3742 commented 8 years ago

Ok - "Confused END_FINALLY" is Batavia's way of telling you an exception happened somewhere internally. It's the equivalent of Python's problem where an exception gets caught and rethrown, and then you lose the original source of the exception.

It's not going to be trivial to solve, but as long as you've got some perseverance, a can-do attitude, and you can drive the javascript debugger in your browser, it's just going to be time consuming, not difficult.

The place to start: the exception says "undefined is not a valid argument for instanceof". This means that somewhere in the chain evaluating "" > bytearray(b"hello world"), a call is made to check obj instanceof type - but type is not defined. Internally, Batavia shouldn't ever be getting an undefined - it uses None in a few places, but if you're getting undefined, it means that either the wrong type is being passed around, or a non-existent attribute has been requested.

So - the first debugging step - find out which call to instanceof is raising the exception, and trace back until you find where the undefined value has come from. Once you've got that worked out, you need to make sure you don't get an undefined value. Although that last part probably sounds daunting, I'm willing to bet it will be fairly obvious - a really obvious case where a type is missing from a list, or something like that.

jstoebel commented 8 years ago

Fortunately for me, the problem was a simple one: I had misspelled a batavia type.

candeira commented 8 years ago

Hi, jstoebel, I'm working on bytes(), was planning to do str.encode() next and implement bytes/str equality, then do bytearray(). I see from this issue that you are working on bytearray() too, am I mistaken? My working branch is here: https://github.com/pybee/batavia/pull/221

jstoebel commented 8 years ago

Hi @candeira I'm not actively working on this at the moment, opting instead to work on the time module. It sounds like you're motivated so go ahead. You won't be stepping on my toes.

Thanks for the check in though!

candeira commented 8 years ago

No worries, cheers!