[tor-commits] [stem/master] Fix unpickling for python3

atagar at torproject.org atagar at torproject.org
Mon May 18 19:24:05 UTC 2015


commit d8e16b765610160260e7e90693ffb2db08bb7dc5
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon May 18 12:11:47 2015 -0700

    Fix unpickling for python3
    
    Turns out we can't use hasattr() since that triggers __getattr__() as well
    under python3 (but not python2). The note about this...
    
      "... it must do this by comparing the strings, rather than using hasattr or
      testing for an AttributeError."
    
      https://users.cs.cf.ac.uk/J.P.Giddy/python/gotcha/getattr.html
---
 stem/descriptor/__init__.py |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 5b07bc4..e8b581e 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -594,9 +594,15 @@ class Descriptor(object):
     return stem.util.str_tools._to_unicode(digest_hash.hexdigest().upper())
 
   def __getattr__(self, name):
+    # Our constructor sets these, but when unpickling we might lack them. This
+    # check is needed to avoid an infinite loop in that case.
+
+    if name in ('_lazy_loading', 'ATTRIBUTES'):
+      return super(Descriptor, self).__getattribute__(name)
+
     # If attribute isn't already present we might be lazy loading it...
 
-    if hasattr(self, '_lazy_loading') and self._lazy_loading and name in self.ATTRIBUTES:
+    if self._lazy_loading and name in self.ATTRIBUTES:
       default, parsing_function = self.ATTRIBUTES[name]
 
       try:





More information about the tor-commits mailing list