Skip to content
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

tz.tzstr() fails on parsing timezone data file #51

Closed
pganssle opened this issue Mar 3, 2015 · 2 comments
Closed

tz.tzstr() fails on parsing timezone data file #51

pganssle opened this issue Mar 3, 2015 · 2 comments
Assignees
Milestone

Comments

@pganssle
Copy link
Member

pganssle commented Mar 3, 2015

Migrated from launchpad issue 1331576.

Quoting marcog:

With dateutil 2.2, I can't get the tzstr examples working (see below). It throws an error 'str' object has no attribute 'read'. Looking at the code, it fails on self.instream.read(1) while parsing the timezone data file. It works fine on dateutil 2.1.

In [3]: import dateutil.tz
In [4]: dateutil.tz.tzstr('EST5EDT')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-75889fcde3a9> in <module>()
----> 1 dateutil.tz.tzstr("PST")

/Library/Python/2.7/site-packages/dateutil/tz.pyc in __init__(self, s)
    579 self._s = s
    580
--> 581 res = parser._parsetz(s)
    582 if res is None:
    583 raise ValueError("unknown string format")

/Library/Python/2.7/site-packages/dateutil/parser.pyc in _parsetz(tzstr)
    923 DEFAULTTZPARSER = _tzparser()
    924 def _parsetz(tzstr):
--> 925 return DEFAULTTZPARSER.parse(tzstr)
    926
    927

/Library/Python/2.7/site-packages/dateutil/parser.pyc in parse(self, tzstr)
    770 def parse(self, tzstr):
    771 res = self._result()
--> 772 l = _timelex.split(tzstr)
    773 try:
    774

/Library/Python/2.7/site-packages/dateutil/parser.pyc in split(cls, s)
    148
    149 def split(cls, s):
--> 150 return list(cls(s))
    151 split = classmethod(split)
    152
@pganssle
Copy link
Member Author

pganssle commented Mar 3, 2015

Response from Vincent Veselosky (veselosky) on 2014-07-21:

This also fails for me with python-dateutil 2.1. In both 2.1 and 2.2, the function works as documented under Python 3.3.3. When I run it under Python 2.7.5 it throws the exception shown above.

@pganssle
Copy link
Member Author

pganssle commented Mar 3, 2015

Response from Adrian Likins (alikins) on 2015-03-03:

Looks like this only fails on python2 if a non-unicode string is used as the tzstr arg.

dateutil.tz.tzstr('EST5EDT')

FAILS

dateutil.tz.tzstr(u'EST5EDT')

PASSES

dateutil.parser._timelex.init:35 seems to do it.

def __init__(self, instream):
    if isinstance(instream, text_type):
        instream = StringIO(instream)
    self.instream = instream

The isinstance() check fails for a non unicode string.

>>> import six
>>> a = 'EST'
>>> isinstance(a, six.text_type)
False
>>> a = u'EST'
>>> isinstance(a, six.text_type)
True

texttype is from 'six' and gets defined as 'unicode' on python2.

So likely only fails on python2 with non unicode strings passed to tzstr().

test.py has tests for similar usage, but test.py has:

from __future__ import unicode_literals

Turning that off makes the tzstr tests fail. Our code doesn't use unicode_literals, so
str literals passed in cause the errors mentioned.

Something like this (untested) snippet should work:

if isinstance(instream, basestring):
    instream = StringIO(instream)

@pganssle pganssle added this to the 2.5.0 milestone Mar 5, 2015
@pganssle pganssle self-assigned this Mar 5, 2015
pganssle added a commit that referenced this issue Mar 6, 2015
Fix issue with tzstr parsing on Python 2.x. lp:1331576, gh #51
@pganssle pganssle closed this as completed Mar 7, 2015
aschatten pushed a commit to aschatten/dateutil that referenced this issue Nov 16, 2015
aschatten pushed a commit to aschatten/dateutil that referenced this issue Nov 16, 2015
Fix issue with tzstr parsing on Python 2.x. lp:1331576, gh dateutil#51
aschatten pushed a commit to aschatten/dateutil that referenced this issue Nov 16, 2015
@pganssle pganssle mentioned this issue Feb 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant