{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/8408","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/8408/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/8408/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/8408/events","html_url":"https://github.com/numpy/numpy/issues/8408","id":196861163,"number":8408,"title":"Allow numpy to copy internal buffer into memoryview","user":{"login":"realitix","id":6367936,"avatar_url":"https://avatars.githubusercontent.com/u/6367936?v=3","gravatar_id":"","url":"https://api.github.com/users/realitix","html_url":"https://github.com/realitix","followers_url":"https://api.github.com/users/realitix/followers","following_url":"https://api.github.com/users/realitix/following{/other_user}","gists_url":"https://api.github.com/users/realitix/gists{/gist_id}","starred_url":"https://api.github.com/users/realitix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realitix/subscriptions","organizations_url":"https://api.github.com/users/realitix/orgs","repos_url":"https://api.github.com/users/realitix/repos","events_url":"https://api.github.com/users/realitix/events{/privacy}","received_events_url":"https://api.github.com/users/realitix/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-21T07:01:46Z","updated_at":"2016-12-21T08:03:27Z","closed_at":null,"body":"Hello,\r\n\r\nI have a C extension which maps memory address to a pointer.\r\nIn this extension, I return this pointer through a memoryview `PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)`.\r\n\r\nIn my python code, I created a numpy array of float and I want to `memcpy` this array to my memory mapped in C.\r\n\r\nHere what I do:\r\n```\r\nmymemoryview = MyMemoryView() # pointer on a 24 bytes buffer\r\nvertices = np.array([0, -0.5, 0.5, 0.5, -0.5, 0.5], dtype=np.float32)\r\nwrapper = np.array(mymemoryview, copy=False) # Wrap my memoryview into a numpy array without copy\r\nnp.copyto(wrapper, vertices, casting='no')\r\n```\r\n\r\nWhen I execute this code, I get:\r\n```\r\nTypeError: Cannot cast scalar from dtype('float32') to dtype('uint8') according to the rule 'no'\r\n```\r\n\r\nOk so I force my `memoryview` to be a `float32`\r\n```\r\nmymemoryview = MyMemoryView() # pointer on a 24 bytes buffer\r\nvertices = np.array([0, -0.5, 0.5, 0.5, -0.5, 0.5], dtype=np.float32)\r\nwrapper = np.array(mymemoryview, copy=False, dtype=vertices.dtype) # Wrap my memoryview into a numpy array without copy\r\nnp.copyto(wrapper, vertices, casting='no')\r\n```\r\n\r\nReturn:\r\n```\r\nValueError: could not broadcast input array from shape (6) into shape (24)\r\n```\r\n\r\nOf course the size of my `memoryview` is 24 bytes but not 24 float... Numpy doesn't understand...\r\n\r\nI tried with `casting='unsafe'` but it doesn't work either.\r\n\r\n### I would like to create a new function `copyinto` wich copy into a `memoryview`. Do you see another way to do it ?"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/268460447","html_url":"https://github.com/numpy/numpy/issues/8408#issuecomment-268460447","issue_url":"https://api.github.com/repos/numpy/numpy/issues/8408","id":268460447,"user":{"login":"shoyer","id":1217238,"avatar_url":"https://avatars.githubusercontent.com/u/1217238?v=3","gravatar_id":"","url":"https://api.github.com/users/shoyer","html_url":"https://github.com/shoyer","followers_url":"https://api.github.com/users/shoyer/followers","following_url":"https://api.github.com/users/shoyer/following{/other_user}","gists_url":"https://api.github.com/users/shoyer/gists{/gist_id}","starred_url":"https://api.github.com/users/shoyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoyer/subscriptions","organizations_url":"https://api.github.com/users/shoyer/orgs","repos_url":"https://api.github.com/users/shoyer/repos","events_url":"https://api.github.com/users/shoyer/events{/privacy}","received_events_url":"https://api.github.com/users/shoyer/received_events","type":"User","site_admin":false},"created_at":"2016-12-21T08:03:27Z","updated_at":"2016-12-21T08:03:27Z","body":"You can use the view method (https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html) to convert your numpy array to the dtype of the memory view. "}}
{"actor":{"display_login":"shoyer"}}