Rovango / cefpython

Automatically exported from code.google.com/p/cefpython
0 stars 0 forks source link

Can't convert unicode to V8 type... #10

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Here is the python code...

---cut---

import json

def dumpJSON(path, data):
    with open(path, 'w') as f:
        json.dump(data, f, indent=4)

def loadJSON(path):
    with open(path) as f:
        return json.load(f)

--uncut--

Basic usage example:

---cut---

dumpJSON('data.json', {
  key: 'value'
})

var o = loadJSON('data.json')

--uncut--

The above code both raises a python exceptions and kills the CEF instance 
(another bug?)...

This is how it fails:
'''
Exception: PyValueToV8Value() failed: an unsupported python type was passed 
from python to javascript: unicode
'''

The problem lies is that by default, json serializer uses utf-8 for all strings 
(and the file itself)

Versions used:
CEFPython 0.35
Python 2.7.2
OS WinXP SP3

Original issue reported on code.google.com by alex.na...@gmail.com on 28 Aug 2012 at 12:04

GoogleCodeExporter commented 8 years ago
There also appears to be no support for python tuples.

IMO should be converted to Arrays, asymmetric, yes, but that's what you get 
when converting between different type systems.

Original comment by alex.na...@gmail.com on 28 Aug 2012 at 12:20

GoogleCodeExporter commented 8 years ago
Unicode need to be converted to bytes using string.encode, so the code to work 
would need to be changed to:

def loadJSON(path):
    with open(path) as f:
        return json.load(f).encode('utf-8')

There was an another similar issue here:

http://code.google.com/p/cefpython/issues/detail?id=5&can=1

The question is whether we should do the conversion automatically? Call 
.encode('utf-8') on all unicode strings?

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 1:34

GoogleCodeExporter commented 8 years ago
Python tuples - yes we can convert it to array.

Are there any other types that come to your mind that we should support as well?

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 1:35

GoogleCodeExporter commented 8 years ago
The browser handles encodings on a per-file or pre-request basis, so I see no 
reason to deviate from this.
As JS has no separate types for bytes/unicode/strings it would seem logical to 
convert everything to String, the question is what should we use for 
encoding... 
I'd go for POLS here too, and that would be to encode automatically and use 
browser/CEF setting of encoding for that (defaulting to utf-8).

As to the list of types, the short answer would be "everything" simplejson/json 
libs support, but if you want I could compile a full list a bit later.

I'll do a more complete test on this within this week, we are planing a 
pre-alpha release within the first half of September, and at this point 
CEFPython is the primary target for Windows (would be nice if it would be the 
same for U*IX and MacOSX, but that's another issue)

Original comment by alex.na...@gmail.com on 28 Aug 2012 at 2:26

GoogleCodeExporter commented 8 years ago
Should this encoding setting be per browser or per application setting? We 
already have [ApplicationSettings] and [BrowserSettings]. Or maybe should we 
create a separate dictionary for cefpython specific settings so we don't mix it 
with what is already in CEF? To think.

If you could compile a full list that would be great. Currently we support:
 * list
 * bool
 * double
 * int
 * None (null in js)
 * dict (object in js)
 * string
 * function (python callback)
 * instancemethod (python callback, an object's method)

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 2:32

GoogleCodeExporter commented 8 years ago
[ApplicationSettings] has a new option: `unicode_to_bytes_encoding`.

'''What kind of encoding should we use when converting unicode string to bytes 
string? This conversion is done when you pass a unicode string to javascript. 
The default is "utf-8".'''

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 5:35

GoogleCodeExporter commented 8 years ago

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 5:36

GoogleCodeExporter commented 8 years ago
Unicode string and Tuple can now be passed to javascript.

This fix will make it into next release.

If tou think there are more types that should be converted, post them here and 
I will re-open this issue.

Original comment by czarek.t...@gmail.com on 28 Aug 2012 at 7:51

GoogleCodeExporter commented 8 years ago
Version 0.36 released, includes fix for this issue.

Original comment by czarek.t...@gmail.com on 29 Aug 2012 at 5:53

GoogleCodeExporter commented 8 years ago
Project will move to Github. Find this issue at the new address (soon): 
https://github.com/cztomczak/cefpython/issues/10

Original comment by czarek.t...@gmail.com on 24 Aug 2015 at 6:24