Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def attrgetter_atom_split(tokens):
"""Split attrgetter_atom_tokens into (attr_or_method_name, method_args_or_none_if_attr)."""
if len(tokens) == 1: # .attr
return tokens[0], None
elif len(tokens) >= 2 and tokens[1] == "(": # .method(...
if len(tokens) == 2: # .method()
return tokens[0], ""
elif len(tokens) == 3: # .method(args)
return tokens[0], tokens[2]
else:
raise CoconutInternalException("invalid methodcaller literal tokens", tokens)
else:
raise CoconutInternalException("invalid attrgetter literal tokens", tokens)
def assign_to_series(self, name, series_type, item):
"""Assign name to item converted to the given series_type."""
if series_type == "(":
self.add_def(name + " = _coconut.tuple(" + item + ")")
elif series_type == "[":
self.add_def(name + " = _coconut.list(" + item + ")")
else:
raise CoconutInternalException("invalid series match type", series_type)
def addskip(skips, skip):
"""Add a line skip to the skips."""
if skip < 1:
complain(CoconutInternalException("invalid skip of line " + str(skip)))
else:
skips.append(skip)
return skips
argdict_pairs.append(str(i) + ": " + arg)
if not has_question_mark:
raise CoconutInternalException("no question mark in question mark partial", trailer[1])
elif argdict_pairs or extra_args_str:
out = (
"_coconut_partial("
+ out
+ ", {" + ", ".join(argdict_pairs) + "}"
+ ", " + str(len(pos_args))
+ (", " if extra_args_str else "") + extra_args_str
+ ")"
)
else:
raise CoconutDeferredSyntaxError("a non-? partial application argument is required", loc)
else:
raise CoconutInternalException("invalid special trailer", trailer[0])
else:
raise CoconutInternalException("invalid trailer tokens", trailer)
return out
def typedef_handle(self, tokens):
"""Process Python 3 type annotations."""
if len(tokens) == 1: # return typedef
if self.target.startswith("3"):
return " -> " + self.wrap_typedef(tokens[0]) + ":"
else:
return ":\n" + self.wrap_comment(" type: (...) -> " + tokens[0])
else: # argument typedef
if len(tokens) == 3:
varname, typedef, comma = tokens
default = ""
elif len(tokens) == 4:
varname, typedef, default, comma = tokens
else:
raise CoconutInternalException("invalid type annotation tokens", tokens)
if self.target.startswith("3"):
return varname + ": " + self.wrap_typedef(typedef) + default + comma
else:
return varname + default + comma + self.wrap_passthrough(self.wrap_comment(" type: " + typedef) + "\n" + " " * self.tabideal)
def typedef_callable_handle(tokens):
"""Process -> to Callable inside type annotations."""
if len(tokens) == 1:
return '_coconut.typing.Callable[..., ' + tokens[0] + ']'
elif len(tokens) == 2:
return '_coconut.typing.Callable[[' + tokens[0] + '], ' + tokens[1] + ']'
else:
raise CoconutInternalException("invalid Callable typedef tokens", tokens)
decorators, funcdef = tokens
else:
raise CoconutInternalException("invalid function definition tokens", tokens)
# process tokens
raw_lines = funcdef.splitlines(True)
def_stmt = raw_lines.pop(0)
# detect addpattern functions
if def_stmt.startswith("addpattern def"):
def_stmt = def_stmt[len("addpattern "):]
addpattern = True
elif def_stmt.startswith("def"):
addpattern = False
else:
raise CoconutInternalException("invalid function definition statement", def_stmt)
# extract information about the function
func_name, func_args, func_params = None, None, None
with self.complain_on_err():
func_name, func_args, func_params = parse(self.split_func, def_stmt)
# handle addpattern functions
if addpattern:
if func_name is None:
raise CoconutInternalException("could not find name in addpattern function definition", def_stmt)
# binds most tightly, except for TCO
decorators += "@_coconut_addpattern(" + func_name + ")\n"
# handle dotted function definition
undotted_name = None # the function __name__ if func_name is a dotted name
if func_name is not None:
rest = None
if "simple" in stmts and len(stmts) == 1:
out += extra_stmts
rest = stmts[0]
elif "docstring" in stmts and len(stmts) == 1:
out += stmts[0] + extra_stmts
elif "complex" in stmts and len(stmts) == 1:
out += extra_stmts
rest = "".join(stmts[0])
elif "complex" in stmts and len(stmts) == 2:
out += stmts[0] + extra_stmts
rest = "".join(stmts[1])
elif "empty" in stmts and len(stmts) == 1:
out += extra_stmts.rstrip() + stmts[0]
else:
raise CoconutInternalException("invalid inner data tokens", stmts)
# create full data definition
if rest is not None and rest != "pass\n":
out += rest
out += closeindent
return out
argstr = "".join(arg)
if len(arg) == 1:
if star_args or kwd_args or dubstar_args:
raise CoconutDeferredSyntaxError("positional arguments must come first", loc)
pos_args.append(argstr)
elif len(arg) == 2:
if arg[0] == "*":
if kwd_args or dubstar_args:
raise CoconutDeferredSyntaxError("star unpacking must come before keyword arguments", loc)
star_args.append(argstr)
elif arg[0] == "**":
dubstar_args.append(argstr)
else:
kwd_args.append(argstr)
else:
raise CoconutInternalException("invalid function call argument", arg)
return pos_args, star_args, kwd_args, dubstar_args