Odd difference between Python 2.5 and Python 2.6 on MacOS 10.6 using ctypes and libproc proc_pidinfo

Posted by cemasoniv on Stack Overflow See other posts from Stack Overflow or by cemasoniv
Published on 2012-01-20T08:21:18Z Indexed on 2012/06/30 15:17 UTC
Read the original article Hit count: 503

Filed under:
|
|
|
|

I'm trying to determine the current working directory of a process given its PID. The command-line utility lsof does something similar. Here's the source to the python script:

import ctypes
from ctypes import util
import sys

PROC_PIDVNODEPATHINFO = 9

proc = ctypes.cdll.LoadLibrary(util.find_library("libproc"))
print(proc.proc_pidinfo)

class vnode_info(ctypes.Structure):
    _fields_ = [('data', ctypes.c_ubyte * 152)]

class vnode_info_path(ctypes.Structure):
    _fields_ = [('vip_vi', vnode_info), ('vip_path', ctypes.c_char * 1024)]

class proc_vnodepathinfo(ctypes.Structure):
    _fields_ = [('pvi_cdir', vnode_info_path), ('pvi_rdir', vnode_info_path)]

inst = proc_vnodepathinfo()
pid = int(sys.argv[1])
ret = proc.proc_pidinfo( pid, PROC_PIDVNODEPATHINFO, 0, ctypes.byref(inst), ctypes.sizeof(inst) )
print(ret, inst.pvi_cdir.vip_path)

However, even though this script behaves as expected on Python 2.6, it does not work in Python 2.5:

host:dir user$ sudo /usr/bin/python2.6 script.py 2698
<_FuncPtr object at 0x100419ae0>
(2352, '/')
host:dir user$ sudo /usr/bin/python2.5 script.py 2698
<_FuncPtr object at 0x19fdc0>
(0, '')

(PID 2698 is "Activity Monitor.app"). Note the different return values. Since this program strongly based on ctypes, I can't imagine any difference in Python itself that would cause this. The same behavior (as Python 2.5) occurs with my self-built Python 3.2.

I'm not sure what versioning information I can give to help track down the weirdness -- or even come up with a solution for 2.5 -- but here's some stuff:

host:dir user$ otool -L /usr/bin/python2.6
/usr/bin/python2.6:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
host:dir user$ otool -L /usr/bin/python2.5
/usr/bin/python2.5 (architecture i386):
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
/usr/bin/python2.5 (architecture ppc7400):
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)


host:dir user$ uname -a
Darwin host.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

Thanks to anyone that has a clue about what's going on here:)

© Stack Overflow or respective owner

Related posts about python

Related posts about osx