@@ -601,6 +601,19 @@ def _convert_to_py_string(value) -> str:
601601 return py_string
602602
603603
604+ def _clear_error_state ():
605+ """Clear any existing error state from the C library.
606+
607+ This function should be called at the beginning of object initialization
608+ to ensure that stale error states from previous operations don't interfere
609+ with new objects being created.
610+ """
611+ error = _lib .c2pa_error ()
612+ if error :
613+ # Free the error to clear the state
614+ _lib .c2pa_string_free (error )
615+
616+
604617def _parse_operation_result_for_error (
605618 result : ctypes .c_void_p | None ,
606619 check_error : bool = True ) -> Optional [str ]:
@@ -1375,6 +1388,9 @@ def __init__(self,
13751388 C2paError.Encoding: If any of the string inputs
13761389 contain invalid UTF-8 characters
13771390 """
1391+ # Native libs plumbing:
1392+ # Clear any stale error state from previous operations
1393+ _clear_error_state ()
13781394
13791395 self ._closed = False
13801396 self ._initialized = False
@@ -2095,6 +2111,10 @@ def __init__(self, signer_ptr: ctypes.POINTER(C2paSigner)):
20952111 Raises:
20962112 C2paError: If the signer pointer is invalid
20972113 """
2114+ # Native libs plumbing:
2115+ # Clear any stale error state from previous operations
2116+ _clear_error_state ()
2117+
20982118 # Validate pointer before assignment
20992119 if not signer_ptr :
21002120 raise C2paError ("Invalid signer pointer: pointer is null" )
@@ -2352,6 +2372,10 @@ def __init__(self, manifest_json: Any):
23522372 C2paError.Encoding: If manifest JSON contains invalid UTF-8 chars
23532373 C2paError.Json: If the manifest JSON cannot be serialized
23542374 """
2375+ # Native libs plumbing:
2376+ # Clear any stale error state from previous operations
2377+ _clear_error_state ()
2378+
23552379 self ._closed = False
23562380 self ._initialized = False
23572381 self ._builder = None
0 commit comments