Incorrect date decoding in Python 3

Issue #52 resolved
Ivar Refsdal created an issue

Hi.

The following test

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import tempfile
import unittest

from savReaderWriter import SavReader, SavWriter


class DecodeSPSSDateTest(unittest.TestCase):
    def test(self):
        with tempfile.NamedTemporaryFile(suffix='.sav') as tmpfile:
            with SavWriter(tmpfile.name, ['date'], {b'date': 0}, formats={b'date': b'EDATE40'}, ioUtf8=True, ioLocale="C.UTF-8") as writer:
                writer.writerow([writer.spssDateTime(b"2000-01-01", "%Y-%m-%d")])

            with SavReader(tmpfile.name, returnHeader=False, ioUtf8=True, ioLocale="C.UTF-8") as reader:
                print(sys.version_info)
                date = list(reader)[0][0]
                self.assertEqual('2000-01-01', date)

is successful in Python 2, but unsuccessful in Python 3:

F
======================================================================
FAIL: test (tests.test_bug.DecodeSPSSDateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ire/code/datasetconverter/tests/test_bug.py", line 20, in test
    self.assertEqual('2000-01-01', date)
AssertionError: '2000-01-01' != 13166064000.0
-------------------- >> begin captured stdout << ---------------------
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 1 test in 0.012s

FAILED (failures=1)

Or did I miss something in the settings?

Comments (2)

  1. Albert-Jan Roskam repo owner

    Ok, this was caused by the fact that Python 2 does implicit conversion from unicodestrings to bytestrings. Python 3 makes more sense, though.

    Python 2.7.9 (default, Mar  1 2015, 12:57:24) [GCC 4.9.2] on linux2
    >>> u'EDATE' in set([b'EDATE'])
    True
    >>> set([b'EDATE', u'EDATE'])
    set(['EDATE'])
    
    Python 3.5.0 (default, Apr 13 2016, 20:39:27) [GCC 4.9.2] on linux
    >>> u'EDATE' in set([b'EDATE'])   # which is why the conversion to ISO-date string was not done.
    False
    >>> set([b'EDATE', u'EDATE'])
    {b'EDATE', 'EDATE'}
    

    Ok, I will push a commit that passes all tests, including yours, in Py2.7, Py33 and higher, and Pypy. Thanks again Ivar!

    Albert-Jan

  2. Log in to comment