[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Rohan Prasad


Rohan Prasad  added the comment:

I see this makes a ton of sense, thanks! I'll close this issue.

--
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



[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

I think this is the expected behavior.

It is expected by IEEE 754 that nan != nan, so that behavior exists:

>>> nan = float('nan')
>>> nan2 = float('nan')
>>> assert nan != nan2
>>> assert nan != nan

However, for "practicality beats purity" (speed) reasons, we compare containers 
by first checking if the entries are identical (aliases of the same object), 
before falling back to the actual comparisons. So identical entries make for 
equal containers, even if the entries don't compare equal.

>>> assert nan is nan   # quick check for identity succeeds,
>>> assert [nan] == [nan]   # therefore these compare equal

>>> assert nan is not nan2  # quick check for identity fails,
>>> assert nan != nan2  # fall back to actual comparison, which fails
>>> assert [nan] != [nan2]  # therefore these do not compare equal

When you serialize and deserialize the container with pickle, you make new 
entries that are no longer aliases.

>From https://docs.python.org/3.9/reference/expressions.html#comparisons:
"The built-in containers typically assume identical objects are equal to 
themselves."

--
nosy: +Dennis Sweeney

___
Python tracker 

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



[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Rohan Prasad


New submission from Rohan Prasad :

Hi,

I'm having a weird issue with floats in dictionaries and equality comparison 
(see attached screenshot for an example). When I create two dictionaries with a 
nan value they pass an equivalence test. However, if I pickle and unpickle one 
of the dictionaries they become no longer equivalent. I would expect that the 
behavior would be the same irrespective of whether they are pickled/unpickled. 
Is there something else I should be doing here or is there a way to easily 
resolve this (short of writing custom ways to compare equality)?

--
files: numpy-nan-dict-error.png
messages: 385990
nosy: rp4fx12
priority: normal
severity: normal
status: open
title: Equality Errors when Unpickling and Pickling a Dictionary with a nan
type: behavior
versions: Python 3.7, Python 3.8
Added file: https://bugs.python.org/file49781/numpy-nan-dict-error.png

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



Pickling a dictionary

2012-11-07 Thread Devashish Tyagi
So I want to store the current state of a InteractiveInterpreter Object in 
database. In order to achieve this I tried this

obj = InteractiveInterpreter()
local = obj.locals()
pickle.dump(local, open('obj.dump','rw'))

But I received an error say 
TypeError: can't pickle ellipsis objects

From what I understand this shouldn't occur as local is a dictionary. Any 
particular reason for this behaviour?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling a dictionary

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
devashishroc...@gmail.com wrote:
 So I want to store the current state of a InteractiveInterpreter Object in 
 database. In order to achieve this I tried this

 obj = InteractiveInterpreter()
 local = obj.locals()
 pickle.dump(local, open('obj.dump','rw'))

 But I received an error say
 TypeError: can't pickle ellipsis objects

 From what I understand this shouldn't occur as local is a dictionary. Any 
 particular reason for this behaviour?

The contents of the dictionary need to be pickleable as well.  You
probably have an ellipsis object in the dict somewhere.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling a dictionary

2012-11-07 Thread Peter Otten
Devashish Tyagi wrote:

 So I want to store the current state of a InteractiveInterpreter Object in
 database. In order to achieve this I tried this
 
 obj = InteractiveInterpreter()
 local = obj.locals()
 pickle.dump(local, open('obj.dump','rw'))

Assuming InteractiveInterpreter is imported from the code module the above 
will fail with a TypeError. Please copy-and paste code snippets to avoid 
guessing games.

 But I received an error say
 TypeError: can't pickle ellipsis objects
 
 From what I understand this shouldn't occur as local is a dictionary. Any
 particular reason for this behaviour?

For a dict to be pickled all its keys and values have to be pickled, too.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling a dictionary

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 9:16 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
 devashishroc...@gmail.com wrote:
 So I want to store the current state of a InteractiveInterpreter Object in 
 database. In order to achieve this I tried this

 obj = InteractiveInterpreter()
 local = obj.locals()
 pickle.dump(local, open('obj.dump','rw'))

 But I received an error say
 TypeError: can't pickle ellipsis objects

 From what I understand this shouldn't occur as local is a dictionary. Any 
 particular reason for this behaviour?

 The contents of the dictionary need to be pickleable as well.  You
 probably have an ellipsis object in the dict somewhere.

By the way, if you use Python 3 and pickle protocol 3, then Ellipsis
*is* pickleable:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
32 bit (Intel)] on win32
Type help, copyright, credits or license for more information.
 import pickle
 pickle.dumps(Ellipsis, protocol=pickle.HIGHEST_PROTOCOL)
b'\x80\x03cbuiltins\nEllipsis\nq\x00.'

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling a dictionary

2012-11-07 Thread Devashish Tyagi
On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten  wrote:
 Devashish Tyagi wrote:
 
 
 
  So I want to store the current state of a InteractiveInterpreter Object in
 
  database. In order to achieve this I tried this
 
  
 
  obj = InteractiveInterpreter()
 
  local = obj.locals()
 
  pickle.dump(local, open('obj.dump','rw'))
 
 
 
 Assuming InteractiveInterpreter is imported from the code module the above 
 
 will fail with a TypeError. Please copy-and paste code snippets to avoid 
 
 guessing games.

Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

Here is the error

Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/lib/python2.7/pickle.py, line 1370, in dump
Pickler(file, protocol).dump(obj)
  File /usr/lib/python2.7/pickle.py, line 224, in dump
self.save(obj)
  File /usr/lib/python2.7/pickle.py, line 286, in save
f(self, obj) # Call unbound method with explicit self
  File /usr/lib/python2.7/pickle.py, line 649, in save_dict
self._batch_setitems(obj.iteritems())
  File /usr/lib/python2.7/pickle.py, line 663, in _batch_setitems
save(v)
  File /usr/lib/python2.7/pickle.py, line 286, in save
f(self, obj) # Call unbound method with explicit self
  File /usr/lib/python2.7/pickle.py, line 649, in save_dict
self._batch_setitems(obj.iteritems())
  File /usr/lib/python2.7/pickle.py, line 663, in _batch_setitems
save(v)
  File /usr/lib/python2.7/pickle.py, line 306, in save
rv = reduce(self.proto)
  File /usr/lib/python2.7/copy_reg.py, line 70, in _reduce_ex
raise TypeError, can't pickle %s objects % base.__name__
TypeError: can't pickle ellipsis objects


 
 
 
  But I received an error say
 
  TypeError: can't pickle ellipsis objects
 
  
 
  From what I understand this shouldn't occur as local is a dictionary. Any
 
  particular reason for this behaviour?
 
 
 
 For a dict to be pickled all its keys and values have to be pickled, too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling a dictionary

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 10:40 AM, Devashish Tyagi
devashishroc...@gmail.com wrote:
 Here is the code

 from code import InteractiveInterpreter
 import StringIO
 import pickle

 src = StringIO.StringIO()
 inter = InteractiveInterpreter()
 inter.runcode('a = 5')
 local = inter.locals

 pickle.dump(local,open('obj.dump','wb'))

After calling runcode it seems that the __builtins__ dict containing
an 'Ellipsis' binding has been added to inter.locals.  You probably
don't want to include the contents of __builtins__ in your pickle
anyway, so just delete it:

del local['__builtins__']
pickle.dump(local, ...)
-- 
http://mail.python.org/mailman/listinfo/python-list