Skip to main content

Pre-process Compiler Directives

Normal usage:

import CompilerDirectives

import Constants # file containing our constant definitions

Examples

These are shipped in CompilerDiectives\tests

Main.py:

# Main.py

DIAG_PY2_SAVE = True

import CompilerDirectives
CompilerDirectives.PreProcess.stale_pyc_del(__file__)

# import os
# CompilerDirectives.PreProcess.stale_pyc_del(os.path.dirname(__file__))

import Constants
import Module1

Constants.py:

# Constants.py

#DEFINE CONST1 'big'
#DEFINE CONST2 'wide'
#DEF CONST3 3
#DEF CONST4 4

Module1.py:

# Module1.py

print("#DEFINE/#UNDEF test")
#DEFINE a 1
#DEFINE b 'bee', c 3, d 4, e 5, f 6
#UNDEF c, e, k
#COMP 'print(' +  str(__consts__) + ')'

print("#Replace Constants test")
x = a
y = b
print(f"{x=}, {y=}")

print("#INCLUDE test")
if True:
    #INCLUDE 'module2.py', 'non-existant.py'

print("#IFDEF test")
#IFDEF a
print("#IFDEF a - a exists - correct")
#ELSE
print("#IFDEF a - a does not exist - wrong")
#ENDIF

print("#IFNDEF test")
#IFNDEF a
print("#IFNDEF a - a does not exist - wrong")
#ELSE
print("#IFNDEF a - a exists - correct")
#ENDIF

print("#IF / ELIF test")
#IF False
print("#IF False - wrong")
#ELIF False
print("#ELIF False - wrong")
#ELIF True
print("#ELIF True - correct")
#ELIF True
print("#ELIF True(2) - wrong")
#ENDIF

print("#COMP (compile time) test")
#COMP 'print("Hello ' + CONST1 + ' ' + CONST2 +  ' world")'

Module2.py:

# Module2.py

print("in Module 2a... - correct")

print("in Module 2b... - correct")

Diagnostics:

For diagnostic purposes an intermediate modified file can be saved in the __pycache__ directory by setting the diagnostic variable:

DIAG_PY2_SAVE = True

Setting this to True also enables display of speed testing in the Python Console. The writing of this file is automatically disabled when Python is run with __debug__ False.

This can be either done before the import:

DIAG_PY2_SAVE = True

import CompilerDirectives

Or after:

import CompilerDirectives

CompilerDirectives.PreProcess.DIAG_PY2_SAVE = True

Compiler directives:

Define constant

#DEFINE / #DEF <const> <value>

Undefine constant

#UNDEF <const>

Compile time conditionals

#IF <expr> / #ELIF <expr> / #ENDIF

Compile time definition conditionals

#IFDEF <expr> / #IFNDEF <expr> / #ENDIF

Include file

#INCLUDE <file>

‘Compile’ time expression evaluation, remove ‘#COMP’ from resulting line

#COMP <expr>

Multiple items can be included on the same line delimited by ‘,’ (commas):

#DEF <const1> <value1>, <const2> <value2>

#UNDEF <const1>, <const2>

#INCLUDE <file1>, <file2>

Constant replacement:

After CompilerDirectives is imported all further imports will be subject to replacement of the Constants with their respective values if previously defined. All files imported can contain further constant definitions and un-definitions which will be added to / removed from the application level ‘global’ Constants list.

Constant replacement will happen only once for each import with the resulting byte-code being saved in the *.pyc __pycache__ file. When a modified file is imported which contains any compiler directives all *.pyc files in the __pycache__ directory which are older than that imported file will be deleted so as to force their re-compilation the next time they are accessed.

Development:

For development purposes the following can be used to remove all earlier *.pyc files than whenever the test module was last modified so that these then get recreated and hence pre-processed.:

import CompilerDirectives

CompilerDirectives.PreProcess.stale_pyc_del(__file__)

Alternatively to remove all earlier *.pyc files on each run so that CompilerDirectives runs every time regardless of whether the test module is modified or not pass just the directory.:

import os

import CompilerDirectives

CompilerDirectives.PreProcess.stale_pyc_del(os.path.dirname(__file__))

Known issue:

There is a known issue with tokenize.tokenize not indenting & dedenting correctly when a #CompilerDirective (comment) is the first line after aforementioned indentation change.

Metadata

Release files for CompilerDirectives 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CompilerDirectives 1.0.5
File Size Uploaded Provenance Actions
compilerdirectives-1.0.5.tar.gz 6.7 kB Without provenance Details for compilerdirectives-1.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for CompilerDirectives 1.0.5
File Interpreter ABI Platform Actions
compilerdirectives-1.0.5-py3-none-any.whl Python 3 none any Details for compilerdirectives-1.0.5-py3-none-any.whl

Total release size: 13.6 kB

Release files / compilerdirectives-1.0.5.tar.gz

Download URL compilerdirectives-1.0.5.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5e718b8040694e213e5fa0090e910e24ae3c9b04755095bbd2222373c781adc2
BLAKE2b-256 checksum
How to use checksums
84591fc5af20f8657f180c668ed0ea3dc5fac23a196ceedb8b5ab16a8bc130c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / compilerdirectives-1.0.5-py3-none-any.whl

Download URL compilerdirectives-1.0.5-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9193b744931f89f16b801a758a39292b61dd26f413bbf72786cb0451f029e1f8
BLAKE2b-256 checksum
How to use checksums
bb704dd275bd7900a0ee38e9fd46a56fe6f3bbb9b9d87b90be1dc12a7effadd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for CompilerDirectives 1.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page