(patch for Bash) Bash with embedded Python

William Park opengeometry at yahoo.ca
Sun Feb 16 01:53:13 EST 2003


Finally, Python fully embedded in Bash.  This is around 1.4 Meg.  Not
quite an Emacs, but... :-)  Relevant code piece is included at the end.
Just append it to ./builtins/eval.def, and recompile.  On my Linux
machine, I did
    make CFLAGS="-DEMBED_PYTHON -I/usr/local/include/python2.2"
	 LDFLAGS="-L/usr/local/lib/python2.2 -L/usr/local/lib/python2.2/config
		  -Xlinker -export-dynamic"
	 LOCAL_LIBS="-lpython2.2 -lpthread -lutil -lm"

Usage:

    sendtopy [-f01] arg...

    Send the command-line arguments to Python.  By default, concatenate
    the arguments into a string, and send it to Python via
    PyRun_SimpleString().  It goes through shell parser first; so, it's
    quoting hell.  If '-f' option is specified, then the arguments are
    files, and send the file contents to Python via PyRun_SimpleFile().
    '-0' option stops Python; and, '-1' option initializes Python,
    though not needed.

Example:

    1.	sendtopy print 1.0 + 2.0, \"a\" + \"b\"

    2.	sendtopy import math
	sendtopy print math.pi

    3.	sendtopy A = 1
	sendtopy B = 5.0
	sendtopy print A, B, A+B

    4.  exec 8<<EOF
	C = 9.0
	print A, B, C
	EOF

	sendtopy -f /dev/fd/8

    5.  cat >z <<EOF
	C = 10.0
	print A, B, C, math.pi
	EOF

	sendtopy -f z
	
Use at your risk, and enjoy. 

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution for data management and processing. 



$BUILTIN sendtopy
$FUNCTION sendtopy_builtin
$SHORT_DOC sendtopy [-f01] arg...
Send the command-line arguments to Python.  By default, concatenate the
arguments into a string, and send it to Python via PyRun_SimpleString().
It goes through shell parser first; so, it's quoting hell.  If '-f' option
is specified, then the arguments are files, and send the file contents to
Python via PyRun_SimpleFile().  '-0' option stops Python; and, '-1' option
initializes Python, though not needed.
$END


/* Fully embedded Python.  With Python compiled and installed to /usr/local as
 * usual, Bash can be compiled with
 *
 *	./configure 
 *	make CFLAGS="-DEMBED_PYTHON -I/usr/local/include/python2.2"
 *	     LDFLAGS="-L/usr/local/lib/python2.2 -L/usr/local/lib/python2.2/config
 *	              -Xlinker -export-dynamic"
 *	     LOCAL_LIBS="-lpython2.2 -lpthread -lutil -lm"
 *
 * where '-lpython2.2 -lpthread -lutil -lm' were determined from Python's
 * Makefile, and '-Xlinker -export-dynamic' were determined from 
 *	import distutils.sysconfig
 *	distutils.sysconfig.get_config_var('LINKFORSHARED')
 * as described in Python documentation for embedding.
 *
 * If you don't want Python, then simply do
 *	./configure
 *	make
 *
 * --William Park <opengeometry at yahoo.ca>
 */


#ifdef EMBED_PYTHON
#include "Python.h"
#endif

int
sendtopy_builtin (list)
    register WORD_LIST *list;
{
#ifdef EMBED_PYTHON
    char *arg;
    int opt, flag, out;

    flag = 0;

    reset_internal_getopt ();
    while ((opt = internal_getopt (list, "f01")) != -1) {
	switch (opt) {
	case 'f':
	    flag = 1;
	    break;
	case '1':
	    Py_Initialize();
	    break;
	case '0':
	    Py_Finalize();
	    break;
	default:
	    builtin_usage ();
	    return (EX_USAGE);
	}
    }
    list = loptend;

    if (list == 0) 		/* 0 argument */
	return (EXECUTION_SUCCESS);

    if (! Py_IsInitialized())
	Py_Initialize();

    if (flag) {			/* send file */
	FILE *fd;

	for ( ; list; list = list->next) {
	    arg = list->word->word;
	    if ((fd = fopen (arg, "r")) == NULL) {
		builtin_error ("cannot open file `%s'", arg);
		return (EXECUTION_FAILURE);
	    }
	    out = PyRun_SimpleFile (fd, arg);
	    fclose (fd);
	    if (out)
		return (EXECUTION_FAILURE);
	}
    } else {			/* send string */
	arg = string_list (list);
	out = PyRun_SimpleString (arg);
	free (arg);
	if (out)
	    return (EXECUTION_FAILURE);
    }

    fflush (stdout);
    return (EXECUTION_SUCCESS);
#endif	/* EMBED_PYTHON */
}




More information about the Python-list mailing list