473,416 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 2423
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.