Make parse() accept anything which has a nullary decode() method#418
Make parse() accept anything which has a nullary decode() method#418uckelman wants to merge 2 commits into
Conversation
just a bytes. (In particular, this lets parse() accept a bytearray.)
pganssle
left a comment
There was a problem hiding this comment.
It appears that in Python 2, unicode has a decode method for whatever reason, so I'm not actually sure that this duck typing approach will work.
| if getattr(instream, 'read', None) is None: | ||
| raise TypeError('Parser must be a string or character stream, not ' | ||
| '{itype}'.format(itype=instream.__class__.__name__)) | ||
| if not hasattr(instream, 'read'): |
There was a problem hiding this comment.
I do prefer the getattr(instream, 'read', None) is None idiom over hasattr, at least until Python 2.7 support is dropped, based on this article.
Also, why is this being checked at all? Just to avoid streams that have a .decode() method?
|
I'm not really sure how to properly ducktype I suppose it's not the worst thing in the world to just explicitly add support for |
Deprecate some unnecessary methods in _timelex
|
I'll pull this and try to rebase it against master. I think maybe the right approach here is to use proper duck-typing in Python 3, and use |
|
Fixed in #514. Thank you for your contribution. |
This change fixes Issue #417, by making
parse()use duck typing rather than explicitly checking whether it's been given abytes. Now any object which has a nullarydecode()method can be passed toparse().