[mod] move get_pdb to utils.py
authorLaurent Peuch <cortex@worlddomination.be>
Wed, 22 May 2019 14:23:01 +0200
changeset 12743 a74e77469540
parent 12742 ca698656251c
child 12744 19aef4729d45
[mod] move get_pdb to utils.py
cubicweb/cwctl.py
cubicweb/test/unittest_cwctl.py
cubicweb/utils.py
--- a/cubicweb/cwctl.py	Wed Oct 16 14:49:38 2019 +0200
+++ b/cubicweb/cwctl.py	Wed May 22 14:23:01 2019 +0200
@@ -40,7 +40,7 @@
 from logilab.common.configuration import merge_options
 from logilab.common.decorators import clear_cache
 
-from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
+from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage, utils
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CONFIGURATIONS
 from cubicweb.server import set_debug
 from cubicweb.toolsutils import Command, rm, create_dir, underline_title
@@ -102,16 +102,6 @@
     return [drop_prefix(cube) for cube in cwcfg.available_cubes()]
 
 
-def get_pdb():
-    try:
-        import ipdb
-    except ImportError:
-        import pdb
-        return pdb
-    else:
-        return ipdb
-
-
 class InstanceCommand(Command):
     """base class for command taking one instance id as arguments"""
     arguments = '<instance>'
@@ -208,7 +198,7 @@
                 status = ex.code
 
         if status != 0 and self.config.pdb:
-            pdb = get_pdb()
+            pdb = utils.get_pdb()
 
             if traceback_ is not None:
                 pdb.post_mortem(traceback_)
--- a/cubicweb/test/unittest_cwctl.py	Wed Oct 16 14:49:38 2019 +0200
+++ b/cubicweb/test/unittest_cwctl.py	Wed May 22 14:23:01 2019 +0200
@@ -24,7 +24,7 @@
 
 from logilab.common.clcommands import CommandLine
 
-from cubicweb import cwctl, server
+from cubicweb import utils, server
 from cubicweb.cwctl import ListCommand, InstanceCommand
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.server.migractions import ServerMigrationHelper
@@ -123,7 +123,7 @@
         self.assertEqual(cm.exception.code, 0)
         test_instance.assert_called_with("some_instance")
 
-    @patch.object(cwctl, 'get_pdb')
+    @patch.object(utils, 'get_pdb')
     def test_pdb_not_called(self, get_pdb):
         # CWCTL will finish the program after that
         with self.assertRaises(SystemExit) as cm:
@@ -132,7 +132,7 @@
 
         get_pdb.assert_not_called()
 
-    @patch.object(cwctl, 'get_pdb')
+    @patch.object(utils, 'get_pdb')
     def test_pdb_called(self, get_pdb):
         post_mortem = get_pdb.return_value.post_mortem
         with self.assertRaises(SystemExit) as cm:
--- a/cubicweb/utils.py	Wed Oct 16 14:49:38 2019 +0200
+++ b/cubicweb/utils.py	Wed May 22 14:23:01 2019 +0200
@@ -565,6 +565,17 @@
     return 'javascript: ' + PERCENT_IN_URLQUOTE_RE.sub(r'%25', javascript_code)
 
 
+def get_pdb():
+    "return ipdb if its installed, otherwise pdb"
+    try:
+        import ipdb
+    except ImportError:
+        import pdb
+        return pdb
+    else:
+        return ipdb
+
+
 logger = getLogger('cubicweb.utils')
 
 class QueryCache(object):