Пример #1
0
 public BinaryRetTypeBinder(DynamicMetaObjectBinder operationBinder, PythonConversionBinder conversionBinder) :
     base(new BinderMappingInfo(
             operationBinder,
             ParameterMappingInfo.Parameter(0),
             ParameterMappingInfo.Parameter(1)
         ),
         new BinderMappingInfo(
             conversionBinder,
             ParameterMappingInfo.Action(0)
         )
     ) {
     _opBinder = operationBinder;
     _convBinder = conversionBinder;
 }
Пример #2
0
 public OperationRetBoolBinder(PythonBinaryOperationBinder operationBinder, PythonConversionBinder conversionBinder) :
     base(new BinderMappingInfo(
             operationBinder,
             ParameterMappingInfo.Parameter(0),
             ParameterMappingInfo.Parameter(1)
         ),
         new BinderMappingInfo(
             conversionBinder,
             ParameterMappingInfo.Action(0)
         )
     ) {
     _opBinder = operationBinder;
     _convBinder = conversionBinder;
 }
Пример #3
0
 public DynamicMetaObject BindConvert(PythonConversionBinder binder) {
     return ConvertWorker(binder, binder.Type, binder.ReturnType, binder.ResultKind);
 }
Пример #4
0
 /// <summary>
 ///  Various helpers related to calling Python __*__ conversion methods 
 /// </summary>
 private static DynamicMetaObject/*!*/ GetConversionFailedReturnValue(PythonConversionBinder/*!*/ convertToAction, DynamicMetaObject/*!*/ self) {
     switch (convertToAction.ResultKind) {
         case ConversionResultKind.ImplicitTry:
         case ConversionResultKind.ExplicitTry:
             return new DynamicMetaObject(DefaultBinder.GetTryConvertReturnValue(convertToAction.Type), BindingRestrictions.Empty);
         case ConversionResultKind.ExplicitCast:
         case ConversionResultKind.ImplicitCast:
             DefaultBinder db = PythonContext.GetPythonContext(convertToAction).Binder;
             return DefaultBinder.MakeError(
                 db.MakeConversionError(
                     convertToAction.Type,
                     self.Expression
                 ), 
                 typeof(object)
             );
         default:
             throw new InvalidOperationException(convertToAction.ResultKind.ToString());
     }
 }
 public DynamicConvertExpression(PythonConversionBinder binder, CompilationMode mode, MSAst.Expression target)
 {
     _binder = binder;
     _mode   = mode;
     _target = target;
 }
 public DynamicConvertExpression(PythonConversionBinder binder, CompilationMode mode, MSAst.Expression target) {
     _binder = binder;
     _mode = mode;
     _target = target;
 }
Пример #7
0
        private static DynamicMetaObject MakeEnumeratorOperation(PythonOperationBinder operation, DynamicMetaObject self) {
            if (self.GetLimitType() == typeof(string)) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        typeof(PythonOps).GetMethod("StringEnumerator"),
                        self.Expression
                    ),
                    self.Restrictions
                );
            } else if (self.GetLimitType() == typeof(PythonDictionary)) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        typeof(PythonOps).GetMethod("MakeDictionaryKeyEnumerator"),
                        self.Expression
                    ),
                    self.Restrictions
                );
            } else if (self.Value is IEnumerable ||
                       typeof(IEnumerable).IsAssignableFrom(self.GetLimitType())) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        Expression.Convert(
                            self.Expression,
                            typeof(IEnumerable)
                        ),
                        typeof(IEnumerable).GetMethod("GetEnumerator")
                    ),
                    self.Restrictions
                );

            } else if (self.Value is IEnumerator ||                                 // check for COM object (and fast check when we have values)
                       typeof(IEnumerator).IsAssignableFrom(self.GetLimitType())) { // check if we don't have a value
                DynamicMetaObject ieres = new DynamicMetaObject(
                    Ast.Convert(
                        self.Expression,
                        typeof(IEnumerator)
                    ),
                    self.Restrict(self.GetLimitType()).Restrictions
                );

#if !SILVERLIGHT
                if (ComOps.IsComObject(self.Value)) {
                    ieres = new DynamicMetaObject(
                        Expression.Convert(
                             self.Expression,
                             typeof(IEnumerator)
                         ),
                         ieres.Restrictions.Merge(
                            BindingRestrictions.GetExpressionRestriction(
                                Ast.TypeIs(self.Expression, typeof(IEnumerator))
                            )
                        )
                    );
                }
#endif

                return ieres;
            }

            ParameterExpression tmp = Ast.Parameter(typeof(IEnumerator), "enum");
            IPythonConvertible pyConv = self as IPythonConvertible;
            PythonConversionBinder convBinder = new PythonConversionBinder(BinderState.GetBinderState(operation), typeof(IEnumerator), ConversionResultKind.ExplicitTry);

            DynamicMetaObject res;
            if (pyConv != null) {
                res = pyConv.BindConvert(convBinder);
            } else {
                res = convBinder.Bind(self, new DynamicMetaObject[0]);
            }

            return new DynamicMetaObject(
                Expression.Block(
                    new[] { tmp },
                    Ast.Condition(
                        Ast.NotEqual(
                            Ast.Assign(tmp, res.Expression),
                            AstUtils.Constant(null)
                        ),
                        tmp,
                        Ast.Call(
                            typeof(PythonOps).GetMethod("ThrowTypeErrorForBadIteration"),
                            BinderState.GetCodeContext(operation),
                            self.Expression
                        )
                    )
                ),
                res.Restrictions
            );
        }