From 4134f09505205f8f123cac0c0844d2e3bff3a0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Tue, 27 Jan 2026 20:27:55 +0100 Subject: [PATCH 1/2] fix backslashes in code --- README.md | 66 +++++++++++++++++++++++------------------------ example_basic.py | 10 +++---- example_sdl.py | 7 ++--- example_struct.py | 4 +-- tinycc.py | 3 ++- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index c751899..a42d9b5 100644 --- a/README.md +++ b/README.md @@ -43,43 +43,43 @@ make install ### Examples A simple example with the 'run' state: ```python ->>> from tinycc import TinyCC ->>> c_code = '#include ' ->>> c_code += 'void main(void) {' ->>> c_code += ' printf("Hello World!\n");' ->>> c_code += '}' ->>> state = TinyCC().create_state('run') ->>> state.compile(c_code) ->>> state.run([]) +from tinycc import TinyCC +c_code = '#include \n' +c_code += 'void main(void) {' +c_code += ' printf("Hello World!\\n");' +c_code += '}' +state = TinyCC().create_state('run') +state.compile(c_code) +state.run([]) ``` Example with inline code: ```python ->>> from tinycc import TinyCC, InlineGenerator ->>> from ctypes import c_int ->>> ->>> gen = InlineGenerator() ->>> ->>> # C function to be used from Python -... @gen.c_function(c_int, c_int, c_int, c_int) -... def add_mul(a, b, c): -... "return mul(a + b, c);" # calls the Python function mul -... ->>> # Python function to be used from C -... @gen.callable_function(c_int, c_int, c_int) -... def mul(a, b): -... return a * b -... ->>> # compile the code -... state = TinyCC().create_state() ->>> state.compile(gen.code) ->>> state.relocate() ->>> ->>> # bind to state for symbol resolution -... gen.bind_state(state) ->>> ->>> # use it -... add_mul(23, 42, 7) +from tinycc import TinyCC, InlineGenerator +from ctypes import c_int + +gen = InlineGenerator() + +# C function to be used from Python +@gen.c_function(c_int, c_int, c_int, c_int) +def add_mul(a, b, c): + "return mul(a + b, c);" # calls the Python function mul + +# Python function to be used from C +@gen.callable_function(c_int, c_int, c_int) +def mul(a, b): + return a * b + +# compile the code +state = TinyCC().create_state() +state.compile(gen.code) +state.relocate() + +# bind to state for symbol resolution +gen.bind_state(state) + +# use it +add_mul(23, 42, 7) 455 ``` See the example files for more usage ideas. diff --git a/example_basic.py b/example_basic.py index 176cdbc..58a0de4 100644 --- a/example_basic.py +++ b/example_basic.py @@ -32,11 +32,11 @@ int i; char **pos = argv; - printf("Hello Python world from C!\n"); + printf("Hello Python world from C!\\n"); /* list arguments */ for (i=0; i +#define SDL_DISABLE_IMMINTRIN_H #include /* callback definition */ @@ -27,7 +28,7 @@ SDL_Surface* screenSurface = NULL; if(SDL_Init(SDL_INIT_VIDEO) < 0 ) { - printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + printf("SDL could not initialize! SDL_Error: %s\\n", SDL_GetError()); return; } @@ -37,7 +38,7 @@ width, height, SDL_WINDOW_SHOWN); if(!window) { - printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + printf("Window could not be created! SDL_Error: %s\\n", SDL_GetError()); return; } @@ -75,7 +76,7 @@ def get_color(r, g, b): # here: take the SDL2 header and library from the # SDL2-win32 folder in Windows if sys.platform == 'win32': - state.add_include_path('SDL2-win32\include') + state.add_include_path('SDL2-win32\\include') state.add_link_path('SDL2-win32') # load library by hand # this is needed for 2 reasons: diff --git a/example_struct.py b/example_struct.py index 5aa48ce..ae97cd7 100644 --- a/example_struct.py +++ b/example_struct.py @@ -20,7 +20,7 @@ def get_address(self): @gen.c_method(c_int, c_int) def add_a(self, num): """ - printf("add_a:\n"); + printf("add_a:\\n"); self->a += num; return self->a; """ @@ -28,7 +28,7 @@ def add_a(self, num): @gen.c_method(c_int) def adder_c(self): """ - printf("Test.adder_c: %d + %d = %d\n", + printf("Test.adder_c: %d + %d = %d\\n", self->a, self->b, self->a + self->b); return self->a + self->b; """ diff --git a/tinycc.py b/tinycc.py index 40b1d0a..b5f0ef4 100644 --- a/tinycc.py +++ b/tinycc.py @@ -91,7 +91,7 @@ if sys.platform == 'win32': WINDOWS = True TCCPATH = os.path.join(MODULEDIR, 'win32') - TCCLIB = os.path.join(MODULEDIR, 'win32\libtcc.dll') + TCCLIB = os.path.join(MODULEDIR, 'win32\\libtcc.dll') # tcc error function type @@ -514,6 +514,7 @@ def compile(self, source): """ Compile the sourcecode in `source`. """ + print(self._encode(source).decode('utf-8')) if self.tcc.lib.tcc_compile_string(self.ctx, self._encode(source)) == -1: raise TccException('compile error') self._compiled = True From 3122f0ddb347274a6b9e3f1c173464de2046c162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 28 Jan 2026 13:35:19 +0100 Subject: [PATCH 2/2] fix backslash --- example_sdl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_sdl.py b/example_sdl.py index fd3a3dd..5802d00 100644 --- a/example_sdl.py +++ b/example_sdl.py @@ -84,7 +84,7 @@ def get_color(r, g, b): # and leaves the actual DLL loading to Windows # - SDL2.dll is not in the common search paths of Windows # therefore the autoloading will fail - sdl = CDLL('SDL2-win32\SDL2.dll') + sdl = CDLL('SDL2-win32\\SDL2.dll') state.add_library('SDL2') state.compile(C_CODE)