Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Doc/library/stdtypes.rst

Issue 15944: memoryviews and ctypes
Patch Set: Created 4 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_buffer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 .. XXX: reference/datamodel and this have quite a few overlaps! 1 .. XXX: reference/datamodel and this have quite a few overlaps!
2 2
3 3
4 .. _bltin-types: 4 .. _bltin-types:
5 5
6 ************** 6 **************
7 Built-in Types 7 Built-in Types
8 ************** 8 **************
9 9
10 The following sections describe the standard types that are built into the 10 The following sections describe the standard types that are built into the
(...skipping 3543 matching lines...) Expand 10 before | Expand all | Expand 10 after
3554 File "<stdin>", line 1, in <module> 3554 File "<stdin>", line 1, in <module>
3555 ValueError: operation forbidden on released memoryview object 3555 ValueError: operation forbidden on released memoryview object
3556 3556
3557 .. versionadded:: 3.2 3557 .. versionadded:: 3.2
3558 3558
3559 .. method:: cast(format[, shape]) 3559 .. method:: cast(format[, shape])
3560 3560
3561 Cast a memoryview to a new format or shape. *shape* defaults to 3561 Cast a memoryview to a new format or shape. *shape* defaults to
3562 ``[byte_length//new_itemsize]``, which means that the result view 3562 ``[byte_length//new_itemsize]``, which means that the result view
3563 will be one-dimensional. The return value is a new memoryview, but 3563 will be one-dimensional. The return value is a new memoryview, but
3564 the buffer itself is not copied. Supported casts are 1D -> C-contiguous 3564 the buffer itself is not copied. Casting is only supported for
3565 and C-contiguous -> 1D. 3565 C-contiguous views, and only when either the old or new shape is
3566 one-dimensional.
3566 3567
3567 Both formats are restricted to single element native formats in 3568 The new format is restricted to a single element native format in
3568 :mod:`struct` syntax. One of the formats must be a byte format 3569 :mod:`struct` syntax. Any C-contiguous view may be cast to a
3569 ('B', 'b' or 'c'). The byte length of the result must be the same 3570 one-dimensional byte view, using the 'B', 'b' or 'c' formats.
3571 Otherwise, the original view must be in one of those byte formats.
3572 The byte length of the result must be the same
3570 as the original length. 3573 as the original length.
3571 3574
3572 Cast 1D/long to 1D/unsigned bytes:: 3575 Cast 1D/long to 1D/unsigned bytes::
3573 3576
3574 >>> import array 3577 >>> import array
3575 >>> a = array.array('l', [1,2,3]) 3578 >>> a = array.array('l', [1,2,3])
3576 >>> x = memoryview(a) 3579 >>> x = memoryview(a)
3577 >>> x.format 3580 >>> x.format
3578 'l' 3581 'l'
3579 >>> x.itemsize 3582 >>> x.itemsize
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3637 >>> x = memoryview(buf) 3640 >>> x = memoryview(buf)
3638 >>> y = x.cast('L', shape=[2,3]) 3641 >>> y = x.cast('L', shape=[2,3])
3639 >>> len(y) 3642 >>> len(y)
3640 2 3643 2
3641 >>> y.nbytes 3644 >>> y.nbytes
3642 48 3645 48
3643 >>> y.tolist() 3646 >>> y.tolist()
3644 [[0, 1, 2], [3, 4, 5]] 3647 [[0, 1, 2], [3, 4, 5]]
3645 3648
3646 .. versionadded:: 3.3 3649 .. versionadded:: 3.3
3650
3651 .. versionchanged:: 3.6
3652 Original format no longer restricted when casting to a byte view.
3647 3653
3648 There are also several readonly attributes available: 3654 There are also several readonly attributes available:
3649 3655
3650 .. attribute:: obj 3656 .. attribute:: obj
3651 3657
3652 The underlying object of the memoryview:: 3658 The underlying object of the memoryview::
3653 3659
3654 >>> b = bytearray(b'xyz') 3660 >>> b = bytearray(b'xyz')
3655 >>> m = memoryview(b) 3661 >>> m = memoryview(b)
3656 >>> m.obj is b 3662 >>> m.obj is b
(...skipping 943 matching lines...) Expand 10 before | Expand all | Expand 10 after
4600 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0] ``, and 4606 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0] ``, and
4601 similarly for tuples. 4607 similarly for tuples.
4602 4608
4603 .. [3] They must have since the parser can't tell the type of the operands. 4609 .. [3] They must have since the parser can't tell the type of the operands.
4604 4610
4605 .. [4] Cased characters are those with general category property being one of 4611 .. [4] Cased characters are those with general category property being one of
4606 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecas e). 4612 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecas e).
4607 4613
4608 .. [5] To format only a tuple you should therefore provide a singleton tuple who se only 4614 .. [5] To format only a tuple you should therefore provide a singleton tuple who se only
4609 element is the tuple to be formatted. 4615 element is the tuple to be formatted.
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_buffer.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+