Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: ctypes Usage Note Date: Tue, 2 Aug 2016 05:17:19 +0000 Lines: 28 Message-ID: References: <4747f520-8091-486c-9801-3b9cee209799@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de bi4b4WlA5s4+mylHPs31rwj0S4SPAvFM36/2YLcd9VbQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none)': 0.07; 'api': 0.09; '(currently': 0.09; 'cached': 0.09; 'contexts': 0.09; 'mode,': 0.09; 'pointless': 0.09; 'subject:ctypes': 0.09; 'instead.': 0.15; '2016': 0.16; 'find_library': 0.16; 'handle,': 0.16; 'instead:': 0.16; 'libc': 0.16; 'mapped': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'library': 0.20; 'windows': 0.20; 'aug': 0.20; 'lawrence': 0.22; 'precise': 0.22; 'am,': 0.23; 'passing': 0.23; 'sets': 0.23; 'split': 0.23; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; '(it': 0.29; 'searches': 0.29; 'allows': 0.30; 'that.': 0.30; 'problem': 0.33; "skip:' 20": 0.34; 'tue,': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'path': 0.35; 'but': 0.36; 'instead': 0.36; '(and': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'shared': 0.38; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'due': 0.65; 'presumably': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-transfer-encoding; bh=4DKzkg101rzkPkLP3pXCWpO0HTZ+uiLL4NLRAmty90M=; b=TOwU1QQexTz2TeB/ufUg/hNM7kM3eF3d38cYoS6G8Cr24I9CZY5wukmupD7TrqSTpI WdALcCQLxbeUboxLhUXBHfkxMllracbcZgeaPA/0CtWFVOo84AyOgRMJoSYupR9PUgG3 AtmCIK66SXM6M0YkgnJRivMW4FNSCfDLWFhOYn3GD81BgvZ0jqMRTHaMEGq++0LyqOT6 Fae13nROjE5ywojG3iQ76eAPaSC04xHGReyYqq26Hy43ZCq59NTdrtIfDgJ1atR3/3ui 2/Yp9jiPUQKZJrJJrBpT0Ekrtryb7lKYU9aV6ljlwjp3ixSqy3lJOi+K3IMuyrh64mIq s0oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=4DKzkg101rzkPkLP3pXCWpO0HTZ+uiLL4NLRAmty90M=; b=heJn8pzJ7HsY222yEjcmeoli4Fa0bcGIrb/YdhHo4GxRi92fy5NFKz+gNoWKSdDoxn B2HwdPmC3zD1cEjV+DfTfww7fMbatjWVWNfhZD5JQS/4JDG57qyRu1uSJtkZe2eqoALI +iH8ZWs06tEF1Ml911Uxouaa30ZWKf8QhUD3fupILw4Nd9P4CfSC6VTp6Vh9gU7MfnDN vm1tPvsJ9pckqrUaMdbZBNu1JDrs+pyFGuV0XhaAIMghNqspYFW3xla5RleHH3mjajte k4lf/G6h2j9pdOnn+qIX4q64Z99pcx9VDj5mDIMeB4U4CD2SlQBaMVsYg5ulQkfQRHD3 wLXw== X-Gm-Message-State: AEkoouuiE4nz22qZlbsGkp/t07bxL606iBslpNHHUdIQNsxWqgtdkwE3j2t3WYNSa6blPlQat1CASO5sEVdnCg== X-Received: by 10.107.56.87 with SMTP id f84mr59867052ioa.188.1470115080394; Mon, 01 Aug 2016 22:18:00 -0700 (PDT) In-Reply-To: <4747f520-8091-486c-9801-3b9cee209799@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <4747f520-8091-486c-9801-3b9cee209799@googlegroups.com> Xref: csiph.com comp.lang.python:112204 On Tue, Aug 2, 2016 at 1:41 AM, Lawrence D=E2=80=99Oliveiro wrote: > > Don=E2=80=99t do that. Do this instead: > > libc =3D ctypes.cdll.LoadLibrary("libc.so.6") I recommend using ctypes.CDLL instead. ctypes.cdll is pretty much pointless on Unix systems (and a potential problem on Windows due to cached function pointers). It also doesn't allow passing the mode, handle, and use_errno parameters. As to the library name, you may instead want to use find_library for Unix shared libraries: >>> ctypes.util.find_library('c') 'libc.so.6' >>> ctypes.util.find_library('python3.5m') 'libpython3.5m.so.1.0' It's pretty much pointless on Windows since it just searches PATH for the given name. It doesn't match how Windows LoadLibrary(Ex) really works, which allows precise control over exactly what gets searched via AddDllDirectory, SetDefaultDllDirectories, and activation contexts (i.e. ActivateActCtx). Plus find_library('c') no longer even works in 3.5+ on Windows (it returns None) because the CRT has been split into API sets (currently all mapped to ucrtbase.dll, but presumably that can change with servicing).