(patch for Bash) Bash with embedded Python

(patch for Bash) Bash with embedded Python

Post by William Par » Mon, 17 Feb 2003 15:54:16



[ This is a repost of the following article:                               ]

[ Subject: (patch for Bash) Bash with embedded Python                      ]
[ Newsgroups: comp.lang.python                                             ]

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.

--

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
 *

 */

#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 */

Quote:}

--

Linux solution for data management and processing.
 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. *newbie*: nonsense font after cat of binaries

3. (patch for Bash) Python command substitution in Bash

4. how to install type1 fonts?

5. (patch for Bash) Embedded web server + Bash

6. creator 3d vs elite 3d?

7. (patch for Bash) Python style [a:b] substring

8. Missing libpng for XEmacs

9. Bashing bash (Was Re: bash or user error with set -e and subshells)

10. Bash calls TCL, TCL calls Bash, 2nd Bash never reads input

11. (patch for Bash) GTK+2 + Bash

12. (patch for Bash) Bash + PostgreSQL + MySQL

13. (patch for Bash) XML + Bash