Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102776
| Path | csiph.com!2.eu.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | eryk sun <eryksun@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Copying void * string to |
| Date | Wed, 10 Feb 2016 18:23:24 -0600 |
| Lines | 45 |
| Message-ID | <mailman.23.1455150247.22075.python-list@python.org> (permalink) |
| References | <004a01d163fb$aa7d8930$ff789b90$@ladybridge.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de Wu8jou20ZQesdZ1xqYo8LwQxCv2hPasPhDOrfy3DEwZw== |
| Return-Path | <eryksun@gmail.com> |
| 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; 'memory.': 0.05; 'dynamically': 0.07; 'strings.': 0.07; 'terminated': 0.07; 'attribute.': 0.09; 'func': 0.09; 'pointers': 0.09; 'subclass': 0.09; 'subclasses': 0.09; 'subject:string': 0.09; 'python': 0.10; 'def': 0.13; 'wed,': 0.15; '"value"': 0.16; '2016': 0.16; 'boolean': 0.16; 'func.restype': 0.16; 'item)': 0.16; 'item):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject: * ': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'pointer': 0.18; 'string,': 0.18; 'variable': 0.18; 'library': 0.20; 'cc:2**0': 0.20; 'martin': 0.22; 'converted': 0.22; 'ctypes': 0.22; 'pass': 0.22; 'am,': 0.23; 'feb': 0.23; 'sets': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'allocated': 0.27; 'function': 0.28; 'pointer.': 0.29; 'raise': 0.29; 'code': 0.30; 'returned': 0.32; 'class': 0.33; 'problem': 0.33; 'null': 0.33; 'received:google.com': 0.35; 'false': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'several': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40 |
| 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 :cc:content-type; bh=u2sA/R3DuWqZAwa/hXzPx17LDWNrJOT5qG2dNlypOLo=; b=DydCdkhRHmtcvEZq45oKHiMZBmRQNoH9LTyfHqgHZQx0++cHDc0iB64TKsUCX25+Sc NjRtNdQQEpZo/fUwmaMHJEEE0tNcWCIC0i0xt60vjft3jEADIIo4ItC9AlOUHGVVkDa7 0dLv1+m4/td6MDzdCY2g/KAkNXfSpUoj1O4mIYacC0GotSjdfEUL525n6hjkxBa1AJWG CEF6A9QxbQrS87G8elbuwONM7dMw3Y3pwtYlUUp1wpbbhtKYoGF6J3GYnWJpZDnAcY9P T5Av+JAavceuvC0++JSeSWkgBZQc1lT8qqGA1kTnrtEPPEEXojk2jOzUVkhjmxPZE0LM tqcg== |
| 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:cc:content-type; bh=u2sA/R3DuWqZAwa/hXzPx17LDWNrJOT5qG2dNlypOLo=; b=To8vFAcVf3isWkMKuDpcJwGKn0lJs8+ND7Gsf7pB3XSl4UX09I05ioqseW0LHNSqO6 RvU5iY1aAIAAveiLGyvCJVyV6vd+LFdLyYQ93Hn8dy9vfqm9jiZquiy1j9w17FlSH0GT +e++abs5VAxmSAwCmb3AlVscYWCSZhXVb/IODLuwf1YB7TL5tn+xWxViz4H0cD7nufWT oLQ56WKs3vTcIv9I9SnN32zcLdbFpdSHiPN+2zQxHLCyrXLjffiY7oIMmieC28r5Qd2Y YrflOsGLb6IVcvlJby3nJWFaYE1K8XAuPx7GYk20GB4bh9hMDiOIuBKtoA+Jz0eLHZsz aGUg== |
| X-Gm-Message-State | AG10YOTZMqQXZcO2/UZcgufep8LiH99mOclgM6aXphOyr6kGKMfNCymq+PA6HkFcOyOZZWOzRaJp+ebOoXrMYg== |
| X-Received | by 10.50.2.70 with SMTP id 6mr13066250igs.74.1455150243881; Wed, 10 Feb 2016 16:24:03 -0800 (PST) |
| In-Reply-To | <004a01d163fb$aa7d8930$ff789b90$@ladybridge.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| 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 | <http://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> |
| Xref | csiph.com comp.lang.python:102776 |
Show key headers only | View raw
On Wed, Feb 10, 2016 at 6:07 AM, Martin Phillips
<martinphillips@ladybridge.com> wrote:
>
> Several functions in the C library return pointers to dynamically allocated w_char null
> terminated strings. I need to copy the string to a Python variable and call an existing
> library function that will free the dynamically allocate memory.
>
> My test code for this is
>
> def Test(fno, item):
> func = mylib. MyFunc
> func.restype = ct.c_void_p
> s = func(fno, item)
> result = s
> mylib.free(s)
> return result
>
> The problem is with the line that sets the result variable. I need this to make a copy of
> the dynamically allocated string, not the pointer to it.
There are several options, but I think the simplest is to use a
subclass of ctypes.c_wchar_p. subclasses of simple types don't get
converted automatically. Copy the string using the "value" attribute.
Then free() the pointer. But only copy and free the result if it isn't
NULL (i.e. a false boolean value).
import ctypes
mylib = ctypes.CDLL('path/to/mylib')
class MyLibError(Exception):
pass
class my_wchar_p(ctypes.c_wchar_p):
pass
mylib.MyFunc.restype = my_wchar_p
def test(fno, item):
s = mylib.MyFunc(fno, item)
if s:
result = s.value
mylib.free(s)
return result
raise MyLibError('mylib.MyFunc returned NULL')
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Copying void * string to eryk sun <eryksun@gmail.com> - 2016-02-10 18:23 -0600
csiph-web