Ticket #6066: 6066_v2.diff

File 6066_v2.diff, 5.6 KB (added by borko, 4 years ago)

diff 2

  • twisted/internet/base.py

    diff --git a/twisted/internet/base.py b/twisted/internet/base.py
    index 46e9217..5f10c6c 100644
    a b class DelayedCall: 
    176176        if self._str is not None:
    177177            return self._str
    178178        if hasattr(self, 'func'):
    179             # This code should be replaced by a utility function in reflect;
    180             # see ticket #6066:
    181             if hasattr(self.func, '__qualname__'):
    182                 func = self.func.__qualname__
    183             elif hasattr(self.func, '__name__'):
    184                 func = self.func.func_name
    185                 if hasattr(self.func, 'im_class'):
    186                     func = self.func.im_class.__name__ + '.' + func
    187             else:
    188                 func = reflect.safe_repr(self.func)
     179            func = reflect.getFunctionName(self.func)
    189180        else:
    190181            func = None
    191182
  • twisted/internet/task.py

    diff --git a/twisted/internet/task.py b/twisted/internet/task.py
    index 6e7b908..11afa17 100644
    a b class LoopingCall: 
    244244
    245245
    246246    def __repr__(self):
    247         if hasattr(self.f, '__qualname__'):
    248             func = self.f.__qualname__
    249         elif hasattr(self.f, '__name__'):
    250             func = self.f.__name__
    251             if hasattr(self.f, 'im_class'):
    252                 func = self.f.im_class.__name__ + '.' + func
    253         else:
    254             func = reflect.safe_repr(self.f)
    255 
    256247        return 'LoopingCall<%r>(%s, *%s, **%s)' % (
    257             self.interval, func, reflect.safe_repr(self.a),
     248            self.interval, reflect.getFunctionName(self.f), reflect.safe_repr(self.a),
    258249            reflect.safe_repr(self.kw))
    259250
    260251
  • twisted/python/_reflectpy3.py

    diff --git a/twisted/python/_reflectpy3.py b/twisted/python/_reflectpy3.py
    index c0451e9..04c6856 100644
    a b def _determineClassName(x): 
    286286            return '<BROKEN CLASS AT 0x%x>' % unsignedID(c)
    287287
    288288
     289def getFunctionName(function):
     290    """
     291    Return name of the function passed.
     292
     293    @param funptr: Searched function.
     294
     295    @return: Function name
     296    """
     297    if hasattr( function , '__qualname__'):
     298        func = function.__qualname__
     299    elif hasattr( function, '__name__'):
     300        func = function.func_name
     301        if hasattr(function, 'im_class'):
     302            func = function.im_class.__name__ + '.' + func
     303    else:
     304        func = safe_repr(function)
     305    return func
     306
     307
    289308def _safeFormat(formatter, o):
    290309    """
    291310    Helper function for L{safe_repr} and L{safe_str}.
  • twisted/python/reflect.py

    diff --git a/twisted/python/reflect.py b/twisted/python/reflect.py
    index 541d99f..9c87b1d 100644
    a b from twisted.python._reflectpy3 import ( 
    3737    addMethodNamesToDict)
    3838from twisted.python._reflectpy3 import namedModule, namedObject, namedClass
    3939from twisted.python._reflectpy3 import InvalidName, ModuleNotFound
    40 from twisted.python._reflectpy3 import ObjectNotFound, namedAny
     40from twisted.python._reflectpy3 import ObjectNotFound, namedAny, getFunctionName
    4141from twisted.python._reflectpy3 import filenameToModuleName
    4242from twisted.python._reflectpy3 import qual, safe_str, safe_repr
    4343
    class Summer(Accessor): 
    249249
    250250    deprecatedModuleAttribute(
    251251        Version("Twisted", 12, 1, 0),
    252         "Summer is a child class of twisted.python.reflect.Accessor which is " 
     252        "Summer is a child class of twisted.python.reflect.Accessor which is "
    253253        "deprecated.", "twisted.python.reflect", "Summer")
    254254
    255255    def reallySet(self, k,v):
    def accumulateClassDict(classObj, attr, adict, baseClass=None): 
    412412
    413413      class Soy:
    414414        properties = {\"taste\": \"bland\"}
    415    
     415
    416416      class Plant:
    417417        properties = {\"colour\": \"green\"}
    418    
     418
    419419      class Seaweed(Plant):
    420420        pass
    421    
     421
    422422      class Lunch(Soy, Seaweed):
    423423        properties = {\"vegan\": 1 }
    424    
     424
    425425      dct = {}
    426    
     426
    427427      accumulateClassDict(Lunch, \"properties\", dct)
    428    
     428
    429429      print dct
    430430
    431431    {\"taste\": \"bland\", \"colour\": \"green\", \"vegan\": 1}
    __all__ = [ 
    534534    'accumulateMethods',
    535535    'accumulateClassDict', 'accumulateClassList', 'isSame', 'isLike',
    536536    'modgrep', 'isOfType', 'findInstances', 'objgrep', 'filenameToModuleName',
    537     'fullyQualifiedName']
     537    'fullyQualifiedName', 'getFunctionName']
  • twisted/python/test/test_reflectpy3.py

    diff --git a/twisted/python/test/test_reflectpy3.py b/twisted/python/test/test_reflectpy3.py
    index acd8f8d..e942dc4 100644
    a b class LookupsTestCase(TestCase): 
    350350
    351351
    352352
     353class GetFunctionNameTestCase(TestCase):
     354    """
     355    Tests for L{getFunctionName}.
     356    """
     357
     358    def test_function(self):
     359        """
     360        L{getFunctionName} should return function name passed in the argument.
     361        """
     362        self.assertEqual(
     363            reflect.getFunctionName(reflect.getFunctionName),
     364            "getFunctionName")
     365
     366
     367    def test_memberFunction(self):
     368        """
     369        L{getFunctionName} should return fully qualified function name passed in the argument.
     370        """
     371        self.assertEqual(
     372            reflect.getFunctionName(self.test_memberFunction),
     373            "GetFunctionNameTestCase.test_memberFunction")
     374
     375
     376    def test_safeRepr(self):
     377        """
     378        L{getFunctionName} should return string representation of non-function object passed.
     379        """
     380        i = 45
     381        self.assertEqual(reflect.getFunctionName(i), "45")
     382        string = "string"
     383        self.assertEqual(reflect.getFunctionName(string), "'string'")
     384
     385
     386
    353387class Breakable(object):
    354388
    355389    breakRepr = False