[pypy-dev] replacing/modifying __import__

Amaury Forgeot d'Arc amauryfa at gmail.com
Mon Feb 13 15:42:30 CET 2012

2012/2/13 Timothy Baldridge <tbaldridge at gmail.com>

> I'm in the process of writing a Clojure->Python bytecode compiler
> (http://github.com/halgari/clojure-py). The project is going quite
> well and runs great on PyPy and CPython. However there is one feature
> I'm not quite sure how to implement in PyPy. What I would like, is to
> extend 'import' so that it can handle .clj files:
> import clojure.core # where there is a file called clojure/core.clj
> Now in CPython I can simply ovewrite __builtins__.__import__ with a
> new function that adds additional functionality to __import__.
> However, in PyPy we can't change __builtins__. So is there a better
> option? The interop between clojure and python is so good already,
> that I'd hate to make users run:
> import clojure
> clojure.import("core.clj")
> Any ideas?

Have you tried with an import hook? sys.path_hooks is a list of callables
tried to handle a sys.path item. For example, the zipimporter is already
implemented as a path_hook, it catches sys.path entries that ends with

You could design a clojureimporter that would handle any entry in sys.path
which ends with "/closure" (or better, which have a __init__.clj file)

There is also sys.meta_path, which can be used for further customization,
and may be needed if you want to have both .py and .cjl files in the same

Both kinds of hooks are normally supported by both CPython and PyPy...

Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20120213/fa558061/attachment-0001.html>

More information about the pypy-dev mailing list