[Distutils] Installing packages using pip

Nick Coghlan ncoghlan at gmail.com
Tue Nov 17 02:51:30 EST 2015

On 16 November 2015 at 23:38, Paul Moore <p.f.moore at gmail.com> wrote:
> (I don't know what Unix does, I suspect it retains an old
> copy of the shared library for the process until the process exists,
> in which case you'd see a different issue, that you do an upgrade, but
> your process still uses the old code till you restart).

Marius explained the lower level technical details, but the relevant
API at the Python level is the "fileno()" method on file-like objects:
once you have a file descriptor, you can access the kernel object
representing the open file directly, and the kernel doesn't care if
the original filesystem path has been remapped to refer to something
else. The persistent identifier at the filesystem level is the inode
number, rather than the filesystem path.

After opening a file, the inode numbers match:

>>> f = open("example", "w")
>>> os.stat("example").st_ino
>>> os.stat(f.fileno()).st_ino

The filesystem's reference to the inode can be dropped, without losing
the kernel's reference:

>>> os.remove("example")
>>> os.stat("example").st_ino
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'example'
>>> os.stat(f.fileno()).st_ino

The original filesystem path can then be mapped to a new inode:

>>> f2 = open("example", "w")
>>> os.stat("example").st_ino
>>> os.stat(f.fileno()).st_ino
>>> os.stat(f2.fileno()).st_ino

As Wayne noted, the fact shared libraries can be overwritten while
processes are using them is then just an artifact of this general
property of *nix style filesystem access.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Distutils-SIG mailing list