Add clear_write_flag method to builders

Fixes #111
This commit is contained in:
Jason Paryani
2016-08-09 12:26:18 -07:00
parent d85200cee3
commit 97824b957b
2 changed files with 9 additions and 2 deletions

View File

@@ -46,7 +46,7 @@ cdef class _DynamicStructBuilder:
cdef DynamicStruct_Builder thisptr
cdef public object _parent
cdef public bint is_root
cdef bint _is_written
cdef public bint _is_written
cdef object _schema
cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot=?, bint tryRegistry=?)

View File

@@ -1140,7 +1140,7 @@ cdef class _DynamicStructBuilder:
if not self.is_root:
raise KjException("You can only call write() on the message's root struct.")
if self._is_written:
_warnings.warn("This message has already been written once. Be very careful that you're not setting Text/Struct/List fields more than once, since that will cause memory leaks (both in memory and in the serialized data). You can disable this warning by setting the `_is_written` field of this object to False after every write.")
_warnings.warn("This message has already been written once. Be very careful that you're not setting Text/Struct/List fields more than once, since that will cause memory leaks (both in memory and in the serialized data). You can disable this warning by calling the `clear_write_flag` method of this object after every write.")
def write(self, file):
"""Writes the struct's containing message to the given file object in unpacked binary format.
@@ -1437,6 +1437,13 @@ cdef class _DynamicStructBuilder:
size = self.thisptr.totalSize()
return _MessageSize(size.wordCount, size.capCount)
def clear_write_flag(self):
"""A method used to clear the _is_written flag.
This allows you to write the struct more than once without seeing any warnings.
"""
self._is_written = False
def __reduce_ex__(self, proto):
return _struct_reducer, (self.schema.node.id, self.to_bytes())