why does this bytea import fail when locale is activated ?
Karsten Hilbert <[email protected]> Wed, 30 Aug 2006 09:29:34 +0200
| Newsgroups | gmane.comp.python.db.pypgsql.user |
|---|---|
| Message-ID | <[email protected]> |
--ZPt4rx8FFjLCG7dd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
The attached script fails when the locale is activated (at
the top) and succeeds when it is commented out.
Can someone please explain why ?
I am out of my wits.
Thanks
Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346
--ZPt4rx8FFjLCG7dd
Content-Type: text/x-python; charset=us-ascii
Content-Disposition: attachment; filename="test-bytea-import.py"
import locale
# this is what makes the difference:
# likely because it somehow changes which encoding
# Python thinks comes back from file.read()
locale.setlocale(locale.LC_ALL, '')
import sys, os.path
import pyPgSQL.PgSQL as dbapi
__license__ = "GPL"
dsn = "::gnumed_v2:any-doc:any-doc"
#dsn = "salaam.homeunix.com::gnumed_v2:any-doc:any-doc"
fname = sys.argv[1]
encodings = 'win1250 win1252 latin1 iso-8859-15 sql_ascii latin9'.split()
log = open('test-bytea-import.log', 'wb')
log.write('running on:\n')
log.write(sys.version + '\n')
log.write(sys.platform + '\n')
conn = dbapi.connect(dsn=dsn + '\n')
log.write(str(conn.version) + '\n')
conn.close()
del conn
log.write('importing: [%s]' % fname + '\n')
for encoding in encodings:
print "encoding:", encoding
log.write("----------------------------------------------" + '\n')
log.write("testing encoding: [%s]" % encoding + '\n')
log.write("Python string encoding [%s]" % sys.getdefaultencoding() + '\n')
# reading file
log.write("os.path.getsize(%s): [%s]" % (fname, os.path.getsize(fname)) + '\n')
f = file(fname, "rb")
img_data = f.read()
f.close()
log.write("type(img_data): [%s]" % type(img_data) + '\n')
log.write("len(img_data) : [%s]" % len(img_data) + '\n')
img_obj = dbapi.PgBytea(img_data)
del(img_data)
log.write("len(img_obj) : [%s]" % len(str(img_obj)) + '\n')
log.write("type(img_obj): [%s]" % type(img_obj) + '\n')
# setting connection level client encoding
try:
if encoding == 'sql_ascii':
conn = dbapi.connect(dsn=dsn, unicode_results=0)
else:
conn = dbapi.connect(dsn=dsn, client_encoding = (encoding, 'strict'), unicode_results=0)
curs = conn.cursor()
cmd = "set client_encoding to '%s'" % encoding
curs.execute(cmd)
curs.close()
except:
log.write("cannot set encoding [%s] in dbapi.connect()" % encoding + '\n')
conn = dbapi.connect(dsn=dsn)
curs = conn.cursor()
try:
# import data
cmd = "create table test (data bytea)"
curs.execute(cmd)
cmd = "insert into test (data) values (%s)"
curs.execute(cmd, img_obj)
cmd = "select octet_length(data) from test"
curs.execute(cmd)
log.write("INSERTed octet_length(test.data): [%s]" % curs.fetchall()[0][0] + '\n')
cmd = "update test set data=%s"
curs.execute(cmd, img_obj)
cmd = "select octet_length(data) from test"
curs.execute(cmd)
log.write("UPDATEd octet_length(test.data): [%s]" % curs.fetchall()[0][0] + '\n')
cmd = "select data from test"
curs.execute(cmd)
rows = curs.fetchone()
log.write("len(SELECT) : [%s]" % len(str(rows[0])) + '\n')
except:
log.write('cannot test encoding [%s]' % encoding + '\n')
t, v, tb = sys.exc_info()
log.write(str(t) + '\n')
log.write(str(v) + '\n')
# finish
conn.rollback()
curs.close()
conn.close()
log.close()
#=======================================================================
# $Log: test-bytea-import.py,v $
# Revision 1.2 2006/08/29 22:18:28 ncq
# - improve
#
# Revision 1.1 2006/07/01 08:43:58 ncq
# - first version
#
#
--ZPt4rx8FFjLCG7dd
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
--ZPt4rx8FFjLCG7dd
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Pypgsql-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pypgsql-users
--ZPt4rx8FFjLCG7dd--