From d9db48257b717b3222255f1150d8c41c57f6e0cf Mon Sep 17 00:00:00 2001
From: tguo-aa <tguo@tguo.lan.appannie.com>
Date: Tue, 29 Sep 2015 08:30:37 +0800
Subject: [PATCH] Add self.assertNotNested() in CreateString

And also add a test case.
If you try to nest CreateString you will get a clear exception.
---
 python/flatbuffers/builder.py | 2 ++
 tests/py_test.py              | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/python/flatbuffers/builder.py b/python/flatbuffers/builder.py
index 3e7f3ceb..ed509704 100644
--- a/python/flatbuffers/builder.py
+++ b/python/flatbuffers/builder.py
@@ -350,6 +350,8 @@ class Builder(object):
     def CreateString(self, s):
         """CreateString writes a null-terminated byte string as a vector."""
 
+        self.assertNotNested()
+
         if isinstance(s, compat.string_types):
             x = s.encode()
         elif isinstance(s, compat.binary_type):
diff --git a/tests/py_test.py b/tests/py_test.py
index ae389a15..4f3dae92 100644
--- a/tests/py_test.py
+++ b/tests/py_test.py
@@ -1130,6 +1130,13 @@ class TestExceptions(unittest.TestCase):
         assertRaises(self, lambda: b.PrependUOffsetTRelative(1),
                      flatbuffers.builder.OffsetArithmeticError)
 
+    def test_create_string_is_nested_error(self):
+        b = flatbuffers.Builder(0)
+        b.StartObject(0)
+        s = 'test1'
+        assertRaises(self, lambda: b.CreateString(s),
+                     flatbuffers.builder.ObjectIsNestedError)
+
 
 def CheckAgainstGoldDataGo():
     try:
-- 
GitLab