mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue #6360: Simplify string decoding in xmlrpc.client.
This commit is contained in:
parent
fc477048e1
commit
e671fd206b
1 changed files with 4 additions and 17 deletions
|
@ -152,24 +152,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
datetime = None
|
datetime = None
|
||||||
|
|
||||||
def _decode(data, encoding, is8bit=re.compile("[\x80-\xff]").search):
|
|
||||||
# decode non-ascii string (if possible)
|
|
||||||
if encoding and is8bit(data):
|
|
||||||
data = str(data, encoding)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def escape(s):
|
def escape(s):
|
||||||
s = s.replace("&", "&")
|
s = s.replace("&", "&")
|
||||||
s = s.replace("<", "<")
|
s = s.replace("<", "<")
|
||||||
return s.replace(">", ">",)
|
return s.replace(">", ">",)
|
||||||
|
|
||||||
def _stringify(string):
|
|
||||||
# convert to 7-bit ascii if possible
|
|
||||||
try:
|
|
||||||
return string.decode("ascii")
|
|
||||||
except (UnicodeError, TypeError, AttributeError):
|
|
||||||
return string
|
|
||||||
|
|
||||||
__version__ = "1.0.1"
|
__version__ = "1.0.1"
|
||||||
|
|
||||||
# xmlrpc integer limits
|
# xmlrpc integer limits
|
||||||
|
@ -755,8 +742,8 @@ class Unmarshaller:
|
||||||
|
|
||||||
def end_string(self, data):
|
def end_string(self, data):
|
||||||
if self._encoding:
|
if self._encoding:
|
||||||
data = _decode(data, self._encoding)
|
data = data.decode(self._encoding)
|
||||||
self.append(_stringify(data))
|
self.append(data)
|
||||||
self._value = 0
|
self._value = 0
|
||||||
dispatch["string"] = end_string
|
dispatch["string"] = end_string
|
||||||
dispatch["name"] = end_string # struct keys are always strings
|
dispatch["name"] = end_string # struct keys are always strings
|
||||||
|
@ -774,7 +761,7 @@ class Unmarshaller:
|
||||||
dict = {}
|
dict = {}
|
||||||
items = self._stack[mark:]
|
items = self._stack[mark:]
|
||||||
for i in range(0, len(items), 2):
|
for i in range(0, len(items), 2):
|
||||||
dict[_stringify(items[i])] = items[i+1]
|
dict[items[i]] = items[i+1]
|
||||||
self._stack[mark:] = [dict]
|
self._stack[mark:] = [dict]
|
||||||
self._value = 0
|
self._value = 0
|
||||||
dispatch["struct"] = end_struct
|
dispatch["struct"] = end_struct
|
||||||
|
@ -811,7 +798,7 @@ class Unmarshaller:
|
||||||
|
|
||||||
def end_methodName(self, data):
|
def end_methodName(self, data):
|
||||||
if self._encoding:
|
if self._encoding:
|
||||||
data = _decode(data, self._encoding)
|
data = data.decode(self._encoding)
|
||||||
self._methodname = data
|
self._methodname = data
|
||||||
self._type = "methodName" # no params
|
self._type = "methodName" # no params
|
||||||
dispatch["methodName"] = end_methodName
|
dispatch["methodName"] = end_methodName
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue