Buckets:
| """Structured caller regions with source-derived local data/effect contracts. | |
| Regions are ordinary contiguous source, transplanted INLINE. Thus loops, | |
| returns, local variables, and the actual continuation retain native semantics. | |
| Dependency-closed spans are discovered from code, never task stage labels. | |
| """ | |
| from __future__ import annotations | |
| import ast,copy,hashlib,symtable | |
| from dataclasses import dataclass | |
| from flow import analyze | |
| def normalized(nodes):return ast.dump(ast.Module(body=nodes,type_ignores=[]),include_attributes=False) | |
| def bindings(nodes): | |
| result=set() | |
| for node in nodes: | |
| if isinstance(node,(ast.FunctionDef,ast.ClassDef,ast.AsyncFunctionDef)):result.add(node.name) | |
| elif isinstance(node,(ast.Import,ast.ImportFrom)): | |
| for a in node.names:result.add(a.asname or a.name.split('.')[0]) | |
| else:result|={n.id for n in ast.walk(node)if isinstance(n,ast.Name)and isinstance(n.ctx,(ast.Store,ast.Del))} | |
| return result | |
| def effects(nodes,local): | |
| reads,writes,must,known,abrupt=analyze(nodes,local);return reads,writes,abrupt | |
| class Region: | |
| key:str | |
| function:str | |
| path:tuple | |
| start:int | |
| stop:int | |
| nodes:list | |
| reads:set | |
| writes:set | |
| exits:set | |
| spine:tuple | |
| global_names:set | |
| must_writes:set=None | |
| def contract(self):return (self.function,tuple(sorted(self.reads)),tuple(sorted(self.writes)),tuple(sorted(self.exits)),self.spine,tuple(sorted(self.must_writes or()))) | |
| def fingerprint(self):return hashlib.sha256(normalized(self.nodes).encode()).hexdigest() | |
| def discover(source): | |
| tree=ast.parse(source);table=symtable.symtable(source,'<region-source>','exec');tables={t.get_name():t for t in table.get_children()};module_names=bindings(tree.body);regions={} | |
| def visit_block(fn,body,path,spine,local): | |
| records=[effects([s],local)for s in body];spans=set() | |
| for i,stmt in enumerate(body): | |
| if isinstance(stmt,(ast.FunctionDef,ast.AsyncFunctionDef,ast.ClassDef,ast.Global,ast.Nonlocal)):continue | |
| spans.add((i,i+1)) | |
| # Close the seed over later consumers and updates of its state. | |
| affected=set(records[i][1]);last=i | |
| for j in range(i+1,len(body)): | |
| if affected&(records[j][0]|records[j][1]):last=j;affected|=records[j][1] | |
| if last>i:spans.add((i,last+1)) | |
| # Carry preparatory definitions from the SAME lexical block. All | |
| # intervening source stays in order; nothing is arbitrarily moved. | |
| left=i;needed=set(records[i][0]);changed=True | |
| while changed: | |
| changed=False | |
| for j in range(left-1,-1,-1): | |
| if records[j][1]&needed: | |
| needed=(needed-records[j][1])|records[j][0];left=j;changed=True;break | |
| if left<i:spans.add((left,i+1)) | |
| if len(body)>1:spans.add((0,len(body))) | |
| for lo,hi in sorted(spans): | |
| nodes=body[lo:hi] | |
| if any(isinstance(n,ast.Name)and n.id=='_sg_install_bindings'for s in nodes for n in ast.walk(s)):continue | |
| if any(isinstance(n,(ast.Global,ast.Nonlocal,ast.Yield,ast.YieldFrom,ast.Await))for s in nodes for n in ast.walk(s)):continue | |
| reads,writes,exits=effects(nodes,local);key=fn+':'+repr(path)+':'+str(lo)+':'+str(hi) | |
| globals_used={n.id for s in nodes for n in ast.walk(s)if isinstance(n,ast.Name)and isinstance(n.ctx,ast.Load)and n.id in module_names-local} | |
| regions[key]=Region(key,fn,path,lo,hi,copy.deepcopy(nodes),reads,writes,exits,spine,globals_used,analyze(nodes,local)[2]) | |
| for i,stmt in enumerate(body): | |
| if isinstance(stmt,(ast.FunctionDef,ast.ClassDef,ast.AsyncFunctionDef)):continue | |
| for field,value in ast.iter_fields(stmt): | |
| if field=='handlers': | |
| for k,handler in enumerate(value):visit_block(fn,handler.body,path+((i,field,k),),spine+('except',),local) | |
| elif isinstance(value,list)and value and all(isinstance(x,ast.stmt)for x in value): | |
| visit_block(fn,value,path+((i,field),),spine+(type(stmt).__name__+':'+field,),local) | |
| for fn in tree.body: | |
| if not isinstance(fn,ast.FunctionDef):continue | |
| if fn.name.startswith('_sg_'):continue | |
| local=set(tables[fn.name].get_locals())|set(tables[fn.name].get_parameters()) | |
| visit_block(fn.name,fn.body,(),(),local) | |
| # Module-level exports/constants are source regions too. Only bindings | |
| # actually read by function code are candidates; compiler bootstrap and | |
| # unrelated metadata never become arbitrary algorithm parameters. | |
| public_reads=set() | |
| for fn in tables.values(): | |
| if fn.get_type()=='function':public_reads|={s.get_name()for s in fn.get_symbols()if s.is_referenced()and s.is_global()} | |
| for i,node in enumerate(tree.body): | |
| if not isinstance(node,(ast.Assign,ast.AnnAssign)):continue | |
| reads,writes,exits=effects([node],module_names) | |
| if not writes&public_reads or any(n.startswith('__')for n in writes):continue | |
| key='@module:(): '+str(i);used={n.id for n in ast.walk(node)if isinstance(n,ast.Name)and isinstance(n.ctx,ast.Load)and n.id in module_names-writes} | |
| regions[key]=Region(key,'@module',(),i,i+1,[copy.deepcopy(node)],reads,writes,exits,(),used,analyze([node],module_names)[2]) | |
| return regions | |
| def at(tree,region): | |
| if region.function=='@module':return tree.body | |
| fn=next(n for n in tree.body if isinstance(n,ast.FunctionDef)and n.name==region.function);body=fn.body | |
| for step in region.path: | |
| stmt=body[step[0]] | |
| body=getattr(stmt,step[1])if len(step)==2 else getattr(stmt,step[1])[step[2]].body | |
| return body | |
| class OwnGlobals(ast.NodeTransformer): | |
| def __init__(self,names,owner):self.names=set(names);self.owner=owner;self.shadow=[] | |
| def visit_Name(self,node): | |
| if isinstance(node.ctx,ast.Load)and node.id in self.names and not any(node.id in scope for scope in self.shadow): | |
| return ast.copy_location(ast.Subscript(value=ast.Name(id=self.owner,ctx=ast.Load()),slice=ast.Constant(value=node.id),ctx=ast.Load()),node) | |
| return node | |
| def visit_Lambda(self,node): | |
| names={a.arg for a in node.args.posonlyargs+node.args.args+node.args.kwonlyargs};self.shadow.append(names);node=self.generic_visit(node);self.shadow.pop();return node | |
| def visit_FunctionDef(self,node): | |
| names=bindings(node.body)|{a.arg for a in node.args.posonlyargs+node.args.args+node.args.kwonlyargs};self.shadow.append(names);node=self.generic_visit(node);self.shadow.pop();return node | |
| visit_AsyncFunctionDef=visit_FunctionDef | |
| def replace(source,edits): | |
| """edits are (receiver region, donor region, private namespace symbol).""" | |
| tree=ast.parse(source) | |
| # Descending addresses preserve all untouched source locations. The caller | |
| # rejects overlapping edits before this function is reached. | |
| for receiver,donor,owner in sorted(edits,key=lambda e:(repr(e[0].path),e[0].start),reverse=True): | |
| nodes=copy.deepcopy(donor.nodes);rewrite=OwnGlobals(donor.global_names,owner);nodes=[rewrite.visit(n)for n in nodes] | |
| body=at(tree,receiver);body[receiver.start:receiver.stop]=nodes | |
| return ast.unparse(ast.fix_missing_locations(tree))+'\n' | |
| def overlaps(a,b): | |
| if a.function!=b.function:return False | |
| if a.function=='@module'and(getattr(a,'fragments',None)or getattr(b,'fragments',None)): | |
| aa=getattr(a,'fragments',None)or[(a.lo,a.hi)];bb=getattr(b,'fragments',None)or[(b.lo,b.hi)] | |
| return any(max(lo,x)<min(hi,y)for lo,hi in aa for x,y in bb) | |
| if a.path==b.path:return max(a.start,b.start)<min(a.stop,b.stop) | |
| for outer,inner in [(a,b),(b,a)]: | |
| if inner.path[:len(outer.path)]==outer.path and len(inner.path)>len(outer.path): | |
| index=inner.path[len(outer.path)][0] | |
| if outer.start<=index<outer.stop:return True | |
| return False | |
Xet Storage Details
- Size:
- 8 kB
- Xet hash:
- c7bc2e2036f6a234bf351587cbf5846e35709476202b21d1cfd9e2b9f8c5b864
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.