-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add nested dict support #208
Changes from 6 commits
b117e5e
a2ebf1c
b9bc4a7
35dffcf
8623bbb
e2c5faa
12b21d0
642b375
04b1bcd
0684e26
56f6dac
3c321ca
b21907f
31edf83
56d50a6
33020ef
b241664
7a69ee8
0e07ff5
c3ca11a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -601,6 +601,74 @@ def test_table_from_table_iter_indirect(self): | |
self.assertEqual(list(table2.keys()), [1, 2, 3]) | ||
self.assertEqual(set(table2.values()), set([1, 2, "foo"])) | ||
|
||
def test_table_from_nested_dict(self): | ||
data = {"a": {"a": "foo"}, "b": {"b": "bar"}} | ||
table = self.lua.table_from(data, recursive=True) | ||
self.assertEqual(table["a"]["a"], "foo") | ||
self.assertEqual(table["b"]["b"], "bar") | ||
self.lua.globals()["data"] = table | ||
self.lua.eval("assert(data.a.a=='foo', 'failed')") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test where we make sure that this raises an exception if the condition fails, so that the test actually fails? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I would add it later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you've added such a test. self.assertEqual(self.lua.eval("data.a.a"), 'foo') Feel free to add a test helper method to make this shorter: def assertLuaResult(lua_expression, result):
self.assertEqual(self.lua.eval(lua_expression), result) |
||
self.lua.eval("assert(data.b.b=='bar', 'failed')") | ||
self.lua.eval("assert(type(data.a)=='table', 'failed, expect table, got '..type(data.a))") | ||
self.lua.eval("assert(type(data.b)=='table', 'failed, expect table, got '..type(data.b))") | ||
self.lua.execute("""function itertable(table) | ||
for k,v in pairs(table) do | ||
print(k) | ||
if type(v) == "table" then | ||
itertable(v) | ||
else | ||
print(v) | ||
end | ||
end | ||
end | ||
print('\\n') | ||
itertable(data) | ||
""") | ||
scoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
del self.lua.globals()["data"] | ||
scoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_table_from_nested_list(self): | ||
data = {"a": {"a": "foo"}, "b": [1, 2, 3]} | ||
table = self.lua.table_from(data, recursive=True) | ||
self.assertEqual(table["a"]["a"], "foo") | ||
self.assertEqual(table["b"][1], 1) | ||
self.assertEqual(table["b"][2], 2) | ||
self.assertEqual(table["b"][3], 3) | ||
self.lua.globals()["data"] = table | ||
self.lua.eval("assert(data.a.a=='foo', 'failed')") | ||
self.lua.eval("assert(#data.b==3, 'failed')") | ||
self.lua.eval("assert(type(data.a)=='table', 'failed, expect table, got '..type(data.a))") | ||
self.lua.eval("assert(type(data.b)=='table', 'failed, expect table, got '..type(data.b))") | ||
self.lua.execute("""function itertable(table) | ||
for k,v in pairs(table) do | ||
print(k) | ||
if type(v) == "table" then | ||
itertable(v) | ||
else | ||
print(v) | ||
end | ||
end | ||
end | ||
print('\\n') | ||
itertable(data) | ||
""") | ||
del self.lua.globals()["data"] | ||
|
||
def test_table_from_nested_list_bad(self): | ||
data = {"a": {"a": "foo"}, "b": [1, 2, 3]} | ||
table = self.lua.table_from(data, recursive=False) # in this case, lua will get userdata instead of table | ||
self.assertEqual(table["a"]["a"], "foo") | ||
self.assertEqual(table["b"][0], 1) | ||
self.assertEqual(table["b"][1], 2) | ||
self.assertEqual(table["b"][2], 3) | ||
self.lua.globals()["data"] = table | ||
|
||
def test(): | ||
self.lua.eval("assert(type(data.a)=='table', 'failed, expect table, got '..type(data.a))") | ||
self.lua.eval("assert(type(data.b)=='table', 'failed, expect table, got '..type(data.b))") | ||
|
||
self.assertRaises(lupa.LuaError, test) | ||
del self.lua.globals()["data"] | ||
|
||
# FIXME: it segfaults | ||
# def test_table_from_generator_calling_lua_functions(self): | ||
# func = self.lua.eval("function (obj) return obj end") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's probably an error to handle if the allocation fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that used to prevent from over nesting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lua_createtable
has no return value, so how do we know if it fails?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think this applies to a couple of other conversions as well. Lua has an error handler that uses longjmp, that probably applies here, too. That is quite annoying, because Lupa does not handle this currently (and it's really not trivial to handle, especially in Cython).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply. I think detect reference cycles in the data structures is not easy, and probably we should just limit the maximum depth of recursion