3232ALL_CHARS  =  str (bytearray (range (256 ))) if  PY2  else  bytes (range (256 ))
3333
3434MAX_FORMAT_DEPTH  =  200 
35+ MAX_PARSE_DEPTH  =  200 
36+ 
3537class  _LLSD :
3638    __metaclass__  =  abc .ABCMeta 
3739
@@ -209,7 +211,7 @@ def _parse_datestr(datestr):
209211    return  datetime .datetime (year , month , day , hour , minute , second , usec )
210212
211213
212- def  _bool_to_python (node ):
214+ def  _bool_to_python (node ,  depth = 0 ):
213215    "Convert boolean node to a python object." 
214216    val  =  node .text  or  '' 
215217    try :
@@ -220,35 +222,35 @@ def _bool_to_python(node):
220222       return  bool (val )
221223
222224
223- def  _int_to_python (node ):
225+ def  _int_to_python (node ,  depth = 0 ):
224226    "Convert integer node to a python object." 
225227    val  =  node .text  or  '' 
226228    if  not  val .strip ():
227229        return  0 
228230    return  int (val )
229231
230232
231- def  _real_to_python (node ):
233+ def  _real_to_python (node ,  depth = 0 ):
232234    "Convert floating point node to a python object." 
233235    val  =  node .text  or  '' 
234236    if  not  val .strip ():
235237        return  0.0 
236238    return  float (val )
237239
238240
239- def  _uuid_to_python (node ):
241+ def  _uuid_to_python (node ,  depth = 0 ):
240242    "Convert uuid node to a python object." 
241243    if  node .text :
242244        return  uuid .UUID (hex = node .text )
243245    return  uuid .UUID (int = 0 )
244246
245247
246- def  _str_to_python (node ):
248+ def  _str_to_python (node ,  depth = 0 ):
247249    "Convert string node to a python object." 
248250    return  node .text  or  '' 
249251
250252
251- def  _bin_to_python (node ):
253+ def  _bin_to_python (node ,  depth = 0 ):
252254    base  =  node .get ('encoding' ) or  'base64' 
253255    try :
254256        if  base  ==  'base16' :
@@ -267,38 +269,38 @@ def _bin_to_python(node):
267269        return  LLSDParseError ("Bad binary data: "  +  str (exc ))
268270
269271
270- def  _date_to_python (node ):
272+ def  _date_to_python (node ,  depth = 0 ):
271273    "Convert date node to a python object." 
272274    val  =  node .text  or  '' 
273275    if  not  val :
274276        val  =  "1970-01-01T00:00:00Z" 
275277    return  _parse_datestr (val )
276278
277279
278- def  _uri_to_python (node ):
280+ def  _uri_to_python (node ,  depth = 0 ):
279281    "Convert uri node to a python object." 
280282    val  =  node .text  or  '' 
281283    return  uri (val )
282284
283285
284- def  _map_to_python (node ):
286+ def  _map_to_python (node ,  depth = 0 ):
285287    "Convert map node to a python object." 
286288    result  =  {}
287289    for  index  in  range (len (node ))[::2 ]:
288290        if  node [index ].text  is  None :
289-             result ['' ] =  _to_python (node [index + 1 ])
291+             result ['' ] =  _to_python (node [index + 1 ],  depth + 1 )
290292        else :
291-             result [node [index ].text ] =  _to_python (node [index + 1 ])
293+             result [node [index ].text ] =  _to_python (node [index + 1 ],  depth + 1 )
292294    return  result 
293295
294296
295- def  _array_to_python (node ):
297+ def  _array_to_python (node ,  depth = 0 ):
296298    "Convert array node to a python object." 
297-     return  [_to_python (child ) for  child  in  node ]
299+     return  [_to_python (child ,  depth + 1 ) for  child  in  node ]
298300
299301
300302NODE_HANDLERS  =  dict (
301-     undef = lambda  x : None ,
303+     undef = lambda  x , y : None ,
302304    boolean = _bool_to_python ,
303305    integer = _int_to_python ,
304306    real = _real_to_python ,
@@ -312,9 +314,12 @@ def _array_to_python(node):
312314)
313315
314316
315- def  _to_python (node ):
317+ def  _to_python (node ,  depth = 0 ):
316318    "Convert node to a python object." 
317-     return  NODE_HANDLERS [node .tag ](node )
319+     if  depth  >  MAX_PARSE_DEPTH :
320+         raise  LLSDParseError ("Cannot serialize depth of more than %d"  %  MAX_FORMAT_DEPTH )
321+ 
322+     return  NODE_HANDLERS [node .tag ](node , depth )
318323
319324
320325class  LLSDBaseFormatter (object ):
0 commit comments