diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-23 19:26:28 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-23 19:58:43 -0400 |
| commit | 7c74d702a9632a8c7264d6972e46985de3fb2487 (patch) | |
| tree | 9db49b27827f580b856671186aae2d40952d74f2 /lib/sqlalchemy | |
| parent | bf03d4332ae35e2087b175f8a2e0291d2f4c9aa0 (diff) | |
| download | sqlalchemy-7c74d702a9632a8c7264d6972e46985de3fb2487.tar.gz | |
Make boolean processors consistent between Py/C; coerce to 1/0
The processing performed by the :class:`.Boolean` datatype for backends
that only feature integer types has been made consistent between the
pure Python and C-extension versions, in that the C-extension version
will accept any integer value from the database as a boolean, not just
zero and one; additionally, non-boolean integer values being sent to
the database are coerced to exactly zero or one, instead of being
passed as the original integer value.
Change-Id: I01e647547fd7047bd549dd70e1fa202c51e8328b
Fixes: #3730
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/cextension/processors.c | 14 | ||||
| -rw-r--r-- | lib/sqlalchemy/processors.py | 4 |
2 files changed, 4 insertions, 14 deletions
diff --git a/lib/sqlalchemy/cextension/processors.c b/lib/sqlalchemy/cextension/processors.c index 5357e34dc..5b7527c20 100644 --- a/lib/sqlalchemy/cextension/processors.c +++ b/lib/sqlalchemy/cextension/processors.c @@ -22,28 +22,18 @@ typedef int Py_ssize_t; static PyObject * int_to_boolean(PyObject *self, PyObject *arg) { - long l = 0; + int l = 0; PyObject *res; if (arg == Py_None) Py_RETURN_NONE; - -#if PY_MAJOR_VERSION >= 3 - l = PyLong_AsLong(arg); -#else - l = PyInt_AsLong(arg); -#endif + l = PyObject_IsTrue(arg); if (l == 0) { res = Py_False; } else if (l == 1) { res = Py_True; - } else if ((l == -1) && PyErr_Occurred()) { - /* -1 can be either the actual value, or an error flag. */ - return NULL; } else { - PyErr_SetString(PyExc_ValueError, - "int_to_boolean only accepts None, 0 or 1"); return NULL; } diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index b57e6740b..98f8a2759 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -53,7 +53,7 @@ def boolean_to_int(value): if value is None: return None else: - return int(value) + return int(bool(value)) def py_fallback(): @@ -111,7 +111,7 @@ def py_fallback(): if value is None: return None else: - return value and True or False + return bool(value) DATETIME_RE = re.compile( "(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)(?:\.(\d+))?") |
