[py3k] Don't encode strings to bytes

str.join([bytes]) fails with TypeError

NB: maybe the API should be full Unicode in python 2 as well?

Related to #296242.

authorRémi Cardona <remi.cardona@free.fr>
changeset388d5ab49c86
branchdefault
phasepublic
hiddenno
parent revision#866b6972d993 [py3k] print function
child revision#40bd9b8b1e09 [py3k] Stop using dict.iter{keys,values,items}()
files modified by this revision
fd.py
propagation.py
# HG changeset patch
# User Rémi Cardona <remi.cardona@free.fr>
# Date 1405925035 -7200
# Mon Jul 21 08:43:55 2014 +0200
# Node ID 388d5ab49c862f289707e08f038b468dfcda6c91
# Parent 866b6972d993324496b189238f4115b5a773e0ef
[py3k] Don't encode strings to bytes

str.join([bytes]) fails with TypeError

NB: maybe the API should be full Unicode in python 2 as well?

Related to #296242.

diff --git a/fd.py b/fd.py
@@ -27,10 +27,11 @@
1  The Expression and BinaryExpression classes can be constructed using the
2  make_expression factory function.  """
3 
4  from __future__ import print_function
5 
6 +import sys
7  import operator
8 
9  from logilab.constraint.propagation import AbstractDomain, BasicConstraint, \
10                                             ConsistencyFailure, \
11                                             AbstractConstraint
@@ -281,11 +282,11 @@
12      """create a new constraint of type Expression or BinaryExpression
13      The chosen class depends on the number of variables in the constraint"""
14      # encode unicode
15      vars = []
16      for var in variables:
17 -        if type(var) == type(u''):
18 +        if sys.version_info < (3,) and type(var) == type(u''):
19              vars.append(var.encode())
20          else:
21              vars.append(var)
22      if len(vars) == 2:
23          if constraint_type is not None:
diff --git a/propagation.py b/propagation.py
@@ -15,10 +15,11 @@
24  #
25  # You should have received a copy of the GNU Lesser General Public License along
26  # with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.
27  """The code of the constraint propagation algorithms"""
28  from __future__ import print_function
29 +import sys
30  from operator import mul as MUL
31  from time import strftime
32  from logilab.constraint.interfaces import DomainInterface, ConstraintInterface
33 
34  def quiet_printer(*args):
@@ -36,11 +37,11 @@
35      def __init__(self, variables, domains, constraints = None, printer=print):
36          # encode unicode
37          self._printer = printer
38 
39          for i, var in enumerate(variables):
40 -            if type(var) == type(u''):
41 +            if sys.version_info < (3,) and type(var) == type(u''):
42                  variables[i] = var.encode()
43 
44          self._variables = variables   # list of variable names
45          self._domains = domains    # maps variable name to domain object
46          self._constraints = [] # list of constraint objects