Skip to content

Commit

Permalink
fixes buggy convertion of string to unicode (closes #966)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascamata committed Jul 26, 2016
1 parent c4c3487 commit 4555466
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _set_starting_position(self):

if self.config.location:
try:
location_str = u'{}'.format(self.config.location)
location_str = u'{}'.format(str(self.config.location))

This comment has been minimized.

Copy link
@Nihisil

Nihisil Jul 26, 2016

Contributor

@douglascamata

With this str() code will crash on non unicode string (you can try to set location: "Москва"):

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

Without str() it is working both for non unicode and unicode strings. Why we need it?

This comment has been minimized.

Copy link
@douglascamata

douglascamata Jul 26, 2016

Author Member

It breaks the code if self.config.location is None, because the .format call expects a string. This happened to a few users. Don't ask me why, that if should've prevented it.

This comment has been minimized.

Copy link
@douglascamata

douglascamata Jul 26, 2016

Author Member

Weird:

% python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> location = "Москва"
>>> location
'\xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0'
>>> print location
Москва

This comment has been minimized.

Copy link
@douglascamata

douglascamata Jul 26, 2016

Author Member

@Nihisil location_str = unicode(location.decode('utf-8')) please test is this fixes.

This comment has been minimized.

Copy link
@Nihisil

Nihisil Jul 26, 2016

Contributor

@douglascamata

Sorry, I didn't tell it clear.

Try to set "location": "Москва" in your config.json and run the bot without -l command argument.

In that case code will crash both on the:

location_str = unicode(location.decode('utf-8'))

and

str(self.config.location)

If we want to prevent None value, we can do it like this, I think:

location_str = u'{}'.format(self.config.location or 'None')

It will handle both situations: with non unicode string and with None value.

location = (self._get_pos_by_name(location_str.replace(" ", "")))
self.position = location
self.api.set_position(*self.position)
Expand Down

1 comment on commit 4555466

@tpmodding
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! thanks

Please sign in to comment.