API reference
- exception starlark_go.ConversionError(error: str, error_type: str | None = None, *extra_args: Any)
Bases:
StarlarkError
A base class for conversion errors.
- exception starlark_go.ConversionToPythonFailed(error: str, error_type: str | None = None, *extra_args: Any)
Bases:
ConversionError
An error when converting a Starlark value to a Python value.
This exception is raied by
starlark_go.Starlark.eval()
andstarlark_go.Starlark.get()
when a Starlark value can not be converted to a Python value.
- exception starlark_go.ConversionToStarlarkFailed(error: str, error_type: str | None = None, *extra_args: Any)
Bases:
ConversionError
An error when converting a Python value to a Starlark value.
This exception is raied by
starlark_go.Starlark.set()
when a Python value can not be converted to a Starlark value.
- exception starlark_go.EvalError(error: str, error_type: str, filename: str, line: int, column: int, function_name: str, backtrace: str)
Bases:
StarlarkError
A Starlark evaluation error.
This exception is raised when otherwise valid code attempts an illegal operation, such as adding a string to an integer.
- exception starlark_go.ResolveError(error: str, error_type: str, errors: Tuple[ResolveErrorItem, ...])
Bases:
StarlarkError
A Starlark resolution error.
This exception is raised by
starlark_go.Starlark.eval()
orstarlark_go.Starlark.exec()
when an undefined name is referenced.- errors
A list of locations where resolution errors occurred. A ResolveError may contain one or more locations.
- Type:
- exception starlark_go.StarlarkError(error: str, error_type: str | None = None, *extra_args: Any)
Bases:
Exception
Base class for Starlark errors.
All Starlark-specific errors that are raised by
starlark_go.Starlark
are derived from this class.
- exception starlark_go.SyntaxError(error: str, error_type: str, msg: str, filename: str, line: int, column: int)
Bases:
StarlarkError
A Starlark syntax error.
This exception is raised when syntatically invalid code is passed to
starlark_go.Starlark.eval()
orstarlark_go.Starlark.exec()
.- filename
The name of the file that the error occurred in (taken from the
filename
parameter tostarlark_go.Starlark.eval()
orstarlark_go.Starlark.exec()
.)- Type:
- class starlark_go.ResolveErrorItem(msg: str, line: int, column: int)
Bases:
object
A location associated with a
ResolveError
.
- class starlark_go.Starlark(*, globals=None, print=None)
Bases:
object
Create a Starlark object. A Starlark object contains a set of global variables, which can be manipulated by executing Starlark code.
- Parameters:
globals (Mapping[str, Any]) – Initial set of global variables. Keys must be strings. Values can be any type supported by
set()
.print (Callable[[str], Any]) – A function to call in place of Starlark’s
print()
function. If unspecified, Starlark’sprint()
function will be forwarded to Python’s built-inprint()
.
- eval(expr, *, filename=None, convert=True, print=None)
Evaluate a Starlark expression. The expression passed to
eval
must evaluate to a value. Function definitions, variable assignments, and control structures are not allowed byeval
. To use those, please useexec()
.- Parameters:
expr (str) – A string containing a Starlark expression to evaluate
filename (Optional[str]) – An optional filename to use in exceptions, if evaluting the expression fails.
convert (bool) – If True, convert the result of the expression into a Python value. If False, return a string containing the representation of the expression in Starlark. Defaults to True.
print (Callable[[str], Any]) – A function to call in place of Starlark’s
print()
function. If unspecified, Starlark’sprint()
function will be forwarded to Python’s built-inprint()
.
- Raises:
ConversionToPythonFailed – if the value is of an unsupported type for conversion.
EvalError – if there is a Starlark evaluation error
ResolveError – if there is a Starlark resolution error
SyntaxError – if there is a Starlark syntax error
StarlarkError – if there is an unexpected error
- Return type:
- exec(defs, *, filename=None, print=None)
Execute Starlark code. All legal Starlark constructs may be used with
exec
.exec
does not return a value. To evaluate the value of a Starlark expression, please use func:eval.- Parameters:
defs (str) – A string containing Starlark code to execute
filename (Optional[str]) – An optional filename to use in exceptions, if evaluting the expression fails.
print (Callable[[str], Any]) – A function to call in place of Starlark’s
print()
function. If unspecified, Starlark’sprint()
function will be forwarded to Python’s built-inprint()
.
- Raises:
EvalError – if there is a Starlark evaluation error
ResolveError – if there is a Starlark resolution error
SyntaxError – if there is a Starlark syntax error
StarlarkError – if there is an unexpected error
- get(name, default_value=Ellipsis)
Get the value of a Starlark global variable.
Conversion from most Starlark data types is supported:
Starlark dict (and IterableMapping) to Python
dict
For the aggregate types (
dict
,list
,set
, andtuple
,) all keys and/or values must also be one of the supported types.Attempting to get the value of any other Starlark type will raise a
ConversionToPythonFailed
.- Parameters:
- Raises:
KeyError – if there is no global value named
name
defined.ConversionToPythonFailed – if the value is of an unsupported type for conversion.
- Return type:
- pop(name, default_value=Ellipsis)
Remove a Starlark global variable, and return its value.
If a value of
name
does not exist, and nodefault_value
has been specified, raiseKeyError
. Otherwise, returndefault_value
.- Parameters:
- Raises:
KeyError – if there is no global value named
name
defined.ConversionToPythonFailed – if the value is of an unsupported type for conversion.
- Return type:
- set(**kwargs)
Set the value of one or more Starlark global variables.
For each keyword parameter specified, one global variable is set.
Conversion from most basic Python types is supported:
Python
dict
(and other objects that implement the mapping protocol) to Starlark dictPython
list
(and other objects that implement the sequence protocol) to Starlark list
For the aggregate types (
dict
,list
,set
, andtuple
,) all keys and/or values must also be one of the supported types.Attempting to set a value of any other Python type will raise a
ConversionToStarlarkFailed
.- Raises:
ConversionToStarlarkFailed – if a value is of an unsupported type for conversion.
- starlark_go.configure_starlark(*, allow_set=None, allow_global_reassign=None, allow_recursion=None)
Change what features the Starlark interpreter allows. Unfortunately, this manipulates global variables, and affects all Starlark interpreters in your application. It is not possible to have one Starlark interpreter with
allow_set=True
and another withallow_set=False
simultaneously.All feature flags are initially
False
.See the starlark-go documentation for more information.