patch to Modules/posixmodule.c for Java JVM compatibility

Bill Janssen janssen at parc.xerox.com
Fri Jul 9 16:14:43 EDT 1999


Guido,

I've been experimenting with loading Python into a Java VM.  This is
part of expanded ILU capabilities for the next ILU release -- one will
be able to build class libraries in C, C++, or Python that can then be
loaded into any of the other ILU languages.  I've found a small
problem with the Modules/posixmodule.c code, which assumes that the
"environ" space provided by the POSIX environment is writable.  I'm
not sure of what the POSIX standard says about this, but I find that
it's not true with Java (JDK 1.2 on Solaris 2.6), and Python will
segfault when loaded via a native library into the JVM.  By doing a
copy when forming the os.environ dictionary, this can be avoided.
Here's a patch, which will apply to 1.5.2.  Enjoy.

Bill

*** 1.1	1999/07/06 02:21:15
--- Modules/posixmodule.c	1999/07/09 20:03:06
***************
*** 278,284 ****
--- 278,287 ----
  convertenviron()
  {
  	PyObject *d;
+ 	char buf[1024];
  	char **e;
+ 	char *copy, *p;
+ 
  	d = PyDict_New();
  	if (d == NULL)
  		return NULL;
***************
*** 287,302 ****
  	/* XXX This part ignores errors */
  	for (e = environ; *e != NULL; e++) {
  		PyObject *v;
! 		char *p = strchr(*e, '=');
  		if (p == NULL)
  			continue;
  		v = PyString_FromString(p+1);
  		if (v == NULL)
  			continue;
  		*p = '\0';
! 		(void) PyDict_SetItemString(d, *e, v);
  		*p = '=';
  		Py_DECREF(v);
  	}
  #if defined(PYOS_OS2)
      {
--- 290,314 ----
  	/* XXX This part ignores errors */
  	for (e = environ; *e != NULL; e++) {
  		PyObject *v;
! 		if (strlen(*e) < sizeof(buf)) {
! 		    copy = buf;
! 		    strcpy (copy, *e);
! 		} else {
! 		    copy = malloc(strlen(*e) + 1);
! 		    strcpy (copy, *e);
! 		}
! 		p = strchr(copy, '=');
  		if (p == NULL)
  			continue;
  		v = PyString_FromString(p+1);
  		if (v == NULL)
  			continue;
  		*p = '\0';
! 		(void) PyDict_SetItemString(d, copy, v);
  		*p = '=';
  		Py_DECREF(v);
+ 		if (copy != buf)
+ 		    free(copy);
  	}
  #if defined(PYOS_OS2)
      {




More information about the Python-list mailing list