Raise an error for dates > 10K instead of returning Y9999

Close #1307.
This commit is contained in:
Daniele Varrazzo 2021-11-11 20:01:14 +01:00
parent 1454b14ae0
commit 1a0c02a6f4
3 changed files with 21 additions and 4 deletions

8
NEWS
View File

@ -1,10 +1,16 @@
Current release
---------------
What's new in psycopg 2.9.2
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Raise `ValueError` for dates >= Y10k (:ticket:`#1307`)
What's new in psycopg 2.9.1
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Fix regression with named `sql.Placeholder` (:ticket:`1291`).
Fix regression with named `sql.Placeholder` (:ticket:`#1291`).
What's new in psycopg 2.9

View File

@ -71,7 +71,6 @@ typecast_PYDATE_cast(const char *str, Py_ssize_t len, PyObject *curs)
return NULL;
}
else {
if (y > 9999) y = 9999;
obj = PyObject_CallFunction(
(PyObject*)PyDateTimeAPI->DateType, "iii", y, m, d);
}
@ -172,8 +171,6 @@ _parse_noninftz(const char *str, Py_ssize_t len, PyObject *curs)
mm += 1;
ss -= 60;
}
if (y > 9999)
y = 9999;
tzinfo_factory = ((cursorObject *)curs)->tzinfo_factory;
if (n >= 5 && tzinfo_factory != Py_None) {

View File

@ -115,12 +115,26 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.DATETIME = psycopg2.extensions.PYDATETIME
self.INTERVAL = psycopg2.extensions.PYINTERVAL
def test_parse_10k_date(self):
# datetime does not support dates larger than date.max
self.assertEqual(self.DATE('9999-12-31', self.curs), date(9999, 12, 31))
self.assertRaises(ValueError, self.DATE, '10000-01-01', self.curs)
def test_parse_bc_date(self):
# datetime does not support BC dates
self.assertEqual(self.DATE('0001-01-01', self.curs), date(1, 1, 1))
self.assertRaises(ValueError, self.DATE, '00042-01-01 BC', self.curs)
def test_parse_10k_datetime(self):
# datetime does not support dates larger than date.max
self.assertEqual(self.DATETIME('9999-12-31 23:59:59', self.curs),
datetime(9999, 12, 31, 23, 59, 59))
self.assertRaises(ValueError, self.DATE, '10000-01-01', self.curs)
def test_parse_bc_datetime(self):
# datetime does not support BC dates
self.assertEqual(self.DATETIME('0001-01-01 13:30:29', self.curs),
datetime(1, 1, 1, 13, 30, 29))
self.assertRaises(ValueError, self.DATETIME,
'00042-01-01 13:30:29 BC', self.curs)