Repository: flightaware/tohil (language: python)

Issue:
Core dump when feeding a tclobj to a dict
Environment: Python 3.7.6 with tohil 4.0.0 on FreeBSD

Expected behavior: Some sort of Python error about not being able to build a dict from a standard sequence (needs to be a sequence of pairs or an actual mapping)

Actual behavior:
```python                                                                                                                                                                                                                                                        
Python 3.7.6 (default, Jan 30 2020, 01:17:40)
[Clang 8.0.0 (tags/RELEASE_800/final 356365)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
I >>> import tohil
I >>> dict(tohil.tclobj('a b'))
[1]    54019 bus error (core dumped)  python3
```
Of note, `dict(tohil.tclobj())` actually produces an empty dict as I would expect in that case.


Discussion / hints:

Fix patch:
diff --git a/generic/tohil.c b/generic/tohil.c
index 992f16f..584bbb8 100644
--- a/generic/tohil.c
+++ b/generic/tohil.c
@@ -2244,7 +2244,7 @@ static PyTypeObject TohilTclObj_IterType = {
     .tp_flags = Py_TPFLAGS_DEFAULT,
     .tp_doc = "tohil tclobj iterator type",
     .tp_dealloc = (destructor)TohilTclObjIter_dealloc,
-    .tp_iter = (getiterfunc)TohilTclObjIter,
+    .tp_iter = (getiterfunc)PyObject_SelfIter,
     .tp_iternext = (iternextfunc)TohilTclObj_iternext,
 };
 


Test patch:
diff --git a/tests/test_tclobj.py b/tests/test_tclobj.py
index 47fe7e5..34a2c2a 100644
--- a/tests/test_tclobj.py
+++ b/tests/test_tclobj.py
@@ -251,5 +251,17 @@ def test_tclobj24(self):
         with self.assertRaises(IndexError):
             t.pop()
 
+    def test_tclobj25(self):
+        """tohil.tclobj dict wrapping tests"""
+        # This isn't recommended (you should use the to= kwarg), but it works
+        self.assertEqual(dict(tohil.tclobj()), {})
+        # And here's why it's not recommended
+        with self.assertRaises(ValueError):
+            dict(tohil.tclobj("1 2"))
+        with self.assertRaises(TypeError):
+            # iterating on the tclobj gives us more tclobjs, which aren't
+            # hashable and so cannot be keys
+            dict(tohil.tclobj("{1 2}"))
+
 if __name__ == "__main__":
     unittest.main()