{"id":47,"date":"2007-12-05T14:19:24","date_gmt":"2007-12-05T20:19:24","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/2007\/12\/05\/pyrex-from-confusion-to-enlightenment\/"},"modified":"2007-12-05T14:19:24","modified_gmt":"2007-12-05T20:19:24","slug":"pyrex-from-confusion-to-enlightenment","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2007\/12\/05\/pyrex-from-confusion-to-enlightenment\/","title":{"rendered":"Pyrex &#8211; from confusion to enlightenment"},"content":{"rendered":"<p>My last post explained my frustration trying to get pyrex to do *anything*.  Since then, several people posted solutions and e-mailed me with more ideas (thanks allefant and Filip Wasilewski).  My review was focused on the usability of pyrex as a python-&gt;C translator, since that&#8217;s what I was working on with both shed-skin and pypy\/rpython.  It didn&#8217;t hold up so well.  This is because &#8230; <strong>pyrex ISN&#8217;T a python-&gt;C translator<\/strong>.<\/p>\n<p>pyrex is its own special language that contains both python and C stuff.  pyrex does not do type inference.  What pyrex provides is a way to mix both python and python-like C code together in the same files.  The trick to understanding pyrex is to know where the line between python and C is in pyrex.<\/p>\n<p>Here are some of the gotchas I experienced.  I wasn&#8217;t able to detail them in my previous post because I didn&#8217;t really &#8220;get it&#8221;.  Now I think I do, so I&#8217;ll explain some stuff so you can try out pyrex without getting confused the way I did:<\/p>\n<p><strong>Gotcha #1: float() is a python function<\/strong><\/p>\n<pre>cdef int a\r\ncdef float b\r\na = 3\r\nb = float(a)<\/pre>\n<pre><\/pre>\n<p>Solution: use pyrex&#8217;s casting to avoid massive object-conversion speed penalties<\/p>\n<pre>b = &lt;float&gt;a<\/pre>\n<p><strong>Gotcha #2: pyrex doesn&#8217;t do type inference<\/strong><\/p>\n<pre>def set_video(int w, int h):\r\n    s = Surface()\r\n    s.surf = SDL_SetVideoMode(w,h,32,0)\r\n    return s<\/pre>\n<pre>pyrex: Cannot convert 'SDL_Surface *' to Python object<\/pre>\n<p>Solution: to access your C-members you must declare the variable&#8217;s type first<\/p>\n<pre>def set_video(int w, int h):\r\n    cdef Surface s\r\n    s = Surface()\r\n    s.surf = SDL_SetVideoMode(w,h,32,0)\r\n    return s<\/pre>\n<p><strong>Gotcha #3: def makes python functions &#8211; which are slow<\/strong><\/p>\n<pre>def set_pixel(self,int x, int y, int color)<\/pre>\n<p>Solution: To be able to access a fast version of a method in C and be able to access the same method in python you must define two versions of it<\/p>\n<pre>cdef _set_pixel(self,int x, int y, int color):\r\n    pass # do this really fast<\/pre>\n<pre>def set_pixel(self,int x, int y, int color):\r\n    return self._set_pixel(x,y,color)<\/pre>\n<p>All that said, I think pyrex is really neat for writing extensions!\u00c2\u00a0 One of the best features it has is properties (which are like descriptors, only easier).\u00c2\u00a0 Just be careful if you use it to not mistake it for python.\u00c2\u00a0 Lastly, by using those tips I was able to get my julia example to be full speed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My last post explained my frustration trying to get pyrex to do *anything*. Since then, several people posted solutions and e-mailed me with more ideas (thanks allefant and Filip Wasilewski). My review was focused on the usability of pyrex as a python-&gt;C translator, since that&#8217;s what I was working on with both shed-skin and pypy\/rpython. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8,2],"tags":[29],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-development","category-languages","category-python","tag-pyrex"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}