PYTHONPATH and embedding python

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Oct 10 08:49:04 CEST 2001


On Tue, 9 Oct 2001, Ken Seehof wrote:

> > ---
> >   char *path, *newpath;
> >
> >   path=Py_GetPath();
> >   newpath=strcat(path, ":."); // or ";.", possibly
> >   PySys_SetPath(newpath);
> >   free(newpath);
> > ---
>
> Yikes!  That doesn't look quite right to me.
>
> strcat(char *dest, char *src) returns its first argument after concatenating
> *src to it.  In your example, path points to static storage returned by
> Py_GetPath.  Your code proceeds to concatenate onto it (blasting away at
> static memory reserved for the python library), and then frees it (note that
> newpath==path at the end) which it doesn't have a right to do.
>
> You probably mean something like this:
>
> // untested code:
>   char *path, *newpath;
>   path=Py_GetPath();
>   newpath=new char[strlen(path)+4];
>   strcpy(newpath, path);
>   strcat(newpath, ":.");  // ":." for unix, or ";." for windows
>   PySys_SetPath(newpath);
>   free(newpath);

D'oh! Yeah, that's what I meant. I guess I never noticed anything because the
module I used to test it is this " big.

> from the docs:
> char* Py_GetPath ()
> Return the default module search path; this is computed from the program
> name (set by Py_SetProgramName() above) and some environment variables. The
> returned string consists of a series of directory names separated by a
> platform dependent delimiter character. The delimiter character is ":" on
> Unix, ";" on DOS/Windows, and "\n" (the ASCII newline character) on
> Macintosh. The returned string points into static storage; the caller should
> not modify its value. The value is available to Python code as the list
> sys.path, which may be modified to change the future search path for loaded
> modules.
>
> Hmm, I can't seem to find any documentation on PySys_SetPath.  I wonder if
> it automatically converts the delimiter (":" <=> ";").

An awful lot of C functions in Python/sysmodule.c aren't documented. But no,
it doesn't do anything to the passed string other than turn it into a list
based on the system-default path delimiter found in Include/osdefs.h.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>






More information about the Python-list mailing list