From f685f9ea4ed8ad14178ad7f48b5237171c94162b Mon Sep 17 00:00:00 2001
From: "Meredith L. Patterson" <mlp@thesmartpolitenerd.com>
Date: Mon, 18 Nov 2013 21:50:28 -0600
Subject: [PATCH] there's the uint8_t problem (mostly) sorted

---
 src/bindings/python/hammer_tests.py | 10 +++++-----
 src/bindings/swig/hammer.i          | 16 ++++++++++++++--
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/bindings/python/hammer_tests.py b/src/bindings/python/hammer_tests.py
index 3f0596c..d1c18db 100644
--- a/src/bindings/python/hammer_tests.py
+++ b/src/bindings/python/hammer_tests.py
@@ -6,7 +6,7 @@ class TestTokenParser(unittest.TestCase):
     def setUpClass(cls):
         cls.parser = h.h_token("95\xa2", 3)
     def test_success(self):
-        self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes.token, "95\xa2")
+        self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes, "95\xa2")
     def test_partial_fails(self):
         self.assertEqual(h.h_parse(self.parser, "95", 2), None)
 
@@ -226,7 +226,7 @@ class TestSequence(unittest.TestCase):
     def setUpClass(cls):
         cls.parser = h.h_sequence(h.h_ch("a"), h.h_ch("b"))
     def test_success(self):
-        self.assertEqual(h.h_parse(self.parser, "ab").ast.token_data.seq, ["a", "b"])
+        self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["a", "b"])
     def test_failure(self):
         self.assertEqual(h.h_parse(self.parser, "a", 1), None)
         self.assertEqual(h.h_parse(self.parser, "b", 1), None)
@@ -255,7 +255,7 @@ class TestChoice(unittest.TestCase):
 class TestButNot(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
-        cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab"))
+        cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab", 2))
     def test_success(self):
         self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
         self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.bytes, "a")
@@ -439,7 +439,7 @@ class TestAnd3(unittest.TestCase):
 class TestNot1(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
-        cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++")), h.h_ch("b"))
+        cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++", 2)), h.h_ch("b"))
     def test_success(self):
         self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", "+", "b"])
     def test_failure(self):
@@ -448,7 +448,7 @@ class TestNot1(unittest.TestCase):
 class TestNot2(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
-        cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_sequence(h.h_ch("+"), h.h_not(h.h_ch("+"))), h.h_token("++")), h.h_ch("b"))
+        cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_sequence(h.h_ch("+"), h.h_not(h.h_ch("+"))), h.h_token("++", 2)), h.h_ch("b"))
     def test_success(self):
         self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", ["+"], "b"])
         self.assertEqual(h.h_parse(self.parser, "a++b", 4).ast.token_data.seq, ["a", "++", "b"])
diff --git a/src/bindings/swig/hammer.i b/src/bindings/swig/hammer.i
index ad61e49..412743a 100644
--- a/src/bindings/swig/hammer.i
+++ b/src/bindings/swig/hammer.i
@@ -11,6 +11,20 @@
 %typemap(out) uint8_t* {
   $result = PyString_FromString((char*)$1);
  }
+%typemap(in) uint8_t {
+  if (PyInt_Check($input)) {
+    $1 = PyInt_AsLong($input);
+  }
+  else if (!PyString_Check($input)) {
+    PyErr_SetString(PyExc_ValueError, "Expecting a string");
+    return NULL;
+  } else {
+    $1 = *(uint8_t*)PyString_AsString($input);
+  }
+ }
+%typemap(out) HBytes* {
+  $result = PyString_FromStringAndSize((char*)$1->token, $1->len);
+ }
 #else
   #warning no uint8_t* typemaps defined
 #endif
@@ -24,5 +38,3 @@
 %include "allocator.h"
 %include "hammer.h"
 
-
-
-- 
GitLab