Skip to main content

Python object as namespace notation. A simple subset of python that represents objects as namespaces.

Project description

What is Ponn?

JSON, the ubiquitous JavaScript Object Notation is a nice format for data exchange between machines, specially when humans may want to peek and quickly understand the exchanged messages. JSON, however, is very limited in its data types and it can be brittle to write by hand (no comments, no trailing commas, no multi-line strings, etc).

Ponn is a simple subset of Python that represents objects as namespaces. It is designed to be a more human-friendly alternative to JSON for configuration files and sacrifices performance for ease of use (at least when used by Python developers). It is very similar to PON, the Python Object Notation, but it represents objects as Python modules instead of top-level dictionaries.

Ponn is also more flexible than PON (and JSON) in that it allows for more complex data structures and some limited form of programming (e.g. you can use variables, conditional statements, function execution, and imports).

Ponn files are thus valid Python files that can be executed and imported as modules. This module simply provides a secure way to load Ponn files by checking if they contain only valid Ponn constructs before executing them.

Usage

To load a Ponn file, simply use the load function from the ponn module:

from ponn import load

config = load("config.ponn")
print(config["database"]["host"])  # Accessing some inner structure 

The load() function returns a dictionary object mapping top-level variable names to their values.

The Ponn subset

Ponn supports a subset of Python syntax that is safe to execute from untrusted sources. Ponn can execute imports and call arbitrary functions from a whitelist, so obviously the security depends on the security of the functions you allow to be executed.

What is allowed in Ponn files?

  • Comments
  • Variable assignments (except dunder variables)
  • All python literals (strings, numbers, lists, tuples, dictionaries, sets, etc)
  • Function calls to whitelisted functions
  • Imports of whitelisted modules
  • Conditional statements (if, elif, else) and ternary expressions
  • Match statements
  • Class definitions using dataclasses (and without methods)

What is forbidden in Ponn files?

  • Any function calls not explicitly allowed in the whitelist
  • Attribute access to dunder methods or attributes (e.g. __import__, __class__, etc)
  • Some built-in functions that are not in the whitelist (e.g. open, eval, exec, etc)
  • Some functions are converted to no-op to prevent side effects (e.g. print).
  • Loops and comprehensions (for, while) are not allowed to prevent unbounded execution.
  • Function definitions (def, lambda) are not allowed to prevent infinite recursion.
  • Class definitions of arbitrary classes.
  • The with statement is not allowed to prevent resource leaks.
  • The async and await keywords.

How does it work?

Ponn uses the ast module to parse the Ponn file and check if it contains only valid Ponn syntax. If the syntax is valid, it executes the file in a restricted environment where only the whitelisted functions and modules are available. Some builtins are modified to prevent side effects or trigger errors when considered unsafe (e.g. print is converted to a no-op, open raises an error, etc).

How safe is it?

Probably safe. Ponn is designed to be a safe subset of Python, but given the extreme flexibility and reflective capabilities of Python, it is very difficult to guarantee absolute safety. Many sandobox implementations have been broken in the past, but they all aimed for a more generic sandboxed environment execution (turing complete subsets of Python, not only flexible data representation).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ponn-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ponn-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file ponn-0.1.0.tar.gz.

File metadata

  • Download URL: ponn-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.22

File hashes

Hashes for ponn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 346f8a29dd8d9a89d16a6d01372a93d018b930f2055dc4b2c6d0b6d87f008ed2
MD5 abf50b1c7f817e4e92315d8bda910cd8
BLAKE2b-256 c2999ab0ec9d5eea499585c7e5e4005a623d8c2b515a78c5f1d7bc08553ccf90

See more details on using hashes here.

File details

Details for the file ponn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ponn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.22

File hashes

Hashes for ponn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a0af2650f5ff505c270fa4cfed0b501a4d86710f39121f9ea93ce6b3ececcbe
MD5 59df1fb80db214f4ad7c3ae91e911051
BLAKE2b-256 721f04575195a9cc675dc81482f5d87b303129b4eae83dadee6c038c80421214

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page