#NEWLINE# def lua2python( lua , obj , encoding = 'utf-8' , strict = True , max_depth = 100 , sparse_limit = 10 ) : #NEWLINE# #INDENT# "\n    Recursively convert Lua ``obj`` to Python objects.\n\n    When ``encoding`` is None, binary data is not decoded to unicode;\n    otherwise it is decoded using this encoding.\n\n    When ``strict`` is True, lua2python raises an exception for Lua objects\n    which can't be converted to Python. When ``strict`` is False these objects\n    are returned as lupa wrappers.\n    " #NEWLINE# def l2p( obj , depth ) : #NEWLINE# #INDENT# if ( depth <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( "Can't convert Lua object to Python: depth limit is reached" ) #NEWLINE# #UNINDENT# if isinstance ( obj , dict ) : #NEWLINE# #INDENT# return { l2p ( key , ( depth - 1 ) ) : l2p ( value , ( depth - 1 ) ) for ( key , value ) in six . iteritems ( obj ) } #NEWLINE# #UNINDENT# if isinstance ( obj , list ) : #NEWLINE# #INDENT# return [ l2p ( el , ( depth - 1 ) ) for el in obj ] #NEWLINE# #UNINDENT# if isinstance ( obj , tuple ) : #NEWLINE# #INDENT# return tuple ( [ l2p ( el , ( depth - 1 ) ) for el in obj ] ) #NEWLINE# #UNINDENT# if isinstance ( obj , set ) : #NEWLINE# #INDENT# return { l2p ( el , ( depth - 1 ) ) for el in obj } #NEWLINE# #UNINDENT# if ( lupa . lua_type ( obj ) == 'table' ) : #NEWLINE# #INDENT# if _table_is_array ( lua , obj ) : #NEWLINE# #INDENT# res = [ ] #NEWLINE# prev_key = 0 #NEWLINE# for ( key , value ) in obj . items ( ) : #NEWLINE# #INDENT# if ( not isinstance ( key , int ) ) : #NEWLINE# #INDENT# raise ValueError ( ( "Can't build a Python list from Lua table: invalid key %r" % key ) ) #NEWLINE# #UNINDENT# if ( key <= prev_key ) : #NEWLINE# #INDENT# raise ValueError ( ( "Can't build a Python list from Lua table: bad index %s" % key ) ) #NEWLINE# #UNINDENT# filler_size = ( ( key - prev_key ) - 1 ) #NEWLINE# if ( filler_size > sparse_limit ) : #NEWLINE# #INDENT# raise ValueError ( 'Lua table is too sparse. Try not to use nil values.' ) #NEWLINE# #UNINDENT# res . extend ( ( [ None ] * filler_size ) ) #NEWLINE# res . append ( l2p ( value , ( depth - 1 ) ) ) #NEWLINE# prev_key = key #NEWLINE# #UNINDENT# return res #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return { l2p ( key , ( depth - 1 ) ) : l2p ( value , ( depth - 1 ) ) for ( key , value ) in obj . items ( ) } #NEWLINE# #UNINDENT# #UNINDENT# if ( strict and ( lupa . lua_type ( obj ) is not None ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Lua %s objects are not allowed.' % lupa . lua_type ( obj ) ) ) #NEWLINE# #UNINDENT# if ( ( encoding is not None ) and isinstance ( obj , bytes ) ) : #NEWLINE# #INDENT# obj = obj . decode ( encoding ) #NEWLINE# #UNINDENT# return obj #NEWLINE# #UNINDENT# return l2p ( obj , depth = max_depth )