[PD-cvs] externals/grill/py/source py.cpp, 1.34, 1.35 pybase.cpp, 1.13, 1.14 pybase.h, 1.10, 1.11 pyext.cpp, 1.45, 1.46
Thomas Grill
xovo at users.sourceforge.net
Thu Mar 23 02:42:07 CET 2006
Update of /cvsroot/pure-data/externals/grill/py/source
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24934/source
Modified Files:
py.cpp pybase.cpp pybase.h pyext.cpp
Log Message:
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
Index: pybase.h
===================================================================
RCS file: /cvsroot/pure-data/externals/grill/py/source/pybase.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** pybase.h 12 Dec 2005 00:18:42 -0000 1.10
--- pybase.h 23 Mar 2006 01:42:05 -0000 1.11
***************
*** 54,58 ****
void AddCurrentPath(flext_base *o);
- void GetModulePath(const char *mod,char *dir,int len);
void SetArgs();
--- 54,57 ----
Index: pybase.cpp
===================================================================
RCS file: /cvsroot/pure-data/externals/grill/py/source/pybase.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** pybase.cpp 12 Dec 2005 00:18:42 -0000 1.13
--- pybase.cpp 23 Mar 2006 01:42:05 -0000 1.14
***************
*** 102,106 ****
post("------------------------------------------------");
post("py/pyext %s - python script objects",PY__VERSION);
! post("(C)2002-2005 Thomas Grill - http://grrrr.org/ext");
post("");
post("using Python %s",Py_GetVersion());
--- 102,106 ----
post("------------------------------------------------");
post("py/pyext %s - python script objects",PY__VERSION);
! post("(C)2002-2006 Thomas Grill - http://grrrr.org/ext");
post("");
post("using Python %s",Py_GetVersion());
***************
*** 221,224 ****
--- 221,225 ----
pybase::pybase()
: module(NULL)
+ , dict(NULL)
#ifdef FLEXT_THREADS
, thrcount(0)
***************
*** 388,392 ****
char *editor = getenv("EDITOR");
! 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";
--- 389,393 ----
char *editor = getenv("EDITOR");
! 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";
***************
*** 425,436 ****
}
bool pybase::ImportModule(const char *name)
{
if(name) {
! if(modname == name) return true;
! modname = name;
}
else
modname.clear();
return ReloadModule();
}
--- 426,546 ----
}
+ 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) {
! // module with the same name is already loaded
! if(module) return true;
! }
! else
! modname = name;
}
else
modname.clear();
+
+ UnimportModule();
return ReloadModule();
}
***************
*** 438,466 ****
void pybase::UnimportModule()
{
! if(!module) return;
!
! FLEXT_ASSERT(dict && module_obj && module_dict);
! 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
! 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());
else {
// if no module name given, take py module
--- 548,586 ----
void pybase::UnimportModule()
{
! if(module) {
! FLEXT_ASSERT(dict && module_obj && module_dict);
! 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
! module = NULL;
! dict = NULL;
! }
}
bool pybase::ReloadModule()
{
SetArgs();
PyObject *newmod;
! 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
***************
*** 471,475 ****
if(!newmod) {
// unload faulty module
! if(module) UnimportModule();
}
else {
--- 591,596 ----
if(!newmod) {
// unload faulty module
! UnimportModule();
! return false;
}
else {
***************
*** 477,526 ****
module = newmod;
dict = PyModule_GetDict(module); // borrowed
! ok = 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
}
--- 598,603 ----
module = newmod;
dict = PyModule_GetDict(module); // borrowed
! return true;
}
}
Index: pyext.cpp
===================================================================
RCS file: /cvsroot/pure-data/externals/grill/py/source/pyext.cpp,v
retrieving revision 1.45
retrieving revision 1.46
diff -C2 -d -r1.45 -r1.46
*** pyext.cpp 12 Dec 2005 14:42:54 -0000 1.45
--- pyext.cpp 23 Mar 2006 01:42:05 -0000 1.46
***************
*** 163,170 ****
}
- char dir[1024];
- GetModulePath(modnm,dir,sizeof(dir));
- AddToPath(dir);
-
ImportModule(modnm);
}
--- 163,166 ----
Index: py.cpp
===================================================================
RCS file: /cvsroot/pure-data/externals/grill/py/source/py.cpp,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** py.cpp 11 Aug 2005 15:00:58 -0000 1.34
--- py.cpp 23 Mar 2006 01:42:05 -0000 1.35
***************
*** 157,166 ****
}
! if(*modnm) {
! char dir[1024];
! GetModulePath(modnm,dir,sizeof(dir));
! AddToPath(dir);
ImportModule(modnm);
- }
else
ImportModule(NULL);
--- 157,162 ----
}
! if(*modnm)
ImportModule(modnm);
else
ImportModule(NULL);
***************
*** 220,224 ****
if(sn) {
! if(!module || !strcmp(sn,PyModule_GetName(module))) {
ImportModule(sn);
Register(GetRegistry(REGNAME));
--- 216,221 ----
if(sn) {
! // if(!module || !strcmp(sn,PyModule_GetName(module)))
! {
ImportModule(sn);
Register(GetRegistry(REGNAME));
***************
*** 245,249 ****
{
post("");
! post("%s %s - python script object, (C)2002-2005 Thomas Grill",thisName(),PY__VERSION);
#ifdef FLEXT_DEBUG
post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
--- 242,246 ----
{
post("");
! post("%s %s - python script object, (C)2002-2006 Thomas Grill",thisName(),PY__VERSION);
#ifdef FLEXT_DEBUG
post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
More information about the Pd-cvs
mailing list