wordnik / wordnik-python

Wordnik Python public library
227 stars 47 forks source link

Windows: AttributeError when adding Unicode to word list #19

Closed hugovk closed 10 years ago

hugovk commented 10 years ago

I can add Unicode words to Worknik lists with a Mac but not Windows. I don't have the Mac with me now to compare configurations but both have Python 2.7.

This can easily be reproduced with testAddWordsToWordList() in WordListApiTest.py.

With no changes the test passes. But if, for example, you change the string "delicious" on line 62 to Unicode u"delicious" it fails.

https://github.com/wordnik/wordnik-python/blob/master/tests/WordListApiTest.py#L62

Output of python tests\WordListApiTest.py:

E...
======================================================================
ERROR: testAddWordsToWordList (__main__.WordListApiTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests\WordListApiTest.py", line 71, in testAddWordsToWordList
    self.authToken, body=wordsToAdd)
  File "./wordnik\WordListApi.py", line 191, in addWordsToWordList
    postData, headerParams)
  File "./wordnik\swagger.py", line 61, in callAPI
    data = self.sanitizeForSerialization(postData)
  File "./wordnik\swagger.py", line 103, in sanitizeForSerialization
    return [self.sanitizeForSerialization(subObj) for subObj in obj]
  File "./wordnik\swagger.py", line 112, in sanitizeForSerialization
    for (key, val) in objDict.iteritems()
  File "./wordnik\swagger.py", line 113, in <dictcomp>
    if key != 'swaggerTypes'}
  File "./wordnik\swagger.py", line 110, in sanitizeForSerialization
    objDict = obj.__dict__
AttributeError: 'unicode' object has no attribute '__dict__'

----------------------------------------------------------------------
Ran 4 tests in 5.131s

FAILED (errors=1)
hugovk commented 10 years ago

Unicode tests added to PR #20.

hugovk commented 10 years ago

So, on my Mac: These tests fail too, when run from a folder with freshly downloaded source. They pass when using the installed version of Wordnik. My real code works too, and uses the installed version of Wordnik.

On Windows: Tests fail from downloaded Wordnik. Real code fails using installed Wordnik.

I'll investigate further.

hugovk commented 10 years ago

Comparing my site-packages\wordnik for each machine, both match except for line 100 of swagger.py (in sanitizeForSerialization(self, obj))

https://github.com/wordnik/wordnik-python/blob/536de5bb93e6e343ebc3a8ef9b56051cc00b2653/wordnik/swagger.py#L100

PC and latest source have: elif type(obj) in [str, int, long, float, bool]: Mac has: elif type(obj) in [str, int, long, float, bool, unicode]:

I'm not entirely sure where that unicode came from. All the other source files have April 15th timestamps but swagger.py has April 16th. It's possible it's a change I made but forgot about. (At this time I switched from the older Wordnik Python library/API to the new one, and perhaps this was in the old one?)

Anyway, the tests pass and Unicode words can be successfully added to Wordnik lists. I'll make a pull request with a changed swagger.py.

mrjf commented 10 years ago

@hugovk Thanks for the great error report and PRs! I've merged yours and added some more tests and fixes for unicode in 5c589c0844b05e5d4154e65ba302aa62e0262f05 . Much appreciated.