{"action":"opened","issue":{"url":"https://api.github.com/repos/pygobject/pycairo/issues/110","repository_url":"https://api.github.com/repos/pygobject/pycairo","labels_url":"https://api.github.com/repos/pygobject/pycairo/issues/110/labels{/name}","comments_url":"https://api.github.com/repos/pygobject/pycairo/issues/110/comments","events_url":"https://api.github.com/repos/pygobject/pycairo/issues/110/events","html_url":"https://github.com/pygobject/pycairo/issues/110","id":312709552,"number":110,"title":"Unable to use the C API from a C extension","user":{"login":"P403n1x87","id":20231758,"avatar_url":"https://avatars1.githubusercontent.com/u/20231758?v=4","gravatar_id":"","url":"https://api.github.com/users/P403n1x87","html_url":"https://github.com/P403n1x87","followers_url":"https://api.github.com/users/P403n1x87/followers","following_url":"https://api.github.com/users/P403n1x87/following{/other_user}","gists_url":"https://api.github.com/users/P403n1x87/gists{/gist_id}","starred_url":"https://api.github.com/users/P403n1x87/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/P403n1x87/subscriptions","organizations_url":"https://api.github.com/users/P403n1x87/orgs","repos_url":"https://api.github.com/users/P403n1x87/repos","events_url":"https://api.github.com/users/P403n1x87/events{/privacy}","received_events_url":"https://api.github.com/users/P403n1x87/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-04-09T22:24:34Z","updated_at":"2018-04-09T22:24:34Z","closed_at":null,"author_association":"NONE","body":"I must be doing something very silly, but I'm unable to build a C extension that makes use of the `pycairo` C API. If I use external symbols and link against `_cairo.*.so` then everything works fine, but with the C API, `gcc` produces weird machine code. The interpreter crashes when it gets to this point (just to show the kind of assembly generated; of course this bit of code means nothing on its own):\r\n\r\n~~~ asm\r\n > │0x7ffff3b680f3 <Canvas_show+35> mov    rax,QWORD PTR ds:0x8                                                                                                                                                                 │\r\n   │0x7ffff3b680fb <Canvas_show+43> ud2                                                                                                                                                                                         │\r\n   │0x7ffff3b680fd                  add    BYTE PTR [rax],al                                                                                                                                                                    │\r\n   │0x7ffff3b680ff                  add    BYTE PTR [rax-0x7d],cl                                                                                                                                                               │\r\n   │0x7ffff3b68102 <_fini+2>        in     al,dx                                                                                                                                                                                │\r\n   │0x7ffff3b68103 <_fini+3>        or     BYTE PTR [rax-0x7d],cl                                                                                                                                                               │\r\n   │0x7ffff3b68106 <_fini+6>        (bad)                                                                                                                                                                                       │\r\n   │0x7ffff3b68107 <_fini+7>        or     bl,al                                                                                                                                                                                │\r\n   │0x7ffff3b68109                  add    BYTE PTR [rax],al                                                                                                                                                                    │\r\n   │0x7ffff3b6810b                  add    BYTE PTR [rax],al                                                                                                                                                                    │\r\n   │0x7ffff3b6810d                  add    BYTE PTR [rax],al                                                                                                                                                                    │\r\n   │0x7ffff3b6810f                  add    BYTE PTR [rbx+0x61],ah                                                                                                                                                               │\r\n   │0x7ffff3b68112                  imul   esi,DWORD PTR [rdx+0x6f],0x5041432e\r\n~~~\r\n\r\n(taken verbatim from `gdb`) which I cannot relate to any parts of my C code, since this should correspond to\r\n\r\n~~~ C\r\n  PyObject *args = Py_BuildValue(\"(O)\", PycairoContext_FromContext(self->context, &PycairoContext_Type, NULL));\r\n  for (running = 1; running;)\r\n  {\r\n    Canvas_on_draw(self, args, NULL);\r\n    ...\r\n  }\r\n~~~\r\n\r\nAll that I'm doing is including the `pycairo.h` header file whenever I need some symbols from the `pycairo` module, and call `import_cairo` from `PyInit_x11` (I'm targeting Python 3)\r\n\r\n~~~ c\r\nPyMODINIT_FUNC\r\nPyInit_x11(void)\r\n{\r\n  PyObject* m;\r\n\r\n  if (PyType_Ready(&CanvasType) < 0)\r\n    return NULL;\r\n\r\n  m = PyModule_Create(&canvasmodule);\r\n  if (m == NULL)\r\n    return NULL;\r\n\r\n  if (import_cairo() < 0)\r\n    return NULL;\r\n\r\n  Py_INCREF(&CanvasType);\r\n  PyModule_AddObject(m, \"Canvas\", (PyObject *)&CanvasType);\r\n  return m;\r\n}\r\n~~~\r\n\r\nThe import seems to go well since the problem happens after I instantiate an instance of `Canvas` (an object I'm defining in my extension), and I call the `show()` method (`Canvas_show`).\r\n\r\nI've tried both a manual compilation/linking as well as the standard `distutil` solution, but they both produce the same machine code. Hence something weird must be happening when I import `pycairo.h` into my extension.\r\n\r\nI'm sorry that I cannot provide the full source, but at the minute I only have a stub of the actual stuff that I will need at the end. Please let me know if you need more details to clarify the situation further."}}
{"actor":{"display_login":"P403n1x87"}}