From owner-freebsd-python@FreeBSD.ORG Fri Feb 13 07:50:03 2015 Return-Path: Delivered-To: freebsd-python@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2BD08B22; Fri, 13 Feb 2015 07:50:03 +0000 (UTC) Received: from smtprelay02.ispgateway.de (smtprelay02.ispgateway.de [80.67.31.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DA860860; Fri, 13 Feb 2015 07:50:02 +0000 (UTC) Received: from [89.182.253.61] (helo=localhost) by smtprelay02.ispgateway.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84) (envelope-from ) id 1YMAtW-0001KP-Jq; Fri, 13 Feb 2015 08:42:34 +0100 Date: Fri, 13 Feb 2015 08:42:33 +0100 From: Marcus von Appen To: Craig Rodrigues Subject: Re: ctypes problem loading libc.so on FreeBSD current Message-ID: <20150213074233.GA1064@medusa.sysfault.org> Reply-To: Marcus von Appen Mail-Followup-To: Craig Rodrigues , freebsd-python@freebsd.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="IJpNTDwzlM2Ie8A6" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Df-Sender: MTEyNTc0Mg== Cc: freebsd-python@freebsd.org X-BeenThere: freebsd-python@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: FreeBSD-specific Python issues List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Feb 2015 07:50:03 -0000 --IJpNTDwzlM2Ie8A6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On, Fri Feb 13, 2015, Craig Rodrigues wrote: > Hi, > > I am looking at this blog post for loading libc with ctypes: > > http://blogs.freebsdish.org/rpaulo/2008/11/30/sysctlbyname3-and-others-from-python/ > > I am using this version of python from ports: > > > Name : python > Version : 2.7_2,2 > Installed on : Mon Jan 12 22:10:27 PST 2015 > Origin : lang/python > Architecture : freebsd:11:x86:64 > Prefix : /usr/local > Categories : python lang ipv6 > Licenses : > Maintainer : python@FreeBSD.org > WWW : http://www.python.org/ > Comment : The "meta-port" for the default version of Python > interpreter > Annotations : > repo_type : binary > repository : FreeBSD > Flat size : 38.0B > Description : > Python is an interpreted object-oriented programming language, and is > often compared to Tcl, Perl or Scheme. > This is a meta port to the Python interpreter and provides symbolic links > to bin/python, bin/pydoc, bin/idle and so on to allow compatibility with > version agnostic python scripts. > > WWW: http://www.python.org/ > > > > I have this test program > > import ctypes > > mylib = ctypes.CDLL("libc.so") > print(mylib) > > On FreeBSD 9, this program works, and I get a handle to libc.so. > > On FreeBSD-CURRENT, I get this: > > File "a.py", line 3, in > mylib = ctypes.CDLL("libc.so") > File "/usr/local/lib/python2.7/ctypes/__init__.py", line 365, in __init__ > self._handle = _dlopen(self._name, mode) > OSError: /usr/lib/libc.so: invalid file format > > > The problem seems to be that on FreeBSD 9, > /usr/lib/libc.so is a symlink to /lib/libc.so.7, while > on FreeBSD-CURRENT, /usr/lib/libc.so contains this: > > /* $FreeBSD: head/lib/libc/libc.ldscript 258283 2013-11-17 22:52:17Z peter > $ */ > GROUP ( /lib/libc.so.7 /usr/lib/libc_nonshared.a > /usr/lib/libssp_nonshared.a ) > > > Any ideas what the problem is? You described the problem above. ctypes is unable to load linker scripts. The general advise is to use find_library(): >>> import ctypes >>> import ctypes.util >>> clib = ctypes.util.find_library("c") >>> print clib libc.so.7 >>> dll = ctypes.CDLL(clib) Cheers Marcus --IJpNTDwzlM2Ie8A6 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iEYEARECAAYFAlTdqukACgkQi68/ErJnpkfOOACgjAZ0WLC/8oCYZ05bSn+OXAyF L+0An0GbuTKrdJbiPhJCcZuE7N/ng6m3 =3z9g -----END PGP SIGNATURE----- --IJpNTDwzlM2Ie8A6--