RE: How to embed Python in an application?

Hammond, Mark (MHammond@jm.cmutual.com.au)
Fri, 24 Mar 95 12:48:00 PST

> The point is that I have to be able to "register" the
> editor commands in such a way that they can be called
> from Python programs, exactly like in Tcl and REXX.
> This point is never touched in the documentation.

The best way to do this is to write an extension module in C. This
extension module can export "normal" methods, and can also expose objects,
each with their own methods.

eg, you may export some "environment" type methods (eg, StartEditor), and
maybe an "EditorApp" object (having methods like FileOpen - general app
stuff), and maybe "EditorFile" or "EditorView" objects - with methods like
"Search", "Indent" - whatever.

>
> I like the syntax and the rationale behind Python, so
> I would really like to use it, but I need some
> suggestions.
Maybe give an overview of how you would like to structure the Python
integration (ie, will Python become the core editor functionality, or will
just extensions to the editor be written in Python, etc. Then, we can give
some specific suggestions. I have embedded Python in a reasonably large
application (which includes a _really_ basic editor - autoindent is about
its only feature :)

One comment I will make is that Python has been a pleasure to embed! The
doco is a bit short in this area (although I believe 1.2 will address this
to some extent). But the general structure of Python means I have never
doubted my decision to use Python even once. As I come up with ideas I
never dreamt of before, it slips right into Python without any problems.

And maybe even more to the point - Guido is very responsive to any
limitations discovered, which means I have almost no hacks to work around
Python - only a few to work around MFC :-)
>
> BTW, how small can the interpreter be made by eliminating
> "esoteric" modules? The part embedded in an editor should
> just get the basic stuff and file handling...

I dont know a specific size, but removing unwanted modules would not be a
problem.

Although, you should never underestimate the inventiveness of your clients.
eg, you may decide the socket module is not needed, but a client may try to
write an extension module that auto FTP's a file from a remote site, and
FTP's it back at save time.

I'd be more inclined to package a full Python. Another option here would be
to build the "esoteric" modules as Dynamically loadable modules, and to save
distribution size, you could make these modules an "optional extra"

What platforms will your editor run on?

Mark (MHammond@cmutual.com.au)