Beispiel #1
0
        public async Task HandleWithAsync(WithStatement node, CancellationToken cancellationToken = default) {
            cancellationToken.ThrowIfCancellationRequested();
            foreach (var item in node.Items.Where(x => x.Variable != null)) {
                var contextManager = await Eval.GetValueFromExpressionAsync(item.ContextManager, cancellationToken);
                var cmType = contextManager.GetPythonType();

                var enter = cmType?.GetMember(node.IsAsync ? @"__aenter__" : @"__enter__")?.GetPythonType<IPythonFunctionType>();
                if (enter != null) {
                    var context = await Eval.GetValueFromFunctionTypeAsync(enter, null, null, cancellationToken);
                    if (item.Variable is NameExpression nex && !string.IsNullOrEmpty(nex.Name)) {
                        Eval.DeclareVariable(nex.Name, context, Eval.GetLoc(item));
                    }
                }
            }
        }