Roundup Robot added the comment:
New changeset e33f2b8b937f by Stefan Krah in branch '3.5':
Issue #15944: memoryview: Allow arbitrary formats when casting to bytes.
https://hg.python.org/cpython/rev/e33f2b8b937f
New changeset c7c4b8411037 by Stefan Krah in branch 'default':
Merge #15944.
Stefan Krah added the comment:
Done. Thanks for the patch.
--
components: +Interpreter Core
resolution: - fixed
stage: patch review - resolved
status: open - closed
versions: +Python 3.5
___
Python tracker rep...@bugs.python.org
Stefan Krah added the comment:
Ok, shall we sneak this past Larry for 3.5?
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
___
Antoine Pitrou added the comment:
Why not :)
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
___
Python-bugs-list mailing list
Antoine Pitrou added the comment:
Le 07/08/2015 14:57, Stefan Krah a écrit :
If people are content with writing m[124:128] = b'abcd' and accept
that tolist() etc. won't represent the original structure of the
object, then let's do it.
As long as the casting has to be explicit, this sounds
Antoine Pitrou added the comment:
The proposal sounds reasonable to me.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
___
Stefan Krah added the comment:
If people are content with writing m[124:128] = b'abcd' and accept
that tolist() etc. won't represent the original structure of the
object, then let's do it.
On the bright side, it is less work. -- I'll review the patch.
--
Martin Panter added the comment:
Assuming Issue 23756 is resolved and various standard library functions are
meant to work with any C-contiguous buffer, then it makes sense to me for
memoryview.cast(B) to work for any C-contiguous buffer. I also got the
impression that David, Yuriy, and
eryksun added the comment:
A functional memoryview for ctypes objects would avoid having to use
workarounds, such as the following:
d = ctypes.c_double()
b = (ctypes.c_char * ctypes.sizeof(d)).from_buffer(d)
b[:] = b'abcdefgh'
d.value
8.540883223036124e+194
or using
Martin Panter added the comment:
Here is a patch that allows any “C-contiguous” memoryview() to be cast to a
byte view. Apart from the test that was explicitly checking that this wasn’t
supported, the rest of the test suite still passes. I basically removed the
check that was generating the
Stefan Krah added the comment:
The question is whether we want this behavior.
--
assignee: - skrah
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
Stefan Krah added the comment:
Yuriy: cast() does not do this. What's requested is that e.g. a
single float is represented as a bytes object instead of a float.
Thus, you'd be able to do:
m[0] = b'\x00\x00\x00\x01'
This has other implications, for example, two NaNs would compare
equal.
Yuriy Syrovetskiy added the comment:
You don't need `raw=True`, `.cast('b')` already must do this. But
unfortunately, is is not implemented yet.
--
nosy: +cblp
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
Martin Panter added the comment:
In my experience, I tend to only use memoryview() for “bytes-like” buffers (but
see Issue 23756 about clarifying what this means). Example from
/Lib/_compression.py:67:
def readinto(self, b):
with memoryview(b) as view, view.cast(B) as byte_view:
Changes by Martin Panter vadmium...@gmail.com:
--
nosy: +vadmium
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
___
Python-bugs-list
Changes by Stefan Krah stefan-use...@bytereef.org:
--
versions: +Python 3.5 -Python 3.3
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
Stefan Krah added the comment:
We could add a flag memoryview(x, raw=True) to the constructor. This view
would behave exactly like the regular one except that it ignores buf.format
entirely.
So you could do assignments like:
m[10] = b'\x00\x00\x00\x01'
This would be more flexible in
Changes by Josh Rosenberg shadowranger+pyt...@gmail.com:
--
nosy: +josh.r
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
___
Stefan Krah added the comment:
As I understand it, you prefer memoryviews where the format is
purely informational, whereas we now have typed memoryviews.
Typed memoryviews are certainly useful, in fact they are
present in Cython, see here for examples:
David Beazley added the comment:
There's probably a bigger discussion about memoryviews for a rainy day.
However, the number one thing that would save all of this in my book would be
to make sure cast('B') is universally supported regardless of format including
endianness--especially in the
David Beazley added the comment:
One followup note---I think it's fine to punt on cast('B') if the memoryview is
non-contiguous. That's a rare case that's probably not as common.
--
___
Python tracker rep...@bugs.python.org
New submission from David Beazley:
I've been playing with the interaction of ctypes and memoryviews and am curious
about intended behavior. Consider the following:
import ctypes
d = ctypes.c_double()
m = memoryview(d)
m.ndim
0
m.shape
()
m.readonly
False
m.itemsize
8
As you can see,
Antoine Pitrou added the comment:
You can still read the underlying representation:
d = ctypes.c_double(0.6)
m = memoryview(d)
bytes(m)
b'33\xe3?'
d.value = 0.7
bytes(m)
b'ff\xe6?'
--
nosy: +pitrou, skrah
___
Python tracker
David Beazley added the comment:
I don't want to read the representation by copying it into a bytes object. I
want direct access to the underlying memory--including the ability to modify
it. As it stands now, it's completely useless.
--
___
Stefan Krah added the comment:
0-dim memory is indexed by x[()]. The ctypes example has an additional
problem, because format=d is not yet implemented in memoryview.
Only native single character formats in struct module syntax are
implemented, and d in struct module syntax means standard size,
Stefan Krah added the comment:
BTW, if c_double means native machine double, then ctypes should
fill in Py_buffer.format with d and not d in order to be PEP-3118
compatible.
--
___
Python tracker rep...@bugs.python.org
David Beazley added the comment:
Even with the d format, I'm not sure why it can't be cast to simple byte-view.
None of that seems to work at all.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
David Beazley added the comment:
I don't think memoryviews should be imposing any casting restrictions at all.
It's low level. Get out of the way.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
Stefan Krah added the comment:
The decision was made in order to be able to cast back and forth between
known formats. Otherwise one would be able to cast from 'd' to 'B'
but not from 'B' to 'd'.
Python 3.4 will have support for all formats in struct module syntax,
but all non-native formats
Stefan Krah added the comment:
So you want to be able to segfault the core interpreter using the
builtins?
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
David Beazley added the comment:
No, I want to be able to access the raw bytes sitting behind a memoryview as
bytes without all of this casting and reinterpretation. Just show me the raw
bytes. Not doubles, not ints, not structure packing, not copying into byte
strings, or whatever. Is
David Beazley added the comment:
Just to be specific, why is something like this not possible?
d = ctypes.c_double()
m = memoryview(d)
m[0:8] = b'abcdefgh'
d.value
8.540883223036124e+194
(Doesn't have to be exactly like this, but what's wrong with overwriting bytes
with bytes of a
David Beazley added the comment:
I should add that 0-dim indexing doesn't work as described either:
import ctypes
d = ctypes.c_double()
m = memoryview(d)
m[()]
Traceback (most recent call last):
File stdin, line 1, in module
NotImplementedError: memoryview: unsupported format d
Stefan Krah added the comment:
Please read msg170482. It even contains a copy and paste example!
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15944
___
34 matches
Mail list logo