#20246: Use `with strict_equality(True)` to work around hashing of p-adics
-------------------------------------+-------------------------------------
       Reporter:  saraedum           |        Owner:
           Type:  enhancement        |       Status:  needs_work
       Priority:  major              |    Milestone:  sage-7.2
      Component:  padics             |   Resolution:
       Keywords:  days71             |    Merged in:
        Authors:  Julian RĂ¼th        |    Reviewers:
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  
u/saraedum/use__with_strict_equality_true___to_work_around_hashing_of_p_adics|  
f4c5a9db729ccf911517039fdced282588210133
   Dependencies:  #16342, #16339     |     Stopgaps:
-------------------------------------+-------------------------------------

Comment (by nbruin):

 OK, finally I've found why making objects only hashable part of the time
 is a no-go: weakref callback and deletion finalizers can lead to arbitrary
 code execution. So once you've put an object into a dictionary as a key,
 you have to make sure it remains hashable, because key lookup can happen
 at arbitrary times:
 {{{
 class T(object):
     def __init__(self,V):
         self.V=V
     def __repr__(self):
         return "<T(%s)>"%V
     def __hash__(self):
         if hashflag:
             return hash(self.V)
         else:
             raise TypeError("T is not hashable at the moment")
     def __eq__(self,other):
         return isinstance(other,T) and self.V == other.V
     def __ne__(self,other):
         return not(self==other)
 }}}
 You get this:
 {{{
 sage: v=T(0) #this just needs to be a weakreffable object
 sage: k=T(1)
 sage: D=weakref.WeakValueDictionary()
 sage: hashflag=True
 sage: D[k]=v
 sage: hashflag=False
 sage: del v
 Exception TypeError: TypeError('T is not hashable at the moment',) in
 <function remove at 0x7f384da9a0c8> ignored
 }}}
 (and yes, particularly the cache for `UniqueRepresetation` is a
 `WeakValueDictionary`. That particular one is a slightly safer one than
 what comes bundled with python by default, so this particular issue isn't
 triggered there, but I think it illustrates that in python it's illegal to
 have a hashable object that becomes unhashable afterwards).

 So it's not even a question design preferences: the design is not
 according to the behaviour that python objects are expected to adhere to,
 so it's a no-go unless we decide that sage isn't python anymore.

--
Ticket URL: <http://trac.sagemath.org/ticket/20246#comment:27>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to