Author: Ronan Lamy <[email protected]>
Branch: py3.6
Changeset: r96981:d8a0f174215c
Date: 2019-07-13 14:34 +0000
http://bitbucket.org/pypy/pypy/changeset/d8a0f174215c/

Log:    Merged in mkuffa/pypy/fix-importerror (pull request #652)

        Fix ImportError invalid arguments error wording

diff --git a/pypy/module/exceptions/interp_exceptions.py 
b/pypy/module/exceptions/interp_exceptions.py
--- a/pypy/module/exceptions/interp_exceptions.py
+++ b/pypy/module/exceptions/interp_exceptions.py
@@ -96,7 +96,7 @@
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.pytraceback import PyTraceback, check_traceback
-from rpython.rlib import rwin32
+from rpython.rlib import rwin32, jit
 
 
 def readwrite_attrproperty_w(name, cls):
@@ -323,14 +323,20 @@
     w_name = None
     w_path = None
 
+    @jit.unroll_safe
     def descr_init(self, space, __args__):
         args_w, kw_w = __args__.unpack()
         self.w_name = kw_w.pop('name', space.w_None)
         self.w_path = kw_w.pop('path', space.w_None)
         if kw_w:
-            # CPython displays this, but it's not quite right.
-            raise oefmt(space.w_TypeError,
-                        "ImportError does not take keyword arguments")
+            for keyword in __args__.keywords:
+                if keyword in kw_w:
+                    raise oefmt(
+                        space.w_TypeError,
+                        "'%s' is an invalid keyword argument for this 
function" % (
+                            keyword
+                        )
+                    )
         W_Exception.descr_init(self, space, args_w)
 
 
diff --git a/pypy/module/exceptions/test/test_exc.py 
b/pypy/module/exceptions/test/test_exc.py
--- a/pypy/module/exceptions/test/test_exc.py
+++ b/pypy/module/exceptions/test/test_exc.py
@@ -437,3 +437,24 @@
                 exc = raises(SyntaxError, exec_, source)
                 assert (custom_msg not in exc.value.msg) == (
                     ('print (' in source or 'exec (' in source))
+
+    def test_importerror_kwarg_error(self):
+        msg = "'invalid' is an invalid keyword argument for this function"
+        exc = raises(TypeError,
+                     ImportError,
+                     'test', invalid='keyword', another=True)
+        assert str(exc.value) == msg
+
+        exc = raises(TypeError, ImportError, 'test', invalid='keyword')
+        assert str(exc.value) == msg
+
+        exc = raises(TypeError,
+                     ImportError,
+                     'test', name='name', invalid='keyword')
+        assert str(exc.value) == msg
+
+        exc = raises(TypeError,
+                     ImportError,
+                     'test', path='path', invalid='keyword')
+        assert str(exc.value) == msg
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to