TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Embedding PyPy in a C application

97 点作者 thedjinn大约 9 年前

5 条评论

jamesdutc大约 9 年前
There&#x27;s interesting overlap between tools like `cffi` and `cython`.<p>This is one of the approaches I am pursuing to polish off something I put together recently: rapid prototyping of LD_PRELOADs in Python.<p>I presented an early approach at PyData London last week using LD_AUDIT and `cython`: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=L3j2qw9XZCc" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=L3j2qw9XZCc</a><p>It doesn&#x27;t seem that amazing to be able to write:<p><pre><code> @preload def open(pathname: &#x27;const char*&#x27;, flags: &#x27;int&#x27;) -&gt; &#x27;int&#x27;: return __open__(pathname, flags) </code></pre> After all, this is only slightly more convenient syntax than in C. However, imagine how much functionality you could quickly develop with access to the Python standard library.<p>Even using my clumsy `cython` and `LD_AUDIT` approach, I was able to rapidly develop libraries that interpose file-handling library calls and implement:<p><pre><code> # `less` is used as an example of a libc programme with no concept of Python, git, &amp;c. $ COMMIT_HASH=abc123 gitit less test # interact with files as-of some commit in a git repo via pygit (libgit2) $ runit less test.py # redirect read() so that the contents of the file appears to be the results emitted of having run the file $ zipit less test.gz # redirect read()&#x2F;write() to transparently handle gzip files </code></pre> Each one of these took about half an hour to throw together, which is substantially faster than I could reliably develop them in pure-C (not to mention how much code I avoid having to write myself by relying on Python&#x27;s rich stdlib &amp; ecosystem.)
评论 #11701854 未加载
kensai大约 9 年前
&quot;With only a few lines of code we have written a C program that fires up a PyPy interpreter and runs our Python code as if it was a native C function. Of course I&#x27;ve only shown you the basics here, but the technology is really powerful.&quot;<p>So, C speed with Python semantics?
评论 #11702015 未加载
评论 #11701308 未加载
azag0大约 9 年前
I used cffi recently to embed CPython into a fairly rigid Fortran program to support dynamic scripting. Was a piece of cake.
评论 #11704156 未加载
curiousgal大约 9 年前
Noob question, isn&#x27;t Python already based on C?
评论 #11702927 未加载
评论 #11703259 未加载
solidsnack9000大约 9 年前
Does PyPy embedding get around the GIL?
评论 #11703436 未加载