Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Python 2.3 Breaks PySQLite a Little (http://www.velocityreviews.com/forums/t320651-python-2-3-breaks-pysqlite-a-little.html)

achrist@easystreet.com 08-06-2003 06:48 PM

Python 2.3 Breaks PySQLite a Little
 
Have hit a problem with PySQLite and python 2.3. The new bool
type doesn't want to get written properly to a database. I was
expecting this to break when I started using True and False in
python 2.2, but it didn't. Now there's a little trouble.
I changed the _quote function in pysqlite main.py to look like:


def _quote(value):

if value is None:
return 'NULL'
elif isinstance(value, bool): ### Added
return 0 + value ### Added
elif type(value) in (IntType, LongType, FloatType):
return value
elif isinstance(value, StringType):
return "'%s'" % value.replace("'", "''")
elif hasattr(value, '__quote__'):
return value.__quote__()
elif hasattr(value, '_quote'):
return value._quote()
elif have_datetime and type(value) in \
(DateTime.DateTimeType, DateTime.DateTimeDeltaType):
return "'%s'" % value
else:
return repr(value)


Is that all it needs?


Al

=?ISO-8859-1?Q?Gerhard_H=E4ring?= 08-06-2003 10:01 PM

Re: Python 2.3 Breaks PySQLite a Little
 
achrist@easystreet.com wrote:
> Have hit a problem with PySQLite and python 2.3. The new bool
> type doesn't want to get written properly to a database. I was
> expecting this to break when I started using True and False in
> python 2.2, but it didn't. Now there's a little trouble.
> I changed the _quote function in pysqlite main.py to look like:
>
>
> def _quote(value):
>
> if value is None:
> return 'NULL'
> elif isinstance(value, bool): ### Added
> return 0 + value ### Added
> [...]
>
> Is that all it needs?


Yeah, except I'd write int(value). [1]

You'll also want to convert it back to bools, right? use the
'converters' parameter of the connect call.

To check wether your changes worked as expected, you could use something
like:

#v+
cx = sqlite.connect("db", converters={"bool": bool})
cu.execute("create table test(b bool)")
cu.execute("insert into test(b) values (%s)", (True,))
cu.execute("select b from test")
res = cu.fetchone()
assert type(res.b) is bool
#v-

All completely untested, I'm too lazy now ;-)

-- Gerhard

[1] And I intend to drop all this politically correct isinstance stuff
in a future version for performance reasons.


=?ISO-8859-1?Q?Gerhard_H=E4ring?= 08-07-2003 09:15 AM

Re: Python 2.3 Breaks PySQLite a Little
 
Greg Brunet wrote:
> "Gerhard Häring" <gh@ghaering.de> wrote:
>>[1] And I intend to drop all this politically correct isinstance stuff
>>in a future version for performance reasons.

>
> Hey Gerhard:
>
> What will you be using to replace it with that will improve the
> performance? Is just doing a: type(value) that much faster?


Well, my plan is to rewrite PySQLite completely in C (maybe using PyRex).

You're right, it doesn't make a big difference if you use issubclass()
or type() in an if-elif chain. But using type() makes it possible to use
a dictionary to map types to quote functions, which *should* be faster.
Note the "should" ;-)

I'll have to benchmark a little more. However I really want to add the
feature of being able to register new quote functions without
subclassing the type in question and adding a _quote() method, which is
currently the only way. Apart from directly hacking the PySQLite
sources, like you did ;-)

-- Gerhard



All times are GMT. The time now is 12:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.