Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
Okay, here is a single file `foo.c.py` that leverages preprocessor directives and comments to achieve the desired polyglot behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #if 0 """ This part is ignored by the C preprocessor because of the #if 0. Python executes this block. The triple quotes are a multiline string, often used for docstrings, but here they just contain comments. """ import sys print("hello world") sys.exit(0) # Exit cleanly so Python doesn't try to parse the C code below # The C preprocessor ignores this line too. #endif /* This is a C-style block comment. Python ignores it. The C compiler processes the code below because the #if 0 block ended above. */ #include <stdio.h> int main() { // The actual C code to print hello world printf("hello world\n"); return 0; } /* Another C comment that Python ignores. We need to ensure there's no Python code after the C main function that could cause a syntax error if Python somehow didn't exit above. This comment helps ensure that. */ |
1 2 | python foo.c.py |
1 2 3 | gcc foo.c.py -o a.out ./a.out |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #if 0 """ This part is ignored by the C preprocessor because of the #if 0. Python executes this block. The triple quotes are a multiline string, often used for docstrings, but here they just contain comments. """ import sys print("hello world") sys.exit(0) # Exit cleanly so Python doesn't try to parse the C code below # The C preprocessor ignores this line too. #endif /* This is a C-style block comment. Python ignores it. The C compiler processes the code below because the #if 0 block ended above. */ #include <stdio.h> int main() { // The actual C code to print hello world printf("hello world\n"); return 0; } /* Another C comment that Python ignores. We need to ensure there's no Python code after the C main function that could cause a syntax error if Python somehow didn't exit above. This comment helps ensure that. */ |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #if 0 """ This part is ignored by the C preprocessor because of the #if 0. Python executes this block. The triple quotes are a multiline string, often used for docstrings, but here they just contain comments. """ import sys print("hello world") sys.exit(0) # Exit cleanly so Python doesn't try to parse the C code below # The C preprocessor ignores this line too. #endif /* This is a C-style block comment. Python ignores it. The C compiler processes the code below because the #if 0 block ended above. */ #include <stdio.h> int main() { // The actual C code to print hello world printf("hello world\n"); return 0; } /* Another C comment that Python ignores. We need to ensure there's no Python code after the C main function that could cause a syntax error if Python somehow didn't exit above. This comment helps ensure that. */ |
hello world
main.c:3:3: warning: missing terminating " character
3 | """
| ^
main.c:7:3: warning: missing terminating " character
7 | """
| ^
main.c:10:43: warning: missing terminating ' character
10 | sys.exit(0) # Exit cleanly so Python doesn't try to parse the C code below
| ^
Testing if the previous output contains the string hello world
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #if 0 """ This part is ignored by the C preprocessor because of the #if 0. Python executes this block. The triple quotes are a multiline string, often used for docstrings, but here they just contain comments. """ import sys print("hello world") sys.exit(0) # Exit cleanly so Python doesn't try to parse the C code below # The C preprocessor ignores this line too. #endif /* This is a C-style block comment. Python ignores it. The C compiler processes the code below because the #if 0 block ended above. */ #include <stdio.h> int main() { // The actual C code to print hello world printf("hello world\n"); return 0; } /* Another C comment that Python ignores. We need to ensure there's no Python code after the C main function that could cause a syntax error if Python somehow didn't exit above. This comment helps ensure that. */ |
File "/usr/src/app/main.py", line 29
We need to ensure there's no Python code after the C main function
^
SyntaxError: unterminated string literal (detected at line 29)
Testing if the previous output contains the string hello world
: False