summaryrefslogtreecommitdiff
path: root/external/bsd/tre/dist/python/tre-python.c
diff options
context:
space:
mode:
Diffstat (limited to 'external/bsd/tre/dist/python/tre-python.c')
-rw-r--r--external/bsd/tre/dist/python/tre-python.c86
1 files changed, 66 insertions, 20 deletions
diff --git a/external/bsd/tre/dist/python/tre-python.c b/external/bsd/tre/dist/python/tre-python.c
index bbb24edfadd..0738e5d6e98 100644
--- a/external/bsd/tre/dist/python/tre-python.c
+++ b/external/bsd/tre/dist/python/tre-python.c
@@ -337,9 +337,18 @@ PyTrePattern_search(TrePatternObject *self, PyObject *args)
char *targ;
size_t tlen;
- if (!PyArg_ParseTuple(args, "SO!|i:match", &pstring, &TreFuzzynessType,
+ if (PyTuple_Size(args) > 0 && PyUnicode_Check(PyTuple_GetItem(args, 0)))
+ {
+ if (!PyArg_ParseTuple(args, "UO!|i:search", &pstring, &TreFuzzynessType,
&fz, &eflags))
- return NULL;
+ return NULL;
+ }
+ else
+ {
+ if (!PyArg_ParseTuple(args, "SO!|i:search", &pstring, &TreFuzzynessType,
+ &fz, &eflags))
+ return NULL;
+ }
mo = newTreMatchObject();
if (mo == NULL)
@@ -347,22 +356,35 @@ PyTrePattern_search(TrePatternObject *self, PyObject *args)
nsub = self->rgx.re_nsub + 1;
pm = PyMem_New(regmatch_t, nsub);
- if (pm != NULL)
- {
- mo->am.nmatch = nsub;
- mo->am.pmatch = pm;
- }
- else
+ if (!pm)
{
- /* XXX */
Py_DECREF(mo);
- return NULL;
+ return PyErr_NoMemory();
}
- targ = PyString_AsString(pstring);
- tlen = PyString_Size(pstring);
+ mo->am.nmatch = nsub;
+ mo->am.pmatch = pm;
- rc = tre_reganexec(&self->rgx, targ, tlen, &mo->am, fz->ap, eflags);
+ if (PyUnicode_Check(pstring))
+ {
+ Py_ssize_t len = PyUnicode_GetSize(pstring);
+ wchar_t *buf = calloc(sizeof(wchar_t), len);
+ if(!buf)
+ {
+ Py_DECREF(mo);
+ return PyErr_NoMemory();
+ }
+ PyUnicode_AsWideChar(pstring, buf, len);
+ rc = tre_regawnexec(&self->rgx, buf, len, &mo->am, fz->ap, eflags);
+ free(buf);
+ }
+ else
+ {
+ targ = PyString_AsString(pstring);
+ tlen = PyString_Size(pstring);
+
+ rc = tre_reganexec(&self->rgx, targ, tlen, &mo->am, fz->ap, eflags);
+ }
if (PyErr_Occurred())
{
@@ -392,7 +414,7 @@ PyTrePattern_search(TrePatternObject *self, PyObject *args)
static PyMethodDef TrePattern_methods[] = {
{ "search", (PyCFunction)PyTrePattern_search, METH_VARARGS,
- "try to match against given string, returning " TRE_MODULE ".match object "
+ "try to search in the given string, returning " TRE_MODULE ".match object "
"or None on failure" },
{NULL, NULL}
};
@@ -445,7 +467,7 @@ static PyTypeObject TrePatternType = {
};
static TrePatternObject *
-newTrePatternObject(PyObject *args)
+newTrePatternObject()
{
TrePatternObject *self;
@@ -460,19 +482,43 @@ static PyObject *
PyTre_ncompile(PyObject *self, PyObject *args)
{
TrePatternObject *rv;
- char *pattern;
+ PyUnicodeObject *upattern = NULL;
+ char *pattern = NULL;
int pattlen;
int cflags = 0;
int rc;
- if (!PyArg_ParseTuple(args, "s#|i:compile", &pattern, &pattlen, &cflags))
- return NULL;
+ if (PyTuple_Size(args) > 0 && PyUnicode_Check(PyTuple_GetItem(args, 0)))
+ {
+ if (!PyArg_ParseTuple(args, "U|i:compile", &upattern, &cflags))
+ return NULL;
+ }
+ else
+ {
+ if (!PyArg_ParseTuple(args, "s#|i:compile", &pattern, &pattlen, &cflags))
+ return NULL;
+ }
- rv = newTrePatternObject(args);
+ rv = newTrePatternObject();
if (rv == NULL)
return NULL;
- rc = tre_regncomp(&rv->rgx, (char*)pattern, pattlen, cflags);
+ if (upattern != NULL)
+ {
+ Py_ssize_t len = PyUnicode_GetSize(upattern);
+ wchar_t *buf = calloc(sizeof(wchar_t), len);
+ if(!buf)
+ {
+ Py_DECREF(rv);
+ return PyErr_NoMemory();
+ }
+ PyUnicode_AsWideChar(upattern, buf, len);
+ rc = tre_regwncomp(&rv->rgx, buf, len, cflags);
+ free(buf);
+ }
+ else
+ rc = tre_regncomp(&rv->rgx, (char*)pattern, pattlen, cflags);
+
if (rc != REG_OK)
{
if (!PyErr_Occurred())