~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-09-14 03:01:25 UTC
  • mfrom: (4574.3.12 406113-extension-warnings)
  • Revision ID: pqm@pqm.ubuntu.com-20090914030125-wichbbiuuk4260y4
(mbp) warnings if extensions can't be loaded

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
                  S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
22
22
import sys
23
23
import time
 
24
import warnings
24
25
 
25
26
from bzrlib.lazy_import import lazy_import
26
27
lazy_import(globals(), """
881
882
    return parents
882
883
 
883
884
 
 
885
_extension_load_failures = []
 
886
 
 
887
 
 
888
def failed_to_load_extension(exception):
 
889
    """Handle failing to load a binary extension.
 
890
 
 
891
    This should be called from the ImportError block guarding the attempt to
 
892
    import the native extension.  If this function returns, the pure-Python
 
893
    implementation should be loaded instead::
 
894
 
 
895
    >>> try:
 
896
    >>>     import bzrlib._fictional_extension_pyx
 
897
    >>> except ImportError, e:
 
898
    >>>     bzrlib.osutils.failed_to_load_extension(e)
 
899
    >>>     import bzrlib._fictional_extension_py
 
900
    """
 
901
    # NB: This docstring is just an example, not a doctest, because doctest
 
902
    # currently can't cope with the use of lazy imports in this namespace --
 
903
    # mbp 20090729
 
904
    
 
905
    # This currently doesn't report the failure at the time it occurs, because
 
906
    # they tend to happen very early in startup when we can't check config
 
907
    # files etc, and also we want to report all failures but not spam the user
 
908
    # with 10 warnings.
 
909
    from bzrlib import trace
 
910
    exception_str = str(exception)
 
911
    if exception_str not in _extension_load_failures:
 
912
        trace.mutter("failed to load compiled extension: %s" % exception_str)
 
913
        _extension_load_failures.append(exception_str)
 
914
 
 
915
 
 
916
def report_extension_load_failures():
 
917
    if not _extension_load_failures:
 
918
        return
 
919
    from bzrlib.config import GlobalConfig
 
920
    if GlobalConfig().get_user_option_as_bool('ignore_missing_extensions'):
 
921
        return
 
922
    # the warnings framework should by default show this only once
 
923
    warnings.warn(
 
924
        "bzr: warning: Failed to load compiled extensions:\n"
 
925
        "    %s\n" 
 
926
        "    Bazaar can run, but performance may be reduced.\n"
 
927
        "    Check Bazaar is correctly installed or set ignore_missing_extensions"
 
928
        % '\n    '.join(_extension_load_failures,))
 
929
 
 
930
 
884
931
try:
885
932
    from bzrlib._chunks_to_lines_pyx import chunks_to_lines
886
 
except ImportError:
 
933
except ImportError, e:
 
934
    failed_to_load_extension(e)
887
935
    from bzrlib._chunks_to_lines_py import chunks_to_lines
888
936
 
889
937
 
1466
1514
            try:
1467
1515
                from bzrlib._readdir_pyx import UTF8DirReader
1468
1516
                _selected_dir_reader = UTF8DirReader()
1469
 
            except ImportError:
 
1517
            except ImportError, e:
 
1518
                failed_to_load_extension(e)
1470
1519
                pass
1471
1520
 
1472
1521
    if _selected_dir_reader is None:
1778
1827
        try:
1779
1828
            from bzrlib._readdir_pyx import UTF8DirReader
1780
1829
            file_kind_from_stat_mode = UTF8DirReader().kind_from_mode
1781
 
        except ImportError:
 
1830
        except ImportError, e:
 
1831
            failed_to_load_extension(e)
1782
1832
            from bzrlib._readdir_py import (
1783
1833
                _kind_from_mode as file_kind_from_stat_mode
1784
1834
                )