[Python-Dev] pragmas as callbacks (original) (raw)

M.-A. Lemburg mal@lemburg.com
Tue, 29 Aug 2000 17:37:39 +0200


Greg Wilson wrote:

If a mechanism for providing meta-information about code is going to be added to Python, then I would like it to be flexible enough for developers to define/add their own. It's just like allowing developers to extend the type system with new classes, rather than handing them a fixed set of built-in types and saying, "Good luck". (Most commercial Fortran compilers take the second approach, by providing a bunch of inflexible, vendor-specific pragmas. It's a nightmare...)

I don't think that Python will move in that direction. pragmas are really only meant to add some form of meta-information to a Python source file which would otherwise have to be passed to the compiler in order to produce correct output. It's merely a way of defining compile time flags for Python modules which allow more flexible compilation.

Other tools might also make use of these pragmas, e.g. freeze, to allow inspection of a module without having to execute it.

I think that pragmas are essentially callbacks into the interpreter. When I put:

pragma encoding = "UTF-16" I am telling the interpreter to execute its 'setEncoding()' method right away.

pragmas have a different target: they tell the compiler (or some other non-executing tool) to make a certain assumption about the code it is currently busy compiling.

The compiler is not expected to execute any Python code when it sees a pragma, it will only set a few internal variables according to the values stated in the pragma or simply ignore it if the pragma uses an unknown key and then proceed with compiling.

So, why not present pragmas in that way? I.e., why not expose the Python interpreter as a callable object while the source is being parsed and compiled? I think that:

python.setEncoding("UTF-16") is readable, and can be extended in lots of well-structured ways by exposing exactly as much of the interpreter as is deemed safe. Arguments could be restricted to constants, or built-in operations on constants, to start with, without compromising future extensibility.

The natural place for these APIs would be the sys module... no need for an extra python module or object.

I'd rather not add complicated semantics to pragmas -- they should be able to set flags, but not much more.

-- Marc-Andre Lemburg


Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/