forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.pyx
More file actions
164 lines (142 loc) · 5.71 KB
/
string_utils.pyx
File metadata and controls
164 lines (142 loc) · 5.71 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (c) 2012 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
# In Python 2 all cefpython strings are byte strings, but in Python 3
# all cefpython strings are unicode. Unicode issues were discussed
# in Issue #60: https://github.com/cztomczak/cefpython/issues/60
# Unicode issues were discussed on cython-users, see posts by Stefan
# Behnel: https://groups.google.com/d/topic/cython-users/VICzhVn-zPw/discussion
# See the "basestring" comment in cefpython.pyx.
# Note that bytes != str != unicode != basestring in Cython
# Any bytes/unicode encoding and decoding in cefpython should
# be performed only using functions from this file - let's
# keep it in one place for future fixes - see Issue 60 ("Strings
# should be unicode by default, if bytes is required make it
# explicit").
include "cefpython.pyx"
# TODO: make this configurable through ApplicationSettings.
UNICODE_ENCODE_ERRORS = "replace"
BYTES_DECODE_ERRORS = "replace"
cdef object AnyToPyString(object value):
cdef object valueType = type(value)
if valueType == str or valueType == bytes:
return value
elif PY_MAJOR_VERSION < 3 and valueType == unicode:
# The unicode type is not defined in Python 3
return value
else:
return ""
cdef object CharToPyString(
const char* charString):
if PY_MAJOR_VERSION < 3:
return <bytes>charString
else:
return <unicode>((<bytes>charString).decode(
g_applicationSettings["string_encoding"],
errors=BYTES_DECODE_ERRORS))
cdef bytes PyStringToChar(object pyString):
if PY_MAJOR_VERSION < 3:
return <bytes>pyString
else:
# The unicode type is not defined in Python 3.
if type(pyString) == str:
pyString = <bytes>(pyString.encode(
g_applicationSettings["string_encoding"],
errors=UNICODE_ENCODE_ERRORS))
return pyString
# Not used anywhere so commented out.
# ---
# cdef object CppToPyString(
# cpp_string cppString):
# if PY_MAJOR_VERSION < 3:
# return <bytes>cppString
# else:
# return <unicode>((<bytes>cppString).decode(
# g_applicationSettings["string_encoding"],
# errors=BYTES_DECODE_ERRORS))
# ---
# No need for this function as you can do it in one line.
# Stays here just for the info on how to do it.
# ---
# cdef cpp_string PyToCppString(object pyString) except *:
# cdef cpp_string cppString = pyString
# return cppString
# ---
cdef object CefToPyString(
ConstCefString& cefString):
cdef cpp_string cppString
if cefString.empty():
return ""
IF UNAME_SYSNAME == "Windows":
cdef wchar_t* wcharstr = <wchar_t*> cefString.c_str()
return WidecharToPyString(wcharstr)
ELSE:
cppString = cefString.ToString()
if PY_MAJOR_VERSION < 3:
return <bytes>cppString
else:
return <unicode>((<bytes>cppString).decode(
g_applicationSettings["string_encoding"],
errors=BYTES_DECODE_ERRORS))
cdef bytes CefToPyBytes(
ConstCefString& cefString):
return <bytes>cefString.ToString()
cdef void PyToCefString(
object pyString,
CefString& cefString
) except *:
if PY_MAJOR_VERSION < 3:
# Handle objects that may be converted to string e.g. QString
if not isinstance(pyString, str) and not isinstance(pyString, unicode):
pyString = str(pyString)
if type(pyString) == unicode:
pyString = <bytes>(pyString.encode(
g_applicationSettings["string_encoding"],
errors=UNICODE_ENCODE_ERRORS))
else:
# Handle objects that may be converted to string e.g. QString
if not isinstance(pyString, str) and not isinstance(pyString, bytes):
pyString = str(pyString)
# The unicode type is not defined in Python 3.
if type(pyString) == str:
pyString = <bytes>(pyString.encode(
g_applicationSettings["string_encoding"],
errors=UNICODE_ENCODE_ERRORS))
cdef cpp_string cppString = pyString
# Using cefString.FromASCII() will result in DCHECK failures
# when a non-ascii character is encountered.
cefString.FromString(cppString)
cdef CefString PyToCefStringValue(
object pyString
) except *:
cdef CefString cefString
PyToCefString(pyString, cefString)
return cefString
cdef void PyToCefStringPointer(
object pyString,
CefString* cefString
) except *:
if PY_MAJOR_VERSION < 3:
if type(pyString) == unicode:
pyString = <bytes>(pyString.encode(
g_applicationSettings["string_encoding"],
errors=UNICODE_ENCODE_ERRORS))
else:
# The unicode type is not defined in Python 3.
if type(pyString) == str:
pyString = <bytes>(pyString.encode(
g_applicationSettings["string_encoding"],
errors=UNICODE_ENCODE_ERRORS))
cdef cpp_string cppString = pyString
# When used cefString.FromASCII(), a DCHECK failed
# when passed a unicode string.
cefString.FromString(cppString)
cdef object VoidPtrToString(const void* data, size_t dataLength):
if PY_MAJOR_VERSION < 3:
return <bytes>((<char*>data)[:dataLength])
else:
return <unicode>((<bytes>(<char*>data)[:dataLength]).decode(
g_applicationSettings["string_encoding"],
errors=BYTES_DECODE_ERRORS))
cdef bytes VoidPtrToBytes(const void* data, size_t dataLength):
return <bytes>((<char*>data)[:dataLength])