Modify

Ticket #245 (new defect)

Opened 23 months ago

support for Abstract Base Classes

Reported by: alex.garel@… Owned by: xi
Priority: normal Component: pyyaml
Severity: major Keywords: Abstract Base Class ABC
Cc:

Description

problem

>>> import yaml
>>> from collections import Mapping
>>> class A(Mapping):
...     pass
... 
>>> yaml.dump(A)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/yaml/lib/python2.7/site-packages/yaml/__init__.py", line 202, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/tmp/yaml/lib/python2.7/site-packages/yaml/__init__.py", line 190, in dump_all
    dumper.represent(data)
  File "/tmp/yaml/lib/python2.7/site-packages/yaml/representer.py", line 28, in represent
    node = self.represent_data(data)
  File "/tmp/yaml/lib/python2.7/site-packages/yaml/representer.py", line 61, in represent_data
    node = self.yaml_multi_representers[data_type](self, data)
  File "/tmp/yaml/lib/python2.7/site-packages/yaml/representer.py", line 408, in represent_object
    reduce = data.__reduce_ex__(2)
  File "/tmp/yaml/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle int objects

analysis

This is because of A being handled as an object, so an unbind version object.__reduce_ex__ is called, so protocol version is mistaken with the object (2 is passed in place of self).

>>> type(A)
<class 'abc.ABCMeta'>

solution

>>> from yaml.representer import Representer
>>> from abc import ABCMeta
>>> Representer.add_representer(ABCMeta, Representer.represent_name)
>>> yaml.dump(A)
"!!python/name:__main__.A ''\n"


Attachments

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as new
as The resolution will be set. Next status will be 'closed'
to The owner will be changed from xi. Next status will be 'new'
The owner will be changed from xi to anonymous. Next status will be 'assigned'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.