Re: [PATCH 5/7] py3k: the basestring and unicode types are removed in python 3

Subject: Re: [PATCH 5/7] py3k: the basestring and unicode types are removed in python 3

Date: Mon, 02 Jan 2012 16:15:58 +0100

To: Justus Winter, notmuch@notmuchmail.org

Cc:

From: Sebastian Spaeth


Happy new year. Pushed patches 1-4 of this series so far. Looking fine,
but ugh, the below seems like a rather ugly hack in a function that is
probably called quite often.

Isn't there a more pretty variant avoiding these sys.version_info checks
all over the place?

> @@ -200,9 +201,9 @@ def _str(value):
>  
>      C++ code expects strings to be well formatted and
>      unicode strings to have no null bytes."""
> -    if not isinstance(value, basestring):
> +    if not isinstance(value, basestring if sys.version_info[0] == 2 else str):
>          raise TypeError("Expected str or unicode, got %s" % str(type(value)))
> -    if isinstance(value, unicode):
> +    if sys.version_info[0] == 3 or isinstance(value, unicode):
>          return value.encode('UTF-8')
>      return value
part-000.sig (application/pgp-signature)

Thread: