Browse Source

Make Python problems non-fatal

Suren A. Chilingaryan 8 years ago
parent
commit
195c28f307
4 changed files with 41 additions and 23 deletions
  1. 9 10
      pcilib/pci.c
  2. 26 10
      pcilib/py.c
  3. 1 0
      pcilib/py.h
  4. 5 3
      views/transform.c

+ 9 - 10
pcilib/pci.c

@@ -147,9 +147,8 @@ pcilib_t *pcilib_open(const char *device, const char *model) {
 	
 	err = pcilib_init_py(ctx);
 	if (err) {
-	    pcilib_error("Error (%i) initializing python subsystem", err);
-	    pcilib_close(ctx);
-	    return NULL;
+	    pcilib_warning("Error (%i) initializing python subsystem", err);
+	    pcilib_free_py(ctx);
 	}
 
 	ctx->alloc_reg = PCILIB_DEFAULT_REGISTER_SPACE;
@@ -191,22 +190,22 @@ pcilib_t *pcilib_open(const char *device, const char *model) {
 
 	if (!ctx->model)
 	    ctx->model = strdup(model?model:"pci");
-	    
+
 	err = pcilib_py_add_script_dir(ctx, NULL);
 	if (err) {
-	    pcilib_error("Error (%i) add script path to python path", err);
-	    pcilib_close(ctx);
-	    return NULL;
+	    pcilib_warning("Error (%i) add script path to python path", err);
+	    pcilib_free_py(ctx);
+	    err = 0;
 	}
-	
-	
+
+
 	xmlerr = pcilib_init_xml(ctx, ctx->model);
 	if ((xmlerr)&&(xmlerr != PCILIB_ERROR_NOTFOUND)) {
 	    pcilib_error("Error (%i) initializing XML subsystem for model %s", xmlerr, ctx->model);
 	    pcilib_close(ctx);
 	    return NULL;
 	}
-	
+
 
 	    // We have found neither standard model nor XML
 	if ((err)&&(xmlerr)) {

+ 26 - 10
pcilib/py.c

@@ -135,19 +135,19 @@ int pcilib_init_py(pcilib_t *ctx) {
 		
     ctx->py->main_module = PyImport_AddModule("__parser__");
     if (!ctx->py->main_module) {
-	pcilib_python_error("Error importing python parser");
+	pcilib_python_warning("Error importing python parser");
         return PCILIB_ERROR_FAILED;
     }
 
     ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module);
     if (!ctx->py->global_dict) {
-	pcilib_python_error("Error locating global python dictionary");
+	pcilib_python_warning("Error locating global python dictionary");
         return PCILIB_ERROR_FAILED;
     }
 
     PyObject *pywrap = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER);
     if (!pywrap) {
-	pcilib_python_error("Error importing pcilib python wrapper");
+	pcilib_python_warning("Error importing pcilib python wrapper");
 	return PCILIB_ERROR_FAILED;
     }
 	
@@ -158,7 +158,7 @@ int pcilib_init_py(pcilib_t *ctx) {
     Py_XDECREF(mod_name);
 	
     if (!ctx->py->pcilib_pywrap) {
-	pcilib_python_error("Error initializing python wrapper");
+	pcilib_python_warning("Error initializing python wrapper");
         return PCILIB_ERROR_FAILED;
     }
 #endif /* HAVE_PYTHON */
@@ -173,6 +173,8 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) {
     PyObject *pydict, *pystr, *pyret = NULL;
     char *script_dir;
 
+    if (!ctx->py) return 0;
+
     const char *model_dir = getenv("PCILIB_MODEL_DIR");
     if (!model_dir) model_dir = PCILIB_MODEL_DIR;
 
@@ -188,13 +190,13 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) {
 
     pypath = PySys_GetObject("path");
     if (!pypath) {
-	pcilib_python_error("Can't get python path");
+	pcilib_python_warning("Can't get python path");
 	return PCILIB_ERROR_FAILED;
     }
 
     pynewdir = PyUnicode_FromString(script_dir);
     if (!pynewdir) {
-	pcilib_python_error("Can't create python string");
+	pcilib_python_warning("Can't create python string");
 	return PCILIB_ERROR_MEMORY;
     }
     
@@ -224,7 +226,7 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) {
     Py_DECREF(pynewdir);
 
     if (err) {
-	pcilib_python_error("Can't add directory (%s) to python path", script_dir);
+	pcilib_python_warning("Can't add directory (%s) to python path", script_dir);
 	return err;
     }
 #endif /* HAVE_PYTHON */
@@ -267,6 +269,7 @@ int pcilib_py_load_script(pcilib_t *ctx, const char *script_name) {
     PyObject* pymodule;
     pcilib_script_t *module = NULL;
 
+    if (!ctx->py) return 0;
 
     char *module_name = strdupa(script_name);
     if (!module_name) return PCILIB_ERROR_MEMORY;
@@ -304,7 +307,12 @@ int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_
     PyObject *dict;
     PyObject *pystr;
     pcilib_script_t *module;
-	
+
+    if (!ctx->py) {
+	if (mode_ret) *mode_ret = mode;
+	return 0;
+    }
+
     HASH_FIND_STR(ctx->py->script_hash, script_name, module);
 
     if(!module) {
@@ -343,7 +351,9 @@ pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *va
 
     long ival;
     double fval;
-	
+
+    if (!ctx->py) return NULL;
+
     gstate = PyGILState_Ensure();
     switch(val->type) {
      case PCILIB_TYPE_LONG:
@@ -383,7 +393,9 @@ int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py
     int err = 0;
     PyObject *pyval = (PyObject*)pval;
     PyGILState_STATE gstate;
-	
+
+    if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
+
     gstate = PyGILState_Ensure();
     if (PyLong_Check(pyval)) {
         err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
@@ -536,6 +548,8 @@ int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *va
     char *code;
     PyObject* obj;
 
+    if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
+
     code = pcilib_py_parse_string(ctx, codestr, value);
     if (!code) {
         pcilib_error("Failed to parse registers in the code: %s", codestr);
@@ -572,6 +586,8 @@ int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func
     PyObject *pyval = NULL, *pyret;
     pcilib_script_t *module = NULL;
 
+    if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
+
     HASH_FIND_STR(ctx->py->script_hash, script_name, module);
 
     if (!module) {

+ 1 - 0
pcilib/py.h

@@ -5,6 +5,7 @@
 #include <pcilib/error.h>
 
 #define pcilib_python_error(...)	pcilib_log_python_error(__FILE__, __LINE__, PCILIB_LOG_DEFAULT, PCILIB_LOG_ERROR, __VA_ARGS__)
+#define pcilib_python_warning(...)	pcilib_log_python_error(__FILE__, __LINE__, PCILIB_LOG_DEFAULT, PCILIB_LOG_WARNING, __VA_ARGS__)
 
 typedef struct pcilib_py_s pcilib_py_t;
 typedef void pcilib_py_object;

+ 5 - 3
views/transform.c

@@ -18,7 +18,7 @@ static pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, pcilib_
     pcilib_view_context_t *view_ctx;
     pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(ctx->views[view]);
 
-    if(v->script) {	
+    if (v->script) {
 	pcilib_access_mode_t mode = 0;
 		
 	err = pcilib_py_load_script(ctx, v->script);
@@ -39,11 +39,13 @@ static pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, pcilib_
 
 	if (!v->read_from_reg) v->read_from_reg = "read_from_register";
 	if (!v->write_to_reg) v->write_to_reg = "write_to_register";
+    } else if (!ctx->py) {
+	v->base.mode &= (~PCILIB_REGISTER_RW);
     }
-	
+
     view_ctx = (pcilib_view_context_t*)malloc(sizeof(pcilib_view_context_t));
     if (view_ctx) memset(view_ctx, 0, sizeof(pcilib_view_context_t));
-    
+
     return view_ctx;
 }