diff --git a/capnp/lib/capnp.pxd b/capnp/lib/capnp.pxd index 6ac296a..8cbf3b7 100644 --- a/capnp/lib/capnp.pxd +++ b/capnp/lib/capnp.pxd @@ -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=?) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 72b8244..f4ec511 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -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())