Skip to content

'ascii' codec can't encode character u'\xa0' #89

Closed
wants to merge 1 commit into from

2 participants

@eider-i128

Another Unicode problem that was in there for a quite some time. At the end of each sync session, a summary is printed to log:

job_key.SetVariable( 'popup_title', 'repository synchronisation - ' + self._name + ' - finished' )

updates_text = HydrusData.ConvertIntToPrettyString( num_updates_downloaded ) + ' updates downloaded, ' + HydrusData.ConvertIntToPrettyString( num_updates_processed ) + ' updates processed'

if self._service_type == HC.TAG_REPOSITORY: content_text = HydrusData.ConvertIntToPrettyString( total_content_weight_processed ) + ' mappings added'
elif self._service_type == HC.FILE_REPOSITORY: content_text = HydrusData.ConvertIntToPrettyString( total_content_weight_processed ) + ' files added'

job_key.SetVariable( 'popup_text_1', updates_text + ', and ' + content_text )

print( job_key.ToString() )

Unfortunately, here's what happens on some locales:

Traceback (most recent call last):
File "C:\code\Hydrus\build\client\out00-PYZ.pyz\include.ClientData", line 2344, in Sync
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 112: ordinal not in range(128)

Failed to update public tag repository:

The following exception occured at 2015/11/14 05:41:55:
UnicodeEncodeError

'ascii' codec can't encode character u'\xa0' in position 112: ordinal not in range(128)

Traceback (most recent call last):
File "C:\code\Hydrus\build\client\out00-PYZ.pyz\include.ClientData", line 2344, in Sync
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 112: ordinal not in range(128)

The fix attached to this pull request should take care of that problem. This is very important to fix as the bug causes GUI to display error titled "Failed to update public tag repository" which can be misleading.

@hydrusnetwork
Owner

Thanks for this. I am going to write a separate function, something like HydrusData.Print, to handle this and all other occasions of print() in the program, to automatically encode everything that goes to the log.

I don't know why print() in python 2.7 doesn't automatically do this. Maybe there is a way for me to tell the current environment "Hey, if you ever need to convert back and forth between unicode and a bytestring, just use utf-8", but I don't know it.

@eider-i128

You may try setting environment variable "PYTHONIOENCODING=UTF-8"

https://docs.python.org/release/2.7.10/using/cmdline.html?highlight=pythonioencoding

@eider-i128 eider-i128 closed this Nov 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.