diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/python/PyYAML/py2/yaml/constructor.py | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/python/PyYAML/py2/yaml/constructor.py')
-rw-r--r-- | contrib/python/PyYAML/py2/yaml/constructor.py | 312 |
1 files changed, 156 insertions, 156 deletions
diff --git a/contrib/python/PyYAML/py2/yaml/constructor.py b/contrib/python/PyYAML/py2/yaml/constructor.py index ff4e36828e..41144077b0 100644 --- a/contrib/python/PyYAML/py2/yaml/constructor.py +++ b/contrib/python/PyYAML/py2/yaml/constructor.py @@ -1,12 +1,12 @@ -__all__ = [ - 'BaseConstructor', - 'SafeConstructor', - 'FullConstructor', - 'UnsafeConstructor', - 'Constructor', - 'ConstructorError' -] +__all__ = [ + 'BaseConstructor', + 'SafeConstructor', + 'FullConstructor', + 'UnsafeConstructor', + 'Constructor', + 'ConstructorError' +] from error import * from nodes import * @@ -18,35 +18,35 @@ import binascii, re, sys, types class ConstructorError(MarkedYAMLError): pass - -class timezone(datetime.tzinfo): - def __init__(self, offset): - self._offset = offset - seconds = abs(offset).total_seconds() - self._name = 'UTC%s%02d:%02d' % ( - '-' if offset.days < 0 else '+', - seconds // 3600, - seconds % 3600 // 60 - ) - - def tzname(self, dt=None): - return self._name - - def utcoffset(self, dt=None): - return self._offset - - def dst(self, dt=None): - return datetime.timedelta(0) - - def __copy__(self): - return self.__deepcopy__() - - def __deepcopy__(self, memodict={}): - return self.__class__(self.utcoffset()) - - __repr__ = __str__ = tzname - - + +class timezone(datetime.tzinfo): + def __init__(self, offset): + self._offset = offset + seconds = abs(offset).total_seconds() + self._name = 'UTC%s%02d:%02d' % ( + '-' if offset.days < 0 else '+', + seconds // 3600, + seconds % 3600 // 60 + ) + + def tzname(self, dt=None): + return self._name + + def utcoffset(self, dt=None): + return self._offset + + def dst(self, dt=None): + return datetime.timedelta(0) + + def __copy__(self): + return self.__deepcopy__() + + def __deepcopy__(self, memodict={}): + return self.__class__(self.utcoffset()) + + __repr__ = __str__ = tzname + + class BaseConstructor(object): yaml_constructors = {} @@ -62,14 +62,14 @@ class BaseConstructor(object): # If there are more documents available? return self.check_node() - def check_state_key(self, key): - """Block special attributes/methods from being set in a newly created - object, to prevent user-controlled methods from being called during - deserialization""" - if self.get_state_keys_blacklist_regexp().match(key): - raise ConstructorError(None, None, - "blacklisted key '%s' in instance state found" % (key,), None) - + def check_state_key(self, key): + """Block special attributes/methods from being set in a newly created + object, to prevent user-controlled methods from being called during + deserialization""" + if self.get_state_keys_blacklist_regexp().match(key): + raise ConstructorError(None, None, + "blacklisted key '%s' in instance state found" % (key,), None) + def get_data(self): # Construct and return the next document. if self.check_node(): @@ -111,7 +111,7 @@ class BaseConstructor(object): constructor = self.yaml_constructors[node.tag] else: for tag_prefix in self.yaml_multi_constructors: - if tag_prefix is not None and node.tag.startswith(tag_prefix): + if tag_prefix is not None and node.tag.startswith(tag_prefix): tag_suffix = node.tag[len(tag_prefix):] constructor = self.yaml_multi_constructors[tag_prefix] break @@ -330,7 +330,7 @@ class SafeConstructor(BaseConstructor): return str(value).decode('base64') except (binascii.Error, UnicodeEncodeError), exc: raise ConstructorError(None, None, - "failed to decode base64 data: %s" % exc, node.start_mark) + "failed to decode base64 data: %s" % exc, node.start_mark) timestamp_regexp = re.compile( ur'''^(?P<year>[0-9][0-9][0-9][0-9]) @@ -357,7 +357,7 @@ class SafeConstructor(BaseConstructor): minute = int(values['minute']) second = int(values['second']) fraction = 0 - tzinfo = None + tzinfo = None if values['fraction']: fraction = values['fraction'][:6] while len(fraction) < 6: @@ -369,11 +369,11 @@ class SafeConstructor(BaseConstructor): delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) if values['tz_sign'] == '-': delta = -delta - tzinfo = timezone(delta) - elif values['tz']: - tzinfo = timezone(datetime.timedelta(0)) - return datetime.datetime(year, month, day, hour, minute, second, fraction, - tzinfo=tzinfo) + tzinfo = timezone(delta) + elif values['tz']: + tzinfo = timezone(datetime.timedelta(0)) + return datetime.datetime(year, month, day, hour, minute, second, fraction, + tzinfo=tzinfo) def construct_yaml_omap(self, node): # Note: we do not check for duplicate keys, because it's too @@ -508,18 +508,18 @@ SafeConstructor.add_constructor( SafeConstructor.add_constructor(None, SafeConstructor.construct_undefined) -class FullConstructor(SafeConstructor): - # 'extend' is blacklisted because it is used by - # construct_python_object_apply to add `listitems` to a newly generate - # python instance - def get_state_keys_blacklist(self): - return ['^extend$', '^__.*__$'] - - def get_state_keys_blacklist_regexp(self): - if not hasattr(self, 'state_keys_blacklist_regexp'): - self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')') - return self.state_keys_blacklist_regexp - +class FullConstructor(SafeConstructor): + # 'extend' is blacklisted because it is used by + # construct_python_object_apply to add `listitems` to a newly generate + # python instance + def get_state_keys_blacklist(self): + return ['^extend$', '^__.*__$'] + + def get_state_keys_blacklist_regexp(self): + if not hasattr(self, 'state_keys_blacklist_regexp'): + self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')') + return self.state_keys_blacklist_regexp + def construct_python_str(self, node): return self.construct_scalar(node).encode('utf-8') @@ -535,22 +535,22 @@ class FullConstructor(SafeConstructor): def construct_python_tuple(self, node): return tuple(self.construct_sequence(node)) - def find_python_module(self, name, mark, unsafe=False): + def find_python_module(self, name, mark, unsafe=False): if not name: raise ConstructorError("while constructing a Python module", mark, "expected non-empty name appended to the tag", mark) - if unsafe: - try: - __import__(name) - except ImportError, exc: - raise ConstructorError("while constructing a Python module", mark, - "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) - if name not in sys.modules: + if unsafe: + try: + __import__(name) + except ImportError, exc: + raise ConstructorError("while constructing a Python module", mark, + "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) + if name not in sys.modules: raise ConstructorError("while constructing a Python module", mark, - "module %r is not imported" % name.encode('utf-8'), mark) + "module %r is not imported" % name.encode('utf-8'), mark) return sys.modules[name] - def find_python_name(self, name, mark, unsafe=False): + def find_python_name(self, name, mark, unsafe=False): if not name: raise ConstructorError("while constructing a Python object", mark, "expected non-empty name appended to the tag", mark) @@ -559,15 +559,15 @@ class FullConstructor(SafeConstructor): else: module_name = '__builtin__' object_name = name - if unsafe: - try: - __import__(module_name) - except ImportError, exc: - raise ConstructorError("while constructing a Python object", mark, - "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) - if module_name not in sys.modules: + if unsafe: + try: + __import__(module_name) + except ImportError, exc: + raise ConstructorError("while constructing a Python object", mark, + "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) + if module_name not in sys.modules: raise ConstructorError("while constructing a Python object", mark, - "module %r is not imported" % module_name.encode('utf-8'), mark) + "module %r is not imported" % module_name.encode('utf-8'), mark) module = sys.modules[module_name] if not hasattr(module, object_name): raise ConstructorError("while constructing a Python object", mark, @@ -594,16 +594,16 @@ class FullConstructor(SafeConstructor): class classobj: pass def make_python_instance(self, suffix, node, - args=None, kwds=None, newobj=False, unsafe=False): + args=None, kwds=None, newobj=False, unsafe=False): if not args: args = [] if not kwds: kwds = {} cls = self.find_python_name(suffix, node.start_mark) - if not (unsafe or isinstance(cls, type) or isinstance(cls, type(self.classobj))): - raise ConstructorError("while constructing a Python instance", node.start_mark, - "expected a class, but found %r" % type(cls), - node.start_mark) + if not (unsafe or isinstance(cls, type) or isinstance(cls, type(self.classobj))): + raise ConstructorError("while constructing a Python instance", node.start_mark, + "expected a class, but found %r" % type(cls), + node.start_mark) if newobj and isinstance(cls, type(self.classobj)) \ and not args and not kwds: instance = self.classobj() @@ -614,7 +614,7 @@ class FullConstructor(SafeConstructor): else: return cls(*args, **kwds) - def set_python_instance_state(self, instance, state, unsafe=False): + def set_python_instance_state(self, instance, state, unsafe=False): if hasattr(instance, '__setstate__'): instance.__setstate__(state) else: @@ -622,16 +622,16 @@ class FullConstructor(SafeConstructor): if isinstance(state, tuple) and len(state) == 2: state, slotstate = state if hasattr(instance, '__dict__'): - if not unsafe and state: - for key in state.keys(): - self.check_state_key(key) + if not unsafe and state: + for key in state.keys(): + self.check_state_key(key) instance.__dict__.update(state) elif state: slotstate.update(state) for key, value in slotstate.items(): - if not unsafe: - self.check_state_key(key) - setattr(instance, key, value) + if not unsafe: + self.check_state_key(key) + setattr(instance, key, value) def construct_python_object(self, suffix, node): # Format: @@ -680,87 +680,87 @@ class FullConstructor(SafeConstructor): def construct_python_object_new(self, suffix, node): return self.construct_python_object_apply(suffix, node, newobj=True) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/none', - FullConstructor.construct_yaml_null) + FullConstructor.construct_yaml_null) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/bool', - FullConstructor.construct_yaml_bool) + FullConstructor.construct_yaml_bool) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/str', - FullConstructor.construct_python_str) + FullConstructor.construct_python_str) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/unicode', - FullConstructor.construct_python_unicode) + FullConstructor.construct_python_unicode) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/int', - FullConstructor.construct_yaml_int) + FullConstructor.construct_yaml_int) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/long', - FullConstructor.construct_python_long) + FullConstructor.construct_python_long) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/float', - FullConstructor.construct_yaml_float) + FullConstructor.construct_yaml_float) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/complex', - FullConstructor.construct_python_complex) + FullConstructor.construct_python_complex) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/list', - FullConstructor.construct_yaml_seq) + FullConstructor.construct_yaml_seq) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/tuple', - FullConstructor.construct_python_tuple) + FullConstructor.construct_python_tuple) -FullConstructor.add_constructor( +FullConstructor.add_constructor( u'tag:yaml.org,2002:python/dict', - FullConstructor.construct_yaml_map) + FullConstructor.construct_yaml_map) -FullConstructor.add_multi_constructor( +FullConstructor.add_multi_constructor( u'tag:yaml.org,2002:python/name:', - FullConstructor.construct_python_name) - -class UnsafeConstructor(FullConstructor): - - def find_python_module(self, name, mark): - return super(UnsafeConstructor, self).find_python_module(name, mark, unsafe=True) - - def find_python_name(self, name, mark): - return super(UnsafeConstructor, self).find_python_name(name, mark, unsafe=True) - - def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False): - return super(UnsafeConstructor, self).make_python_instance( - suffix, node, args, kwds, newobj, unsafe=True) - - def set_python_instance_state(self, instance, state): - return super(UnsafeConstructor, self).set_python_instance_state( - instance, state, unsafe=True) - -UnsafeConstructor.add_multi_constructor( - u'tag:yaml.org,2002:python/module:', - UnsafeConstructor.construct_python_module) - -UnsafeConstructor.add_multi_constructor( - u'tag:yaml.org,2002:python/object:', - UnsafeConstructor.construct_python_object) - -UnsafeConstructor.add_multi_constructor( - u'tag:yaml.org,2002:python/object/new:', - UnsafeConstructor.construct_python_object_new) - -UnsafeConstructor.add_multi_constructor( - u'tag:yaml.org,2002:python/object/apply:', - UnsafeConstructor.construct_python_object_apply) - -# Constructor is same as UnsafeConstructor. Need to leave this in place in case -# people have extended it directly. -class Constructor(UnsafeConstructor): - pass + FullConstructor.construct_python_name) + +class UnsafeConstructor(FullConstructor): + + def find_python_module(self, name, mark): + return super(UnsafeConstructor, self).find_python_module(name, mark, unsafe=True) + + def find_python_name(self, name, mark): + return super(UnsafeConstructor, self).find_python_name(name, mark, unsafe=True) + + def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False): + return super(UnsafeConstructor, self).make_python_instance( + suffix, node, args, kwds, newobj, unsafe=True) + + def set_python_instance_state(self, instance, state): + return super(UnsafeConstructor, self).set_python_instance_state( + instance, state, unsafe=True) + +UnsafeConstructor.add_multi_constructor( + u'tag:yaml.org,2002:python/module:', + UnsafeConstructor.construct_python_module) + +UnsafeConstructor.add_multi_constructor( + u'tag:yaml.org,2002:python/object:', + UnsafeConstructor.construct_python_object) + +UnsafeConstructor.add_multi_constructor( + u'tag:yaml.org,2002:python/object/new:', + UnsafeConstructor.construct_python_object_new) + +UnsafeConstructor.add_multi_constructor( + u'tag:yaml.org,2002:python/object/apply:', + UnsafeConstructor.construct_python_object_apply) + +# Constructor is same as UnsafeConstructor. Need to leave this in place in case +# people have extended it directly. +class Constructor(UnsafeConstructor): + pass |