Patchwork [4,of,5,import-refactor] hg: gracefully handle PyPy

login
register
mail settings
Submitter Gregory Szorc
Date Nov. 22, 2015, 6:14 a.m.
Message ID <1101c57a493083f9fd3d.1448172846@ubuntu-main>
Download mbox | patch
Permalink /patch/11579/
State Accepted
Headers show

Comments

Gregory Szorc - Nov. 22, 2015, 6:14 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1448168629 28800
#      Sat Nov 21 21:03:49 2015 -0800
# Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
# Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
hg: gracefully handle PyPy

PyPy isn't compatible with C extensions nor the on demand module import
mechanism.

In this patch, we add detection for PyPy in the "hg" script. When
executed via PyPy, we will now only attempt to load the pure Python
modules and will disable the on demand importer. Running ./hg with
PyPy from a source checkout will "just work," enabling easier
development and testing with PyPy.
Pierre-Yves David - Nov. 23, 2015, 3:45 a.m.
On 11/21/2015 10:14 PM, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1448168629 28800
> #      Sat Nov 21 21:03:49 2015 -0800
> # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
> # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
> hg: gracefully handle PyPy
>
> PyPy isn't compatible with C extensions nor the on demand module import
> mechanism.

First 4 pushed to the clowncopter. Nice rework.

Check commit complains about find_module name, but I'll ignore that.
Gregory Szorc - Nov. 23, 2015, 4:09 a.m.
On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David <
pierre-yves.david@ens-lyon.org> wrote:

>
>
> On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>
>> # HG changeset patch
>> # User Gregory Szorc <gregory.szorc@gmail.com>
>> # Date 1448168629 28800
>> #      Sat Nov 21 21:03:49 2015 -0800
>> # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>> # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>> hg: gracefully handle PyPy
>>
>> PyPy isn't compatible with C extensions nor the on demand module import
>> mechanism.
>>
>
> First 4 pushed to the clowncopter. Nice rework.
>
> Check commit complains about find_module name, but I'll ignore that.


There's nothing we can do about that: "find_module" is part of the import
hook interface, which is defined by Python itself.
Pierre-Yves David - Nov. 23, 2015, 5:08 a.m.
On 11/22/2015 08:09 PM, Gregory Szorc wrote:
> On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
> wrote:
>
>
>
>     On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>
>         # HG changeset patch
>         # User Gregory Szorc <gregory.szorc@gmail.com
>         <mailto:gregory.szorc@gmail.com>>
>         # Date 1448168629 28800
>         #      Sat Nov 21 21:03:49 2015 -0800
>         # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>         # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>         hg: gracefully handle PyPy
>
>         PyPy isn't compatible with C extensions nor the on demand module
>         import
>         mechanism.
>
>
>     First 4 pushed to the clowncopter. Nice rework.
>
>     Check commit complains about find_module name, but I'll ignore that.
>
>
> There's nothing we can do about that: "find_module" is part of the
> import hook interface, which is defined by Python itself.

And that is why I'll ignore it ;-)

However this series is breaking test-extensions.t when running from 
repository without local build (make local never ran). The pure finding 
logic apparently get confused when trying to generate __version__.py 
leading to the Mercurial to be isntall with -0+0 as a version number.

Can you look into that and send me a followup quickly ?
Gregory Szorc - Nov. 23, 2015, 7:16 p.m.
On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David <
pierre-yves.david@ens-lyon.org> wrote:

>
>
> On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>
>> On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
>> wrote:
>>
>>
>>
>>     On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>>
>>         # HG changeset patch
>>         # User Gregory Szorc <gregory.szorc@gmail.com
>>         <mailto:gregory.szorc@gmail.com>>
>>         # Date 1448168629 28800
>>         #      Sat Nov 21 21:03:49 2015 -0800
>>         # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>>         # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>>         hg: gracefully handle PyPy
>>
>>         PyPy isn't compatible with C extensions nor the on demand module
>>         import
>>         mechanism.
>>
>>
>>     First 4 pushed to the clowncopter. Nice rework.
>>
>>     Check commit complains about find_module name, but I'll ignore that.
>>
>>
>> There's nothing we can do about that: "find_module" is part of the
>> import hook interface, which is defined by Python itself.
>>
>
> And that is why I'll ignore it ;-)
>
> However this series is breaking test-extensions.t when running from
> repository without local build (make local never ran). The pure finding
> logic apparently get confused when trying to generate __version__.py
> leading to the Mercurial to be isntall with -0+0 as a version number.
>
> Can you look into that and send me a followup quickly ?


I'm unable to reproduce. Do you have more exact STR? (I think I'm getting
the run-tests.py arguments wrong.)
Pierre-Yves David - Nov. 23, 2015, 8 p.m.
On 11/23/2015 11:16 AM, Gregory Szorc wrote:
> On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David
> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
> wrote:
>
>
>
>     On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>
>         On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>         <pierre-yves.david@ens-lyon.org
>         <mailto:pierre-yves.david@ens-lyon.org>
>         <mailto:pierre-yves.david@ens-lyon.org
>         <mailto:pierre-yves.david@ens-lyon.org>>>
>         wrote:
>
>
>
>              On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>
>                  # HG changeset patch
>                  # User Gregory Szorc <gregory.szorc@gmail.com
>         <mailto:gregory.szorc@gmail.com>
>                  <mailto:gregory.szorc@gmail.com
>         <mailto:gregory.szorc@gmail.com>>>
>                  # Date 1448168629 28800
>                  #      Sat Nov 21 21:03:49 2015 -0800
>                  # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>                  # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>                  hg: gracefully handle PyPy
>
>                  PyPy isn't compatible with C extensions nor the on
>         demand module
>                  import
>                  mechanism.
>
>
>              First 4 pushed to the clowncopter. Nice rework.
>
>              Check commit complains about find_module name, but I'll
>         ignore that.
>
>
>         There's nothing we can do about that: "find_module" is part of the
>         import hook interface, which is defined by Python itself.
>
>
>     And that is why I'll ignore it ;-)
>
>     However this series is breaking test-extensions.t when running from
>     repository without local build (make local never ran). The pure
>     finding logic apparently get confused when trying to generate
>     __version__.py leading to the Mercurial to be isntall with -0+0 as a
>     version number.
>
>     Can you look into that and send me a followup quickly ?
>
>
> I'm unable to reproduce. Do you have more exact STR? (I think I'm
> getting the run-tests.py arguments wrong.)

cd tests
hg purge --all
./run-tests.py test-extensions.y
Gregory Szorc - Nov. 23, 2015, 8:21 p.m.
On Mon, Nov 23, 2015 at 12:00 PM, Pierre-Yves David <
pierre-yves.david@ens-lyon.org> wrote:

>
>
> On 11/23/2015 11:16 AM, Gregory Szorc wrote:
>
>> On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David
>> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
>> wrote:
>>
>>
>>
>>     On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>>
>>         On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>>         <pierre-yves.david@ens-lyon.org
>>         <mailto:pierre-yves.david@ens-lyon.org>
>>         <mailto:pierre-yves.david@ens-lyon.org
>>         <mailto:pierre-yves.david@ens-lyon.org>>>
>>         wrote:
>>
>>
>>
>>              On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>>
>>                  # HG changeset patch
>>                  # User Gregory Szorc <gregory.szorc@gmail.com
>>         <mailto:gregory.szorc@gmail.com>
>>                  <mailto:gregory.szorc@gmail.com
>>         <mailto:gregory.szorc@gmail.com>>>
>>                  # Date 1448168629 28800
>>                  #      Sat Nov 21 21:03:49 2015 -0800
>>                  # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>>                  # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>>                  hg: gracefully handle PyPy
>>
>>                  PyPy isn't compatible with C extensions nor the on
>>         demand module
>>                  import
>>                  mechanism.
>>
>>
>>              First 4 pushed to the clowncopter. Nice rework.
>>
>>              Check commit complains about find_module name, but I'll
>>         ignore that.
>>
>>
>>         There's nothing we can do about that: "find_module" is part of the
>>         import hook interface, which is defined by Python itself.
>>
>>
>>     And that is why I'll ignore it ;-)
>>
>>     However this series is breaking test-extensions.t when running from
>>     repository without local build (make local never ran). The pure
>>     finding logic apparently get confused when trying to generate
>>     __version__.py leading to the Mercurial to be isntall with -0+0 as a
>>     version number.
>>
>>     Can you look into that and send me a followup quickly ?
>>
>>
>> I'm unable to reproduce. Do you have more exact STR? (I think I'm
>> getting the run-tests.py arguments wrong.)
>>
>
> cd tests
> hg purge --all
> ./run-tests.py test-extensions.y
>

I ran purge before, but not without --all. Looks like a .egg-info directory
was interfering! I can reproduce this now. Looking at a fix.
Gregory Szorc - Nov. 24, 2015, 4:01 a.m.
On Mon, Nov 23, 2015 at 12:00 PM, Pierre-Yves David <
pierre-yves.david@ens-lyon.org> wrote:

>
>
> On 11/23/2015 11:16 AM, Gregory Szorc wrote:
>
>> On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David
>> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
>> wrote:
>>
>>
>>
>>     On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>>
>>         On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>>         <pierre-yves.david@ens-lyon.org
>>         <mailto:pierre-yves.david@ens-lyon.org>
>>         <mailto:pierre-yves.david@ens-lyon.org
>>         <mailto:pierre-yves.david@ens-lyon.org>>>
>>         wrote:
>>
>>
>>
>>              On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>>
>>                  # HG changeset patch
>>                  # User Gregory Szorc <gregory.szorc@gmail.com
>>         <mailto:gregory.szorc@gmail.com>
>>                  <mailto:gregory.szorc@gmail.com
>>         <mailto:gregory.szorc@gmail.com>>>
>>                  # Date 1448168629 28800
>>                  #      Sat Nov 21 21:03:49 2015 -0800
>>                  # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>>                  # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>>                  hg: gracefully handle PyPy
>>
>>                  PyPy isn't compatible with C extensions nor the on
>>         demand module
>>                  import
>>                  mechanism.
>>
>>
>>              First 4 pushed to the clowncopter. Nice rework.
>>
>>              Check commit complains about find_module name, but I'll
>>         ignore that.
>>
>>
>>         There's nothing we can do about that: "find_module" is part of the
>>         import hook interface, which is defined by Python itself.
>>
>>
>>     And that is why I'll ignore it ;-)
>>
>>     However this series is breaking test-extensions.t when running from
>>     repository without local build (make local never ran). The pure
>>     finding logic apparently get confused when trying to generate
>>     __version__.py leading to the Mercurial to be isntall with -0+0 as a
>>     version number.
>>
>>     Can you look into that and send me a followup quickly ?
>>
>>
>> I'm unable to reproduce. Do you have more exact STR? (I think I'm
>> getting the run-tests.py arguments wrong.)
>>
>
> cd tests
> hg purge --all
> ./run-tests.py test-extensions.y


OK. This is a "fun" regression.

Basically, setup.py is adding "mercurial/pure" to PYTHONPATH. This allows
the `hg log` and `hg id` invocations performed as part of calculating the
version string to work if no C extensions are present.

This issue is fixed in the dropped part 5 by changing setup.py to perform
the `hg` invocations with an import policy of "python only."

Since this regression only seems to impact test-extension.t and rewriting
the series to avoid the regression would involve a bit of work, is it
acceptable if I send along a proper part 5 [as opposed to rewriting the
series]? Feel free to drop this series from clowncopter in the interim.
Gregory Szorc - Nov. 24, 2015, 4:22 a.m.
On Mon, Nov 23, 2015 at 8:01 PM, Gregory Szorc <gregory.szorc@gmail.com>
wrote:

> On Mon, Nov 23, 2015 at 12:00 PM, Pierre-Yves David <
> pierre-yves.david@ens-lyon.org> wrote:
>
>>
>>
>> On 11/23/2015 11:16 AM, Gregory Szorc wrote:
>>
>>> On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David
>>> <pierre-yves.david@ens-lyon.org <mailto:pierre-yves.david@ens-lyon.org>>
>>> wrote:
>>>
>>>
>>>
>>>     On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>>>
>>>         On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>>>         <pierre-yves.david@ens-lyon.org
>>>         <mailto:pierre-yves.david@ens-lyon.org>
>>>         <mailto:pierre-yves.david@ens-lyon.org
>>>         <mailto:pierre-yves.david@ens-lyon.org>>>
>>>         wrote:
>>>
>>>
>>>
>>>              On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>>>
>>>                  # HG changeset patch
>>>                  # User Gregory Szorc <gregory.szorc@gmail.com
>>>         <mailto:gregory.szorc@gmail.com>
>>>                  <mailto:gregory.szorc@gmail.com
>>>         <mailto:gregory.szorc@gmail.com>>>
>>>                  # Date 1448168629 28800
>>>                  #      Sat Nov 21 21:03:49 2015 -0800
>>>                  # Node ID 1101c57a493083f9fd3dc2eb7832f862c587cd5a
>>>                  # Parent  b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>>>                  hg: gracefully handle PyPy
>>>
>>>                  PyPy isn't compatible with C extensions nor the on
>>>         demand module
>>>                  import
>>>                  mechanism.
>>>
>>>
>>>              First 4 pushed to the clowncopter. Nice rework.
>>>
>>>              Check commit complains about find_module name, but I'll
>>>         ignore that.
>>>
>>>
>>>         There's nothing we can do about that: "find_module" is part of
>>> the
>>>         import hook interface, which is defined by Python itself.
>>>
>>>
>>>     And that is why I'll ignore it ;-)
>>>
>>>     However this series is breaking test-extensions.t when running from
>>>     repository without local build (make local never ran). The pure
>>>     finding logic apparently get confused when trying to generate
>>>     __version__.py leading to the Mercurial to be isntall with -0+0 as a
>>>     version number.
>>>
>>>     Can you look into that and send me a followup quickly ?
>>>
>>>
>>> I'm unable to reproduce. Do you have more exact STR? (I think I'm
>>> getting the run-tests.py arguments wrong.)
>>>
>>
>> cd tests
>> hg purge --all
>> ./run-tests.py test-extensions.y
>
>
> OK. This is a "fun" regression.
>
> Basically, setup.py is adding "mercurial/pure" to PYTHONPATH. This allows
> the `hg log` and `hg id` invocations performed as part of calculating the
> version string to work if no C extensions are present.
>
> This issue is fixed in the dropped part 5 by changing setup.py to perform
> the `hg` invocations with an import policy of "python only."
>
> Since this regression only seems to impact test-extension.t and rewriting
> the series to avoid the regression would involve a bit of work, is it
> acceptable if I send along a proper part 5 [as opposed to rewriting the
> series]? Feel free to drop this series from clowncopter in the interim.
>
>
Actually, please drop the entire series: I need to rewrite a clowncoptered
patch to avoid a worse regression.
Pierre-Yves David - Nov. 24, 2015, 5:33 a.m.
On 11/23/2015 08:22 PM, Gregory Szorc wrote:
> On Mon, Nov 23, 2015 at 8:01 PM, Gregory Szorc <gregory.szorc@gmail.com
> <mailto:gregory.szorc@gmail.com>> wrote:
>
>     On Mon, Nov 23, 2015 at 12:00 PM, Pierre-Yves David
>     <pierre-yves.david@ens-lyon.org
>     <mailto:pierre-yves.david@ens-lyon.org>> wrote:
>
>
>
>         On 11/23/2015 11:16 AM, Gregory Szorc wrote:
>
>             On Sun, Nov 22, 2015 at 9:08 PM, Pierre-Yves David
>             <pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>
>             <mailto:pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>>>
>             wrote:
>
>
>
>                  On 11/22/2015 08:09 PM, Gregory Szorc wrote:
>
>                      On Sun, Nov 22, 2015 at 7:45 PM, Pierre-Yves David
>                      <pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>
>                      <mailto:pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>>
>                      <mailto:pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>
>                      <mailto:pierre-yves.david@ens-lyon.org
>             <mailto:pierre-yves.david@ens-lyon.org>>>>
>                      wrote:
>
>
>
>                           On 11/21/2015 10:14 PM, Gregory Szorc wrote:
>
>                               # HG changeset patch
>                               # User Gregory Szorc
>             <gregory.szorc@gmail.com <mailto:gregory.szorc@gmail.com>
>                      <mailto:gregory.szorc@gmail.com
>             <mailto:gregory.szorc@gmail.com>>
>                               <mailto:gregory.szorc@gmail.com
>             <mailto:gregory.szorc@gmail.com>
>                      <mailto:gregory.szorc@gmail.com
>             <mailto:gregory.szorc@gmail.com>>>>
>                               # Date 1448168629 28800
>                               #      Sat Nov 21 21:03:49 2015 -0800
>                               # Node ID
>             1101c57a493083f9fd3dc2eb7832f862c587cd5a
>                               # Parent
>             b1f0ae9d3abf60a1326e305237c31ce3fee7262b
>                               hg: gracefully handle PyPy
>
>                               PyPy isn't compatible with C extensions
>             nor the on
>                      demand module
>                               import
>                               mechanism.
>
>
>                           First 4 pushed to the clowncopter. Nice rework.
>
>                           Check commit complains about find_module name,
>             but I'll
>                      ignore that.
>
>
>                      There's nothing we can do about that: "find_module"
>             is part of the
>                      import hook interface, which is defined by Python
>             itself.
>
>
>                  And that is why I'll ignore it ;-)
>
>                  However this series is breaking test-extensions.t when
>             running from
>                  repository without local build (make local never ran).
>             The pure
>                  finding logic apparently get confused when trying to
>             generate
>                  __version__.py leading to the Mercurial to be isntall
>             with -0+0 as a
>                  version number.
>
>                  Can you look into that and send me a followup quickly ?
>
>
>             I'm unable to reproduce. Do you have more exact STR? (I
>             think I'm
>             getting the run-tests.py arguments wrong.)
>
>
>         cd tests
>         hg purge --all
>         ./run-tests.py test-extensions.y
>
>
>     OK. This is a "fun" regression.
>
>     Basically, setup.py is adding "mercurial/pure" to PYTHONPATH. This
>     allows the `hg log` and `hg id` invocations performed as part of
>     calculating the version string to work if no C extensions are present.
>
>     This issue is fixed in the dropped part 5 by changing setup.py to
>     perform the `hg` invocations with an import policy of "python only."
>
>     Since this regression only seems to impact test-extension.t and
>     rewriting the series to avoid the regression would involve a bit of
>     work, is it acceptable if I send along a proper part 5 [as opposed
>     to rewriting the series]? Feel free to drop this series from
>     clowncopter in the interim.
>
>
> Actually, please drop the entire series: I need to rewrite a
> clowncoptered patch to avoid a worse regression.

Series have been properly obliterated in the clowncopter.
Bryan O'Sullivan - Nov. 24, 2015, 10:29 p.m.
On Sun, Nov 22, 2015 at 1:14 AM, Gregory Szorc <gregory.szorc@gmail.com>
wrote:

> +if '__pypy__' in sys.builtin_module_names:
> +    pypy = True
> +else:
> +    pypy = False
>
>
pypy = '__pypy__' in sys.builtin_module_names

Patch

diff --git a/hg b/hg
--- a/hg
+++ b/hg
@@ -10,48 +10,62 @@ 
 import imp
 import os
 import sys
 
 if os.environ.get('HGUNICODEPEDANTRY', False):
     reload(sys)
     sys.setdefaultencoding("undefined")
 
+# PyPy doesn't load C extensions and isn't compatible with demand importing.
+#
+# The canonical way to do this is to test platform.python_implementation().
+# But we don't import platform anywhere, so don't require it here.
+if '__pypy__' in sys.builtin_module_names:
+    pypy = True
+else:
+    pypy = False
 
 libdir = '@LIBDIR@'
 
 # Rules for how modules can be loaded. Values are:
 #    c - require C extensions
 #    allow - allow pure Python implementation when C loading fails
 #    py - only load pure Python modules
 modulepolicy = '@MODULELOADPOLICY@'
 
 # By default, require the C extensions for performance reasons.
 if modulepolicy == '@' 'MODULELOADPOLICY' '@':
     modulepolicy = 'c'
 
+# PyPy isn't compatible with C extensions. So don't try to load them.
+if pypy:
+    modulepolicy = 'py'
+
 # Environment variable can always force settings.
 modulepolicy = os.environ.get('HGMODULEPOLICY', modulepolicy)
 
 if libdir != '@' 'LIBDIR' '@':
     if not os.path.isabs(libdir):
         libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               libdir)
         libdir = os.path.abspath(libdir)
     sys.path.insert(0, libdir)
 
-# enable importing on demand to reduce startup time
-try:
-    from mercurial import demandimport; demandimport.enable()
-except ImportError:
-    import sys
-    sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
-                     ' '.join(sys.path))
-    sys.stderr.write("(check your install and PYTHONPATH)\n")
-    sys.exit(-1)
+# On demand importing doesn't work with PyPy (yet).
+if not pypy:
+    # Enable importing on demand to reduce startup time.
+    try:
+        from mercurial import demandimport; demandimport.enable()
+    except ImportError:
+        import sys
+        sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
+                         ' '.join(sys.path))
+        sys.stderr.write("(check your install and PYTHONPATH)\n")
+        sys.exit(-1)
 
 # Install a PEP-302 custom module finder and loader that knows how to
 # import modules with implementations in both Python and C.
 
 # List of modules that have both Python and C implementations. See also the
 # set of .py files under mercurial/pure/.
 dualmodules = set([
     'mercurial.base85',