]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 61203-61204 via svnmerge from
authorChristian Heimes <christian@cheimes.de>
Mon, 3 Mar 2008 19:18:51 +0000 (19:18 +0000)
committerChristian Heimes <christian@cheimes.de>
Mon, 3 Mar 2008 19:18:51 +0000 (19:18 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r61203 | christian.heimes | 2008-03-03 13:40:17 +0100 (Mon, 03 Mar 2008) | 3 lines

  Initialized merge tracking via "svnmerge" with revisions "1-60195" from
  svn+ssh://pythondev@svn.python.org/python/branches/trunk-math
........
  r61204 | christian.heimes | 2008-03-03 19:28:04 +0100 (Mon, 03 Mar 2008) | 1 line

  Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
........

Doc/library/inspect.rst
Lib/inspect.py
Lib/test/regrtest.py
Lib/test/test_abc.py

index 03ee17f95c9f59a4ee59a10898748fd127690363..c2bc15c2a01ba442dc36661f42af340948be215d 100644 (file)
@@ -263,6 +263,12 @@ attributes:
 
    Return true if the object is a user-defined or built-in function or method.
 
+.. function:: isabstract(object)
+
+   Return true if the object is an abstract base class.
+
+   .. versionadded:: 2.6
+
 
 .. function:: ismethoddescriptor(object)
 
index ddd7529b5467921019d6f9755492fd1f8d96f921..ceaea5ae111a478bffb96ad1fb29359b716cfa36 100644 (file)
@@ -39,12 +39,16 @@ import dis
 import imp
 import tokenize
 import linecache
+from abc import ABCMeta
 from operator import attrgetter
 from collections import namedtuple
 # These constants are from Include/code.h.
 CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 0x1, 0x2, 0x4, 0x8
 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
 
+# See Include/object.h
+TPFLAGS_IS_ABSTRACT = 1 << 20
+
 # ----------------------------------------------------------- type-checking
 def ismodule(object):
     """Return true if the object is a module.
@@ -241,6 +245,10 @@ def isgenerator(object):
     """Return true if the object is a generator object."""
     return isinstance(object, types.GeneratorType)
 
+def isabstract(object):
+    """Return true if the object is an abstract base class (ABC)."""
+    return object.__flags__ & TPFLAGS_IS_ABSTRACT
+
 def getmembers(object, predicate=None):
     """Return all members of an object as (name, value) pairs sorted by name.
     Optionally, only return members that satisfy a given predicate."""
index 50ed93ebd52210dae6d4de555244dc8770056f06..cb7cf7512ef22d1ba1529364f96af91a87f43264 100755 (executable)
@@ -135,6 +135,7 @@ import warnings
 import re
 import io
 import traceback
+from inspect import isabstract
 
 # I see no other way to suppress these warnings;
 # putting them in test_grammar.py has no effect:
@@ -689,7 +690,6 @@ def cleanup_test_droppings(testname, verbose):
 def dash_R(the_module, test, indirect_test, huntrleaks):
     # This code is hackish and inelegant, but it seems to do the job.
     import copy_reg, _abcoll
-    from abc import _Abstract
 
     if not hasattr(sys, 'gettotalrefcount'):
         raise Exception("Tracking reference leaks requires a debug build "
@@ -701,7 +701,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
     pic = sys.path_importer_cache.copy()
     abcs = {}
     for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
-        if not issubclass(abc, _Abstract):
+        if not isabstract(abc):
             continue
         for obj in abc.__subclasses__() + [abc]:
             abcs[obj] = obj._abc_registry.copy()
@@ -741,7 +741,6 @@ def dash_R_cleanup(fs, ps, pic, abcs):
     import _strptime, linecache, dircache
     import urlparse, urllib, urllib2, mimetypes, doctest
     import struct, filecmp, _abcoll
-    from abc import _Abstract
     from distutils.dir_util import _path_created
     from weakref import WeakSet
 
@@ -757,7 +756,7 @@ def dash_R_cleanup(fs, ps, pic, abcs):
 
     # Clear ABC registries, restoring previously saved ABC registries.
     for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
-        if not issubclass(abc, _Abstract):
+        if not isabstract(abc):
             continue
         for obj in abc.__subclasses__() + [abc]:
             obj._abc_registry = abcs.get(obj, WeakSet()).copy()
index 7a932fcf30987f6517fdc460def97c70c7285df0..d631c3ebf5061ecea99e4a070e538d7800772f2c 100644 (file)
@@ -7,6 +7,7 @@ import unittest
 from test import test_support
 
 import abc
+from inspect import isabstract
 
 
 class TestABC(unittest.TestCase):
@@ -41,19 +42,23 @@ class TestABC(unittest.TestCase):
                 def bar(self): pass  # concrete
             self.assertEqual(C.__abstractmethods__, {"foo"})
             self.assertRaises(TypeError, C)  # because foo is abstract
+            self.assert_(isabstract(C))
             class D(C):
                 def bar(self): pass  # concrete override of concrete
             self.assertEqual(D.__abstractmethods__, {"foo"})
             self.assertRaises(TypeError, D)  # because foo is still abstract
+            self.assert_(isabstract(D))
             class E(D):
                 def foo(self): pass
             self.assertEqual(E.__abstractmethods__, set())
             E()  # now foo is concrete, too
+            self.failIf(isabstract(E))
             class F(E):
                 @abstractthing
                 def bar(self): pass  # abstract override of concrete
             self.assertEqual(F.__abstractmethods__, {"bar"})
             self.assertRaises(TypeError, F)  # because bar is abstract now
+            self.assert_(isabstract(F))
 
     def test_subclass_oldstyle_class(self):
         class A: