diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2020-06-29 22:37:58 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2020-06-29 22:37:58 (GMT) |
commit | d3d57aa61bb44fd0bdad460c8c173743ca808733 (patch) | |
tree | a427000cc26a7af91b8a1b42e80aeddd4212edd4 /plugins/pychrysalide/common/pathname.c | |
parent | d436818deded4064c5476111f980189836b360c7 (diff) |
Improved some common helpers inside the Python API.
Diffstat (limited to 'plugins/pychrysalide/common/pathname.c')
-rw-r--r-- | plugins/pychrysalide/common/pathname.c | 53 |
1 files changed, 34 insertions, 19 deletions
diff --git a/plugins/pychrysalide/common/pathname.c b/plugins/pychrysalide/common/pathname.c index 8a18ae3..c83c27d 100644 --- a/plugins/pychrysalide/common/pathname.c +++ b/plugins/pychrysalide/common/pathname.c @@ -40,6 +40,13 @@ +#define PYTHON_PATHNAME_DOC \ + "Path manipulations in Python for Chrysalide.\n" \ + "\n" \ + "There is no constructor for this class: its methods" \ + " are static methods only." + + /* Calcule le chemin relatif entre deux fichiers donnés. */ static PyObject *py_build_relative_filename(PyObject *, PyObject *); @@ -69,6 +76,15 @@ static PyObject *py_build_relative_filename(PyObject *self, PyObject *args) int ret; /* Bilan de lecture des args. */ char *relative; /* Chemin d'accès construit */ +#define BUILD_RELATIVE_FILENAME_METHOD PYTHON_METHOD_DEF \ +( \ + build_relative_filename, "ref, target", \ + METH_VARARGS | METH_STATIC, py, \ + "Compute the relative path between two files.\n" \ + "\n" \ + "Both arguments must be strings." \ +) + ret = PyArg_ParseTuple(args, "ss", &ref, &target); if (!ret) return NULL; @@ -104,6 +120,15 @@ static PyObject *py_build_absolute_filename(PyObject *self, PyObject *args) int ret; /* Bilan de lecture des args. */ char *relative; /* Chemin d'accès construit */ +#define BUILD_ABSOLUTE_FILENAME_METHOD PYTHON_METHOD_DEF \ +( \ + build_absolute_filename, "ref, target", \ + METH_VARARGS | METH_STATIC, py, \ + "Compute the absolute path for a file.\n" \ + "\n" \ + "Both arguments must be strings." \ +) + ret = PyArg_ParseTuple(args, "ss", &ref, &target); if (!ret) return NULL; @@ -117,9 +142,7 @@ static PyObject *py_build_absolute_filename(PyObject *self, PyObject *args) else { result = PyUnicode_FromString(relative); - free(relative); - } return result; @@ -142,31 +165,25 @@ static PyObject *py_build_absolute_filename(PyObject *self, PyObject *args) PyTypeObject *get_python_pathname_type(void) { static PyMethodDef py_pathname_methods[] = { - - { "build_relative_filename", py_build_relative_filename, - METH_VARARGS | METH_STATIC, - "build_relative_filename(ref, target, /)\n--\n\nCompute the relative path between two files." - }, - { "build_absolute_filename", py_build_absolute_filename, - METH_VARARGS | METH_STATIC, - "build_absolute_filename(ref, target, /)\n--\n\nCompute the absolute path for a file." - }, + BUILD_RELATIVE_FILENAME_METHOD, + BUILD_ABSOLUTE_FILENAME_METHOD, { NULL } - }; static PyTypeObject py_pathname_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "pychrysalide.core.pathname", - .tp_basicsize = sizeof(PyObject), + .tp_name = "pychrysalide.common.pathname", + .tp_basicsize = sizeof(PyObject), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT, + .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_doc = "Path manipulations in Python for Chrysalide.", + .tp_doc = PYTHON_PATHNAME_DOC, - .tp_methods = py_pathname_methods + .tp_methods = py_pathname_methods, + + .tp_new = no_python_constructor_allowed, }; @@ -196,8 +213,6 @@ bool ensure_python_pathname_is_registered(void) if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { - //type->tp_new = PyType_GenericNew; - if (PyType_Ready(type) != 0) return false; |