[pypy-commit] pypy fix-importerror: Fix ImportError invalid arguments error wording

beezz pypy.commits at gmail.com
Sat Jul 13 10:35:15 EDT 2019


Author: Michal Kuffa <michal.kuffa at gmail.com>
Branch: fix-importerror
Changeset: r96978:5f86d11cfec0
Date: 2019-07-13 14:58 +0200
http://bitbucket.org/pypy/pypy/changeset/5f86d11cfec0/

Log:	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
@@ -328,9 +328,14 @@
         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
+


More information about the pypy-commit mailing list