Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

6 May, 2024: The networking issue during the past two days has been identified and appears to be fixed. Will keep monitoring.


devel / comp.lang.python / Re: Can you mock a C function using ctypes?

SubjectAuthor
o Re: Can you mock a C function using ctypes?Grant Edwards

1
Re: Can you mock a C function using ctypes?

<mailman.445.1663293471.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19525&group=comp.lang.python#19525

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b....@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: Can you mock a C function using ctypes?
Date: Thu, 15 Sep 2022 18:57:47 -0700 (PDT)
Lines: 27
Message-ID: <mailman.445.1663293471.20444.python-list@python.org>
References: <63237ff4.c80a0220.23ec1.ce9c@mx.google.com>
<CACL+1avqHT-nzGm4JtY_bu2s6NA-SYLOoNZwXu1iU9KhxyiWgQ@mail.gmail.com>
<6323d81b.050a0220.7552d.1ed1@mx.google.com>
X-Trace: news.uni-berlin.de 2exxxZMU8Jb57wg9t7VeAQ1P5r9d5ZrYloHDn/vKqFPQ==
Return-Path: <grant.b.edwards@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=UqIHJ5yl;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.011
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'fairly': 0.05; 'library.':
0.05; 'e.g.': 0.07; 'trivial': 0.09; 'typically': 0.09;
'convention,': 0.16; 'dummy': 0.16; 'eryk': 0.16;
'from:addr:grant.b.edwards': 0.16; 'from:name:grant edwards':
0.16; 'prototypes': 0.16; 'wrote:': 0.16; 'python': 0.16; 'grant':
0.17; 'calls': 0.19; 'to:addr:python-list': 0.20; 'version': 0.23;
'subject:using': 0.23; 'library': 0.26; 'function': 0.27; 'done':
0.28; 'example,': 0.28; 'header:User-Agent:1': 0.30; 'guess':
0.32; 'subject:Can': 0.32; 'windows': 0.34; 'received:google.com':
0.34; 'received:209.85.166': 0.35; 'from:addr:gmail.com': 0.35;
'using': 0.37; 'received:209.85': 0.37; 'could': 0.38;
'received:209': 0.39; 'use': 0.39; 'skip:u 20': 0.39; 'case.':
0.40; 'provide': 0.60; 'forward': 0.62; 'pass': 0.64; 'global':
0.73; 'subject:you': 0.84; 'pointer': 0.84; 'prototype': 0.84;
'similarly': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:user-agent:references:subject:from:date:message-id:from:to:cc
:subject:date; bh=GCrGnA7B4bFrrVgoPASkKppjzcVunFMbmhtcCtAuvrU=;
b=UqIHJ5yl32lulNthYo/9iR7pP5fqfQFN1icK9e/ISH5Skg2IdGRKbgj4l0+GyunLzN
AKL4QmH4RQhZ4ZSMCgtYEXCz09ObBm40k+Vdys7cH8v4x3jIFLvV/I1lfFBENXbTLOTQ
lipJ2SN5rR2oYneS6hb4zz/dUGX54qKTbFsI8yACYep1wSnWCGEMDFvC3/WvH6PiDWzg
fsCc9gCxtFNK4zmrT+X7BffG6DkrqsRJ0O7T8wPesXJoiWhZxo366Lt+Bc4G+UCuvKDS
yI7yUVy/Xb+Hqtlktm+xBYxS93EY92/vnKVSOwcLGo61axxGQLPxsUT99qv/800/aJol
D77g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:user-agent:references:subject:from:date:message-id
:x-gm-message-state:from:to:cc:subject:date;
bh=GCrGnA7B4bFrrVgoPASkKppjzcVunFMbmhtcCtAuvrU=;
b=vArrIcX/ZUPfU0dFFy1eIaUSRz21FNBBeYixGbGaRyhKpMM78RcrQer3Bq3fSk6sU1
EfkExc3VEqAKLVMy6oVNg7Du805EhMH7RUNHuOZgPkTeNok0Tvg4otIBMOHk1e6KyJM0
/rPCwA4Olo9nxX/HMArw/OuZs+18mOC6iVCW8Kwg/H4XrJNI2HjQxUNXfr3r1txadS5p
VUOuAIRd/aOkb1DiJSCxKGnogqEaTjgE0Yz9yDYEoJjMIYvWZ4T6uTLGhcHddHLV8ahk
5UB5VKwEf4u3/EQiWOk+66EGGWpg+X+24EB7Xbqq9Uu/cGo4uL7VIrVkdKsfM+ERXz1h
XoIg==
X-Gm-Message-State: ACrzQf29pX+WkwnYYa9y7XX9WzYHHpakUILvPHNP1kUyxzFotC4EVt/j
hbuCI4li+we0a0RhcKIVk6GgcXBlxL0=
X-Google-Smtp-Source: AMsMyM7XNFcl1HRqJUKxdHOIIIE2Dwu0IgIsfcXyEIN2/iRiK2nC0qadC8Uz8NOvpzcMwk0QzNBl7w==
X-Received: by 2002:a6b:3e87:0:b0:6a1:ac62:c8db with SMTP id
l129-20020a6b3e87000000b006a1ac62c8dbmr1092443ioa.160.1663293468256;
Thu, 15 Sep 2022 18:57:48 -0700 (PDT)
User-Agent: slrn/1.0.3 (Linux)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <6323d81b.050a0220.7552d.1ed1@mx.google.com>
X-Mailman-Original-References: <63237ff4.c80a0220.23ec1.ce9c@mx.google.com>
<CACL+1avqHT-nzGm4JtY_bu2s6NA-SYLOoNZwXu1iU9KhxyiWgQ@mail.gmail.com>
 by: Grant Edwards - Fri, 16 Sep 2022 01:57 UTC

On 2022-09-15, Eryk Sun <eryksun@gmail.com> wrote:
> On 9/15/22, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>>
>> Can that be done using ctypes?
>>
>> For example, I open a library that contains functon foo() where foo()
>> calls external function bar() which is not contained in the library.
>> Then, I provide a Python bar() function that gets called by foo() when
>> foo() is called?
>
> That's straight forward if the library allows the application to pass
> a function pointer to bar().

That's typically not the case.

Does the pointer have to be passed? Or can it be "poked" into a global
variable? If so, I guess it would be fairly trivial to write a dummy
version of bar() in C that calls a function via a global pointer that
could be set via ctypes?

> ctypes function prototypes are defined by ctypes.CFUNCTYPE(restype,
> *argtypes, use_errno=False, use_last_error=False) for the cdecl
> calling convention, or similarly by ctypes.WINFUNCTYPE() to use the
> Windows stdcall calling convention. A prototype can be instantiated
> as a function pointer that calls a Python function, e.g. c_bar =
> prototype(bar).

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor