Skip to content

bpo-34454: Clean up datetime.fromisoformat surrogate handling #8959

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

Merged
merged 6 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Retain non-sanitized dtstr for error printing
This does not create an extra string, it just holds on to a reference to
the original input string for purposes of creating the error message.
  • Loading branch information
pganssle committed Oct 21, 2018
commit eb8e17632353044c9c45ed4b16552b65e056960b
9 changes: 9 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os
import pickle
import random
import re
import struct
import unittest

Expand Down Expand Up @@ -2676,6 +2677,14 @@ def test_fromisoformat_fails_datetime(self):
with self.assertRaises(ValueError):
self.theclass.fromisoformat(bad_str)

def test_fromisoformat_fails_surrogate(self):
# Test that when fromisoformat() fails with a surrogate character as
# the separator, the error message contains the original string
dtstr = "2018-01-03\ud80001:0113"

with self.assertRaisesRegex(ValueError, re.escape(repr(dtstr))):
self.theclass.fromisoformat(dtstr)

def test_fromisoformat_utc(self):
dt_str = '2014-04-19T13:21:13+00:00'
dt = self.theclass.fromisoformat(dt_str)
Expand Down
10 changes: 5 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4902,13 +4902,13 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
}

int needs_decref = 0;
dtstr = _sanitize_isoformat_str(dtstr, &needs_decref);
if (dtstr == NULL) {
PyObject *dtstr_clean = _sanitize_isoformat_str(dtstr, &needs_decref);
if (dtstr_clean == NULL) {
goto error;
}

Py_ssize_t len;
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len);
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len);

if (dt_ptr == NULL) {
goto invalid_string_error;
Expand Down Expand Up @@ -4960,7 +4960,7 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {

Py_DECREF(tzinfo);
if (needs_decref) {
Py_DECREF(dtstr);
Py_DECREF(dtstr_clean);
}
return dt;

Expand All @@ -4969,7 +4969,7 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {

error:
if (needs_decref) {
Py_DECREF(dtstr);
Py_DECREF(dtstr_clean);
}

return NULL;
Expand Down