{"action":"created","comment":{"url":"https://api.github.com/repos/aldanor/pybind11/pulls/comments/69378989","id":69378989,"diff_hunk":"@@ -163,11 +216,121 @@ template <typename T> constexpr const int npy_format_descriptor<\n \n #define DECL_FMT(Type, NumPyName, Name) template<> struct npy_format_descriptor<Type> { \\\n     enum { value = array::API::NumPyName }; \\\n+    static object dtype() { \\\n+        if (auto ptr = array::lookup_api().PyArray_DescrFromType_(value)) \\\n+            return object(ptr, true); \\\n+        pybind11_fail(\"Unsupported buffer format!\"); \\\n+    } \\\n     static PYBIND11_DESCR name() { return _(Name); } }\n-DECL_FMT(float, NPY_FLOAT_, \"float32\"); DECL_FMT(double, NPY_DOUBLE_, \"float64\"); DECL_FMT(bool, NPY_BOOL_, \"bool\");\n-DECL_FMT(std::complex<float>, NPY_CFLOAT_, \"complex64\"); DECL_FMT(std::complex<double>, NPY_CDOUBLE_, \"complex128\");\n+DECL_FMT(float, NPY_FLOAT_, \"float32\");\n+DECL_FMT(double, NPY_DOUBLE_, \"float64\");\n+DECL_FMT(bool, NPY_BOOL_, \"bool\");\n+DECL_FMT(std::complex<float>, NPY_CFLOAT_, \"complex64\");\n+DECL_FMT(std::complex<double>, NPY_CDOUBLE_, \"complex128\");\n #undef DECL_FMT\n \n+struct field_descriptor {\n+    const char *name;\n+    int offset;\n+    object descr;\n+};\n+\n+template <typename T>\n+struct npy_format_descriptor<T, typename std::enable_if<is_pod_struct<T>::value>::type> {\n+    static PYBIND11_DESCR name() { return _(\"user-defined\"); }\n+\n+    static object dtype() {\n+        if (!dtype_())\n+            pybind11_fail(\"NumPy: unsupported buffer format!\");\n+        return object(dtype_(), true);\n+    }\n+\n+    static const char* format() {\n+        if (!dtype_())\n+            pybind11_fail(\"NumPy: unsupported buffer format!\");\n+        return format_();\n+    }\n+\n+    static void register_dtype(std::initializer_list<field_descriptor> fields) {\n+        array::API& api = array::lookup_api();\n+        auto args = dict();\n+        list names { }, offsets { }, formats { };\n+        for (auto field : fields) {\n+            if (!field.descr)\n+                pybind11_fail(\"NumPy: unsupported field dtype\");\n+            names.append(str(field.name));\n+            offsets.append(int_(field.offset));\n+            formats.append(field.descr);\n+        }\n+        args[\"names\"] = names;\n+        args[\"offsets\"] = offsets;\n+        args[\"formats\"] = formats;\n+        // This is essentially the same as calling np.dtype() constructor in Python and passing\n+        // it a dict of the form {'names': ..., 'formats': ..., 'offsets': ...}.\n+        if (!api.PyArray_DescrConverter_(args.release().ptr(), &dtype_()) || !dtype_())","path":"include/pybind11/numpy.h","position":244,"original_position":244,"commit_id":"6537e69d6072a3f0de507601e7571b13a56a23b2","original_commit_id":"6537e69d6072a3f0de507601e7571b13a56a23b2","user":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"body":"https://github.com/aldanor/pybind11/pull/1/files/6537e69d6072a3f0de507601e7571b13a56a23b2#diff-7174f0e1ad0a58b4622057815d7f8199R287","created_at":"2016-07-02T15:02:33Z","updated_at":"2016-07-02T15:02:33Z","html_url":"https://github.com/aldanor/pybind11/pull/1#discussion_r69378989","pull_request_url":"https://api.github.com/repos/aldanor/pybind11/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/comments/69378989"},"html":{"href":"https://github.com/aldanor/pybind11/pull/1#discussion_r69378989"},"pull_request":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/aldanor/pybind11/pulls/1","id":74677383,"html_url":"https://github.com/aldanor/pybind11/pull/1","diff_url":"https://github.com/aldanor/pybind11/pull/1.diff","patch_url":"https://github.com/aldanor/pybind11/pull/1.patch","issue_url":"https://api.github.com/repos/aldanor/pybind11/issues/1","number":1,"state":"open","locked":false,"title":"Structured arrays support","user":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"body":"This branch represents work-in-progress re: structured arrays support.\r\n\r\nSee `example18.cpp`, `example18.py` and `example18.ref` for the attached tests. Most of the implementation is contained in `numpy.h`.\r\n\r\n- Added `py::memoryview` wrapper type.\r\n- Added `buffer_info::as_pybuffer()` to allow extracting a `Py_buffer` object from `buffer_info`.\r\n- `py::array` constructor from buffer logic had to be rewritten to let numpy handle buffer conversion instead of figuring it out manually, using memoryviews.\r\n- Added `PYBIND11_DTYPE` macro for defining dtypes for structured arrays.\r\n- `npy_format_descriptor::value` had to be changed to `npy_format_descriptor::dtype()` since the typenums are only sufficient for built-in types. Internally, it is converted to a descriptor anyway by numpy. However, this is backwards-compatible since the typenums for non-structured types are still accessible via `::value`.\r\n- `format_descriptor::value` had to be changed to `format_descriptor::format()` so as to be able to use numpy's logic to general buffer format strings at runtime. However, the change is backwards compatible since the format strings known at compile time are still accessible via `::value`.\r\n\r\nCurrently, most basic things work, but It may require doing some more detailed work with casting, further tests, docs, etc.","created_at":"2016-06-21T20:13:30Z","updated_at":"2016-07-02T15:02:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"17154358bd454b4f1cb7342d77cbb86c59bd5f45","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/aldanor/pybind11/pulls/1/commits","review_comments_url":"https://api.github.com/repos/aldanor/pybind11/pulls/1/comments","review_comment_url":"https://api.github.com/repos/aldanor/pybind11/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aldanor/pybind11/issues/1/comments","statuses_url":"https://api.github.com/repos/aldanor/pybind11/statuses/6537e69d6072a3f0de507601e7571b13a56a23b2","head":{"label":"aldanor:recarray","ref":"recarray","sha":"6537e69d6072a3f0de507601e7571b13a56a23b2","user":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"repo":{"id":49825893,"name":"pybind11","full_name":"aldanor/pybind11","owner":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aldanor/pybind11","description":"Seamless operability between C++11 and Python","fork":true,"url":"https://api.github.com/repos/aldanor/pybind11","forks_url":"https://api.github.com/repos/aldanor/pybind11/forks","keys_url":"https://api.github.com/repos/aldanor/pybind11/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aldanor/pybind11/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aldanor/pybind11/teams","hooks_url":"https://api.github.com/repos/aldanor/pybind11/hooks","issue_events_url":"https://api.github.com/repos/aldanor/pybind11/issues/events{/number}","events_url":"https://api.github.com/repos/aldanor/pybind11/events","assignees_url":"https://api.github.com/repos/aldanor/pybind11/assignees{/user}","branches_url":"https://api.github.com/repos/aldanor/pybind11/branches{/branch}","tags_url":"https://api.github.com/repos/aldanor/pybind11/tags","blobs_url":"https://api.github.com/repos/aldanor/pybind11/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aldanor/pybind11/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aldanor/pybind11/git/refs{/sha}","trees_url":"https://api.github.com/repos/aldanor/pybind11/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aldanor/pybind11/statuses/{sha}","languages_url":"https://api.github.com/repos/aldanor/pybind11/languages","stargazers_url":"https://api.github.com/repos/aldanor/pybind11/stargazers","contributors_url":"https://api.github.com/repos/aldanor/pybind11/contributors","subscribers_url":"https://api.github.com/repos/aldanor/pybind11/subscribers","subscription_url":"https://api.github.com/repos/aldanor/pybind11/subscription","commits_url":"https://api.github.com/repos/aldanor/pybind11/commits{/sha}","git_commits_url":"https://api.github.com/repos/aldanor/pybind11/git/commits{/sha}","comments_url":"https://api.github.com/repos/aldanor/pybind11/comments{/number}","issue_comment_url":"https://api.github.com/repos/aldanor/pybind11/issues/comments{/number}","contents_url":"https://api.github.com/repos/aldanor/pybind11/contents/{+path}","compare_url":"https://api.github.com/repos/aldanor/pybind11/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aldanor/pybind11/merges","archive_url":"https://api.github.com/repos/aldanor/pybind11/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aldanor/pybind11/downloads","issues_url":"https://api.github.com/repos/aldanor/pybind11/issues{/number}","pulls_url":"https://api.github.com/repos/aldanor/pybind11/pulls{/number}","milestones_url":"https://api.github.com/repos/aldanor/pybind11/milestones{/number}","notifications_url":"https://api.github.com/repos/aldanor/pybind11/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aldanor/pybind11/labels{/name}","releases_url":"https://api.github.com/repos/aldanor/pybind11/releases{/id}","deployments_url":"https://api.github.com/repos/aldanor/pybind11/deployments","created_at":"2016-01-17T16:41:31Z","updated_at":"2016-01-17T16:41:32Z","pushed_at":"2016-07-02T14:33:19Z","git_url":"git://github.com/aldanor/pybind11.git","ssh_url":"git@github.com:aldanor/pybind11.git","clone_url":"https://github.com/aldanor/pybind11.git","svn_url":"https://github.com/aldanor/pybind11","homepage":null,"size":1189,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"aldanor:master","ref":"master","sha":"3b48482f02dcd549003125e542d64328a9467fa2","user":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"repo":{"id":49825893,"name":"pybind11","full_name":"aldanor/pybind11","owner":{"login":"aldanor","id":2418513,"avatar_url":"https://avatars.githubusercontent.com/u/2418513?v=3","gravatar_id":"","url":"https://api.github.com/users/aldanor","html_url":"https://github.com/aldanor","followers_url":"https://api.github.com/users/aldanor/followers","following_url":"https://api.github.com/users/aldanor/following{/other_user}","gists_url":"https://api.github.com/users/aldanor/gists{/gist_id}","starred_url":"https://api.github.com/users/aldanor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aldanor/subscriptions","organizations_url":"https://api.github.com/users/aldanor/orgs","repos_url":"https://api.github.com/users/aldanor/repos","events_url":"https://api.github.com/users/aldanor/events{/privacy}","received_events_url":"https://api.github.com/users/aldanor/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aldanor/pybind11","description":"Seamless operability between C++11 and Python","fork":true,"url":"https://api.github.com/repos/aldanor/pybind11","forks_url":"https://api.github.com/repos/aldanor/pybind11/forks","keys_url":"https://api.github.com/repos/aldanor/pybind11/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aldanor/pybind11/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aldanor/pybind11/teams","hooks_url":"https://api.github.com/repos/aldanor/pybind11/hooks","issue_events_url":"https://api.github.com/repos/aldanor/pybind11/issues/events{/number}","events_url":"https://api.github.com/repos/aldanor/pybind11/events","assignees_url":"https://api.github.com/repos/aldanor/pybind11/assignees{/user}","branches_url":"https://api.github.com/repos/aldanor/pybind11/branches{/branch}","tags_url":"https://api.github.com/repos/aldanor/pybind11/tags","blobs_url":"https://api.github.com/repos/aldanor/pybind11/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aldanor/pybind11/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aldanor/pybind11/git/refs{/sha}","trees_url":"https://api.github.com/repos/aldanor/pybind11/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aldanor/pybind11/statuses/{sha}","languages_url":"https://api.github.com/repos/aldanor/pybind11/languages","stargazers_url":"https://api.github.com/repos/aldanor/pybind11/stargazers","contributors_url":"https://api.github.com/repos/aldanor/pybind11/contributors","subscribers_url":"https://api.github.com/repos/aldanor/pybind11/subscribers","subscription_url":"https://api.github.com/repos/aldanor/pybind11/subscription","commits_url":"https://api.github.com/repos/aldanor/pybind11/commits{/sha}","git_commits_url":"https://api.github.com/repos/aldanor/pybind11/git/commits{/sha}","comments_url":"https://api.github.com/repos/aldanor/pybind11/comments{/number}","issue_comment_url":"https://api.github.com/repos/aldanor/pybind11/issues/comments{/number}","contents_url":"https://api.github.com/repos/aldanor/pybind11/contents/{+path}","compare_url":"https://api.github.com/repos/aldanor/pybind11/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aldanor/pybind11/merges","archive_url":"https://api.github.com/repos/aldanor/pybind11/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aldanor/pybind11/downloads","issues_url":"https://api.github.com/repos/aldanor/pybind11/issues{/number}","pulls_url":"https://api.github.com/repos/aldanor/pybind11/pulls{/number}","milestones_url":"https://api.github.com/repos/aldanor/pybind11/milestones{/number}","notifications_url":"https://api.github.com/repos/aldanor/pybind11/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aldanor/pybind11/labels{/name}","releases_url":"https://api.github.com/repos/aldanor/pybind11/releases{/id}","deployments_url":"https://api.github.com/repos/aldanor/pybind11/deployments","created_at":"2016-01-17T16:41:31Z","updated_at":"2016-01-17T16:41:32Z","pushed_at":"2016-07-02T14:33:19Z","git_url":"git://github.com/aldanor/pybind11.git","ssh_url":"git@github.com:aldanor/pybind11.git","clone_url":"https://github.com/aldanor/pybind11.git","svn_url":"https://github.com/aldanor/pybind11","homepage":null,"size":1189,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/1"},"html":{"href":"https://github.com/aldanor/pybind11/pull/1"},"issue":{"href":"https://api.github.com/repos/aldanor/pybind11/issues/1"},"comments":{"href":"https://api.github.com/repos/aldanor/pybind11/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aldanor/pybind11/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/aldanor/pybind11/statuses/6537e69d6072a3f0de507601e7571b13a56a23b2"}}}}
{"actor":{"display_login":"aldanor"}}