[py3k] work with str (and not bytes) in python 3
authorRémi Cardona <remi.cardona@free.fr>
Wed, 23 Jul 2014 09:27:39 +0200
changeset 779 d8451fa72dcc
parent 778 e96aa511f9db
child 780 43e30d2417b1
[py3k] work with str (and not bytes) in python 3 Related to #1167312
rql/nodes.py
rql/stmts.py
--- a/rql/nodes.py	Fri Jul 25 21:02:29 2014 +0200
+++ b/rql/nodes.py	Wed Jul 23 09:27:39 2014 +0200
@@ -428,7 +428,7 @@
 
     def __init__(self, r_type, optional=None):
         Node.__init__(self)
-        self.r_type = r_type.encode()
+        self.r_type = r_type
         self.optional = None
         self.set_optional(optional)
 
@@ -545,7 +545,7 @@
         if operator == '~=':
             operator = 'ILIKE'
         assert operator in CMP_OPERATORS, operator
-        self.operator = operator.encode()
+        self.operator = operator
         self.optional = optional
         if value is not None:
             self.append(value)
@@ -578,11 +578,11 @@
             if self.optional in ('right', 'both'):
                 rhsopt = '?'
             return '%s%s %s %s%s' % (self.children[0].as_string(encoding, kwargs),
-                                     lhsopt, self.operator.encode(),
+                                     lhsopt, self.operator,
                                      self.children[1].as_string(encoding, kwargs), rhsopt)
         if self.operator == '=':
             return self.children[0].as_string(encoding, kwargs)
-        return '%s %s' % (self.operator.encode(),
+        return '%s %s' % (self.operator,
                           self.children[0].as_string(encoding, kwargs))
 
     def __repr__(self):
@@ -595,12 +595,12 @@
 
     def __init__(self, operator, lhs=None, rhs=None):
         BinaryNode.__init__(self, lhs, rhs)
-        self.operator = operator.encode()
+        self.operator = operator
 
     def as_string(self, encoding=None, kwargs=None):
         """return the tree as an encoded rql string"""
         return '(%s %s %s)' % (self.children[0].as_string(encoding, kwargs),
-                               self.operator.encode(),
+                               self.operator,
                                self.children[1].as_string(encoding, kwargs))
 
     def __repr__(self):
@@ -638,13 +638,13 @@
 
     def __init__(self, operator, child=None):
         Node.__init__(self)
-        self.operator = operator.encode()
+        self.operator = operator
         if child is not None:
             self.append(child)
 
     def as_string(self, encoding=None, kwargs=None):
         """return the tree as an encoded rql string"""
-        return '%s%s' % (self.operator.encode(),
+        return '%s%s' % (self.operator,
                          self.children[0].as_string(encoding, kwargs))
 
     def __repr__(self):
@@ -666,7 +666,7 @@
 
     def __init__(self, name):
         Node.__init__(self)
-        self.name = name.strip().upper().encode()
+        self.name = name.strip().upper()
 
     def initargs(self, stmt):
         """return list of arguments to give to __init__ to clone this node"""
@@ -734,6 +734,7 @@
         """return the tree as an encoded rql string (an unicode string is
         returned if encoding is None)
         """
+        u = str if sys.version_info >= (3,) else unicode
         if self.type is None:
             return 'NULL'
         if self.type in ('etype', 'Date', 'Datetime', 'Int', 'Float'):
@@ -745,7 +746,7 @@
             #     and linked relation
             if kwargs is not None:
                 value = kwargs.get(self.value, '???')
-                if isinstance(value, unicode):
+                if sys.version_info < (3,) and isinstance(value, u):
                     if encoding:
                         value = quote(value.encode(encoding))
                     else:
@@ -756,7 +757,7 @@
                     value = repr(value)
                 return value
             return '%%(%s)s' % self.value
-        if isinstance(self.value, unicode):
+        if sys.version_info < (3,) and isinstance(self.value, u):
             if encoding is not None:
                 return quote(self.value.encode(encoding))
             return uquote(self.value)
@@ -892,7 +893,7 @@
     __slots__ = ('name', 'stinfo', 'stmt')
 
     def __init__(self, name):
-        self.name = name.strip().encode()
+        self.name = name.strip()
         # used to collect some global information about the syntax tree
         self.stinfo = {
             # link to VariableReference objects in the syntax tree
--- a/rql/stmts.py	Fri Jul 25 21:02:29 2014 +0200
+++ b/rql/stmts.py	Wed Jul 23 09:27:39 2014 +0200
@@ -911,7 +911,7 @@
         #if etype == 'Any':
         #    raise BadRQLQuery('"Any" is not supported in DELETE statement')
         vref.parent = self
-        self.main_variables.append( (etype.encode(), vref) )
+        self.main_variables.append( (etype, vref) )
 
     def add_main_relation(self, relation):
         """add a relation to the list of deleted relations"""
@@ -1000,7 +1000,7 @@
         """add a variable to the list of inserted variables"""
         if etype == 'Any':
             raise BadRQLQuery('"Any" is not supported in INSERT statement')
-        self.main_variables.append( (etype.encode(), vref) )
+        self.main_variables.append( (etype, vref) )
         vref.parent = self
         self.inserted_variables[vref.variable] = 1