Run a sequence of AST nodes. The execution mode depends on the interactivity parameter. Parameters ---------- nodelist : list A sequence of AST nodes to run. cell_name : str Will be passed to the compiler as the filename of the cell. Typic
(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr',
compiler=compile, result=None)
| 3223 | return node |
| 3224 | |
| 3225 | async def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr', |
| 3226 | compiler=compile, result=None): |
| 3227 | """Run a sequence of AST nodes. The execution mode depends on the |
| 3228 | interactivity parameter. |
| 3229 | |
| 3230 | Parameters |
| 3231 | ---------- |
| 3232 | nodelist : list |
| 3233 | A sequence of AST nodes to run. |
| 3234 | cell_name : str |
| 3235 | Will be passed to the compiler as the filename of the cell. Typically |
| 3236 | the value returned by ip.compile.cache(cell). |
| 3237 | interactivity : str |
| 3238 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', |
| 3239 | specifying which nodes should be run interactively (displaying output |
| 3240 | from expressions). 'last_expr' will run the last node interactively |
| 3241 | only if it is an expression (i.e. expressions in loops or other blocks |
| 3242 | are not displayed) 'last_expr_or_assign' will run the last expression |
| 3243 | or the last assignment. Other values for this parameter will raise a |
| 3244 | ValueError. |
| 3245 | |
| 3246 | Experimental value: 'async' Will try to run top level interactive |
| 3247 | async/await code in default runner, this will not respect the |
| 3248 | interactivity setting and will only run the last node if it is an |
| 3249 | expression. |
| 3250 | |
| 3251 | compiler : callable |
| 3252 | A function with the same interface as the built-in compile(), to turn |
| 3253 | the AST nodes into code objects. Default is the built-in compile(). |
| 3254 | result : ExecutionResult, optional |
| 3255 | An object to store exceptions that occur during execution. |
| 3256 | |
| 3257 | Returns |
| 3258 | ------- |
| 3259 | True if an exception occurred while running code, False if it finished |
| 3260 | running. |
| 3261 | """ |
| 3262 | if not nodelist: |
| 3263 | return |
| 3264 | |
| 3265 | if interactivity == 'last_expr_or_assign': |
| 3266 | if isinstance(nodelist[-1], _assign_nodes): |
| 3267 | asg = nodelist[-1] |
| 3268 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: |
| 3269 | target = asg.targets[0] |
| 3270 | elif isinstance(asg, _single_targets_nodes): |
| 3271 | target = asg.target |
| 3272 | else: |
| 3273 | target = None |
| 3274 | if isinstance(target, ast.Name): |
| 3275 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) |
| 3276 | ast.fix_missing_locations(nnode) |
| 3277 | nodelist.append(nnode) |
| 3278 | interactivity = 'last_expr' |
| 3279 | |
| 3280 | _async = False |
| 3281 | if interactivity == 'last_expr': |
| 3282 | if isinstance(nodelist[-1], ast.Expr): |
no test coverage detected