From 6a9d891b6cbe0bf1cf8f6042b6fcc4c95a5a5d6a Mon Sep 17 00:00:00 2001 From: Leo Developer Date: Sat, 17 Sep 2022 21:39:23 +0200 Subject: [PATCH 1/3] fix None as key --- lupa/_lupa.pyx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lupa/_lupa.pyx b/lupa/_lupa.pyx index 7b7e78a1..487d8768 100644 --- a/lupa/_lupa.pyx +++ b/lupa/_lupa.pyx @@ -430,6 +430,8 @@ cdef class LuaRuntime: for obj in args: if isinstance(obj, dict): for key, value in obj.iteritems(): + if key is None: + raise ValueError("can't use None/nil as key") py_to_lua(self, L, key) py_to_lua(self, L, value) lua.lua_rawset(L, -3) @@ -446,6 +448,8 @@ cdef class LuaRuntime: elif isinstance(obj, Mapping): for key in obj: + if key is None: + raise ValueError("can't use None/nil as key") value = obj[key] py_to_lua(self, L, key) py_to_lua(self, L, value) From 41b7a7433096c337901a8619d98b40c0ff0d8034 Mon Sep 17 00:00:00 2001 From: Leo Developer Date: Sat, 17 Sep 2022 21:41:39 +0200 Subject: [PATCH 2/3] add test and change type of error --- lupa/_lupa.pyx | 4 ++-- lupa/tests/test.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lupa/_lupa.pyx b/lupa/_lupa.pyx index 487d8768..ea3bf3fc 100644 --- a/lupa/_lupa.pyx +++ b/lupa/_lupa.pyx @@ -431,7 +431,7 @@ cdef class LuaRuntime: if isinstance(obj, dict): for key, value in obj.iteritems(): if key is None: - raise ValueError("can't use None/nil as key") + raise TypeError("can't use None/nil as key") py_to_lua(self, L, key) py_to_lua(self, L, value) lua.lua_rawset(L, -3) @@ -449,7 +449,7 @@ cdef class LuaRuntime: elif isinstance(obj, Mapping): for key in obj: if key is None: - raise ValueError("can't use None/nil as key") + raise TypeError("can't use None/nil as key") value = obj[key] py_to_lua(self, L, key) py_to_lua(self, L, value) diff --git a/lupa/tests/test.py b/lupa/tests/test.py index 48d92b42..760fa39f 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -566,6 +566,7 @@ def test_table_from_bad(self): self.assertRaises(TypeError, self.lua.table_from, 5) self.assertRaises(TypeError, self.lua.table_from, None) self.assertRaises(TypeError, self.lua.table_from, {"a": 5}, 123) + self.assertRaises(TypeError, self.lua.table_from, {None: 0}) # def test_table_from_nested(self): # table = self.lua.table_from({"obj": {"foo": "bar"}}) From 4920b8da54a8e0184af213170f392cda392441ec Mon Sep 17 00:00:00 2001 From: Leo Developer Date: Sat, 17 Sep 2022 21:57:06 +0200 Subject: [PATCH 3/3] convert to python.none instead of erroring --- lupa/_lupa.pyx | 8 ++------ lupa/tests/test.py | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lupa/_lupa.pyx b/lupa/_lupa.pyx index ea3bf3fc..ba7303cf 100644 --- a/lupa/_lupa.pyx +++ b/lupa/_lupa.pyx @@ -430,9 +430,7 @@ cdef class LuaRuntime: for obj in args: if isinstance(obj, dict): for key, value in obj.iteritems(): - if key is None: - raise TypeError("can't use None/nil as key") - py_to_lua(self, L, key) + py_to_lua(self, L, key, wrap_none=True) py_to_lua(self, L, value) lua.lua_rawset(L, -3) @@ -448,10 +446,8 @@ cdef class LuaRuntime: elif isinstance(obj, Mapping): for key in obj: - if key is None: - raise TypeError("can't use None/nil as key") value = obj[key] - py_to_lua(self, L, key) + py_to_lua(self, L, key, wrap_none=True) py_to_lua(self, L, value) lua.lua_rawset(L, -3) else: diff --git a/lupa/tests/test.py b/lupa/tests/test.py index 760fa39f..67873011 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -494,10 +494,11 @@ def test_create_table_args_kwargs(self): self.assertEqual(6, len(table)) def test_table_from_dict(self): - table = self.lua.table_from({"foo": 1, "bar": 20, "baz": "spam"}) + table = self.lua.table_from({"foo": 1, "bar": 20, "baz": "spam", None: "python.none"}) self.assertEqual( 1, table['foo']) self.assertEqual( 20, table['bar']) self.assertEqual("spam", table['baz']) + self.assertEqual("python.none", table[None]) self.assertEqual(0, len(table)) @@ -566,7 +567,6 @@ def test_table_from_bad(self): self.assertRaises(TypeError, self.lua.table_from, 5) self.assertRaises(TypeError, self.lua.table_from, None) self.assertRaises(TypeError, self.lua.table_from, {"a": 5}, 123) - self.assertRaises(TypeError, self.lua.table_from, {None: 0}) # def test_table_from_nested(self): # table = self.lua.table_from({"obj": {"foo": "bar"}})