Skip to content

Raise ProgrammingError for nan and inf #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions MySQLdb/_mysql_exceptions.py → MySQLdb/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"""_mysql_exceptions: Exception classes for _mysql and MySQLdb.
"""Exception classes for _mysql and MySQLdb.

These classes are dictated by the DB API v2.0:

https://www.python.org/dev/peps/pep-0249/
"""

try:
from exceptions import Exception, StandardError, Warning
except ImportError:
# Python 3
StandardError = Exception
from .compat import StandardError


class MySQLError(StandardError):
Expand All @@ -20,6 +15,7 @@ class Warning(Warning, MySQLError):
"""Exception raised for important warnings like data truncations
while inserting, etc."""


class Error(MySQLError):
"""Exception that is the base class of all other error exceptions
(not Warning)."""
Expand Down
2 changes: 1 addition & 1 deletion MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,7 @@ init_mysql(void)
(PyObject *)&_mysql_ResultObject_Type))
goto error;
Py_INCREF(&_mysql_ResultObject_Type);
if (!(emod = PyImport_ImportModule("MySQLdb._mysql_exceptions"))) {
if (!(emod = PyImport_ImportModule("MySQLdb._exceptions"))) {
PyErr_Print();
goto error;
}
Expand Down
2 changes: 2 additions & 0 deletions MySQLdb/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
unicode = unicode
unichr = unichr
long = long
StandardError = StandardError
else:
PY2 = False
unicode = str
unichr = chr
long = int
StandardError = Exception
2 changes: 1 addition & 1 deletion MySQLdb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from MySQLdb import cursors, _mysql
from MySQLdb.compat import unicode, PY2
from MySQLdb._mysql_exceptions import (
from MySQLdb._exceptions import (
Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError,
NotSupportedError, ProgrammingError,
Expand Down
3 changes: 3 additions & 0 deletions MySQLdb/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import *
from MySQLdb.compat import PY2, long, unicode
from MySQLdb._exceptions import ProgrammingError

NoneType = type(None)

Expand Down Expand Up @@ -66,6 +67,8 @@ def Unicode2Str(s, d):

def Float2Str(o, d):
s = repr(o)
if s in ('inf', 'nan'):
raise ProgrammingError("%s can not be used with MySQL" % s)
if 'e' not in s:
s += 'e0'
return s
Expand Down
4 changes: 2 additions & 2 deletions MySQLdb/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys

from .compat import unicode
from ._mysql_exceptions import (
from ._exceptions import (
Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError,
NotSupportedError, ProgrammingError)
Expand Down Expand Up @@ -48,7 +48,7 @@ class BaseCursor(object):
#: Default value of max_allowed_packet is 1048576.
max_stmt_length = 64*1024

from ._mysql_exceptions import (
from ._exceptions import (
MySQLError, Warning, Error, InterfaceError,
DatabaseError, DataError, OperationalError, IntegrityError,
InternalError, ProgrammingError, NotSupportedError,
Expand Down
4 changes: 2 additions & 2 deletions doc/_mysql_exceptions.rst → doc/_exceptions.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
_mysql_exceptions Module
_exceptions Module
========================

.. automodule:: _mysql_exceptions
.. automodule:: MySQLdb._exceptions
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion doc/_mysql.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
_mysql Module
=============

.. automodule:: _mysql
.. automodule:: MySQLdb._mysql
:members:
:undoc-members:
:show-inheritance:
8 changes: 4 additions & 4 deletions doc/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Okay, so you want to use ``_mysql`` anyway. Here are some examples.

The simplest possible database connection is::

import _mysql
from MySQLdb import _mysql
db=_mysql.connect()

This creates a connection to the MySQL server running on the local
Expand Down Expand Up @@ -162,8 +162,8 @@ substitution, so you have to pass a complete query string to
WHERE price < 5""")

There's no return value from this, but exceptions can be raised. The
exceptions are defined in a separate module, ``_mysql_exceptions``,
but ``_mysql`` exports them. Read DB API specification PEP-249_ to
exceptions are defined in a separate module, ``MySQLdb._exceptions``,
but ``MySQLdb._mysql`` exports them. Read DB API specification PEP-249_ to
find out what they are, or you can use the catch-all ``MySQLError``.

.. _PEP-249: https://www.python.org/dev/peps/pep-0249/
Expand Down Expand Up @@ -213,7 +213,7 @@ implicitly asked for one row, since we didn't specify ``maxrows``.
The other oddity is: Assuming these are numeric columns, why are they
returned as strings? Because MySQL returns all data as strings and
expects you to convert it yourself. This would be a real pain in the
ass, but in fact, ``_mysql`` can do this for you. (And ``MySQLdb``
ass, but in fact, ``MySQLdb._mysql`` can do this for you. (And ``MySQLdb``
does do this for you.) To have automatic type conversion done, you
need to create a type converter dictionary, and pass this to
``connect()`` as the ``conv`` keyword parameter.
Expand Down
2 changes: 1 addition & 1 deletion metadata.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ classifiers:
Topic :: Database
Topic :: Database :: Database Engines/Servers
py_modules:
MySQLdb._mysql_exceptions
MySQLdb._exceptions
MySQLdb.compat
MySQLdb.connections
MySQLdb.converters
Expand Down