Kaydet (Commit) ab405d55 authored tarafından Vinay Sajip's avatar Vinay Sajip

Closes #20444: Merged fix from 3.4.

...@@ -277,23 +277,14 @@ def valid_ident(s): ...@@ -277,23 +277,14 @@ def valid_ident(s):
return True return True
# The ConvertingXXX classes are wrappers around standard Python containers, class ConvertingMixin(object):
# and they serve to convert any suitable values in the container. The """For ConvertingXXX's, this mixin class provides common functions"""
# conversion converts base dicts, lists and tuples to their wrapped
# equivalents, whereas strings which match a conversion format are converted
# appropriately.
#
# Each wrapper should have a configurator attribute holding the actual
# configurator to use for conversion.
class ConvertingDict(dict): def convert_with_key(self, key, value, replace=True):
"""A converting dictionary wrapper."""
def __getitem__(self, key):
value = dict.__getitem__(self, key)
result = self.configurator.convert(value) result = self.configurator.convert(value)
#If the converted value is different, save for next time #If the converted value is different, save for next time
if value is not result: if value is not result:
if replace:
self[key] = result self[key] = result
if type(result) in (ConvertingDict, ConvertingList, if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple): ConvertingTuple):
...@@ -301,62 +292,55 @@ class ConvertingDict(dict): ...@@ -301,62 +292,55 @@ class ConvertingDict(dict):
result.key = key result.key = key
return result return result
def get(self, key, default=None): def convert(self, value):
value = dict.get(self, key, default)
result = self.configurator.convert(value) result = self.configurator.convert(value)
#If the converted value is different, save for next time
if value is not result: if value is not result:
self[key] = result
if type(result) in (ConvertingDict, ConvertingList, if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple): ConvertingTuple):
result.parent = self result.parent = self
result.key = key
return result return result
# The ConvertingXXX classes are wrappers around standard Python containers,
# and they serve to convert any suitable values in the container. The
# conversion converts base dicts, lists and tuples to their wrapped
# equivalents, whereas strings which match a conversion format are converted
# appropriately.
#
# Each wrapper should have a configurator attribute holding the actual
# configurator to use for conversion.
class ConvertingDict(dict, ConvertingMixin):
"""A converting dictionary wrapper."""
def __getitem__(self, key):
value = dict.__getitem__(self, key)
return self.convert_with_key(key, value)
def get(self, key, default=None):
value = dict.get(self, key, default)
return self.convert_with_key(key, value)
def pop(self, key, default=None): def pop(self, key, default=None):
value = dict.pop(self, key, default) value = dict.pop(self, key, default)
result = self.configurator.convert(value) return self.convert_with_key(key, value, replace=False)
if value is not result:
if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple):
result.parent = self
result.key = key
return result
class ConvertingList(list): class ConvertingList(list, ConvertingMixin):
"""A converting list wrapper.""" """A converting list wrapper."""
def __getitem__(self, key): def __getitem__(self, key):
value = list.__getitem__(self, key) value = list.__getitem__(self, key)
result = self.configurator.convert(value) return self.convert_with_key(key, value)
#If the converted value is different, save for next time
if value is not result:
self[key] = result
if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple):
result.parent = self
result.key = key
return result
def pop(self, idx=-1): def pop(self, idx=-1):
value = list.pop(self, idx) value = list.pop(self, idx)
result = self.configurator.convert(value) return self.convert(value)
if value is not result:
if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple):
result.parent = self
return result
class ConvertingTuple(tuple): class ConvertingTuple(tuple, ConvertingMixin):
"""A converting tuple wrapper.""" """A converting tuple wrapper."""
def __getitem__(self, key): def __getitem__(self, key):
value = tuple.__getitem__(self, key) value = tuple.__getitem__(self, key)
result = self.configurator.convert(value) # Can't replace a tuple entry.
if value is not result: return self.convert_with_key(key, value, replace=False)
if type(result) in (ConvertingDict, ConvertingList,
ConvertingTuple):
result.parent = self
result.key = key
return result
class BaseConfigurator(object): class BaseConfigurator(object):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment