Skip to content

Commit

Permalink
CONPY-258: Fixed ValueError exception if ZEROFILL flag is defined
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
9EOR9 committed Apr 6, 2023
1 parent 3827ae3 commit 6afeaa5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mariadb/mariadb_codecs.c
Expand Up @@ -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:
{
Expand Down
16 changes: 16 additions & 0 deletions testing/test/integration/test_cursor.py
Expand Up @@ -1490,6 +1490,22 @@ def test_conpy225(self):
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:
Expand Down

0 comments on commit 6afeaa5

Please sign in to comment.