Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67494
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.013 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'read- only': 0.09; 'valueerror:': 0.09; 'api': 0.11; 'python': 0.11; 'bullet': 0.16; 'bytearray': 0.16; 'clears': 0.16; 'dice': 0.16; 'expect,': 0.16; 'read-only.': 0.16; 'redundant': 0.16; 'sure.': 0.16; 'which...': 0.16; 'writable': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'bytes': 0.24; 'proxy': 0.24; 'fairly': 0.24; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'on,': 0.29; 'thus': 0.29; 'message- id:@mail.gmail.com': 0.30; 'class': 0.32; 'url:python': 0.33; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'object,': 0.36; 'method': 0.36; 'possible': 0.36; 'url:org': 0.36; 'error.': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'worry': 0.60; 'url:3': 0.61; 'first': 0.61; 'making': 0.63; 'more': 0.64; 'mar': 0.68; 'useful.': 0.68; 'apart': 0.72; 'subject:get': 0.81; 'bite': 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-type:content-transfer-encoding; bh=XOYwrUdXovz6i6eLC/aI3uj5oA16syWJRAQAMrzlbZw=; b=xFN3lmCXagSog5oPhP6MH6rJO+/ystA0091QngcG5Co3AUidJ/hOhlthOIcfITTZJ4 hC2fC1DlFMOAvkP9GWVGIGAh6zJRBwJ8/6cXJacB9LsfVqb7u6yP51+hdFL63wm1clnc BU+UoJcrDPrhK5vYTuQICGuFHt/R6z46kYubne8mBQqix/uglqsg44tnLxAGMH0LQy3/ lTtUQbu2bc4RG5HmLi8N6af7RCcMa40HXyAEkxF+NG4GoRZaf+AR8JkinDqehITWvV/q 8cAFe2Xt4a4Kimpo3JyRVeNSP44mhowd4FrkQTJm18SoFC+pu0gArd/gPd2ZBh0f1gij IyrQ== |
| X-Received | by 10.66.66.66 with SMTP id d2mr16553603pat.80.1393810101024; Sun, 02 Mar 2014 17:28:21 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <lf0h3g$m7r$1@ger.gmane.org> |
| References | <lf0h3g$m7r$1@ger.gmane.org> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Sun, 2 Mar 2014 18:27:40 -0700 |
| Subject | Re: how to get bytes from bytearray without copying |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7613.1393810104.18130.python-list@python.org> (permalink) |
| Lines | 41 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1393810104 news.xs4all.nl 2897 [2001:888:2000:d::a6]:56217 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:67494 |
Show key headers only | View raw
On Sun, Mar 2, 2014 at 5:07 PM, Juraj Ivančić <juraj.ivancic@gmail.com> wrote:
> Is it possible to somehow 'steal' bytearray's buffer and make it a read-only
> bytes? I failed to find a way to do this, and would like to make sure.
>
> My use case is, I would expect, fairly common. I read a certain (potentially
> very large) amount of data from the network into a pre-allocated bytearray.
> From that point on, this data is logically read-only. To prevent making
> redundant copies, I wrap it in a memoryview, and then slice and dice it. The
> problem with this memoryview is that it, and its slices, are considered
> writable, and thus cannot be hashed:
>
> ValueError: cannot hash writable memoryview object
>
> The only way (AFAICT) to make this work is to first create a bytes object
> from bytearray, but this copies the data. I don't need this copy, so I'd
> like to avoid it, because of both principle and performance reasons.
>
> Is there any reason why bytearray isn't able to release/convert its buffer
> to bytes? I see that it has a clear() method which... well... clears it. The
> former would be much more useful.
>
> I would also be content if there is some way of making memoryview
> artificially read-only to avoid the above error.
Python 3.3 has a C API function to create a memoryview for a char*,
that can be made read-only.
http://docs.python.org/3/c-api/memoryview.html#PyMemoryView_FromMemory
I don't see a way to do what you want in pure Python, apart from
perhaps writing an elaborate proxy class that would just be a poor
man's memoryview. Or you could bite the bullet and copy everything
once at the start to create a bytes object, and then never have to
worry about it again.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to get bytes from bytearray without copying Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-02 18:27 -0700
csiph-web