2

I'm using PyV8 and I'd like to call a javascript function with undefined. It seems that evaluating both undefined and null return Python's None value:

>>> evaljs("undefined")
None
>>> evaljs("null")
None

The problem, of course, is that they're not the same in javascript:

>>> evaljs("undefined === null")
False
False
>>> evaljs("undefined") == evaljs("null")
None
None
True

Is there any nice way to go about doing this? I'd actually like to write a Python function, callable from javascript, which returns undefined in certain cases and null in other cases.

EDIT for SSCCE-ness:

import PyV8

context = PyV8.JSContext()

def evaljs(s):
    with context:
        res = context.eval(s)
        print res
        return res
Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
Claudiu
  • 224,032
  • 165
  • 485
  • 680

2 Answers2

0

As you can see here: null object in Python?, there is no null or undefined in Python, just None... so you would only be able to represent undefined and null from JavaScript as None in Python. JavaScript makes a distinction between the two for its own reasons and, as far as I can tell from my own experiences, "undefined" is fairly unique to JavaScript.

The only thing I can think of would be to get down into the source and see how undefined and null are being tracked in the JavaScript v8 engine and trying to have that object return when you evaluate JavaScript statements with null or undefined results... but that's quite the daunting task.

Community
  • 1
  • 1
Graham Robertson
  • 808
  • 5
  • 10
0

The answer is: in the current version there is no way to do this.

Claudiu
  • 224,032
  • 165
  • 485
  • 680