@@ -30,6 +30,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
*/
31
31
32
32
#include "pythontarget.h"
33
+ #include "util.h"
33
34
34
35
//////////// set Function(s) /////////////
35
36
/**
@@ -71,7 +72,7 @@ static PyObject* py_SET(PyObject *self, PyObject *args) {
71
72
72
73
generic_port_instance_struct * port = PyCapsule_GetPointer (p -> port , "port" );
73
74
if (port == NULL ) {
74
- fprintf ( stderr , "Null pointer received.\n " );
75
+ error_print ( "Null pointer received." );
75
76
exit (1 );
76
77
}
77
78
@@ -124,7 +125,7 @@ static PyObject* py_schedule(PyObject *self, PyObject *args) {
124
125
125
126
void * action = PyCapsule_GetPointer (act -> action ,"action" );
126
127
if (action == NULL ) {
127
- fprintf ( stderr , "Null pointer received.\n " );
128
+ error_print ( "Null pointer received." );
128
129
exit (1 );
129
130
}
130
131
@@ -170,7 +171,7 @@ static PyObject* py_schedule_copy(PyObject *self, PyObject *args) {
170
171
171
172
void * action = PyCapsule_GetPointer (act -> action ,"action" );
172
173
if (action == NULL ) {
173
- fprintf ( stderr , "Null pointer received.\n " );
174
+ error_print ( "Null pointer received." );
174
175
exit (1 );
175
176
}
176
177
@@ -320,7 +321,7 @@ port_iter(PyObject *self) {
320
321
321
322
generic_port_instance_struct * * cport = (generic_port_instance_struct * * )PyCapsule_GetPointer (port -> port ,"port" );
322
323
if (cport == NULL ) {
323
- fprintf ( stderr , "Null pointer received.\n " );
324
+ error_print ( "Null pointer received." );
324
325
exit (1 );
325
326
}
326
327
@@ -386,8 +387,7 @@ port_capsule_get_item(PyObject *self, PyObject *item) {
386
387
generic_port_instance_struct * * cport = (generic_port_instance_struct * * )PyCapsule_GetPointer (port -> port ,"port" );
387
388
if (cport == NULL )
388
389
{
389
- fprintf (stderr , "Null pointer received.\n" );
390
- exit (1 );
390
+ error_print_and_exit ("Null pointer received." );
391
391
}
392
392
393
393
//Py_INCREF(cport[index]->value);
@@ -433,9 +433,7 @@ port_capsule_assign_get_item(PyObject *self, PyObject *item, PyObject* value) {
433
433
static Py_ssize_t
434
434
port_length (PyObject * self ) {
435
435
generic_port_capsule_struct * port = (generic_port_capsule_struct * )self ;
436
- #ifdef VERBOSE
437
- printf ("Getting the length, which is %d.\n" , port -> width );
438
- #endif
436
+ DEBUG_PRINT ("Getting the length, which is %d." , port -> width );
439
437
return (Py_ssize_t )port -> width ;
440
438
}
441
439
@@ -834,15 +832,13 @@ PyObject* convert_C_port_to_py(void* port, int width) {
834
832
// Create the action struct in Python
835
833
PyObject * cap = PyObject_GC_New (generic_port_capsule_struct , & port_capsule_t );
836
834
if (cap == NULL ) {
837
- fprintf (stderr , "Failed to convert port.\n" );
838
- exit (1 );
835
+ error_print_and_exit ("Failed to convert port." );
839
836
}
840
837
841
838
// Create the capsule to hold the void* port
842
839
PyObject * capsule = PyCapsule_New (port , "port" , NULL );
843
840
if (capsule == NULL ) {
844
- fprintf (stderr , "Failed to convert port.\n" );
845
- exit (1 );
841
+ error_print_and_exit ("Failed to convert port." );
846
842
}
847
843
848
844
// Fill in the Python port struct
@@ -902,15 +898,13 @@ PyObject* convert_C_action_to_py(void* action) {
902
898
// Create the action struct in Python
903
899
PyObject * cap = PyObject_GC_New (generic_action_capsule_struct , & action_capsule_t );
904
900
if (cap == NULL ) {
905
- fprintf (stderr , "Failed to convert action.\n" );
906
- exit (1 );
901
+ error_print_and_exit ("Failed to convert action." );
907
902
}
908
903
909
904
// Create the capsule to hold the void* action
910
905
PyObject * capsule = PyCapsule_New (action , "action" , NULL );
911
906
if (capsule == NULL ) {
912
- fprintf (stderr , "Failed to convert action.\n" );
913
- exit (1 );
907
+ error_print_and_exit ("Failed to convert action." );
914
908
}
915
909
916
910
// Fill in the Python action struct
@@ -964,9 +958,7 @@ get_python_function(string module, string class, int instance_id, string func) {
964
958
is_initialized = 1 ;
965
959
}
966
960
967
- #ifdef VERBOSE
968
- printf ("Starting the function start()\n" );
969
- #endif
961
+ DEBUG_PRINT ("Starting the function start()." );
970
962
971
963
// Necessary PyObject variables to load the react() function from test.py
972
964
PyObject * pFileName , * pModule , * pDict , * pClasses , * pClass , * pFunc ;
@@ -976,9 +968,7 @@ get_python_function(string module, string class, int instance_id, string func) {
976
968
// Initialize the Python interpreter
977
969
Py_Initialize ();
978
970
979
- #ifdef VERBOSE
980
- printf ("Initialized the Python interpreter.\n" );
981
- #endif
971
+ DEBUG_PRINT ("Initialized the Python interpreter." );
982
972
983
973
// If the Python module is already loaded, skip this.
984
974
if (globalPythonModule == NULL ) {
@@ -989,8 +979,7 @@ get_python_function(string module, string class, int instance_id, string func) {
989
979
char cwd [PATH_MAX ];
990
980
if ( getcwd (cwd , sizeof (cwd )) == NULL )
991
981
{
992
- fprintf (stderr , "Failed to get the current working directory.\n" );
993
- exit (0 );
982
+ error_print_and_exit ("Failed to get the current working directory." );
994
983
}
995
984
996
985
wchar_t wcwd [PATH_MAX ];
@@ -999,15 +988,11 @@ get_python_function(string module, string class, int instance_id, string func) {
999
988
1000
989
Py_SetPath (wcwd );
1001
990
1002
- #ifdef VERBOSE
1003
- printf ("Loading module %s in %s.\n" , module , cwd );
1004
- #endif
991
+ DEBUG_PRINT ("Loading module %s in %s." , module , cwd );
1005
992
1006
993
pModule = PyImport_Import (pFileName );
1007
994
1008
- #ifdef VERBOSE
1009
- printf ("Loaded module %p.\n" , pModule );
1010
- #endif
995
+ DEBUG_PRINT ("Loaded module %p." , pModule );
1011
996
1012
997
// Free the memory occupied by pFileName
1013
998
Py_DECREF (pFileName );
@@ -1018,7 +1003,7 @@ get_python_function(string module, string class, int instance_id, string func) {
1018
1003
pDict = PyModule_GetDict (pModule );
1019
1004
if (pDict == NULL ) {
1020
1005
PyErr_Print ();
1021
- fprintf ( stderr , "Failed to load contents of module %s.\n " , module );
1006
+ error_print ( "Failed to load contents of module %s." , module );
1022
1007
return 1 ;
1023
1008
}
1024
1009
@@ -1040,7 +1025,7 @@ get_python_function(string module, string class, int instance_id, string func) {
1040
1025
pClasses = PyDict_GetItem (globalPythonModuleDict , list_name );
1041
1026
if (pClasses == NULL ){
1042
1027
PyErr_Print ();
1043
- fprintf ( stderr , "Failed to load class list \"%s\" in module %s.\n " , class , module );
1028
+ error_print ( "Failed to load class list \"%s\" in module %s." , class , module );
1044
1029
return 1 ;
1045
1030
}
1046
1031
@@ -1049,27 +1034,21 @@ get_python_function(string module, string class, int instance_id, string func) {
1049
1034
pClass = PyList_GetItem (pClasses , instance_id );
1050
1035
if (pClass == NULL ) {
1051
1036
PyErr_Print ();
1052
- fprintf ( stderr , "Failed to load class \"%s[%d]\" in module %s.\n " , class , instance_id , module );
1037
+ error_print ( "Failed to load class \"%s[%d]\" in module %s." , class , instance_id , module );
1053
1038
return 1 ;
1054
1039
}
1055
1040
1056
- #ifdef VERBOSE
1057
- printf ("Loading function %s.\n" , func );
1058
- #endif
1041
+ DEBUG_PRINT ("Loading function %s." , func );
1042
+
1059
1043
// Get the function react from test.py
1060
1044
pFunc = PyObject_GetAttrString (pClass , func );
1061
1045
1062
- #ifdef VERBOSE
1063
- printf ("Loaded function %p.\n" , pFunc );
1064
- #endif
1065
-
1046
+ DEBUG_PRINT ("Loaded function %p." , pFunc );
1066
1047
1067
1048
// Check if the funciton is loaded properly
1068
1049
// and if it is callable
1069
1050
if (pFunc && PyCallable_Check (pFunc )) {
1070
- #ifdef VERBOSE
1071
- printf ("Attempting to call function %s from class %s[%d].\n" , func , class , instance_id );
1072
- #endif
1051
+ DEBUG_PRINT ("Calling function %s from class %s[%d]." , func , class , instance_id );
1073
1052
Py_INCREF (pFunc );
1074
1053
return pFunc ;
1075
1054
}
@@ -1079,19 +1058,17 @@ get_python_function(string module, string class, int instance_id, string func) {
1079
1058
{
1080
1059
PyErr_Print ();
1081
1060
}
1082
- fprintf ( stderr , "Function %s was not found or is not callable.\n " , func );
1061
+ error_print ( "Function %s was not found or is not callable." , func );
1083
1062
}
1084
1063
Py_XDECREF (pFunc );
1085
1064
Py_DECREF (globalPythonModule );
1086
1065
}
1087
1066
else {
1088
1067
PyErr_Print ();
1089
- fprintf ( stderr , "Failed to load \"%s\"\n " , module );
1068
+ error_print ( "Failed to load \"%s\". " , module );
1090
1069
}
1091
1070
1092
- #ifdef VERBOSE
1093
- printf ("Done with start()\n" );
1094
- #endif
1071
+ DEBUG_PRINT ("Done with start()." );
1095
1072
1096
1073
if (is_initialized == 0 ) {
1097
1074
/* We are the first to initilize the Pyton interpreter. Destroy it when done. */
0 commit comments