[issue41639] Unpickling derived class of list does not call __init__()

2021-04-26 Thread Gregory P. Smith


Gregory P. Smith  added the comment:

this looks resolved?

--
nosy: +gregory.p.smith
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41639] Unpickling derived class of list does not call __init__()

2020-09-07 Thread Andy Maier


Andy Maier  added the comment:

And just to be complete: That did not require implementing __reduce__().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41639] Unpickling derived class of list does not call __init__()

2020-09-07 Thread Andy Maier


Andy Maier  added the comment:

Thanks for the clarification.

Just for the record:

I have implemented __setstate__() such that it completely restores the state 
from just the inherited list state. That makes it independent of whether 
__init__() or __new__() is called:

def __getstate__(self):
state = self.__dict__.copy()
del state['_lc_list']
return state

def __setstate__(self, state):
self.__dict__.update(state)
self._lc_list = _lc_list(self)

This issue can be closed.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41639] Unpickling derived class of list does not call __init__()

2020-08-26 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

>From https://docs.python.org/3/library/pickle.html#pickling-class-instances:

When a class instance is unpickled, its __init__() method is usually not 
invoked.

If you want to change this behavior you have to implement the __reduce__ or 
__reduce_ex__ methods or register the object type in the global or per-pickler 
dispatch table. For example:

class NocaseList(list):

def __reduce__(self):
return self.__class__, (), None, iter(self)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41639] Unpickling derived class of list does not call __init__()

2020-08-26 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +pitrou, serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41639] Unpickling derived class of list does not call __init__()

2020-08-26 Thread Andy Maier


New submission from Andy Maier :

Unpickling an object of a user class that derives from list seems to miss 
calling the user class's __init__() method:

Consider this script, which defines a derived class of the built-in list for 
the purpose of creating a case insensitive list. The real example has all 
methods of list implemented, but this subset example also shows the issue:

-
import pickle

def _lc_list(lst):
result = list()
for value in lst:
result.append(value.lower())
return result

class NocaseList(list):

#def __new__(cls, iterable=()):
#self = super(NocaseList, cls).__new__(cls, iterable)
#print("Debug: __new__()  for self={}".format(id(self)))
#return self

def __init__(self, iterable=()):
print("Debug: __init__() for self={}".format(id(self)))
super(NocaseList, self).__init__(iterable)
self.lc_list = _lc_list(self)

def append(self, value):
super(NocaseList, self).append(value)
self.lc_list.append(value.lower())

def extend(self, iterable):
super(NocaseList, self).extend(iterable)
for value in iterable:
self.lc_list.append(value.lower())

ncl = NocaseList(['A', 'b'])
pkl = pickle.dumps(ncl)
nclx = pickle.loads(pkl)
-

$ python ./pickle_extend.py 
Debug: __init__() for self=4498704880
Traceback (most recent call last):
  File "./pickle_extend.py", line 32, in 
nclx = pickle.loads(pkl)
  File "./pickle_extend.py", line 28, in extend
self.lc_list.append(value.lower())
AttributeError: 'NocaseList' object has no attribute 'lc_list'

Uncommenting the __new__() method in the script shows that __new__() is called 
but not __init__():

$ python ./pickle_extend.py 
Debug: __new__()  for self=4359683024
Debug: __init__() for self=4359683024
Debug: __new__()  for self=4360134304
Traceback (most recent call last):
  File "./pickle_extend.py", line 32, in 
nclx = pickle.loads(pkl)
  File "./pickle_extend.py", line 28, in extend
self.lc_list.append(value.lower())
AttributeError: 'NocaseList' object has no attribute 'lc_list'

--
messages: 375909
nosy: andymaier
priority: normal
severity: normal
status: open
title: Unpickling derived class of list does not call __init__()
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com