diff --git a/source/globals.cpp b/source/globals.cpp index eff05d604..53c71d8a1 100644 --- a/source/globals.cpp +++ b/source/globals.cpp @@ -333,12 +333,26 @@ void pythonpath_init(const char *name,const char *value) const char * pythonpath = getenv("PYTHONPATH"); if ( pythonpath != NULL ) { - int len = snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%.*s",(int)sizeof(global_pythonpath)-2,value); - snprintf(global_pythonpath+len,sizeof(global_pythonpath)-1-len,":%.*s",(int)(sizeof(global_pythonpath)-2),pythonpath); + if ( value != NULL ) + { + int len = snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%.*s",(int)sizeof(global_pythonpath)-2,value); + snprintf(global_pythonpath+len,sizeof(global_pythonpath)-1-len,":%.*s",(int)(sizeof(global_pythonpath)-2),pythonpath); + } + else + { + snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%.*s",(int)sizeof(global_pythonpath)-2,pythonpath); + } } else { - snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%.*s",(int)sizeof(global_pythonpath)-2,value); + if ( value != NULL ) + { + snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%.*s",(int)sizeof(global_pythonpath)-2,value); + } + else + { + snprintf(global_pythonpath,sizeof(global_pythonpath)-1,"%s",""); + } } } /* Add more derivative directories here */ diff --git a/source/load.cpp b/source/load.cpp index 64bb78ed5..8b5e969b3 100755 --- a/source/load.cpp +++ b/source/load.cpp @@ -8341,7 +8341,7 @@ int GldLoader::process_macro(char *line, int size, char *_filename, int linenum) strcpy(line,"\n"); return TRUE; } - int rc = my_instance->subcommand("%s/" PACKAGE "-%s",global_execdir,strchr(line,'#')+1); + int rc = my_instance->subcommand("%s/" PACKAGE "-%s",getenv("GLD_BIN"),strchr(line,'#')+1); if ( rc != 127 ) { strcpy(line,"\n"); @@ -8546,10 +8546,14 @@ bool GldLoader::load_import(const char *from, char *to, int len) } strcpy(to,from); char *glmext = strrchr(to,'.'); - if ( glmext == NULL ) + if ( glmext == NULL || (unsigned long long)glmext < (unsigned long long)strrchr(to,'/') ) + { strcat(to,".glm"); + } else + { strcpy(glmext,".glm"); + } char load_options[1024] = ""; char load_options_var[64]; snprintf(load_options_var,sizeof(load_options_var)-1,"%s_load_options",ext); @@ -8589,12 +8593,8 @@ bool GldLoader::load_import(const char *from, char *to, int len) STATUS GldLoader::load_python(const char *filename) { - extern PyObject *gridlabd_module; - if ( gridlabd_module == NULL ) - { - python_embed_init(0,NULL); - } - return python_embed_import(filename,global_pythonpath) == NULL ? FAILED : SUCCESS; + python_embed_init(0,NULL); + return python_embed_run_file(filename) ? FAILED : SUCCESS; } /** Load a file @@ -8629,7 +8629,7 @@ STATUS GldLoader::loadall(const char *fname) if ( ext != NULL && ( strcmp(ext,".py") == 0 || strncmp(ext,".py ",4) == 0 || strncmp(ext,".py\t",4) == 0 ) ) { - return load_python(fname); + return load_python(fname) ? SUCCESS : FAILED; } // non-glm file diff --git a/source/python_embed.cpp b/source/python_embed.cpp index 3328d6f14..5cd9b70c3 100644 --- a/source/python_embed.cpp +++ b/source/python_embed.cpp @@ -8,17 +8,20 @@ PyObject *gridlabd_module = NULL; void python_embed_init(int argc, const char *argv[]) { - program = Py_DecodeLocale(argv[0],NULL); - Py_SetProgramName(program); - Py_Initialize(); - main_module = PyModule_GetDict(PyImport_AddModule("__main__")); - if ( main_module == NULL ) + if ( gridlabd_module == NULL ) { - throw_exception("python_embed_init(argc=%d,argv=(%s,...)): unable to load module __main__ module",argc,argv?argv[0]:"NULL"); + program = Py_DecodeLocale(argv[0],NULL); + Py_SetProgramName(program); + Py_Initialize(); + main_module = PyModule_GetDict(PyImport_AddModule("__main__")); + if ( main_module == NULL ) + { + throw_exception("python_embed_init(argc=%d,argv=(%s,...)): unable to load module __main__ module",argc,argv?argv[0]:"NULL"); + } + gridlabd_module = PyInit_gridlabd(); + Py_INCREF(gridlabd_module); + Py_INCREF(main_module); } - gridlabd_module = PyInit_gridlabd(); - Py_INCREF(gridlabd_module); - Py_INCREF(main_module); } void *python_loader_init(int argc, const char **argv) @@ -53,6 +56,12 @@ void python_reset_stream(PyObject *pModule, const char *stream_name) if ( pStringIO ) Py_DECREF(pStringIO); } +int python_embed_run_file(const char *filename) +{ + FILE *fp = fopen(filename,"rb"); + return fp ? PyRun_SimpleFile(fp,filename) : -1; +} + PyObject *python_embed_import(const char *module, const char *path) { // fix module search path diff --git a/source/python_embed.h b/source/python_embed.h index 6cc2da451..108a4f766 100644 --- a/source/python_embed.h +++ b/source/python_embed.h @@ -28,6 +28,7 @@ void python_embed_init(int argc, const char *argv[]); void *python_loader_init(int argc, const char **argv); void python_embed_term(); PyObject *python_embed_import(const char *module, const char *path=NULL); +int python_embed_run_file(const char *filename); bool python_embed_call(PyObject *pModule, const char *name, const char *vargsfmt, va_list varargs, void *result); std::string python_eval(const char *command); bool python_parser(const char *line=NULL, void *context = NULL);