changeset 18:7dccdab5061e

Fix problems with infinite recursion into unicode strings and magical null objects
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 17 Jan 2012 18:07:54 +0100
parents 5793a417efe4
children b21203132ef6
files jpath.py
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jpath.py	Wed Nov 09 09:58:30 2011 +0100
+++ b/jpath.py	Tue Jan 17 18:07:54 2012 +0100
@@ -55,7 +55,14 @@
 import operator
 
 __all__ = ['get', 'get_all']
-__version__ = '1.1'
+__version__ = '1.2'
+
+
+# Make it work both in python 2.x and 3.x
+try:
+    basestring
+except NameError:
+    basestring = str
 
 
 def _pop_token(tokens):
@@ -291,17 +298,23 @@
                     data = data.values()
                 except AttributeError:
                     pass
+                orig_data = data
                 try:
                     # Skip non-iterable datatypes.
                     data = iter(data)
                 except TypeError:
                     continue
                 for item in data:
-                    if not isinstance(item, str):
+                    if isinstance(item, basestring):
                         # Strings are nasty, because they can be iterated
                         # recursively idefinitely.
-                        stack.append(item)
-                        yield item
+                        continue
+                    if item is orig_data:
+                        # We are iterating over something that behaves like
+                        # the strings, refuse to descend.
+                        break
+                    stack.append(item)
+                    yield item
         token = _pop_token(tokens)
         for item in dive(data):
             try: