[pypy-commit] pypy cpyext-fast-typecheck: make sure to raise TypeError if you pass keywords to a slot which does not expect them

antocuni pypy.commits at gmail.com
Thu Mar 22 11:47:47 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-fast-typecheck
Changeset: r94081:8b23d59cea09
Date: 2018-03-22 16:47 +0100
http://bitbucket.org/pypy/pypy/changeset/8b23d59cea09/

Log:	make sure to raise TypeError if you pass keywords to a slot which
	does not expect them

diff --git a/pypy/module/cpyext/methodobject.py b/pypy/module/cpyext/methodobject.py
--- a/pypy/module/cpyext/methodobject.py
+++ b/pypy/module/cpyext/methodobject.py
@@ -214,6 +214,9 @@
 
 
 class W_PyCWrapperObject(W_Root):
+    """
+    Abstract class; for concrete subclasses, see slotdefs.py
+    """
 
     def __init__(self, space, pto, method_name, doc, func, offset):
         self.space = space
@@ -229,6 +232,9 @@
     def descr_call(self, space, w_self, __args__):
         return self.call(space, w_self, __args__)
 
+    def call(self, w_self, __args__):
+        raise NotImplementedError
+
     def get_func_to_call(self):
         func_to_call = self.func
         if self.offset:
@@ -248,12 +254,15 @@
         assert func_to_call
         return func_to_call
 
-    def check_args(self, __args__, arity):
-        # XXX: check for keywords
+    def check_args(self, __args__, arity, accept_kw=False):
         length = len(__args__.arguments_w)
         if length != arity:
             raise oefmt(self.space.w_TypeError, "expected %d arguments, got %d",
                         arity, length)
+        if not accept_kw and __args__.keywords:
+            raise oefmt(self.space.w_TypeError,
+                        "wrapper %s doesn't take any keyword arguments",
+                        self.method_name)
 
     def descr_method_repr(self):
         return self.space.newtext("<slot wrapper '%s' of '%s' objects>" %
diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -824,6 +824,7 @@
         obj = module.new_obj()
         assert obj[100] == 42
         raises(TypeError, "obj.__getitem__(100, 101)")
+        raises(TypeError, "obj.__getitem__(100, a=42)")
 
     def test_mp_ass_subscript(self):
         module = self.import_extension('foo', [


More information about the pypy-commit mailing list