diff options
| author | rin <rin@NetBSD.org> | 2017-11-17 16:11:11 +0000 |
|---|---|---|
| committer | rin <rin@NetBSD.org> | 2017-11-17 16:11:11 +0000 |
| commit | 5b042ff1c6fb488a60b35fea15bef04fc4ffc7cd (patch) | |
| tree | e6d308edffcf18c659373a4950ec0808ac9f7a5a /external/bsd/tre/dist/python | |
| parent | 051d2cffd12a265f76b86ad11c7a720e6ecb2e19 (diff) | |
Import tre from https://github.com/laurikari/tre as of 10171117:
- tre_reg*b() functions are added, that take bytes literally.
- minor bug fixes
Diffstat (limited to 'external/bsd/tre/dist/python')
| -rw-r--r-- | external/bsd/tre/dist/python/setup.py | 4 | ||||
| -rw-r--r-- | external/bsd/tre/dist/python/setup.py.in | 39 | ||||
| -rw-r--r-- | external/bsd/tre/dist/python/tre-python.c | 86 |
3 files changed, 108 insertions, 21 deletions
diff --git a/external/bsd/tre/dist/python/setup.py b/external/bsd/tre/dist/python/setup.py index b420a4cac37..00d1e6c7561 100644 --- a/external/bsd/tre/dist/python/setup.py +++ b/external/bsd/tre/dist/python/setup.py @@ -3,6 +3,7 @@ # Copyright (c) 2009 Ville Laurikari <ville@laurikari.net> # +import distutils.sysconfig from distutils.core import setup, Extension import sys import os @@ -14,7 +15,8 @@ include_dirs = ["../lib"] libraries = ["tre"] if sys.platform == "win32": - data_files = ["tre.dll"] + # Place tre.dll in site-packages, next to tre.pyd. + data_files = [(distutils.sysconfig.get_python_lib(), ["tre.dll"])] include_dirs += ["../win32"] shutil.copy("../win32/Release/tre.dll", ".") libraries = ["../win32/Release/tre"] diff --git a/external/bsd/tre/dist/python/setup.py.in b/external/bsd/tre/dist/python/setup.py.in new file mode 100644 index 00000000000..681b613f7e2 --- /dev/null +++ b/external/bsd/tre/dist/python/setup.py.in @@ -0,0 +1,39 @@ +# setup.py - Builds and installs the TRE Python language bindings module +# +# Copyright (c) 2009 Ville Laurikari <ville@laurikari.net> +# + +import distutils.sysconfig +from distutils.core import setup, Extension +import sys +import os +import shutil + +version = "@TRE_VERSION@" +data_files = [] +include_dirs = ["../lib"] +libraries = ["tre"] + +if sys.platform == "win32": + # Place tre.dll in site-packages, next to tre.pyd. + data_files = [(distutils.sysconfig.get_python_lib(), ["tre.dll"])] + include_dirs += ["../win32"] + shutil.copy("../win32/Release/tre.dll", ".") + libraries = ["../win32/Release/tre"] + +setup(name = "tre", + version = version, + description = "Python module for TRE", + author = "Ville Laurikari", + author_email = "ville@laurikari.net", + license = "2-clause BSD", + url = "http://laurikari.net/tre/", + data_files = data_files, + ext_modules = [Extension("tre", + sources = ["tre-python.c"], + define_macros = [("HAVE_CONFIG_H", None)], + include_dirs = include_dirs, + libraries = libraries + ), + ], + ) 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()) |
