Skip to content

Commit

Permalink
Merge pull request #995 from arushi019/master
Browse files Browse the repository at this point in the history
fix #913 : Added _parseBoolean function to enforce correct Lexical-to-value mapping
  • Loading branch information
nicholascar authored Apr 6, 2020
2 parents 4b114b3 + 2d4dfce commit 604d63e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
12 changes: 11 additions & 1 deletion rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,16 @@ def _unhexlify(value):
value = value.encode()
return unhexlify(value)

def _parseBoolean(value):
true_accepted_values = ['1', 'true']
false_accepted_values = ['0', 'false']
new_value = value.lower()
if new_value in true_accepted_values:
return True
if new_value not in false_accepted_values:
warnings.warn('Parsing weird boolean, % r does not map to True or False' % value, category = DeprecationWarning)
return False

# Cannot import Namespace/XSD because of circular dependencies
_XSD_PFX = 'http://www.w3.org/2001/XMLSchema#'
_RDF_PFX = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
Expand Down Expand Up @@ -1552,7 +1562,7 @@ def _castPythonToLiteral(obj, datatype):
URIRef(_XSD_PFX + 'normalizedString'): None,
URIRef(_XSD_PFX + 'token'): None,
URIRef(_XSD_PFX + 'language'): None,
URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() == 'true',
URIRef(_XSD_PFX + 'boolean'): _parseBoolean,
URIRef(_XSD_PFX + 'decimal'): Decimal,
URIRef(_XSD_PFX + 'integer'): long_type,
URIRef(_XSD_PFX + 'nonPositiveInteger'): int,
Expand Down
25 changes: 24 additions & 1 deletion test/test_literal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

import rdflib # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from six import integer_types, PY3, string_types


Expand Down Expand Up @@ -100,6 +100,29 @@ def testNoDanglingPoint(self):
out = vv._literal_n3(use_plain=True)
self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
"""confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""
def testTrueBoolean(self):
test_value = Literal("tRue", datatype = _XSD_BOOLEAN)
self.assertTrue(test_value.value)
test_value = Literal("1",datatype = _XSD_BOOLEAN)
self.assertTrue(test_value.value)

def testFalseBoolean(self):
test_value = Literal("falsE", datatype = _XSD_BOOLEAN)
self.assertFalse(test_value.value)
test_value = Literal("0",datatype = _XSD_BOOLEAN)
self.assertFalse(test_value.value)

def testNonFalseBoolean(self):
test_value = Literal("abcd", datatype = _XSD_BOOLEAN)
self.assertRaises(DeprecationWarning)
self.assertFalse(test_value.value)
test_value = Literal("10",datatype = _XSD_BOOLEAN)
self.assertRaises(DeprecationWarning)
self.assertFalse(test_value.value)



class TestBindings(unittest.TestCase):

Expand Down

0 comments on commit 604d63e

Please sign in to comment.