Showing posts with label Python-executes-c-files. Show all posts
Showing posts with label Python-executes-c-files. Show all posts

Monday, October 28, 2013

Python can run .c files


By Vasudev Ram

Python can run .c files - I'm not joking.

I was taking another look at the vr_debug function mentioned in my previous post:

A simple Python debugging function.

To run the program, I was going to type:

python vr_debug.py

Using tab completion (similar to what bash on UNIX has) at the Windows command prompt, I accidentally tabbed twice after typing "python vr_", which expanded to vr_debug.py~ (note the ~ character at the end of the filename) instead of vr_debug.py (the file ending in .py~ is a Vim backup file - I use GVim for Windows as my text editor whem programming on Windows).

Since I type fast, I hit Enter before I realized it was the wrong filename. Was surprised to see that Python ran the file, with no error message (like 'not a .py file) and gave the same output as the original .py file.

To check things a bit more, I copied vr_debug.py to a file called just vr_d and ran it with python. Again, it worked.

To check more, I copied vr_d to vr_d.c and ran that .c file with python. It also worked.

The output of the two runs (using Python) of non-dot-py files (one with no extension, one with a .c extension) is below. You can see that there is no error message, and the output is the same as from running vr_debug.py, as shown in my earlier blog post.

C:>python vr_d
z
a : 1
b : 1 'hi'
c : 1 'hi' True
d : 1 'hi' True [2, 3]
d : 1 'hi' True [2, 3] {'a': 'apple', 'b': 'banana'}

C:>copy vr_d vr_d.c
1 file(s) copied.

C:>python vr_d.c
z
a : 1
b : 1 'hi'
c : 1 'hi' True
d : 1 'hi' True [2, 3]
d : 1 'hi' True [2, 3] {'a': 'apple', 'b': 'banana'}

So Python can actually run .c files.

Of course, to be precise, as Thomson and Thompson might say, I should mention that Python can only run .c files if they contain Python code, not C code ... :-)

I'm guessing this works because Python was at least partly influenced by UNIX, and on UNIX, at least shell scripts don't need to have an extension of .sh - they can be called anything. There are many examples of this in the classic UNIX book, The UNIX Programming Environment, by Kernighan and Pike (K&P), which is one of my favorite computer books of all time. You don't need to have the K&P book to check that, of course; just create any simple shell script on Linux (without a .sh or any other extension in the filename) and then run it, after making it executable.

Interestingly, while Googling for "the unix programming environment" (in order to get the Wikipedia link to the book, which I used in the previous paragraph), I noticed that the moment I typed "the unix", Google showed me a list of commonly searched phrases beginning with those two words, and "the unix programming environment" was at the top of that list. See the screenshot below. That phrase even comes before the phrase "the unix operating system".

- Vasudev Ram - Dancing Bison Enterprises

Consulting / training inquiry




Free shipping with purchase of Inc or LLC