Revert my changes, this is a much bigger job.

This commit is contained in:
Jean-Paul Calderone 2019-08-13 15:38:33 -04:00
parent 132cc4605d
commit d69cde293a

View File

@ -1,37 +1,31 @@
"""
Tests to check for Python2 regressions
"""
from types import (
ClassType,
)
from twisted.trial import unittest
from twisted.python.modules import getModule
def is_classic_class(obj):
"""check an object being a classic class"""
# issubclass() is a great idea but it blows up if the first argument is
# not a class. So ... less than completely useful.
return type(obj) is ClassType
def defined_here(obj, where):
return obj.__module__ == where
class PythonTwoRegressions(unittest.TestCase):
"""
A test class to hold Python2 regression tests.
"""
def test_new_style_class(self):
def is_new_style(self, cls):
"""check for being a new-style class"""
# another test could be: issubclass(value, type)
has_class_attr = hasattr(cls, '__class__')
dict_or_slots = '__dict__' in dir(cls) or hasattr(cls, '__slots__')
return has_class_attr and dict_or_slots
def test_old_style_class(self):
"""
All classes defined by Tahoe-LAFS are new-style.
Check if all classes are new-style classes
"""
for mod in getModule("allmydata").walkModules():
# Cannot iterate attributes of unloaded modules.
mod.load()
for attr in mod.iterAttributes():
value = attr.load()
self.assertFalse(
is_classic_class(value) and defined_here(value, mod.name),
"{} appears to be a classic class".format(attr.name),
)
if isinstance(value, str):
# apparently strings are note a new-style class (in Python 2.7)
# so we skip testing them
return
self.assertTrue(self.is_new_style(value),
"{} does not seem to be a new-style class".format(attr.name))