Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml-5.1 fails with Numpy objects #286

Closed
saimn opened this issue Mar 22, 2019 · 2 comments
Closed

Yaml-5.1 fails with Numpy objects #286

saimn opened this issue Mar 22, 2019 · 2 comments

Comments

@saimn
Copy link

saimn commented Mar 22, 2019

#177 is similar, but was closed as fixed, so I'm opening a new issue.

In [1]: import yaml                                                                            

In [2]: import numpy                                                                           

In [3]: yaml.dump({'value': numpy.float64(1.234)})                                             
Out[3]: 'value: !!python/object/apply:numpy.core.multiarray.scalar\n- !!python/object/apply:numpy.dtype\n  args:\n  - f8\n  - 0\n  - 1\n  state: !!python/tuple\n  - 3\n  - <\n  - null\n  - null\n  - null\n  - -1\n  - -1\n  - 0\n- !!binary |\n  WDm0yHa+8z8=\n'

In [4]: yaml.full_load(yaml.dump({'value': numpy.float64(1.234)}))                             
---------------------------------------------------------------------------
ConstructorError                          Traceback (most recent call last)
<ipython-input-4-9a96300ff2cc> in <module>
----> 1 yaml.full_load(yaml.dump({'value': numpy.float64(1.234)}))

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/__init__.py in full_load(stream)
    140     unsafe on untrusted input.
    141     """
--> 142     return load(stream, FullLoader)
    143 
    144 def full_load_all(stream):

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/__init__.py in load(stream, Loader)
    112     loader = Loader(stream)
    113     try:
--> 114         return loader.get_single_data()
    115     finally:
    116         loader.dispose()

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in get_single_data(self)
     41         node = self.get_single_node()
     42         if node is not None:
---> 43             return self.construct_document(node)
     44         return None
     45 

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_document(self, node)
     50             self.state_generators = []
     51             for generator in state_generators:
---> 52                 for dummy in generator:
     53                     pass
     54         self.constructed_objects = {}

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_yaml_map(self, node)
    402         data = {}
    403         yield data
--> 404         value = self.construct_mapping(node)
    405         data.update(value)
    406 

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_mapping(self, node, deep)
    208         if isinstance(node, MappingNode):
    209             self.flatten_mapping(node)
--> 210         return super().construct_mapping(node, deep=deep)
    211 
    212     def construct_yaml_null(self, node):

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_mapping(self, node, deep)
    133                 raise ConstructorError("while constructing a mapping", node.start_mark,
    134                         "found unhashable key", key_node.start_mark)
--> 135             value = self.construct_object(value_node, deep=deep)
    136             mapping[key] = value
    137         return mapping

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_object(self, node, deep)
     92             data = constructor(self, node)
     93         else:
---> 94             data = constructor(self, tag_suffix, node)
     95         if isinstance(data, types.GeneratorType):
     96             generator = data

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in construct_python_object_apply(self, suffix, node, newobj)
    622             listitems = value.get('listitems', [])
    623             dictitems = value.get('dictitems', {})
--> 624         instance = self.make_python_instance(suffix, node, args, kwds, newobj)
    625         if state:
    626             self.set_python_instance_state(instance, state)

~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/yaml/constructor.py in make_python_instance(self, suffix, node, args, kwds, newobj, unsafe)
    568             raise ConstructorError("while constructing a Python instance", node.start_mark,
    569                     "expected a class, but found %r" % type(cls),
--> 570                     node.start_mark)
    571         if newobj and isinstance(cls, type):
    572             return cls.__new__(cls, *args, **kwds)

ConstructorError: while constructing a Python instance
expected a class, but found <class 'builtin_function_or_method'>
  in "<unicode string>", line 1, column 8:
    value: !!python/object/apply:numpy.core ... 
@perlpunk
Copy link
Member

See #265

Since this needs to call a function specified in the YAML, it is considered unsafe (when loading untrusted YAML), so you need to use unsafe_load in the future:

yaml.unsafe_load(y)
# or
yaml.load(y, Loader=yaml.UnsafeLoader)

Please reopen if this isn't working as expected, I tested it here and it works.

@saimn
Copy link
Author

saimn commented Mar 22, 2019

Oh thanks, indeed it works !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants