Fixed range adaptation on Python 3

This commit is contained in:
Daniele Varrazzo 2013-03-16 22:12:41 +00:00
parent af18c29f0b
commit 06bfa801f4

View File

@ -27,7 +27,7 @@
import re import re
from psycopg2._psycopg import ProgrammingError, InterfaceError from psycopg2._psycopg import ProgrammingError, InterfaceError
from psycopg2.extensions import ISQLQuote, adapt, register_adapter from psycopg2.extensions import ISQLQuote, adapt, register_adapter, b
from psycopg2.extensions import new_type, new_array_type, register_type from psycopg2.extensions import new_type, new_array_type, register_type
class Range(object): class Range(object):
@ -196,7 +196,7 @@ class RangeAdapter(object):
r = self.adapted r = self.adapted
if r.isempty: if r.isempty:
return "'empty'::%s" % self.name return b("'empty'::" + self.name)
if r.lower is not None: if r.lower is not None:
a = adapt(r.lower) a = adapt(r.lower)
@ -204,7 +204,7 @@ class RangeAdapter(object):
a.prepare(self._conn) a.prepare(self._conn)
lower = a.getquoted() lower = a.getquoted()
else: else:
lower = 'NULL' lower = b('NULL')
if r.upper is not None: if r.upper is not None:
a = adapt(r.upper) a = adapt(r.upper)
@ -212,10 +212,10 @@ class RangeAdapter(object):
a.prepare(self._conn) a.prepare(self._conn)
upper = a.getquoted() upper = a.getquoted()
else: else:
upper = 'NULL' upper = b('NULL')
return "%s(%s, %s, '%s')" % ( return b(self.name + '(') + lower + b(', ') + upper \
self.name, lower, upper, r._bounds) + b(", '%s')" % r._bounds)
class RangeCaster(object): class RangeCaster(object):
@ -422,17 +422,17 @@ class NumberRangeAdapter(RangeAdapter):
# quoted (they are numbers). Also, I'm lazy and not preparing the # quoted (they are numbers). Also, I'm lazy and not preparing the
# adapter because I assume encoding doesn't matter for these # adapter because I assume encoding doesn't matter for these
# objects. # objects.
lower = adapt(r.lower).getquoted() lower = adapt(r.lower).getquoted().decode('ascii')
else: else:
lower = '' lower = ''
if not r.upper_inf: if not r.upper_inf:
upper = adapt(r.upper).getquoted() upper = adapt(r.upper).getquoted().decode('ascii')
else: else:
upper = '' upper = ''
return "'%s%s,%s%s'" % ( return b("'%s%s,%s%s'" % (
r._bounds[0], lower, upper, r._bounds[1]) r._bounds[0], lower, upper, r._bounds[1]))
# TODO: probably won't work with infs, nans and other tricky cases. # TODO: probably won't work with infs, nans and other tricky cases.
register_adapter(NumericRange, NumberRangeAdapter) register_adapter(NumericRange, NumberRangeAdapter)