Skip to content

Commit

Permalink
Develop fix macro python (arras-energy#95)
Browse files Browse the repository at this point in the history
Signed-off-by: David P. Chassin <dchassin@slac.stanford.edu>
Signed-off-by: David P. Chassin <david.chassin@me.com>
Signed-off-by: Mitchell Victoriano <mitchell.victoriano@gmail.com>
  • Loading branch information
David P. Chassin authored and MitchellAV committed Oct 4, 2023
1 parent 7b92b90 commit 052934d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
20 changes: 17 additions & 3 deletions source/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
18 changes: 9 additions & 9 deletions source/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 18 additions & 9 deletions source/python_embed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions source/python_embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 052934d

Please sign in to comment.