Commits

masklinn committed b8d1143

Implement evaluation of kwargs

Comments (0)

Files changed (2)

         this._inst = null;
         this._func = nativefunc;
     }, py.object, {
-        __call__: function (args) {
+        __call__: function (args, kwargs) {
             // don't want to rewrite __call__ for instancemethod
-            return this._func.call(this._inst, args);
+            return this._func.call(this._inst, args, kwargs);
         },
         toJSON: function () {
             return this._func;
             return py.evaluate(expr.second, context);
         case '(':
             if (expr.second) {
-                var callable = py.evaluate(expr.first, context), args=[];
+                var callable = py.evaluate(expr.first, context);
+                var args = [], kwargs = {};
                 for (var jj=0; jj<expr.second.length; ++jj) {
-                    args.push(py.evaluate(
-                        expr.second[jj], context));
+                    var arg = expr.second[jj];
+                    if (arg.id !== '=') {
+                        // arg
+                        args.push(py.evaluate(arg, context));
+                    } else {
+                        // kwarg
+                        kwargs[arg.first.value] =
+                            py.evaluate(arg.second, context);
+                    }
                 }
-                return callable.__call__(args);
+                return callable.__call__(args, kwargs);
             }
             var tuple_exprs = expr.first,
                 tuple_values = [];
         });
         expect(py.eval('MyType()', {MyType: typ})).to.be(true);
     });
+    it('should accept kwargs', function () {
+        expect(py.eval('foo(ok=True)', {
+            foo: function foo() { return py.True; }
+        })).to.be(true);
+    });
+    it('should be able to get its kwargs', function () {
+        expect(py.eval('foo(ok=True)', {
+            foo: function foo(args, kwargs) { return kwargs.ok; }
+        })).to.be(true);
+    });
+    it('should be able to have both args and kwargs', function () {
+        expect(py.eval('foo(1, 2, 3, ok=True, nok=False)', {
+            foo: function (args, kwargs) {
+                expect(args).to.have.length(3);
+                expect(args[0].toJSON()).to.be(1);
+                expect(kwargs).to.only.have.keys('ok', 'nok')
+                expect(kwargs.nok.toJSON()).to.be(false);
+                return kwargs.ok;
+            }
+        })).to.be(true);
+    });
 });
 describe('issubclass', function () {
     it('should say a type is its own subclass', function () {