Fix datatypes, context and token classes

- Fix leap year calculation into AbstractDateTime
  - Fix _iter_context for yielding AttributeNode tuples
  - Fix '|' select method for attribute matching
  - Fix get_atomized_operand() for XSD decode of untyped data
This commit is contained in:
Davide Brunato 2019-09-21 15:21:25 +02:00
parent 23fc3542ca
commit 18871936cb
5 changed files with 17 additions and 6 deletions

View File

@ -201,7 +201,7 @@ class AbstractDateTime(object):
raise ElementPathTypeError("wrong type %r for year" % type(year))
else:
self._year = year
if isleap(year):
if isleap(year + bool(self.version != '1.0')):
self._dt = datetime.datetime(4, month, day, hour, minute, second, microsecond, tzinfo)
else:
self._dt = datetime.datetime(6, month, day, hour, minute, second, microsecond, tzinfo)

View File

@ -646,6 +646,8 @@ def select(self, context=None):
for item in context.iter():
if item in results:
yield item
elif is_attribute_node(item) and item[1] in results:
yield item[1]
###

View File

@ -223,9 +223,8 @@ class XPathContext(object):
self.item = elem.text
yield self.item
for item in elem.attrib.items():
self.item = item
yield item
for self.item in map(lambda x: AttributeNode(*x), elem.attrib.items()):
yield self.item
if len(elem):
self.size = len(elem)

View File

@ -225,7 +225,15 @@ class XPathToken(Token):
try:
next(selector)
except StopIteration:
return str(value) if isinstance(value, UntypedAtomic) else value
if isinstance(value, UntypedAtomic):
value = str(value)
if self.xsd_type is not None and isinstance(value, string_base_type):
try:
value = self.xsd_type.decode(value)
except (TypeError, ValueError):
msg = "Type {!r} is not appropriate for the context"
self.wrong_context_type(msg.format(type(value)))
return value
else:
self.wrong_context_type("atomized operand is a sequence of length greater than one")

View File

@ -116,13 +116,15 @@ class DateTimeTypesTest(unittest.TestCase):
self.assertRaises(TypeError, AbstractDateTime)
self.assertRaises(TypeError, OrderedDateTime)
def test_init_fromstring(self):
def test_datetime_init_fromstring(self):
self.assertIsInstance(DateTime.fromstring('2000-10-07T00:00:00'), DateTime)
self.assertIsInstance(DateTime.fromstring('-2000-10-07T00:00:00'), DateTime)
self.assertRaises(ValueError, DateTime.fromstring, '00-10-07')
def test_date_init_fromstring(self):
self.assertIsInstance(Date.fromstring('2000-10-07'), Date)
self.assertIsInstance(Date.fromstring('-2000-10-07'), Date)
self.assertIsInstance(Date.fromstring('0000-02-29'), Date)
def test_datetime_repr(self):
dt = DateTime.fromstring('2000-10-07')