[pypy-dev] How to use interperter level code from a module for app level code in another module

Yicong Huang hengha.mao at gmail.com
Mon Jun 8 13:22:23 CEST 2015


By reading modules in pypy/module, finally we found out the workaround
method.

For most of modules in 'pypy/module', their interp code could not be
imported or used by other module's app code.
But there is special module '__pypy__', it exposed several interp
functions, and those code are used by other module's app code.
E.g.
__builtin__/app_functional.py used resizelist_hint and newlist_hint from
__pypy__ module.
But these two functions are actually defined as interp code in ‘__pypy__'
module.

The workaround solution is simple: add one more interface in __pypy__
module's __init__.py and point to the interp file in the module that would
expose.
E.g. if we would like to use function from fcntl/interp_fcntl.py, add below
lines:
import pypy.module.fcntl.interp_fcntl

interpleveldefs = {
    ...
    'xxx' : 'pypy.module.interpfcntl.xxx'}

And then the function is visible to app code of the module by:
from __pypy__ import xxx

And finally, here comes the question why __pypy__ module is special to be
capable to expose interp code?
For a quick glance, we observed some extra code in its __init__.py, which
were not seen from other modules:

 PYC_MAGIC = get_pyc_magic(self.space)
 self.extra_interpdef('PYC_MAGIC', 'space.wrap(%d)' % PYC_MAGIC)

Are the above code do the magic thing to expose interp code?


On Sun, Jun 7, 2015 at 3:47 PM, Armin Rigo <arigo at tunes.org> wrote:

> Hi Yicong,
>
> On 7 June 2015 at 09:33, Yicong Huang <hengha.mao at gmail.com> wrote:
> > In this case, any alternative methods?
>
> I really think you need to look at some other directories
> "pypy/module/*/" and use the same style.  I'm still unsure what you're
> trying to do, so I can't answer you more precisely.  Most probably, it
> is similar to other existing modules.  If it is too different from all
> of them, then you might be using entirely the wrong approach...
>
>
> A bientôt,
>
> Armin.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20150608/053e7ffd/attachment.html>


More information about the pypy-dev mailing list