Update of /cvsroot/pypgsql/pypgsql/pyPgSQL
In directory usw-pr-cvs1:/tmp/cvs-serv12078/pyPgSQL
Modified Files:
PgSQL.py
Log Message:
16APR2002 gh
pgversion.c:
Replace _tolower with the standard C tolower function,
to make this file compile on FreeBSD.
pyPgSQL/PgSQL.py:
Fix the array parsing so it also works with PostgreSQL
versions 7.2.x. The PostgreSQL developers changed the
quoting in the string representation of arrays in 7.2
beta cycle: strings are now only quoted when otherwise
the array representation would be ambiguous. The new
parseArray() method should handle the old and new way
of quoting in arrays. [Bug #539769].
test/PgSQLTestCases.py:
Updated the PostgreSQL version specific tests to cope
with PostgreSQL 7.2.x.
Index: PgSQL.py
===================================================================
RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** PgSQL.py 4 Feb 2002 02:09:52 -0000 1.7
--- PgSQL.py 16 Apr 2002 00:14:01 -0000 1.8
***************
*** 30,33 ****
--- 30,40 ----
# Date Ini Description |
# --------- --- ------------------------------------------------------- |
+ # 16APR2002 gh Fix the array parsing so it also works with PostgreSQL |
+ # versions 7.2.x. The PostgreSQL developers changed the |
+ # quoting in the string representation of arrays in 7.2 |
+ # beta cycle: strings are now only quoted when otherwise |
+ # the array representation would be ambiguous. The new |
+ # parseArray() method should handle the old and new way |
+ # of quoting in arrays. [Bug #539769]. |
# 01FEB2002 bga Fixed a couple of problems found by Steven D. Arnold: |
# 1. A undefined variable was used in a few places where |
***************
*** 469,472 ****
--- 476,538 ----
self.__conn = None
+ def parseArray(self, s):
+ """Parse a PostgreSQL array strings representation.
+ This parses a PostgreSQL array and return a list of the array
+ elements as strings.
+ """
+ class LeaveLoopException(Exception): pass
+
+ lst = []
+ s = s[1:-1] # drop '{' and '}' at start/end
+
+ # If the array is empty, return immediately
+ if len(s) == 0:
+ return lst
+
+ pos = 0
+ try:
+ while 1:
+ if s[pos] == '"':
+ # A quoted element, find the end-quote, which is the next
+ # quote char that is not escaped.
+ end_quote_pos = pos
+ while 1:
+ end_quote_pos = s.find('"', end_quote_pos + 1)
+ assert(end_quote_pos > pos)
+ if s[end_quote_pos - 1] != '\\':
+ break
+ lst.append(s[pos + 1:end_quote_pos])
+
+ # Skip quote char and next comma
+ pos = end_quote_pos + 2
+
+ # If end-of-string. leave loop.
+ if pos >= len(s):
+ break
+ else:
+ # This array element is not quoted, so it ends either at
+ # the next comma that isn't escaped, or at the end of the
+ # string.
+ next_comma_pos = pos
+ while 1:
+ next_comma_pos = s.find(',', next_comma_pos + 1)
+ if next_comma_pos < 0:
+ # This is the last array element.
+ lst.append(s[pos:])
+ raise LeaveLoopException
+ else:
+ if s[next_comma_pos - 1] != '\\':
+ break
+ lst.append(s[pos:next_comma_pos])
+ pos = next_comma_pos + 1
+ except LeaveLoopException:
+ pass
+
+ # Get rid of the array's backslash escaping level
+ def convertEscapes(s):
+ return s.replace('\\"', '"')
+ lst = map(convertEscapes, lst)
+ return lst
+
def typecast(self, colinfo, value):
"""
***************
*** 484,488 ****
if _ia:
# Convert string representation of the array into list.
! exec "_list = %s" % replace(replace(value, '{', '['), '}', ']')
return self.handleArray(colinfo, _list)
--- 550,554 ----
if _ia:
# Convert string representation of the array into list.
! _list = self.parseArray(value)
return self.handleArray(colinfo, _list)
***************
*** 571,575 ****
# There is no need to un-escape lst[_i], it's already been
# done when the PostgreSQL array was converted to a list
! # via 'exec'.
lst[_i] = PgBytea(lst[_i])
else:
--- 637,641 ----
# There is no need to un-escape lst[_i], it's already been
# done when the PostgreSQL array was converted to a list
! # via parseArray().
lst[_i] = PgBytea(lst[_i])
else:
|