Skip to content

gh-135580: Pickle FLOAT opcode supports whitespace #135619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2964,6 +2964,10 @@ def test_float(self):
got = self.loads(pickle)
self.assert_is_copy(value, got)

def test_float_whitespace(self):
self.assertEqual(self.loads(b'F 1.2 \n.'), 1.2)
self.assertEqual(self.loads(b'F \t9 \n.'), 9)

@run_with_locales('LC_ALL', 'de_DE', 'fr_FR', '')
def test_float_format(self):
# make sure that floats are formatted locale independent with proto 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :mod:`pickle` ``FLOAT`` opcode now tolerates trailing and leading whitespace in both C and Python implementations.
17 changes: 5 additions & 12 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5445,25 +5445,18 @@ load_counted_long(PickleState *st, UnpicklerObject *self, int size)
static int
load_float(PickleState *state, UnpicklerObject *self)
{
PyObject *value;
char *endptr, *s;
PyObject *value, *s_obj;
char *s;
Py_ssize_t len;
double d;

if ((len = _Unpickler_Readline(state, self, &s)) < 0)
return -1;
if (len < 2)
return bad_readline(state);

errno = 0;
d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
if (d == -1.0 && PyErr_Occurred())
return -1;
if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
PyErr_SetString(PyExc_ValueError, "could not convert string to float");
return -1;
}
value = PyFloat_FromDouble(d);
s_obj = PyUnicode_FromString(s);
value = PyFloat_FromString(s_obj);

if (value == NULL)
return -1;

Expand Down
Loading