472,779 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Calling pcre with ctypes

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/trun....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 <module>
compiled_re, None, subject, len(subject), 0, 0, ovector, OVECOUNT
ctypes.ArgumentError: argument 7: <type 'exceptions.TypeError'>: 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
)

Jun 27 '08 #1
1 2380
moreati schrieb:
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/trun....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 <module>
compiled_re, None, subject, len(subject), 0, 0, ovector, OVECOUNT
ctypes.ArgumentError: argument 7: <type 'exceptions.TypeError'>: Don't
know how to convert parameter 7

What is the correct way to construct and pass ovector?
'ctypes.c_int * OVECOUNT' does not create an array *instance*, it creates an array *type*,
comparable to a typedef in C. You can create an array instance by calling the type, with
zero or more initializers:

ovector = (ctypes.c_int * OVECOUNT)(0, 1, 2, 3, ...)

Thomas
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Todd Gardner | last post by:
Hello all, Pardon my ignorance here. I would like to talk to someone that has had success in calling ddls in Windows NT/2000/XP. I am wondering where to dload any of these packages? Google...
3
by: robspychala | last post by:
Hi Is there something similar to python's windll for calling DLLs on win32 but meant for calling dylib's on OS X? thanks, r.s.
3
by: news | last post by:
Hi all, I am a beginner on 'c' and pcre...and I am on windows 2000 and VC6 with all of the patches, etc. The following program leaks lots of memory and I only make 1 pcre call. I read the...
18
by: Paul Watson | last post by:
I need to call some Windows APIs. Is the only way to download ctypes or the win32 interfaces? Is there any plan to get ctypes batteries into the standard Python build?
1
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss...
16
by: mainland | last post by:
I use prce to match string. I define a pcre struct point, then compile, pass pcre struct point to 'pcre_compile'. When complete, Is it necessary to use 'pcre_free' to free pcre struct point. ...
13
by: Mangabasi | last post by:
Howdy, I have been trying to call the following Fortran function from Python (using Windows XP, Compaq Fortran and Python 2.4). I tried F2Py, Pyfort and calldll with no success. I think I...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
4
by: taghi | last post by:
I want to call NetShareEnum, a function from netapi32.dll NetShareEnum has this definition: NET_API_STATUS NetShareEnum( __in LPWSTR servername, __in DWORD level, __out LPBYTE...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.