From 22eaba4518f85902489a35dbac0fd97654b59d43 Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Thu, 23 Mar 2006 01:42:05 +0000 Subject: added message bundle functionality (pyext.Bundle class) enable compiled-only scripts (without .py) small optimizations and fixes some optimizations and py reload fix better error message for reload with invalid args enable module packages (module/__init__.py[co]), now also for Max svn path=/trunk/; revision=4750 --- externals/grill/py/source/py.cpp | 11 +- externals/grill/py/source/pybase.cpp | 207 ++++++++++++++++++++++++----------- externals/grill/py/source/pybase.h | 1 - externals/grill/py/source/pyext.cpp | 4 - 4 files changed, 146 insertions(+), 77 deletions(-) (limited to 'externals/grill/py/source') diff --git a/externals/grill/py/source/py.cpp b/externals/grill/py/source/py.cpp index 3ef823a0..c773ffcb 100644 --- a/externals/grill/py/source/py.cpp +++ b/externals/grill/py/source/py.cpp @@ -156,12 +156,8 @@ pyobj::pyobj(int argc,const t_atom *argv) *pt = 0; } - if(*modnm) { - char dir[1024]; - GetModulePath(modnm,dir,sizeof(dir)); - AddToPath(dir); + if(*modnm) ImportModule(modnm); - } else ImportModule(NULL); } @@ -219,7 +215,8 @@ void pyobj::m_set(int argc,const t_atom *argv) ++argv,--argc; if(sn) { - if(!module || !strcmp(sn,PyModule_GetName(module))) { +// if(!module || !strcmp(sn,PyModule_GetName(module))) + { ImportModule(sn); Register(GetRegistry(REGNAME)); } @@ -244,7 +241,7 @@ void pyobj::m_set(int argc,const t_atom *argv) void pyobj::m_help() { post(""); - post("%s %s - python script object, (C)2002-2005 Thomas Grill",thisName(),PY__VERSION); + post("%s %s - python script object, (C)2002-2006 Thomas Grill",thisName(),PY__VERSION); #ifdef FLEXT_DEBUG post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__); #endif diff --git a/externals/grill/py/source/pybase.cpp b/externals/grill/py/source/pybase.cpp index f20328d8..f4762fbf 100644 --- a/externals/grill/py/source/pybase.cpp +++ b/externals/grill/py/source/pybase.cpp @@ -101,7 +101,7 @@ void pybase::lib_setup() post(""); post("------------------------------------------------"); post("py/pyext %s - python script objects",PY__VERSION); - post("(C)2002-2005 Thomas Grill - http://grrrr.org/ext"); + post("(C)2002-2006 Thomas Grill - http://grrrr.org/ext"); post(""); post("using Python %s",Py_GetVersion()); @@ -220,6 +220,7 @@ FLEXT_LIB_SETUP(py,pybase::lib_setup) pybase::pybase() : module(NULL) + , dict(NULL) #ifdef FLEXT_THREADS , thrcount(0) , shouldexit(false),stoptick(0) @@ -387,7 +388,7 @@ void pybase::OpenEditor() char *editor = getenv("EDITOR"); - if(!editor || !strcmp(editor, "/usr/bin/nano") || !strcmp(editor, "/usr/bin/pico") || !strcmp(editor, "/usr/bin/vi")) { + if(!editor) { // || !strcmp(editor, "/usr/bin/nano") || !strcmp(editor, "/usr/bin/pico") || !strcmp(editor, "/usr/bin/vi")) { // no environment variable or console text editor found ... use idle instead (should have come with Python) editor = "idle"; } @@ -424,44 +425,163 @@ void pybase::SetArgs() delete[] sargv; } +static bool getmodulesub(const char *mod,char *dir,int len,char *ext) +{ +#if FLEXT_SYS == FLEXT_SYS_PD + char *name; + int fd = open_via_path("",mod,ext,dir,&name,len,0); + if(fd > 0) { + FLEXT_ASSERT(name && *name); + close(fd); + } + else { + // look for mod/__init__.py + std::string tmp(mod); + int l = tmp.size(); + tmp += "/__init__"; + fd = open_via_path("",tmp.c_str(),ext,dir,&name,len,0); + if(fd > 0) { + FLEXT_ASSERT(name && *name); + close(fd); + // we must remove the module name from dir + char *t = dir+strlen(dir)-l; + FLEXT_ASSERT(!strcmp(mod,t) && t[-1] == '/'); + t[-1] = 0; + } + else + name = NULL; + } + + // if dir is current working directory... name points to dir + if(dir == name) strcpy(dir,"."); +#elif FLEXT_SYS == FLEXT_SYS_MAX + short path; + long type; + char smod[1024]; + strcpy(smod,mod); + strcat(smod,ext); + bool ok = !locatefile_extended(smod,&path,&type,&type,0); + if(ok) + // convert pathname to unix style + path_topathname(path,NULL,smod); + else { + // do path/file.ext combinations work at all under Max? + strcpy(smod,mod); + + short path; + type = 'fold'; + ok = !locatefile_extended(smod,&path,&type,&type,1); + if(ok) { + // convert pathname to unix style (including trailing slash) + path_topathname(path,NULL,smod); + char *end = smod+strlen(smod); + strcpy(end,mod); + strcat(end,"/__init__"); + strcat(end,ext); + + // check if file is really existing: try to open it + FILE *f = fopen(smod,"r"); + if(f) { + *end = 0; // clear module part ... we only need base path + fclose(f); + } + else + ok = false; + } + } + + if(ok) { + // convert path into slash style needed for Python +#if 1 + path_nameconform(smod,dir,PATH_STYLE_SLASH,PATH_TYPE_ABSOLUTE); +#else +#if FLEXT_OS == FLEXT_OS_WIN + char *colon = NULL; +#else + char *colon = strchr(smod,':'); +#endif + if(colon) { + *colon = 0; + strcpy(dir,"/Volumes/"); + strcat(dir,smod); + strcat(dir,colon+1); + } + else + strcpy(dir,smod); +#endif + return true; + } + else + // not found + return false; +#else +#pragma message("Not implemented"); + return false; +#endif +} + +static bool getmodulepath(const char *mod,char *dir,int len) +{ + return + getmodulesub(mod,dir,len,".py") || + getmodulesub(mod,dir,len,".pyc") || + getmodulesub(mod,dir,len,".pyo"); +} + bool pybase::ImportModule(const char *name) { if(name) { - if(modname == name) return true; - modname = name; + if(modname == name) { + // module with the same name is already loaded + if(module) return true; + } + else + modname = name; } else modname.clear(); + + UnimportModule(); return ReloadModule(); } void pybase::UnimportModule() { - if(!module) return; + if(module) { + FLEXT_ASSERT(dict && module_obj && module_dict); - FLEXT_ASSERT(dict && module_obj && module_dict); + Py_DECREF(module); - Py_DECREF(module); + // reference count to module is not 0 here, altough probably the last instance was unloaded + // Python retains one reference to the module all the time + // we don't care - // reference count to module is not 0 here, altough probably the last instance was unloaded - // Python retains one reference to the module all the time - // we don't care - - module = NULL; - dict = NULL; + module = NULL; + dict = NULL; + } } bool pybase::ReloadModule() { - bool ok = false; - SetArgs(); PyObject *newmod; - if(modname.length()) - newmod = module - ?PyImport_ReloadModule(module) - :PyImport_ImportModule((char *)modname.c_str()); + if(modname.length()) { + if(module) + newmod = PyImport_ReloadModule(module); + else { + // search in module path + char dir[1024]; + if(getmodulepath(modname.c_str(),dir,sizeof(dir))) { + AddToPath(dir); + newmod = PyImport_ImportModule((char *)modname.c_str()); + } + else { + PyErr_SetString(PyExc_ImportError,"Module not found in path"); + newmod = NULL; + } + } + } else { // if no module name given, take py module newmod = module_obj; @@ -470,58 +590,15 @@ bool pybase::ReloadModule() if(!newmod) { // unload faulty module - if(module) UnimportModule(); + UnimportModule(); + return false; } else { Py_XDECREF(module); module = newmod; dict = PyModule_GetDict(module); // borrowed - ok = true; + return true; } - return ok; -} - -void pybase::GetModulePath(const char *mod,char *dir,int len) -{ -#if FLEXT_SYS == FLEXT_SYS_PD - // uarghh... pd doesn't show its path for extra modules - - char *name; - int fd = open_via_path("",mod,".py",dir,&name,len,0); - if(fd > 0) close(fd); - else name = NULL; - - // if dir is current working directory... name points to dir - if(dir == name) strcpy(dir,"."); -#elif FLEXT_SYS == FLEXT_SYS_MAX - // how do i get the path in Max/MSP? - short path; - long type; - char smod[1024]; - strcat(strcpy(smod,mod),".py"); - if(!locatefile_extended(smod,&path,&type,&type,-1)) { -#if FLEXT_OS == FLEXT_OS_WIN - path_topathname(path,NULL,dir); -#else - // convert pathname to unix style - path_topathname(path,NULL,smod); - char *colon = strchr(smod,':'); - if(colon) { - *colon = 0; - strcpy(dir,"/Volumes/"); - strcat(dir,smod); - strcat(dir,colon+1); - } - else - strcpy(dir,smod); -#endif - } - else - // not found - *dir = 0; -#else - *dir = 0; -#endif } void pybase::AddToPath(const char *dir) diff --git a/externals/grill/py/source/pybase.h b/externals/grill/py/source/pybase.h index 3645a7af..6b661268 100644 --- a/externals/grill/py/source/pybase.h +++ b/externals/grill/py/source/pybase.h @@ -53,7 +53,6 @@ protected: AtomList args; void AddCurrentPath(flext_base *o); - void GetModulePath(const char *mod,char *dir,int len); void SetArgs(); bool OutObject(flext_base *ext,int o,PyObject *obj); diff --git a/externals/grill/py/source/pyext.cpp b/externals/grill/py/source/pyext.cpp index d7449200..5c573bd2 100644 --- a/externals/grill/py/source/pyext.cpp +++ b/externals/grill/py/source/pyext.cpp @@ -162,10 +162,6 @@ pyext::pyext(int argc,const t_atom *argv,bool sig): } } - char dir[1024]; - GetModulePath(modnm,dir,sizeof(dir)); - AddToPath(dir); - ImportModule(modnm); } else -- cgit v1.2.1