Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#155 Failing float() conversion due empty string

MySQLdb-1.2
closed
Andy Dustman
MySQLdb (285)
5
2012-09-19
2005-10-05
Anonymous
No

I've got an error I'm not quite sure if its a bug or
not. This is how it looks like:

File "~/myfile.py", line 30, in do_query
cur.execute("""SELECT NOW()""")
File
"/usr/lib/python2.3/site-packages/MySQLdb/cursors.py",
line 137, in execute
self.errorhandler(self, exc, value)
File
"/usr/lib/python2.3/site-packages/MySQLdb/connections.py",
line 33, in defaulterrorhandler
raise errorclass, errorvalue
ValueError: empty string for float()

I tried to track down the source of the Exception. By
removing the catch-all Exception handler in
BaseCursor.excute() I found out that it happens at
BaseCursor._fetch_row() in return-Statement:
return self._result.fetch_row(size, self._fetch_type)
So it must be somewhere in _mysql
But it isn't. After studing the C-Source I found out
that the source is in
conversions.py
After replacing the conversions for float() with a
wrapper function that catch the ValueError from float()
I finally found out why the exception is thrown:
float() get a empty string. For for the moment the
patch attached solved the Problem.

I'm using the official Debian package for sarge
(stable). It's the version 1.2.1g2. Python is version
2.3.5.

I'm trying to find out why an empty string is passed.

Discussion

  • Logged In: NO

    The file was not attached, so here it is:

    --- /usr/lib/python2.3/site-packages/MySQLdb/converters.py
    2005-05-01 18:40:37.000000000 +0200
    +++ MySQLdb/converters.py 2005-10-05
    18:11:22.000000000 +0200
    @@ -107,6 +107,14 @@
    def array2Str(o, d):
    return Thing2Literal(o.tostring(), d)

    +def my_float(f):
    + try:
    + return float(f)
    + except ValueError, e:
    + if isinstance(f, str):
    + return 0.0
    + raise e
    +
    conversions = {
    types.IntType: Thing2Str,
    types.LongType: Long2Int,
    @@ -125,9 +133,9 @@
    FIELD_TYPE.TINY: int,
    FIELD_TYPE.SHORT: int,
    FIELD_TYPE.LONG: long,
    - FIELD_TYPE.FLOAT: float,
    - FIELD_TYPE.DOUBLE: float,
    - FIELD_TYPE.DECIMAL: float,
    + FIELD_TYPE.FLOAT: my_float,
    + FIELD_TYPE.DOUBLE: my_float,
    + FIELD_TYPE.DECIMAL: my_float,
    FIELD_TYPE.LONGLONG: long,
    FIELD_TYPE.INT24: int,
    FIELD_TYPE.YEAR: int,

     
  • Andy Dustman
    Andy Dustman
    2005-10-05

    Logged In: YES
    user_id=71372

    Can't reproduce.

    Python 2.4.2 (#1, Oct 3 2005, 09:43:02)
    [GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on
    linux2
    Type "help", "copyright", "credits" or "license" for more
    information.

    import MySQLdb
    db=MySQLdb.connect(read_default_file="~/.my.cnf")
    c=db.cursor()
    c.execute("select now()")
    1L
    c.fetchone()
    (datetime.datetime(2005, 10, 5, 12, 52, 32),)
    MySQLdb.get_client_info()
    '5.0.13-rc'
    db.get_server_info()
    '5.0.13-rc-log'
    MySQLdb.version
    '1.2.0'

     
  • Logged In: NO

    Solved!

    The Problem only occured at special situations. First I
    thought it's a buffer overflow or something, because it
    occured only if I loaded the module in embedded Python
    interpreter in a C program. But after I moved the faulty
    part up all other things and I still get an error I finaly
    found out what it was:
    From former experiments the binary was also linked against
    mysqlclient_r. So if the dynamic loader in python loaded
    this library again mostly all worked well. But as I sad
    mostly ;)

    Sorry for waste your time.

    Cheers, Andy