Repository navigation
Expand file tree
/
Copy pathstring_utils.pyx
More file actions
62 lines (54 loc) · 2.27 KB
/
Copy pathstring_utils.pyx
File metadata and controls
62 lines (54 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (c) 2012 CefPython Authors. All rights reserved.
# License: New BSD License.
# Website: http://code.google.com/p/cefpython/
cdef str CefToPyString(
CefString& cefString):
cdef wchar_t* wcharstr = <wchar_t*> cefString.c_str()
cdef int charstr_bytes = WideCharToMultiByte(
CP_UTF8, 0, wcharstr, -1, NULL, 0, NULL, NULL)
cdef str pyString
# When CefString is an empty string, WideCharToMultiByte returns 0 bytes, it does
# not include the NUL character, so we need to use calloc instead of malloc.
cdef char* charstr = <char*>calloc(charstr_bytes, sizeof(char))
cdef int copied_bytes = WideCharToMultiByte(
CP_UTF8, 0, wcharstr, -1, charstr, charstr_bytes, NULL, NULL)
if bytes == str:
# Python 2.7, bytes and str are the same types.
# "" + charstr >> makes a copy of char*
pyString = "" + charstr
else:
# Python 3.
pyString = (b"" + charstr).decode("utf-8", "ignore")
free(charstr)
return pyString
cdef void PyToCefString(
py_string pyString,
CefString& cefString
) except *:
if bytes == str:
# Python 2.7, bytes and str are the same types.
if type(pyString) == unicode:
pyString = pyString.encode(g_applicationSettings["unicode_to_bytes_encoding"])
else:
# Python 3 requires bytes before converting to char*.
if type(pyString) != bytes:
pyString = pyString.encode("utf-8")
cdef c_string cString = pyString
# When used cefString.FromASCII(), a DCHECK failed when passed a unicode string.
cefString.FromString(cString)
cdef void PyToCefStringPointer(
py_string pyString,
CefString* cefString
) except *:
if bytes == str:
# Python 2.7, bytes and str are the same types.
if type(pyString) == unicode:
pyString = pyString.encode(g_applicationSettings["unicode_to_bytes_encoding"])
cefString.FromASCII(<char*>pyString)
else:
# Python 3 requires bytes before converting to char*.
if type(pyString) != bytes:
pyString = pyString.encode("utf-8")
cdef c_string cString = pyString
# When used cefString.FromASCII(), a DCHECK failed when passed a unicode string.
cefString.FromString(cString)