From owner-freebsd-python@freebsd.org Tue Aug 25 07:43:45 2015 Return-Path: Delivered-To: freebsd-python@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6B4E599A617 for ; Tue, 25 Aug 2015 07:43:45 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D9879168; Tue, 25 Aug 2015 07:43:44 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id t7P7hYlm081042 (version=TLSv1 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Tue, 25 Aug 2015 10:43:34 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua t7P7hYlm081042 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id t7P7hX4r081041; Tue, 25 Aug 2015 10:43:33 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 25 Aug 2015 10:43:33 +0300 From: Konstantin Belousov To: David Naylor Cc: freebsd-python@freebsd.org Subject: Re: ctypes not working (Fwd: [pypy-dev] 2.6.1 and freebsd-9) Message-ID: <20150825074333.GB2072@kib.kiev.ua> References: <1770820.q6b9lEKF14@dragon.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1770820.q6b9lEKF14@dragon.local> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-python@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FreeBSD-specific Python issues List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Aug 2015 07:43:45 -0000 On Mon, Aug 24, 2015 at 11:11:32PM +0200, David Naylor wrote: > Hi list, > > Please see the correspondence below from the pypy team about some failing > tests. Specifically, why is test.py producing None whereas test.c is > producing the desired results? Because dlopen symbol is magic. It is provided by the shared libc.so to satisfy the static linker, but real definition comes from the dynamic linker. The libc.so dlopen() is a null stub. You are asking for the dlopen symbol from libc, which is returned to you in faith, and which cannot load a library for real. While in the C example, you use normal symbol resolution and get the dlopen from the loader. To get a handle to real dlopen with dlopen/dlsym, you should do in C: dlopenX = dlsym(RTLD_DEFAULT, "dlopen"); I briefly looked at the python 2.7.8 documentation for ctypes, but I do not see a way to express this with the module. Most clean way for ctypes to offer the missing functionality would be to provide a phabricated object, access to the symbols of which would do dlsym(RTLD_DEFAULT, XXX) instead of dlsym(some handle, XXX). A complete working C example is below. #include #include #include int main(void) { void *(*dlopen1)(const char *path, int mode); void *libm_handle; dlopen1 = dlsym(RTLD_DEFAULT, "dlopen"); if (dlopen1 == NULL) { fprintf(stderr, "dlsym %s\n", dlerror()); exit(1); } libm_handle = dlopen1("libm.so", RTLD_LOCAL /* why local ? */); if (libm_handle == NULL) { fprintf(stderr, "dlopen1 %s\n", dlerror()); exit(1); } printf("libm.so %p\n", libm_handle); return (0); }