c-preprocessor

python library for metaprogramming like it's the 1970s
Log | Files | Refs | README

commit 58c8d34fa4a368d94b4c5544d5cde18281af2364
Author: quantumish <freifeld.david@gmail.com>
Date:   Sun, 18 Jun 2023 23:58:25 -0700

Initial commit

PoC, just replaces any program with `print('hi')`

Diffstat:
Acpreprocessor/__init__.py | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Acprepross.pth | 1+
Apyproject.toml | 7+++++++
3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/cpreprocessor/__init__.py b/cpreprocessor/__init__.py @@ -0,0 +1,52 @@ +import codecs +import encodings +from encodings import utf_8 +import io + +def encode(input_string): + print(encoded_bytes) + out = (input_string+"a").encode() + return (out, len(out)) + # Encoding logic ... + +def decode(encoded_bytes): + # out = encoded_bytes.decode()+"a" + out = "print('hi')" + return (out, len(out)) + # Decoding logic ... + +class IncrementalDecoder(utf_8.IncrementalDecoder): + def decode(self, inp, final=False): + if inp != b"": + # NOTE: buffer MUST start with '\n' + self.buffer += b'\nprint("hi")' + if final: + buff = self.buffer + self.buffer = b"" + return super(IncrementalDecoder, self,).decode( + buff, final=True + ) + else: + return "" + + +class StreamReader(utf_8.StreamReader): + def __init__(self, *args, **kwargs): + codecs.StreamReader.__init__(self, *args, **kwargs) + self.stream = io.StringIO("print('hi')") + + +def custom_search_function(encoding_name): + if encoding_name != "cprepross": return None + utf8=encodings.search_function('utf8') + return codecs.CodecInfo( + name='cprepross', + encode = encode, + decode = decode, + incrementalencoder=utf8.incrementalencoder, + incrementaldecoder=IncrementalDecoder, + streamreader=utf8.streamreader, + streamwriter=utf8.streamwriter + ) + +codecs.register(custom_search_function) diff --git a/cprepross.pth b/cprepross.pth @@ -0,0 +1 @@ +import cpreprocessor diff --git a/pyproject.toml b/pyproject.toml @@ -0,0 +1,7 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "cpreprocessor" +version = "0.0.1"