Ticket #19 (new defect)

Opened 8 years ago

Last modified 44 years ago

PySyck improperly handles subclassed types

Reported by: tenax.raccoon@… Owned by: xi
Priority: normal Component: pysyck
Severity: normal Keywords:


If you derive from set, list, or dict, PySyck ignores the getstate method in the new class. This makes it more difficult to create a slightly custom data type for two reasons:

  1. The original data type is not preserved.
  2. Any customization in the data storage is not obeyed.

I haven't had a chance to examine the source code at this point, but I suspect that the dict/list/set types are checked for (probably via isinstance) before the getstate protocol is checked.

Two examples of the type being lost:

>>> class OrderedSet(set):
...  def __iter__(self):
...   items = list( set.__iter__(self) )
...   items.sort( key=str )
...   for item in items:
...     yield item
...   return
...  def __getstate__(self):
...   return {'ordered_items': set(self)}
>>> x = OrderedSet( ('Apples', 'Bananas', 'Cucumbers') )
>>> x
OrderedSet(['Apples', 'Cucumbers', 'Bananas'])
>>> print syck.dump(x)
--- !set
- Apples
- Bananas
- Cucumbers

>>> class DefaultDict(dict):
...  def __init__(self, default, *args, **keyargs):
...   self.default = default
...   dict.__init__(self, *args, **keyargs)
...  def __getstate__(self):
...   return {'default': self.default, 'dict': dict(self) }
>>> d = DefaultDict(-1, test='Hello!')
>>> d.__getstate__()
{'default': -1, 'dict': {'test': 'Hello!'}}
>>> print syck.dump(d)
test: Hello!


An example of a regular object with the getstate() method being properly called:

>>> class RegularObject(object):
...  def __init__(self, name, value):
..., self.value = name, value
...  def __getstate__(self):
...   return {'sillyName':, 'badValue': self.value}
>>> a = RegularObject('Answer To Life', 42)
>>> a.__getstate__()
{'badValue': 42, 'sillyName': 'Answer To Life'}
>>> print syck.dump(a)
--- !python/object:__main__.RegularObject
badValue: 42
sillyName: Answer To Life



Add a comment

Modify Ticket

Change Properties
<Author field>
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'

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

Note: See TracTickets for help on using tickets.