From 761dd04914d0bc92fc26bcf9654f94f6be6de74f Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 25 Jun 2024 11:06:47 -0700 Subject: [PATCH 1/3] Added Python functions lf.package_directory() and lf.source_director() --- python/include/pythontarget.h | 16 ++++++++++++++++ python/lib/pythontarget.c | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/python/include/pythontarget.h b/python/include/pythontarget.h index effbe0344..a828e0689 100644 --- a/python/include/pythontarget.h +++ b/python/include/pythontarget.h @@ -102,6 +102,22 @@ PyObject* py_schedule_copy(PyObject* self, PyObject* args); */ PyObject* py_request_stop(PyObject* self, PyObject* args); +/** + * @brief Return the source directory path (where the main .lf file is) as a string. + * @param self The lf object. + * @param args Empty. + * @return PyObject* A Python string. + */ +PyObject* py_source_directory(PyObject* self, PyObject* args); + +/** + * @brief Return the root project directory path as a string. + * @param self The lf object. + * @param args Empty. + * @return PyObject* A Python string. + */ +PyObject* py_package_directory(PyObject* self, PyObject* args); + ////////////////////////////////////////////////////////////// ///////////// Main function callable from Python code PyObject* py_main(PyObject* self, PyObject* args); diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index a485efaf5..3b1265678 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -162,6 +162,14 @@ PyObject* py_request_stop(PyObject* self, PyObject* args) { return Py_None; } +PyObject* py_source_directory(PyObject* self, PyObject* args) { + return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY); +} + +PyObject* py_package_directory(PyObject* self, PyObject* args) { + return PyUnicode_DecodeFSDefault(LF_PACKAGE_DIRECTORY); +} + /** * Parse Python's 'argv' (from sys.argv()) into a pair of C-style * 'argc' (the size of command-line parameters array) @@ -304,6 +312,8 @@ static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = {{"start", py_main, METH_ {"tag", py_lf_tag, METH_NOARGS, NULL}, {"tag_compare", py_tag_compare, METH_VARARGS, NULL}, {"request_stop", py_request_stop, METH_NOARGS, "Request stop"}, + {"source_directory", py_source_directory, METH_NOARGS, "Source directory path for .lf file"}, + {"package_directory", py_package_directory, METH_NOARGS, "Root package directory path"}, {NULL, NULL, 0, NULL}}; /** From 193b6296d2878a886838676458a352748f7ea9f0 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 25 Jun 2024 11:29:06 -0700 Subject: [PATCH 2/3] Format --- python/lib/pythontarget.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index 3b1265678..440290478 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -162,9 +162,7 @@ PyObject* py_request_stop(PyObject* self, PyObject* args) { return Py_None; } -PyObject* py_source_directory(PyObject* self, PyObject* args) { - return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY); -} +PyObject* py_source_directory(PyObject* self, PyObject* args) { return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY); } PyObject* py_package_directory(PyObject* self, PyObject* args) { return PyUnicode_DecodeFSDefault(LF_PACKAGE_DIRECTORY); @@ -307,14 +305,15 @@ PyObject* py_main(PyObject* self, PyObject* py_args) { * @see schedule_copy * @see request_stop */ -static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = {{"start", py_main, METH_VARARGS, NULL}, - {"schedule_copy", py_schedule_copy, METH_VARARGS, NULL}, - {"tag", py_lf_tag, METH_NOARGS, NULL}, - {"tag_compare", py_tag_compare, METH_VARARGS, NULL}, - {"request_stop", py_request_stop, METH_NOARGS, "Request stop"}, - {"source_directory", py_source_directory, METH_NOARGS, "Source directory path for .lf file"}, - {"package_directory", py_package_directory, METH_NOARGS, "Root package directory path"}, - {NULL, NULL, 0, NULL}}; +static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = { + {"start", py_main, METH_VARARGS, NULL}, + {"schedule_copy", py_schedule_copy, METH_VARARGS, NULL}, + {"tag", py_lf_tag, METH_NOARGS, NULL}, + {"tag_compare", py_tag_compare, METH_VARARGS, NULL}, + {"request_stop", py_request_stop, METH_NOARGS, "Request stop"}, + {"source_directory", py_source_directory, METH_NOARGS, "Source directory path for .lf file"}, + {"package_directory", py_package_directory, METH_NOARGS, "Root package directory path"}, + {NULL, NULL, 0, NULL}}; /** * Define the Lingua Franca module. From ae9e5f492dc0a5bdd7ea26781526d5b7044bd795 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 2 Jul 2024 13:41:34 -0400 Subject: [PATCH 3/3] Test for undefined constants --- python/lib/pythontarget.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index 440290478..3a3e7b2b4 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -162,10 +162,24 @@ PyObject* py_request_stop(PyObject* self, PyObject* args) { return Py_None; } -PyObject* py_source_directory(PyObject* self, PyObject* args) { return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY); } +PyObject* py_source_directory(PyObject* self, PyObject* args) { +#ifndef LF_SOURCE_DIRECTORY + // This should not occur. + PyErr_SetString(PyExc_RuntimeError, "LF_SOURCE_DIRECTORY constant is not defined."); + return NULL; +#else + return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY); +#endif +} PyObject* py_package_directory(PyObject* self, PyObject* args) { +#ifndef LF_PACKAGE_DIRECTORY + // This should not occur. + PyErr_SetString(PyExc_RuntimeError, "LF_PACKAGE_DIRECTORY constant is not defined."); + return NULL; +#else return PyUnicode_DecodeFSDefault(LF_PACKAGE_DIRECTORY); +#endif } /**