@@ -730,32 +730,8 @@ def sign_file_with_callback_signer(
730730 source_stream = Stream (source_file )
731731 dest_stream = Stream (dest_file )
732732
733- # Use the internal signing logic to get manifest bytes
734- format_str = mime_type .encode ('utf-8' )
735- manifest_bytes_ptr = ctypes .POINTER (ctypes .c_ubyte )()
736-
737- # Call the native signing function
738- result = _lib .c2pa_builder_sign (
739- builder ._builder ,
740- format_str ,
741- source_stream ._stream ,
742- dest_stream ._stream ,
743- signer ._signer ,
744- ctypes .byref (manifest_bytes_ptr )
745- )
746-
747- if result < 0 :
748- error = _parse_operation_result_for_error (_lib .c2pa_error ())
749- if error :
750- raise C2paError (error )
751-
752- # Capture the manifest bytes if available
753- manifest_bytes = b""
754- if manifest_bytes_ptr :
755- # Convert the C pointer to Python bytes
756- manifest_bytes = bytes (manifest_bytes_ptr [:result ])
757- # Free the C-allocated memory
758- _lib .c2pa_manifest_bytes_free (manifest_bytes_ptr )
733+ # Use the builder's internal signing logic
734+ result , manifest_bytes = builder ._sign_internal (signer , mime_type , source_stream , dest_stream )
759735
760736 # If we have manifest bytes and a data directory, write them
761737 if manifest_bytes and data_dir :
@@ -781,10 +757,6 @@ def sign_file_with_callback_signer(
781757 builder .close ()
782758 if 'signer' in locals ():
783759 signer .close ()
784- if 'source_stream' in locals ():
785- source_stream .close ()
786- if 'dest_stream' in locals ():
787- dest_stream .close ()
788760
789761
790762class Stream :
@@ -1886,7 +1858,7 @@ def _sign_internal(
18861858 signer : Signer ,
18871859 format : str ,
18881860 source_stream : Stream ,
1889- dest_stream : Stream ) -> int :
1861+ dest_stream : Stream ) -> tuple [ int , bytes ] :
18901862 """Internal signing logic shared between sign() and sign_file() methods,
18911863 to use same native calls but expose different API surface.
18921864
@@ -1897,7 +1869,7 @@ def _sign_internal(
18971869 dest_stream: The destination stream
18981870
18991871 Returns:
1900- Size of C2PA data
1872+ A tuple of (size of C2PA data, manifest bytes)
19011873
19021874 Raises:
19031875 C2paError: If there was an error during signing
@@ -1924,11 +1896,15 @@ def _sign_internal(
19241896 if error :
19251897 raise C2paError (error )
19261898
1899+ # Capture the manifest bytes if available
1900+ manifest_bytes = b""
19271901 if manifest_bytes_ptr :
1928- # Free the manifest bytes pointer if it was allocated
1902+ # Convert the C pointer to Python bytes
1903+ manifest_bytes = bytes (manifest_bytes_ptr [:result ])
1904+ # Free the C-allocated memory
19291905 _lib .c2pa_manifest_bytes_free (manifest_bytes_ptr )
19301906
1931- return result
1907+ return result , manifest_bytes
19321908 finally :
19331909 # Ensure both streams are cleaned up
19341910 source_stream .close ()
@@ -1956,14 +1932,15 @@ def sign(
19561932 dest_stream = Stream (dest )
19571933
19581934 # Use the internal stream-base signing logic
1935+ # Ignore the return value since this method returns None
19591936 self ._sign_internal (signer , format , source_stream , dest_stream )
19601937
19611938 def sign_file (self ,
19621939 source_path : Union [str ,
19631940 Path ],
19641941 dest_path : Union [str ,
19651942 Path ],
1966- signer : Signer ) -> int :
1943+ signer : Signer ) -> tuple [ int , bytes ] :
19671944 """Sign a file and write the signed data to an output file.
19681945
19691946 Args:
@@ -1972,7 +1949,7 @@ def sign_file(self,
19721949 signer: The signer to use
19731950
19741951 Returns:
1975- Size of C2PA data
1952+ A tuple of (size of C2PA data, manifest bytes)
19761953
19771954 Raises:
19781955 C2paError: If there was an error during signing
@@ -1989,7 +1966,8 @@ def sign_file(self,
19891966 dest_stream = Stream (dest_file )
19901967
19911968 # Use the internal stream-base signing logic
1992- return self ._sign_internal (signer , mime_type , source_stream , dest_stream )
1969+ result , manifest_bytes = self ._sign_internal (signer , mime_type , source_stream , dest_stream )
1970+ return result , manifest_bytes
19931971
19941972
19951973def format_embeddable (format : str , manifest_bytes : bytes ) -> tuple [int , bytes ]:
0 commit comments