Skip to content

Commit c13696a

Browse files
committed
First commit functional with test.
0 parents  commit c13696a

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build*

CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) 2019 Jimmy Aguilar Mena
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
cmake_minimum_required(VERSION 3.0)
17+
18+
project(CPython)
19+
20+
find_package(PythonInterp)
21+
find_package(PythonLibs)
22+
23+
include_directories(${PYTHON_INCLUDE_DIRS})
24+
25+
add_executable (main main.c)
26+
target_link_libraries (main ${PYTHON_LIBRARIES} )
27+
28+
enable_testing()
29+
30+
add_test(test_5_4 ./main mylib multiply 4 5)
31+
set_tests_properties(test_5_4 PROPERTIES
32+
PASS_REGULAR_EXPRESSION "Result of call: 20"
33+
)

main.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
int
5+
main(int argc, char *argv[])
6+
{
7+
PyObject *pName, *pModule, *pFunc;
8+
PyObject *pArgs, *pValue;
9+
int i;
10+
11+
if (argc < 3) {
12+
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
13+
return 1;
14+
}
15+
16+
Py_Initialize();
17+
PyObject *sysmodule = PyImport_ImportModule("sys");
18+
PyObject *syspath = PyObject_GetAttrString(sysmodule, "path");
19+
PyList_Append(syspath, PyUnicode_FromString("."));
20+
Py_DECREF(syspath);
21+
Py_DECREF(sysmodule);
22+
23+
pName = PyUnicode_DecodeFSDefault(argv[1]);
24+
/* Error checking of pName left out */
25+
26+
pModule = PyImport_Import(pName);
27+
Py_DECREF(pName);
28+
29+
if (pModule != NULL) {
30+
pFunc = PyObject_GetAttrString(pModule, argv[2]);
31+
/* pFunc is a new reference */
32+
33+
if (pFunc && PyCallable_Check(pFunc)) {
34+
pArgs = PyTuple_New(argc - 3);
35+
for (i = 0; i < argc - 3; ++i) {
36+
pValue = PyLong_FromLong(atoi(argv[i + 3]));
37+
if (!pValue) {
38+
Py_DECREF(pArgs);
39+
Py_DECREF(pModule);
40+
fprintf(stderr, "Cannot convert argument\n");
41+
return 1;
42+
}
43+
/* pValue reference stolen here: */
44+
PyTuple_SetItem(pArgs, i, pValue);
45+
}
46+
pValue = PyObject_CallObject(pFunc, pArgs);
47+
Py_DECREF(pArgs);
48+
if (pValue != NULL) {
49+
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
50+
Py_DECREF(pValue);
51+
} else {
52+
Py_DECREF(pFunc);
53+
Py_DECREF(pModule);
54+
PyErr_Print();
55+
fprintf(stderr,"Call failed\n");
56+
return 1;
57+
}
58+
} else {
59+
if (PyErr_Occurred())
60+
PyErr_Print();
61+
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
62+
}
63+
Py_XDECREF(pFunc);
64+
Py_DECREF(pModule);
65+
} else {
66+
PyErr_Print();
67+
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
68+
return 1;
69+
}
70+
if (Py_FinalizeEx() < 0) {
71+
return 120;
72+
}
73+
return 0;
74+
}

mylib.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2019 Jimmy Aguilar Mena
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
def multiply(a,b):
17+
print("Will compute", a, "times", b)
18+
c = 0
19+
for i in range(0, a):
20+
c = c + b
21+
return c

0 commit comments

Comments
 (0)