sony / v8eval

Multi-language bindings to JavaScript engine V8
MIT License
403 stars 37 forks source link

Python 3.x: basestring Compatibility Issue #11

Closed micahcochran closed 8 years ago

micahcochran commented 8 years ago

basestring in the file _v8eval.py is not defined in Python 3.x.

Dump from IPython Console version 3.4.3

In [1]: import v8eval

In [2]: def add(x, y):
   ...:         v8 = v8eval.V8()
   ...:         v8.eval('var add = (x, y) => x + y;')
   ...:         return v8.call('add', [x, y])
   ...: 

In [3]: add(1,2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-2b87182c7dd4> in <module>()
----> 1 add(1,2)

<ipython-input-2-eae51d9e69b5> in add(x, y)
      1 def add(x, y):
      2         v8 = v8eval.V8()
----> 3         v8.eval('var add = (x, y) => x + y;')
      4         return v8.call('add', [x, y])
      5 

/usr/local/lib/python3.4/dist-packages/v8eval.py in eval(self, src)
    154             V8Error: If some JavaScript exception happens.
    155         """
--> 156         if not isinstance(src, basestring):
    157             raise TypeError('source code not string')
    158 

NameError: name 'basestring' is not defined

In [4]: 

Output from $ python3 -m unittest test_v8eval.py:

micah@micah-computer:~/prj2/v8eval/python/test$ python3 -m test_v8eval.py 
/usr/bin/python3: Error while finding spec for 'test_v8eval.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
micah@micah-computer:~/prj2/v8eval/python/test$ python3 -m unittest test_v8eval.py 
E.EException in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/home/micah/prj2/v8eval/python/test/test_v8eval.py", line 15, in run
    self.v8.eval('function inc(x) { return x + 1; }')
  File "/usr/local/lib/python3.4/dist-packages/v8eval.py", line 156, in eval
    if not isinstance(src, basestring):
NameError: name 'basestring' is not defined
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/home/micah/prj2/v8eval/python/test/test_v8eval.py", line 15, in run
    self.v8.eval('function inc(x) { return x + 1; }')
  File "/usr/local/lib/python3.4/dist-packages/v8eval.py", line 156, in eval
    if not isinstance(src, basestring):
NameError: name 'basestring' is not defined

.
======================================================================
ERROR: test_call (test_v8eval.V8TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/micah/prj2/v8eval/python/test/test_v8eval.py", line 38, in test_call
    v8.eval('function inc(x) { return x + 1; }')
  File "/usr/local/lib/python3.4/dist-packages/v8eval.py", line 156, in eval
    if not isinstance(src, basestring):
NameError: name 'basestring' is not defined

======================================================================
ERROR: test_eval (test_v8eval.V8TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/micah/prj2/v8eval/python/test/test_v8eval.py", line 24, in test_eval
    self.assertEqual(v8.eval('1 + 2'), 3)
  File "/usr/local/lib/python3.4/dist-packages/v8eval.py", line 156, in eval
    if not isinstance(src, basestring):
NameError: name 'basestring' is not defined

----------------------------------------------------------------------
Ran 4 tests in 0.659s

FAILED (errors=2)

Python six addresses this by basically doing this:

PY3 = sys.version_info[0] == 3
if PY3:
    string_types = str,
else:
    string_types = basestring,

Anywhere you use basestring you may replace it with string_types.

Note: Python 3.x strings can contain Unicode characters, and Python 2.x basestring only contain bytes (with unicode characters being multiple escaped bytes). This may or many not be a problem for v8, but you should be well aware that Python 2 and 3 are very different in this regard to string types.

This may not be the only compatibility issue regarding Python 3.x.

YoshiyukiMineo commented 8 years ago

Thank you for your report. I will fix this issue.

YoshiyukiMineo commented 8 years ago

I have fixed this issue. Let me know if any issues.

micahcochran commented 8 years ago

Thank you!