[Python-Dev] Pragma-style declaration syntax

M.-A. Lemburg mal@lemburg.com
Mon, 28 Aug 2000 20:57:26 +0200


Greg Wilson wrote:
> 
> > > Marc-Andre Lemburg:
> > > 1. Embed pragma declarations in comments:
> > >     #pragma: name = value
> > >
> > > 2. Reusing a Python keyword to build a new form of statement:
> > >     def name = value
> > >
> > > 3. Add a new keyword:
> > >     decl name = value
> 
> > Guido van Rossum:
> > I say add a new reserved word pragma and accept the consequences.
> > The other solutions are just too ugly.
> 
> Greg Wilson:
> Will pragma values be available at run-time, e.g. in a special
> module-level dictionary variable '__pragma__', so that:
> 
>     pragma "encoding" = "UTF8"
>     pragma "division" = "fractional"
> 
> has the same effect as:
> 
>     __pragma__["encoding"] = "UTF8"
>     __pragma__["division"] = "fractional"
> 
> If that's the case, would it be better to use the dictionary syntax?  Or
> does the special form simplify pragma detection so much as to justify
> adding new syntax?

Pragmas tell the compiler to make certain assumptions about the
scope they appear in. It may be useful have their values available
as __pragma__ dict too, but only for introspection purposes and
then only for objects which support the attribute.

If we were to use a convention such as your proposed dictionary
assignment for these purposes, the compiler would have to treat
these assignments in special ways. Adding a new reserved word is
much cleaner.

> Also, what's the effect of putting a pragma in the middle of a file,
> rather than at the top?  Does 'import' respect pragmas, or are they
> per-file?  I've seen Fortran files that start with 20 lines of:
> 
>     C$VENDOR PROPERTY DEFAULT
> 
> to disable any settings that might be in effect when the file is included
> in another, just so that the author of the include'd file could be sure of
> the semantics of the code he was writing.

The compiler will see the pragma definition as soon as it reaches
it during compilation. All subsequent compilation (up to where
the compilation block ends, i.e. up to module, function and class
boundaries) will be influenced by the setting.

This is in line with all other declarations in Python, e.g. those
of global variables, functions and classes.

Imports do not affect pragmas since pragmas are a compile
time thing.

Here are some possible applications of pragmas (just to toss in
a few ideas):

# Cause global lookups to be cached in function's locals for future
# reuse.
pragma globals = 'constant'

# Cause all Unicode literals in the current scope to be
# interpreted as UTF-8.
pragma encoding = 'utf-8'

# Use -OO style optimizations
pragma optimization = 2

# Default division mode
pragma division = 'float'

The basic syntax in the above examples is:

	"pragma" NAME "=" (NUMBER | STRING+)

It has to be that simple to allow the compiler use the information
at compilation time.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/