Reuse of DB-API 2.0 cursors for multiple queries?

2009-01-28 Thread moreati
Today, I used the adodbapi module against an SQL Server Express
database. I was surprised to get an exception, when I attempted to
submit a second query with my cursor object. The full session is
below.

With cx_Oracle I've become used to reusing a cursor for subsequent
queries. The PEP doesn't specify either way, that I can see. Is this
behaviour left to the implementation, or should I be able to expect a
cursor is reusable?

With thanks, Alex

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import adodbapi
>>> conn = adodbapi.connect('Provider=SQLOLEDB.1;Data 
>>> Source=.\\SQLEXPRESS;Initial Catalog=MYDATABBASE;Integrated 
>>> Security=SSPI;User Instance=False;')
>>> curs = conn.cursor()
>>> curs.execute('select * from localview_roles')
>>> curs.execute('select * from localview_roles')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\Lib\site-packages\adodbapi\adodbapi.py", line 713,
in execut
e
self._executeHelper(operation,False,parameters)
  File "C:\Python25\Lib\site-packages\adodbapi\adodbapi.py", line 664,
in _execu
teHelper
self._raiseCursorError(DatabaseError,tracebackhistory)
  File "C:\Python25\Lib\site-packages\adodbapi\adodbapi.py", line 474,
in _raise
CursorError
eh(self.conn,self,errorclass,errorvalue)
  File "C:\Python25\Lib\site-packages\adodbapi\adodbapi.py", line 60,
in standar
dErrorHandler
raise errorclass(errorvalue)
adodbapi.adodbapi.DatabaseError:
--ADODBAPI
Traceback (most recent call last):
   File "C:\Python25\Lib\site-packages\adodbapi\adodbapi.py", line
650, in _exec
uteHelper
adoRetVal=self.cmd.Execute()
   File "", line 3, in Execute
   File "C:\Python25\lib\site-packages\win32com\client\dynamic.py",
line 258, in
 _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags,
retType, argTypes
) + args)
 com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft OLE
DB Provider
 for SQL Server', u'Cannot create new connection because in manual or
distribute
d transaction mode.', None, 0, -2147467259), None)
-- on command: "select * from localview_roles"
-- with parameters: None
--
http://mail.python.org/mailman/listinfo/python-list


Calling pcre with ctypes

2008-06-17 Thread moreati
Recently I discovered the re module doesn't support POSIX character
classes:

Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> r = re.compile('[:alnum:]+')
>>> print r.match('123')
None

So I thought I'd try out pcre through ctypes, to recreate pcredemo.c
in python. The c code is at:
http://vcs.pcre.org/viewvc/code/trunk/pcredemo.c?view=markup

Partial Python code is below

I'm stuck, from what I can tell a c array, such as:
int ovector[OVECCOUNT];

translates to
ovector = ctypes.c_int * OVECOUNT

but when I pass ovector to a function I get the traceback
$ python pcredemo.py [a-z] fred
Traceback (most recent call last):
  File "pcredemo.py", line 65, in 
compiled_re, None, subject, len(subject), 0, 0, ovector, OVECOUNT
ctypes.ArgumentError: argument 7: : Don't
know how to convert parameter 7

What is the correct way to construct and pass ovector?

With thanks, Alex

# PCRE through ctypes demonstration program

import ctypes
import getopt
import sys

import pcre_const

OVECOUNT = 30 # Should be a multiple of 3

pcre = ctypes.cdll.LoadLibrary('libpcre.so')

compiled_re = None
error   = ctypes.c_char_p()
pattern = ''
subject = ''
name_table  = ctypes.c_ubyte()
erroffset   = ctypes.c_int()
find_all= 0
namecount   = 0
name_entry_size = 0
ovector = ctypes.c_int * OVECOUNT
options = 0

# First, sort out the command line. There is only one possible option
at
# the moment, "-g" to request repeated matching to find all
occurrences,
# like Perl's /g option. We set the variable find_all to a non-zero
value
# if the -g option is present. Apart from that, there must be exactly
two
# arguments.

opts, args = getopt.getopt(sys.argv[1:], 'g')
for o, v in opts:
if o == '-g': find_all = 1

# After the options, we require exactly two arguments, which are the
# pattern, and the subject string.

if len(args) != 2:
print 'Two arguments required: a regex and a subject string'
sys.exit(1)

pattern = args[0]
subject = args[1]
subject_length = len(subject)

# Now we are going to compile the regular expression pattern, and
handle
# and errors that are detected.

compiled_re = pcre.pcre_compile(
pattern, options, ctypes.byref(error),
ctypes.byref(erroffset),
None
)

--
http://mail.python.org/mailman/listinfo/python-list