Wednesday, April 25, 2007

Dynamic prefix of PYTHON

autoconf suggests --prefix switch. Probably, anyone who has compiled a package from source(such as apach or mysql) may be familiar to this switch. If we look into a little further, any binary file needs to be copied into memory (we call this process image), all of file locations must be known beforehand. Therefore, compiling C/C++ sources requires where to find other objects, especially if the objects are *.so.
Then, once it copied into process memory, the process doesn't care where this object file came from. This is a different problem. Probably, anyone who has compiled a package from source for multiple versions suffered for libraries of different versions. One of the primary reason is that compiled object remembers wrong places for library, but different object file exists there as the same name. 'make clean' or 'make distclean' should be used in proper time.

Python, on the other hand, is an interpreted language. Compilation is not required. (*.pyc matters, but let's simplify at this moment. ) Python searches library at runtime, and it loads the library at runtime. So far, no difference. But, the library location does not need to be known beforehand. The most beautiful thing is that the location can be determined by looking at stack frame! This can eliminate the needs of compilation '--prefix' switch.
Consider this segment of code.

This segment of code is very simple (6 lines of code including spaces.) It just prints what is the absolute path of the executable. Regardless how it is called, it should return the same path. If we have [Arbitrary App Home] and, under this [Arbitrary App Home] are subdirectories of bin, etc, and lib. And bin subdir has executable Regardless of how was called, it knows where it is and set right value of the [Arbitrary App Home]. Therefore, we don't need --prefix at all.

This is VERY Nice.

No comments: