Bug #80679 Python Connector escape() errors out on dates
Submitted: 9 Mar 2016 22:13 Modified: 16 Nov 2022 19:36
Reporter: Sam King Email Updates:
Status: Closed Impact on me:
Category:Connector / Python Severity:S3 (Non-critical)
Version:2.1.3 OS:Ubuntu
Assigned to: CPU Architecture:Any
Tags: escape

[9 Mar 2016 22:13] Sam King
The MySQLConverter.escape() method does not work for dates.  Creating a db connection and then calling conn.converter.escape(datetime.datetime.now().date()) throws the error "TypeError: an integer is required".  This happens because dates have a replace method that is different from the replace method on string objects.

The docstring on that method indicates that it should return the value if the value is not a string.  If the code were modified to do that (eg, changing the "else" to an "elif isinstance(value, basestring)" or something like that), it would work.

How to repeat:
Create a DB connection.

Call conn.converter.escape(datetime.datetime.now().date().

Suggested fix:
Modify MySQLConverter.escape() so that it will only do the value = value.replace() logic when value is a string.  

Alternately, add date types to NUMERIC_TYPES (though that might have unintended consequences elsewhere) or add a special case in escape() for date types.
[10 Mar 2016 7:47] Chiranjeevi Battula
Hello  Sam King,

Thank you for the bug report.
Verified this behavior on MySQL Connector/Python 2.1.3.

[10 Mar 2016 7:48] Chiranjeevi Battula
Traceback (most recent call last):
  File "D:\Python\80679.py", line 9, in <module>
  File "C:\Python27\lib\site-packages\mysql\connector\conversion.py", line 145, in escape
    value = value.replace('\\', '\\\\')
TypeError: an integer is required
[16 Nov 2022 19:36] Philip Olson
Posted by developer:
Fixed as of the upcoming MySQL Connector/Python 8.0.32 release, and here's the proposed changelog entry from the documentation team:

Using MySQLConverter.escape() on datetime objects raised this error:
TypeError: an integer is required. Now it does not attempt to escape
values that are not bytes or string types.

Thank you for the bug report.