Skip to content

Commit

Permalink
0
Browse files Browse the repository at this point in the history
  • Loading branch information
srepmub committed Jul 29, 2024
1 parent 859f7d0 commit fff793a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
5 changes: 3 additions & 2 deletions shedskin/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 36,7 @@ class GenerateVisitor: inherits visitor pattern from ast_utils.BaseNodeVisitor,

Types: TypeAlias = set[Tuple['python.Class', int]]
Parent: TypeAlias = Union['python.Class', 'python.Function']
AllParent: TypeAlias = Union['python.Class', 'python.Function', 'python.StaticClass']


class CPPNamer:
Expand Down Expand Up @@ -367,7 368,7 @@ def declare_defs(self, vars: List[Tuple[str, 'python.Variable']], declare: bool)
return "".join(self.group_declarations(pairs))

def get_constant(self, node:ast.Constant) -> Optional[str]:
parent: Union['python.Function', 'python.Class', None] = infer.inode(self.gx, node).parent
parent: Optional[AllParent] = infer.inode(self.gx, node).parent
while isinstance(parent, python.Function) and parent.listcomp: # XXX
parent = parent.parent
if isinstance(parent, python.Function) and (
Expand Down Expand Up @@ -3222,7 3223,7 @@ def do_listcomps(self, declare: bool) -> None:
if lcfunc.mv.module.builtin:
continue

parent: Union['python.Function', 'python.Class', None] = func
parent: Optional[AllParent] = func
while isinstance(parent, python.Function) and parent.listcomp:
parent = parent.parent

Expand Down
50 changes: 33 additions & 17 deletions shedskin/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 44,7 @@
from . import config

Parent: TypeAlias = Union['python.Class', 'python.Function']
AllParent: TypeAlias = Union['python.Class', 'python.Function', 'python.StaticClass']

# --- global variable mv
_mv: 'ModuleVisitor'
Expand Down Expand Up @@ -382,7 383,7 @@ def add_dynamic_constraint(self, parent: ast.AST, child: ast.AST, varname: str,
def add_constraint(self, constraint: Tuple[infer.CNode, infer.CNode], func: Optional['python.Function']) -> None:
infer.in_out(constraint[0], constraint[1])
self.gx.constraints.add(constraint)
parent: Optional[Parent] = func
parent: Optional[AllParent] = func
while isinstance(parent, python.Function) and parent.listcomp: # TODO occurs frequently, ugly typing.. add Function method(s)
parent = parent.parent
if isinstance(parent, python.Function):
Expand Down Expand Up @@ -538,7 539,7 @@ def visit_NamedExpr(self, node:ast.NamedExpr, func:Optional['python.Function']=N

assert isinstance(node.target, ast.Name)

parent : Optional[Parent] = func
parent : Optional[AllParent] = func
while parent and isinstance(parent, python.Function) and parent.listcomp:
parent.misses_by_ref.add(node.target.id)
parent = parent.parent
Expand Down Expand Up @@ -2040,21 2041,36 @@ def visit_ClassDef(self, node: ast.ClassDef, func:Optional['python.Function']=No
return

# --- built-in functions
for cl in [newclass, newclass.parent]:
for ident in ["__setattr__", "__getattr__"]:
func = python.Function(self.gx, getmv())
func.ident = ident
func.parent = cl

if ident == "__setattr__":
func.formals = ["name", "whatsit"]
retexpr = ast.Return(value=None)
self.visit(retexpr, func)
elif ident == "__getattr__":
func.formals = ["name"]

assert cl
cl.funcs[ident] = func
for ident in ["__setattr__", "__getattr__"]:
func = python.Function(self.gx, getmv())
func.ident = ident
func.parent = newclass

if ident == "__setattr__":
func.formals = ["name", "whatsit"]
retexpr = ast.Return(value=None)
self.visit(retexpr, func)
elif ident == "__getattr__":
func.formals = ["name"]

assert newclass
newclass.funcs[ident] = func

newstaticclass = newclass.parent # TODO copy-paste of above for mypy --strict
for ident in ["__setattr__", "__getattr__"]:
func = python.Function(self.gx, getmv())
func.ident = ident
func.parent = newstaticclass

if ident == "__setattr__":
func.formals = ["name", "whatsit"]
retexpr = ast.Return(value=None)
self.visit(retexpr, func)
elif ident == "__getattr__":
func.formals = ["name"]

assert newstaticclass
newstaticclass.funcs[ident] = func

# --- built-in attributes
if "class_" in getmv().classes or "class_" in getmv().ext_classes:
Expand Down
7 changes: 4 additions & 3 deletions shedskin/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 147,7 @@ def __init__(
thing: Any,
dcpa: int = 0,
cpa: int = 0,
parent: Optional[Parent] = None,
parent: Optional[AllParent] = None,
):
self.gx = gx
self.thing = thing
Expand Down Expand Up @@ -928,6 928,7 @@ def possible_argtypes(gx: 'config.GlobalInfo', node: CNode, funcs: PossibleFuncs
while argtypes and not argtypes[-1]:
argtypes = argtypes[:-1]
if func.lambdawrapper:
assert isinstance(node.parent, python.Function)
if starargs and node.parent and node.parent.node and node.parent.node.args.vararg:
func.largs = (
node.parent.xargs[node.dcpa, node.cpa]
Expand Down Expand Up @@ -1996,15 1997,15 @@ def analyze(gx: "config.GlobalInfo", module_name: str) -> None:
nodetypestr(gx, node, inode(gx, node).parent, mv=inode(gx, node).mv)


def register_temp_var(var: 'python.Variable', parent: Optional[Parent]) -> None:
def register_temp_var(var: 'python.Variable', parent: Optional[AllParent]) -> None:
if isinstance(parent, python.Function):
parent.registered_temp_vars.append(var)


def default_var(
gx: "config.GlobalInfo",
name: str,
parent: Optional[Union['python.Function', 'python.Class']],
parent: Optional[AllParent],
worklist: Optional[List[CNode]]=None,
mv: Optional['graph.ModuleVisitor']=None,
exc_name: bool=False
Expand Down
6 changes: 3 additions & 3 deletions shedskin/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 217,7 @@ def __init__(
gx: 'config.GlobalInfo',
mv: 'graph.ModuleVisitor',
node: Optional[ast.FunctionDef]=None,
parent: Optional[Parent]=None,
parent: Optional[AllParent]=None,
inherited_from: Optional[Union['Class', 'Function']]=None, # TODO should be one
):
self.gx = gx
Expand Down Expand Up @@ -274,7 274,7 @@ def __repr__(self) -> str:


class Variable:
def __init__(self, name: str, parent: Optional[Parent]):
def __init__(self, name: str, parent: Optional[AllParent]):
self.name = name
self.parent = parent
self.invisible = False # not in C output
Expand Down Expand Up @@ -367,7 367,7 @@ def lookup_implementor(cl: Class, ident: str) -> Optional[str]:
return None


def lookup_class_module(objexpr: ast.AST, mv: 'graph.ModuleVisitor', parent: Optional[Parent]) -> Tuple[Optional['Class'], Optional['Module']]:
def lookup_class_module(objexpr: ast.AST, mv: 'graph.ModuleVisitor', parent: Optional[AllParent]) -> Tuple[Optional['Class'], Optional['Module']]:
if isinstance(objexpr, ast.Name): # XXX ast.Attribute?
var = lookup_var(objexpr.id, parent, mv)
if var and not var.imported: # XXX cl?
Expand Down

0 comments on commit fff793a

Please sign in to comment.