Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'tutorial': 0.03; 'python3': 0.07; 'returned.': 0.07; 'string': 0.09; 'deprecated': 0.09; 'learn,': 0.09; 'skip:/ 10': 0.09; 'api': 0.11; 'python': 0.11; 'boilerplate,': 0.16; 'bytearray': 0.16; 'c-level': 0.16; 'coercion': 0.16; 'conversions,': 0.16; 'instances,': 0.16; 'portable': 0.16; 'porting': 0.16; 'python3.': 0.16; 'rewriting': 0.16; 'segment': 0.16; 'subject: \n ': 0.16; 'thank-you': 0.16; 'unpacked': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'passing': 0.19; 'replacing': 0.19; 'stefan': 0.19; 'written': 0.21; 'seems': 0.21; 'code,': 0.22; 'input': 0.22; 'memory': 0.22; 'manual': 0.22; 'saying': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'char': 0.24; 'pointer': 0.24; 'simpler': 0.24; "haven't": 0.24; '(or': 0.24; '---': 0.24; "i've": 0.25; 'equivalent': 0.26; 'extension': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'appreciated.': 0.29; 'skip:p 30': 0.29; 'quickly': 0.29; 'direction': 0.30; 'especially': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'reply.': 0.31; 'usually': 0.31; 'fast.': 0.31; 'null;': 0.31; 'figure': 0.32; 'themselves': 0.32; 'quite': 0.32; '(i.e.': 0.33; 'moment': 0.34; 'subject:the': 0.34; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'received:209.85': 0.35; 'created': 0.35; 'skip:s 30': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'interface,': 0.36; 'done': 0.36; 'doing': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'detail': 0.37; 'too': 0.37; 'project': 0.37; 'received:209': 0.37; 'generic': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; 'guidance': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'issues,': 0.61; 'mentioned': 0.61; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; 'first': 0.61; 'here:': 0.62; 'back': 0.62; 'address': 0.63; 'more': 0.64; 'subject:. ': 0.67; 'continuous': 0.68; 'covers': 0.68; 'receive': 0.70; 'excessive': 0.84; 'hand.': 0.84; 'safer': 0.84; 'hand,': 0.93 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=LvT5rwRk45Di8nxnhk/g9DD1uA8koGrzcMPU1NXzthE=; b=io1qPL2n6eB5t08idw1ksLSBDLY5obRrsOnaAlpJ9hVlyeUQezmzrUTkbjI7RLjTl0 tIqD/2OZwXg6WomHim0MAfJDnv0S306Om1Z5COgSUt/KPHhXZrfTIEwbPtYO4TQw6ipy Y7lzSZfKhVpJMZ7sMmPuA09t5uR3bKBdPsuJvfliiSbRRI1P/PBA7/akEHr74i6rl0Ga 8+WzUniNhXF6BhOuCP1JgGwrGtAm3NZWIcHTMSCJ+nPzallaqxZZn+VQ0ijSISCCSxE6 33ggHzCusrJ5YeznpPx+DG6tE4s5JRDg+Kp3vgR3Y+I2U59M+fu69/wewK7mWLKAroEa Hu5g== X-Gm-Message-State: ALoCoQneXrvW4R1mAFqi7WTyBFiuVvQxysTjVwEr9keQEgJegq2uPxnuwHXKYg/z43EQRgdhJnvi X-Received: by 10.68.252.161 with SMTP id zt1mr9543985pbc.130.1390072925311; Sat, 18 Jan 2014 11:22:05 -0800 (PST) Date: Sat, 18 Jan 2014 11:22:02 -0800 From: Mark Heieis User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Porting c extension - PyBuffer_New() deprecated in python3. What's the replacement? References: <52D0DAD5.3020803@alois.ca> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Sat, 18 Jan 2014 21:43:11 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390077792 news.xs4all.nl 2932 [2001:888:2000:d::a6]:44338 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64252 Stefan, Thank-you for the reply. I hadn't considered cpython, unfortunately the extension is too large a project to port at the moment. I ended up replacing the PyBuffer_New() segment with malloc() and passing back an object from PyByteArray_FromStringAndSize(). It seems to work. mrh. On 2014-01-11 01:10, Stefan Behnel wrote: > Mark Heieis, 11.01.2014 06:47: >> I need to convert the following existing c extension code to support Python 3. >> >> // --- existing code ------ >> >> // PyBuffer_New() deprecated in python3 >> if (!(pyBuf = PyBuffer_New(len))) >> { >> return NULL; >> } >> >> // should use memoryview object in python3 >> if (PyObject_AsWriteBuffer(pyBuf, &cbuf, &len)) >> { >> Py_DECREF(pyBuf); >> return NULL ; >> } >> >> // fill in cbuf >> ... >> >> return pyBuf ; >> >> //----------- >> >> I'm somewhat confounded in finding an equivalent (PyBuffer_New()) for >> creating a buffer of size len that has continuous memory in the c extension >> function for python3. cbuf is manipulated/filled in using c, after which >> the created pyBuf is then returned. So far, I haven't found much in the way >> of examples/doc for porting the deprecated Python-/C-level buffer API calls >> to the new C-level buffer API/memoryview object model. >> >> Any guidance or direction to existing doc/example is much appreciated. > If the extension isn't huge, you should consider rewriting it in Cython. > That can usually be done quite quickly - the main thing is to figure out > what the verbose C code actually does and write it down in much simpler > Python code. And it will make it easy to make the code portable and fast. > Also likely safer and more generic and versatile, because Cython covers > away a lot of the annoying boilerplate, ref-counting issues, type > conversions, etc. > > For your specific problem at hand, you could use Cython's memory views: > > http://docs.cython.org/src/userguide/memoryviews.html > > They allow you to convert the input value to a 1-dim char buffer (or > whatever you need, but you mentioned the old Py2 buffer interface, which > can't do much more) by saying > > cdef char[:] my_memview = some_python_object > > If you need to pass the unpacked buffer into C code, you can get the > address as "&my_memview[0]" (i.e. the address of the first item in the > buffer). Memory views themselves support fast slicing and indexing, so you > can efficiently work with them using the normal Python slicing/indexing syntax. > > In case what you actually receive are not arbitrary buffers but simple byte > strings or bytearray instances, you can even use the normal byte string > coercion in Cython and simply say > > cdef char* c_string = some_python_byte_string_object > > and then use that pointer to pass it on into C. > > I've written a string processing tutorial for Cython here: > > http://docs.cython.org/src/tutorial/strings.html > > These things may take a moment to learn, especially if you are used to > doing everything in excessive manual detail in C code, but once you are > through that, you should get things done much more quickly than when trying > to do them by hand. > > Stefan > >