Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup

  • Loading branch information...
commit 70093bd31e28cc038809b325de18fa727b92632c 1 parent 9f734fa
@awahlig authored
Showing with 182 additions and 53 deletions.
  1. +24 −0 LICENCE
  2. +81 −4 README.md
  3. +27 −24 python/pcre.py
  4. +50 −25 src/pcremodule.c
View
24 LICENCE
@@ -0,0 +1,24 @@
+Copyright (c) 2012, Arkadiusz Wahlig
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
85 README.md
@@ -1,4 +1,81 @@
-python-pcre
-===========
-
-Python bindings for PCRE regex engine.
+python-pcre
+===========
+
+Python bindings for PCRE regex engine.
+
+
+Requirements
+============
+
+* PCRE 8.30
+
+So far tested with Python 2.7 only.
+
+
+Building
+========
+
+TBD
+
+
+Installation
+============
+
+TBD
+
+
+Differences between python-pcre and re modules
+==============================================
+
+* slightly different regex syntax
+* `sub()`, `subn()`, `expand()` use `str.format()` instead of `\1`
+* returned group names are always unicode strings
+* `DEBUG`, `LOCALE`, `VERBOSE` flags are not supported
+* pattern caching is not supported
+
+Substitution example
+--------------------
+
+```python
+>>> pcre.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
+... 'static PyObject*\npy_{1}(void)\n{{',
+... 'def myfunc():')
+'static PyObject*\npy_myfunc(void)\n{'
+```
+Notice the {1} and escaped {{ in repl string.
+
+The built-in re module would use \1 and a raw string instead:
+`r'static PyObject*\npy_\1(void)\n{'`
+
+The arguments used in `str.format()` call are:
+* all groups starting from 0 (entire match) as positional arguments,
+* all named groups as keyword arguments.
+
+
+License
+=======
+
+Copyright (c) 2012, Arkadiusz Wahlig
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
51 python/pcre.py
@@ -1,27 +1,30 @@
-# PCRE - Perl-Compatible Regular Expressions
-#
-# Differences between pcre and re modules.
-# - slightly different regex syntax
-# - sub(), subn(), expand() use str.format() instead of \1 subst. (see below)
-# - returned group names are always unicode strings
-# - DEBUG, LOCALE, VERBOSE flags are not supported
-# - pattern caching is not supported
-#
-# Substitution example:
-# >>> pcre.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
-# ... 'static PyObject*\npy_{1}(void)\n{{',
-# ... 'def myfunc():')
-# 'static PyObject*\npy_myfunc(void)\n{'
-#
-# Notice the {1} and escaped {{ in repl string.
-#
-# The built-in re module would use \1 and a raw string instead:
-# r'static PyObject*\npy_\1(void)\n{'
-#
-# The arguments used in str.format() call are:
-# all groups starting from 0 (entire match) as positional arguments,
-# all named groups as keyword arguments.
-#
+""" python-pcre
+
+Copyright (c) 2012, Arkadiusz Wahlig
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+"""
import _pcre
View
75 src/pcremodule.c
@@ -1,5 +1,30 @@
-/* PCRE - Perl-Compatible Regular Expressions
- */
+/* python-pcre
+
+Copyright (c) 2012, Arkadiusz Wahlig
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
#include <Python.h>
#include <structmember.h>
@@ -67,48 +92,48 @@ group_name_by_index(pcre16 *code, int index, PyObject *def)
* Match
*/
-static void
-match_dealloc(PyMatchObject *self)
-{
- Py_XDECREF(self->pattern);
- Py_XDECREF(self->string);
- pcre16_free(self->ovector);
- Py_TYPE(self)->tp_free(self);
-}
-
static PyObject *
-match_getsubstr(PyMatchObject *self, Py_ssize_t index, PyObject *def)
+getsubstr(PyMatchObject *op, Py_ssize_t index, PyObject *def)
{
int pos;
- if (index < 0 || index > self->pattern->groups) {
+ if (index < 0 || index > op->pattern->groups) {
PyErr_SetString(PyExc_IndexError, "no such group");
return NULL;
}
- pos = self->ovector[index * 2];
+ pos = op->ovector[index * 2];
if (pos < 0) {
Py_INCREF(def);
return def;
}
- return PySequence_GetSlice(self->string, pos, self->ovector[index * 2 + 1]);
+ return PySequence_GetSlice(op->string, pos, op->ovector[index * 2 + 1]);
}
static PyObject *
-match_getsubstr_o(PyMatchObject *self, PyObject *index, PyObject *def)
+getsubstro(PyMatchObject *op, PyObject *index, PyObject *def)
{
Py_ssize_t i = -1;
if (PyInt_Check(index) || PyLong_Check(index))
i = PyInt_AsSsize_t(index);
- else if (self->pattern->groupindex) {
- index = PyDict_GetItem(self->pattern->groupindex, index);
+ else if (op->pattern->groupindex) {
+ index = PyDict_GetItem(op->pattern->groupindex, index);
if (index)
- return match_getsubstr_o(self, index, def);
+ return getsubstro(op, index, def);
}
- return match_getsubstr(self, i, def);
+ return getsubstr(op, i, def);
+}
+
+static void
+match_dealloc(PyMatchObject *self)
+{
+ Py_XDECREF(self->pattern);
+ Py_XDECREF(self->string);
+ pcre16_free(self->ovector);
+ Py_TYPE(self)->tp_free(self);
}
static PyObject *
@@ -120,17 +145,17 @@ match_group(PyMatchObject *self, PyObject *args)
size = PyTuple_GET_SIZE(args);
switch (size) {
case 0:
- result = match_getsubstr_o(self, Py_False, Py_None);
+ result = getsubstr(self, 0, Py_None);
break;
case 1:
- result = match_getsubstr_o(self, PyTuple_GET_ITEM(args, 0), Py_None);
+ result = getsubstro(self, PyTuple_GET_ITEM(args, 0), Py_None);
break;
default:
result = PyTuple_New(size);
if (result == NULL)
return NULL;
for (i = 0; i < size; ++i) {
- PyObject *item = match_getsubstr_o(self,
+ PyObject *item = getsubstro(self,
PyTuple_GET_ITEM(args, i), Py_None);
if (item == NULL) {
Py_DECREF(result);
@@ -207,7 +232,7 @@ match_groups(PyMatchObject *self, PyObject *args)
return NULL;
for (index = 1; index <= self->pattern->groups; ++index) {
- PyObject *item = match_getsubstr(self, index, def);
+ PyObject *item = getsubstr(self, index, def);
if (item == NULL) {
Py_DECREF(result);
return NULL;
@@ -235,7 +260,7 @@ match_groupdict(PyMatchObject *self, PyObject *args)
if (self->pattern->groupindex) {
pos = 0;
while (PyDict_Next(self->pattern->groupindex, &pos, &key, &value)) {
- value = match_getsubstr_o(self, value, def);
+ value = getsubstro(self, value, def);
if (value == NULL) {
Py_DECREF(dict);
return NULL;
Please sign in to comment.
Something went wrong with that request. Please try again.