Raise ProgrammingError for nan and inf (#314)

* Raise ProgrammingError when inf or nan is passed
  Fixes #246
* Rename _mysql_exceptions -> _exceptions
This commit is contained in:
INADA Naoki
2018-12-14 16:46:30 +09:00
committed by GitHub
parent 911bef988c
commit 8ad1525c3f
10 changed files with 20 additions and 19 deletions

View File

@ -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: These classes are dictated by the DB API v2.0:
https://www.python.org/dev/peps/pep-0249/ https://www.python.org/dev/peps/pep-0249/
""" """
from .compat import StandardError
try:
from exceptions import Exception, StandardError, Warning
except ImportError:
# Python 3
StandardError = Exception
class MySQLError(StandardError): class MySQLError(StandardError):
@ -20,6 +15,7 @@ class Warning(Warning, MySQLError):
"""Exception raised for important warnings like data truncations """Exception raised for important warnings like data truncations
while inserting, etc.""" while inserting, etc."""
class Error(MySQLError): class Error(MySQLError):
"""Exception that is the base class of all other error exceptions """Exception that is the base class of all other error exceptions
(not Warning).""" (not Warning)."""

View File

@ -2669,7 +2669,7 @@ init_mysql(void)
(PyObject *)&_mysql_ResultObject_Type)) (PyObject *)&_mysql_ResultObject_Type))
goto error; goto error;
Py_INCREF(&_mysql_ResultObject_Type); Py_INCREF(&_mysql_ResultObject_Type);
if (!(emod = PyImport_ImportModule("MySQLdb._mysql_exceptions"))) { if (!(emod = PyImport_ImportModule("MySQLdb._exceptions"))) {
PyErr_Print(); PyErr_Print();
goto error; goto error;
} }

View File

@ -5,8 +5,10 @@ if sys.version_info[0] == 2:
unicode = unicode unicode = unicode
unichr = unichr unichr = unichr
long = long long = long
StandardError = StandardError
else: else:
PY2 = False PY2 = False
unicode = str unicode = str
unichr = chr unichr = chr
long = int long = int
StandardError = Exception

View File

@ -9,7 +9,7 @@ import sys
from MySQLdb import cursors, _mysql from MySQLdb import cursors, _mysql
from MySQLdb.compat import unicode, PY2 from MySQLdb.compat import unicode, PY2
from MySQLdb._mysql_exceptions import ( from MySQLdb._exceptions import (
Warning, Error, InterfaceError, DataError, Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError, DatabaseError, OperationalError, IntegrityError, InternalError,
NotSupportedError, ProgrammingError, NotSupportedError, ProgrammingError,

View File

@ -36,6 +36,7 @@ from MySQLdb._mysql import string_literal, escape
from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import * from MySQLdb.times import *
from MySQLdb.compat import PY2, long, unicode from MySQLdb.compat import PY2, long, unicode
from MySQLdb._exceptions import ProgrammingError
NoneType = type(None) NoneType = type(None)
@ -66,6 +67,8 @@ def Unicode2Str(s, d):
def Float2Str(o, d): def Float2Str(o, d):
s = repr(o) s = repr(o)
if s in ('inf', 'nan'):
raise ProgrammingError("%s can not be used with MySQL" % s)
if 'e' not in s: if 'e' not in s:
s += 'e0' s += 'e0'
return s return s

View File

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

View File

@ -1,7 +1,7 @@
_mysql_exceptions Module _exceptions Module
======================== ========================
.. automodule:: _mysql_exceptions .. automodule:: MySQLdb._exceptions
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

View File

@ -1,7 +1,7 @@
_mysql Module _mysql Module
============= =============
.. automodule:: _mysql .. automodule:: MySQLdb._mysql
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

View File

@ -106,7 +106,7 @@ Okay, so you want to use ``_mysql`` anyway. Here are some examples.
The simplest possible database connection is:: The simplest possible database connection is::
import _mysql from MySQLdb import _mysql
db=_mysql.connect() db=_mysql.connect()
This creates a connection to the MySQL server running on the local This creates a connection to the MySQL server running on the local
@ -162,8 +162,8 @@ substitution, so you have to pass a complete query string to
WHERE price < 5""") WHERE price < 5""")
There's no return value from this, but exceptions can be raised. The There's no return value from this, but exceptions can be raised. The
exceptions are defined in a separate module, ``_mysql_exceptions``, exceptions are defined in a separate module, ``MySQLdb._exceptions``,
but ``_mysql`` exports them. Read DB API specification PEP-249_ to 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``. find out what they are, or you can use the catch-all ``MySQLError``.
.. _PEP-249: https://www.python.org/dev/peps/pep-0249/ .. _PEP-249: https://www.python.org/dev/peps/pep-0249/
@ -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 The other oddity is: Assuming these are numeric columns, why are they
returned as strings? Because MySQL returns all data as strings and 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 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 does do this for you.) To have automatic type conversion done, you
need to create a type converter dictionary, and pass this to need to create a type converter dictionary, and pass this to
``connect()`` as the ``conv`` keyword parameter. ``connect()`` as the ``conv`` keyword parameter.

View File

@ -28,7 +28,7 @@ classifiers:
Topic :: Database Topic :: Database
Topic :: Database :: Database Engines/Servers Topic :: Database :: Database Engines/Servers
py_modules: py_modules:
MySQLdb._mysql_exceptions MySQLdb._exceptions
MySQLdb.compat MySQLdb.compat
MySQLdb.connections MySQLdb.connections
MySQLdb.converters MySQLdb.converters