[PATCH] sys.modify_argv()

Dave Cinege dcinege at psychosis.com
Sat Aug 25 04:19:34 CEST 2001


keywords: modify argv change process listing argv[0] ps output

This patch adds a new feature to the sys module, modify_argv().
As you might have guessed this allows one to change to absolute
argv values of the python process itself. (It is of great wonder
to me why this functionality is not already available...)

I have intentionally left the patch with minimal sanity
checking. Ignorance of detail could lead to dangerous
results. Stronger bountries may be desirable in a final
implementation.

The patch is against Python 2.1.1. Adding this function to another
module or placing it in your own module should be a trvial task.
It is self contained using Py_GetArgcArgv() to grab the location
of argv, already present in the python core.

This patch has been submitted for inclusion into core python.

Documentation on it's use is in the patch itself.

I hereby place this code into the public domain. 

'Diesel' Dave 'Kill a Cop' Cinege
-------------- next part --------------
*** sysmodule.c.orig	Thu Aug 16 21:37:00 2001
--- sysmodule.c	Fri Aug 24 21:48:06 2001
***************
*** 180,185 ****
--- 180,263 ----
  implementation.";
  
  static PyObject *
+ sys_modify_argv(PyObject *self, PyObject *args)
+ {	
+ 	extern void Py_GetArgcArgv(int *argc, char ***argv);
+ 	
+ 	char **argv, *argv_new;
+ 	int start = 1, amt = 1;
+ 	int i, argc, end;
+ 	
+ 	if (!PyArg_ParseTuple(args, "s|ii", &argv_new, &start, &amt))
+ 		return NULL;    
+ 
+ 	if (amt <= 0) {
+ 		PyErr_SetString(PyExc_ValueError, "amount must be greater then zero");
+ 		return NULL;
+ 	}
+ 	
+ 	Py_GetArgcArgv(&argc,&argv);
+ 	
+ 	if (start < 0) {
+ 		PyErr_SetString(PyExc_ValueError, "start must be greater or equal to zero");
+ 		return NULL;
+ 	}
+ 	if (start >= argc) {
+ 		PyErr_SetString(PyExc_ValueError, "start must not exceed argc");
+ 		return NULL;
+ 	}
+ 
+ 	if ((amt - start) > argc)	// Never run beyond last allocated element
+ 		amt = (start - argc);
+ 	end = start + amt - 1;
+ 
+ 	for (i = start; i <= end; i++) {
+ 		char *p_argv;
+ 		
+ 		for (p_argv = argv[i]; *p_argv != 0;) {	// Zero old string 
+ 			*p_argv = 0;  p_argv++;
+ 		}
+ 		// Replace old with new, if there is a new
+ 		for (p_argv = argv[i]; *argv_new != 0 && *argv_new != ',';) {	
+ 			*p_argv = *argv_new;
+ 			p_argv++; argv_new++;
+ 		}
+ 		if (*argv_new == ',')	argv_new++;	// Next if there is new remaining.
+ 	}
+ 
+ 	return Py_None;
+ }
+ 
+ static char modify_argv_doc[] =
+ "modify_argv(string,[start],[amount])\n\
+ \n\
+ Modify the absolute argv (process listing) elements of the python process.\n\
+ Element values are set to zero, then refilled if a replacement value\n\
+ is present. Note: sys.argv will not be changed.\n\
+ \n\
+ string: Comma seperated string of element replacement values.\n\
+ 	Lack of a replacement value for an element will only clear\n\
+ 	the element.\n\
+ start:	argv element to begin with. Default: 1\n\
+ amount:	amount of elements to change. Never exceeds argc. Default: 1\n\
+ \n\
+ WARNING: Unexpected or dangerous results can occur if replacment values\n\
+          exceed the length of the orginal elements.\n\
+ \n\
+ Examples -\n\
+ sys.modify_argv('script') OR ('script',1) OR ('script',1,1)\n\
+ Before:	/usr/local/bin/python ./t.py argument1 argument2 argument3 argument4\n\
+ After:	/usr/local/bin/python script argument1 argument2 argument3 argument4\n\
+ \n\
+ sys.modify_argv('Python',0, len(sys.argv) + 1)\n\
+ Before:	/usr/local/bin/python ./t.py argument1 argument2 argument3 argument4\n\
+ After:	Python\n\
+ \n\
+ sys.modify_argv('script,,[hidden],arg3',1,4)\n\
+ Before: /usr/local/bin/python ./t.py argument1 argument2 argument3 argument4\n\
+ After:	/usr/local/bin/python script           [hidden]  arg3      argument4";
+ 
+ static PyObject *
  sys_setdefaultencoding(PyObject *self, PyObject *args)
  {
  	char *encoding;
***************
*** 415,420 ****
--- 493,499 ----
  #ifdef USE_MALLOPT
  	{"mdebug",	sys_mdebug, 1},
  #endif
+ 	{"modify_argv", sys_modify_argv, 1, modify_argv_doc},
  	{"setdefaultencoding", sys_setdefaultencoding, 1,
  	 setdefaultencoding_doc}, 
  	{"setcheckinterval",	sys_setcheckinterval, 1,


More information about the Python-list mailing list