]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-112243: Don't include comments in f-string debug expressions (GH-112284...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Mon, 20 Nov 2023 15:38:57 +0000 (15:38 +0000)
committerGitHub <noreply@github.com>
Mon, 20 Nov 2023 15:38:57 +0000 (15:38 +0000)
(cherry picked from commit d59feb5dbe5395615d06c30a95e6a6a9b7681d4d)

Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2023-11-20-14-13-02.gh-issue-112243.FKdQnr.rst [new file with mode: 0644]
Parser/tokenizer.c

index 4f05a149a901b2132a0e8fe35542f354c7d2c426..fb364e9c684bd11cb033ba4c153223ce12eb7e8a 100644 (file)
@@ -1579,6 +1579,9 @@ x = (
         self.assertEqual(f'X{x  =  }Y', 'Xx  =  '+repr(x)+'Y')
         self.assertEqual(f"sadsd {1 + 1 =  :{1 + 1:1d}f}", "sadsd 1 + 1 =  2.000000")
 
+        self.assertEqual(f"{1+2 = # my comment
+  }", '1+2 = \n  3')
+
         # These next lines contains tabs.  Backslash escapes don't
         # work in f-strings.
         # patchcheck doesn't like these tabs.  So the only way to test
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-20-14-13-02.gh-issue-112243.FKdQnr.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-20-14-13-02.gh-issue-112243.FKdQnr.rst
new file mode 100644 (file)
index 0000000..d69f29f
--- /dev/null
@@ -0,0 +1 @@
+Don't include comments in f-string debug expressions. Patch by Pablo Galindo
index a59b728e60c1fa53677d1145070f4afaac102da3..d2f967d35ac4b3716418b145c72829615eec65c5 100644 (file)
@@ -406,11 +406,55 @@ set_fstring_expr(struct tok_state* tok, struct token *token, char c) {
         return 0;
     }
 
-    PyObject *res = PyUnicode_DecodeUTF8(
-        tok_mode->last_expr_buffer,
-        tok_mode->last_expr_size - tok_mode->last_expr_end,
-        NULL
-    );
+    PyObject *res = NULL;
+
+    // Check if there is a # character in the expression
+    int hash_detected = 0;
+    for (Py_ssize_t i = 0; i < tok_mode->last_expr_size - tok_mode->last_expr_end; i++) {
+        if (tok_mode->last_expr_buffer[i] == '#') {
+            hash_detected = 1;
+            break;
+        }
+    }
+
+    if (hash_detected) {
+        Py_ssize_t input_length = tok_mode->last_expr_size - tok_mode->last_expr_end;
+        char *result = (char *)PyObject_Malloc((input_length + 1) * sizeof(char));
+        if (!result) {
+            return -1;
+        }
+
+        Py_ssize_t i = 0;
+        Py_ssize_t j = 0;
+
+        for (i = 0, j = 0; i < input_length; i++) {
+            if (tok_mode->last_expr_buffer[i] == '#') {
+                // Skip characters until newline or end of string
+                while (tok_mode->last_expr_buffer[i] != '\0' && i < input_length) {
+                    if (tok_mode->last_expr_buffer[i] == '\n') {
+                        result[j++] = tok_mode->last_expr_buffer[i];
+                        break;
+                    }
+                    i++;
+                }
+            } else {
+                result[j++] = tok_mode->last_expr_buffer[i];
+            }
+        }
+
+        result[j] = '\0';  // Null-terminate the result string
+        res = PyUnicode_DecodeUTF8(result, j, NULL);
+        PyObject_Free(result);
+    } else {
+        res = PyUnicode_DecodeUTF8(
+            tok_mode->last_expr_buffer,
+            tok_mode->last_expr_size - tok_mode->last_expr_end,
+            NULL
+        );
+
+    }
+
+
     if (!res) {
         return -1;
     }