MCPcopy
hub / github.com/scrapinghub/splash / l2p

Function l2p

splash/lua.py:143–193  ·  view source on GitHub ↗
(obj, depth)

Source from the content-addressed store, hash-verified

141 """
142
143 def l2p(obj, depth):
144 if depth <= 0:
145 raise ValueError("Can't convert Lua object to Python: depth limit is reached")
146
147 if isinstance(obj, dict):
148 return {
149 l2p(key, depth-1): l2p(value, depth-1)
150 for key, value in obj.items()
151 }
152
153 if isinstance(obj, list):
154 return [l2p(el, depth-1) for el in obj]
155
156 if isinstance(obj, tuple):
157 return tuple([l2p(el, depth-1) for el in obj])
158
159 if isinstance(obj, set):
160 return {l2p(el, depth-1) for el in obj}
161
162 if lupa.lua_type(obj) == 'table':
163 if _table_is_array(lua, obj):
164 res = []
165 prev_key = 0
166 for key, value in obj.items():
167 if not isinstance(key, int):
168 raise ValueError("Can't build a Python list from Lua table: invalid key %r" % key)
169 if key <= prev_key:
170 raise ValueError("Can't build a Python list from Lua table: bad index %s" % key)
171
172 filler_size = key - prev_key - 1
173 if filler_size > sparse_limit:
174 raise ValueError("Lua table is too sparse. Try not to use nil values.")
175 res.extend([None] * filler_size)
176 res.append(l2p(value, depth-1))
177 prev_key = key
178 return res
179 else:
180 return {
181 l2p(key, depth-1): l2p(value, depth-1)
182 for key, value in obj.items()
183 }
184
185 if strict and lupa.lua_type(obj) is not None:
186 raise ValueError(
187 "Lua %s objects are not allowed." % lupa.lua_type(obj)
188 )
189
190 if encoding is not None and isinstance(obj, bytes):
191 obj = obj.decode(encoding)
192
193 return obj
194
195 return l2p(obj, depth=max_depth)
196

Callers 1

lua2pythonFunction · 0.85

Calls 2

_table_is_arrayFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected