From 1c16de5ceb04d2df847831131de63eeb272d9e97 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:58:31 +0300 Subject: [PATCH 1/4] `tomllib.loads`: Raise TypeError not AttributeError. Improve message --- Lib/test/test_tomllib/test_error.py | 9 +++++++++ Lib/tomllib/_parser.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_tomllib/test_error.py b/Lib/test/test_tomllib/test_error.py index 72446267f04759..d2ef59a29ca350 100644 --- a/Lib/test/test_tomllib/test_error.py +++ b/Lib/test/test_tomllib/test_error.py @@ -39,6 +39,15 @@ def test_invalid_char_quotes(self): tomllib.loads("v = '\n'") self.assertTrue(" '\\n' " in str(exc_info.exception)) + def test_type_error(self): + with self.assertRaises(TypeError) as exc_info: + tomllib.loads(b"v = 1") # type: ignore[arg-type] + self.assertEqual(str(exc_info.exception), "Expected str object, not 'bytes'") + + with self.assertRaises(TypeError) as exc_info: + tomllib.loads(False) # type: ignore[arg-type] + self.assertEqual(str(exc_info.exception), "Expected str object, not 'bool'") + def test_module_name(self): self.assertEqual(tomllib.TOMLDecodeError().__module__, tomllib.__name__) diff --git a/Lib/tomllib/_parser.py b/Lib/tomllib/_parser.py index 45ca7a89630f0e..f25ef68744d26a 100644 --- a/Lib/tomllib/_parser.py +++ b/Lib/tomllib/_parser.py @@ -71,7 +71,12 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n # The spec allows converting "\r\n" to "\n", even in string # literals. Let's do so to simplify parsing. - src = s.replace("\r\n", "\n") + try: + src = __s.replace("\r\n", "\n") + except (AttributeError, TypeError): + raise TypeError( + f"Expected str object, not '{type(__s).__qualname__}'" + ) from None pos = 0 out = Output(NestedDict(), Flags()) header: Key = () From 9d972023b1f4b3749f266d5087de35047914c4ce Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:27:36 +0300 Subject: [PATCH 2/4] Fix variable name --- Lib/tomllib/_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tomllib/_parser.py b/Lib/tomllib/_parser.py index f25ef68744d26a..c2efcb73a7a5c4 100644 --- a/Lib/tomllib/_parser.py +++ b/Lib/tomllib/_parser.py @@ -72,7 +72,7 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n # The spec allows converting "\r\n" to "\n", even in string # literals. Let's do so to simplify parsing. try: - src = __s.replace("\r\n", "\n") + src = s.replace("\r\n", "\n") except (AttributeError, TypeError): raise TypeError( f"Expected str object, not '{type(__s).__qualname__}'" From 23ad2ef4f20a012d012cfb7af85deecc74ee796a Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:54:39 +0300 Subject: [PATCH 3/4] Fix variable name pt. 2 --- Lib/tomllib/_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tomllib/_parser.py b/Lib/tomllib/_parser.py index c2efcb73a7a5c4..5671326646ca5a 100644 --- a/Lib/tomllib/_parser.py +++ b/Lib/tomllib/_parser.py @@ -75,7 +75,7 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n src = s.replace("\r\n", "\n") except (AttributeError, TypeError): raise TypeError( - f"Expected str object, not '{type(__s).__qualname__}'" + f"Expected str object, not '{type(s).__qualname__}'" ) from None pos = 0 out = Output(NestedDict(), Flags()) From 4c85b60907e229502591217a9350a625337732d7 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:44:50 +0300 Subject: [PATCH 4/4] Add NEWS --- .../Library/2024-10-01-12-43-42.gh-issue-124835.SVyp3K.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-01-12-43-42.gh-issue-124835.SVyp3K.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-01-12-43-42.gh-issue-124835.SVyp3K.rst b/Misc/NEWS.d/next/Library/2024-10-01-12-43-42.gh-issue-124835.SVyp3K.rst new file mode 100644 index 00000000000000..09e5a046f83e49 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-01-12-43-42.gh-issue-124835.SVyp3K.rst @@ -0,0 +1,3 @@ +Make :func:`tomllib.loads` raise :exc:`TypeError` not :exc:`AttributeError` +on bad input types that do not have the ``replace`` attribute. Improve error +message when :class:`bytes` is received.