Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Convert Python objects to booleans based on their Python truth value #435

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 1 commit into from
May 15, 2020
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
4 changes: 4 additions & 0 deletions 4 doc/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Version 8.0 (TBD)
`422 <https://github.com/oracle/python-cx_Oracle/pull/422>`__ and
`423 <https://github.com/oracle/python-cx_Oracle/pull/423>`__).
#) Documentation and test suite improvements.
#) Python objects bound to boolean variables are now converted to true or false
based on whether they would be considered true or false in a Python if
statement. (Previously, only True was treated as true and all other Python
values including 1, 1.0, and "foo" were treated as false.)
anthony-tuininga marked this conversation as resolved.
Show resolved Hide resolved


Version 7.3 (December 2019)
Expand Down
2 changes: 1 addition & 1 deletion 2 src/cxoTransform.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ int cxoTransform_fromPython(cxoTransformNum transformNum,

switch (transformNum) {
case CXO_TRANSFORM_BOOLEAN:
dbValue->asBoolean = (pyValue == Py_True);
dbValue->asBoolean = PyObject_IsTrue(pyValue);
return 0;
case CXO_TRANSFORM_BINARY:
case CXO_TRANSFORM_FIXED_CHAR:
Expand Down
39 changes: 39 additions & 0 deletions 39 test/BooleanVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ def testBindFalse(self):
(False,))
self.assertEqual(result, "FALSE")

def testBindFloatAsBoolean(self):
"test binding in a float as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, 0.0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, 1.0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")

def testBindIntegerAsBoolean(self):
"test binding in an integer as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, 0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, 1)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")

def testBindNull(self):
"test binding in a null value"
self.cursor.setinputsizes(None, bool)
Expand All @@ -40,6 +66,19 @@ def testBindOutTrue(self):
(5,))
self.assertEqual(result, True)

def testBindStringAsBoolean(self):
"test binding in a string as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, "")
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, "0")
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")

def testBindTrue(self):
"test binding in a True value"
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.