CONPY-258: Fixed ValueError exception if ZEROFILL flag is defined

For backwards compatibility PyLong_FromString interprets leading
zeros as octal value which will end up in a value error, if the
number contains 2 or more leading zeros.
This commit is contained in:
Georg Richter
2023-04-06 17:22:26 +02:00
parent 3827ae32bd
commit 6afeaa53d1
2 changed files with 27 additions and 1 deletions

View File

@ -450,8 +450,18 @@ field_fetch_fromtext(MrdbCursor *self, char *data, unsigned int column)
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_LONGLONG:
self->values[column]= PyLong_FromString(data, NULL, 0);
{
char *p= data;
/* CONPY-258: remove leading zero's */
if (strlen(p) > 1)
{
while (*p && *p == '0')
p++;
}
self->values[column]= PyLong_FromString(p, NULL, 0);
break;
}
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
{

View File

@ -1490,6 +1490,22 @@ class TestCursor(unittest.TestCase):
self.assertEqual(cursor.affected_rows, 1)
self.assertEqual(cursor.rowcount, 1)
def test_conpy258(self):
connection = create_connection()
cursor = connection.cursor()
cursor.execute("CREATE TEMPORARY TABLE t1 (a INT(9) ZEROFILL)")
cursor.execute("INSERT INTO t1 VALUES(123)")
cursor.execute("SELECT a FROM t1")
row = cursor.fetchone()
self.assertEqual(row[0], 123)
cursor.close()
cursor = connection.cursor(binary=True)
cursor.execute("SELECT a FROM t1")
row = cursor.fetchone()
self.assertEqual(row[0], 123)
cursor.close()
connection.close()
def test_conpy91(self):
with create_connection() as connection:
with connection.cursor() as cursor: