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

3 comments:

Anonymous said...

Windows *IDENTIFIES* file types using file extensions. But if there is no extension value hardcoded in the program that tries to interact with a file, even on Windows this was expected: you tell python to interprete a given file; you explicitely tell it to use it. Whereas when you double click on it, Windows won't be able to indentify the correct filetype by itself.

It's like you were astonished that you can open a .py or .c file using notepad while these types are associated with gvim on your computer.

That said, I know that gcc and go compiler do check for file extension. For the first one it is to spot mistakes in makefiles. For the second one, the reason is still unclear. But these are compiled and produce binary files. Python does not and if the interpreter crashes, it's trivial to find why.

Anonymous said...

I don't get it. What is strange about this behaviour?

Vasudev Ram said...

@Anonymous:

1)

>It's like you were astonished that you can open a .py or .c file using notepad

Nope, I was not astonished. That's just your assumption. I simply discovered this thing, which I had not come across before, didn't find it astonishing, merely an interesting factoid, and blogged about it. Obviously, as a user of computers for many years (leave along being a programmer for the same), I've opened file hundreds of times with non-default-associated programs, on both Windows and UNIX.

2) Why are you SHOUTING? :-)
I can "hear" you quite well without the capitals.

And @PHAM: See this same answer.