ofxclient icon indicating copy to clipboard operation
ofxclient copied to clipboard

Unicode vs. bytes in Python 3.x

Open hosford42 opened this issue 8 years ago • 4 comments

I'm getting a TypeError when I try to list the accounts for my institution:

*** Python 3.4.5 |Anaconda 2.1.0 (64-bit)| (default, Jul  5 2016, 14:53:07) [MSC v.1600 64 bit (AMD64)] on win32. ***

>>> institution.accounts()
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
  File "C:\Anaconda3\lib\site-packages\ofxclient\institution.py", line 143, in accounts
    parsed = OfxParser.parse(resp_handle)
  File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 398, in parse
    ofx_file = OfxPreprocessedFile(file_handle)
  File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 168, in __init__
    super(OfxPreprocessedFile, self).__init__(fh)
  File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 98, in __init__
    self.read_headers()
  File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 104, in read_headers
    head_data = head_data[:head_data.find(six.b('<'))]
TypeError: Can't convert 'bytes' object to str implicitly

hosford42 avatar Aug 18 '16 02:08 hosford42

I've ran into the same issue. Now trying in Python 2.7.

sneilan avatar Sep 10 '16 19:09 sneilan

Ran into this as well. Removing the call to six.b() fixed it, but kept tripping over further conversion/encoding errors.

cu avatar Oct 08 '16 21:10 cu

The issue is that ofxparse expects a "seekable file-like byte stream object". See line 87, https://github.com/jseutter/ofxparse/blob/master/ofxparse/ofxparse.py Ofxclient is providing a StringIO object to ofxparse [StringIO.StringIO for Python 2, IO.StringIO for Python3]. Ofxparse is apparently happy with the Python 2 StringIO.StringIO object, but is not happy with the Python3 IO.StringIO. However converting the IO.StringIO object to an IO.BytesIO object fixes the issue. For example, account.py, line 112: change: return OfxParser.parse(self.download(days=days)) to: return OfxParser.parse(BytesIO(((self.download(days=days)).read()).encode())) seems to work for Python3, assuming IO.BytesIO is imported. I'll fork and request pull in the next few days.

mattprompt avatar Oct 16 '16 16:10 mattprompt

Pull request #30 submitted to resolve this issue.

mattprompt avatar Oct 17 '16 06:10 mattprompt