Ticket #5544: 5544.fixed.patch

File 5544.fixed.patch, 13.5 KB (added by zomux, 2 years ago)
  • twisted/test/test_reflect.py

     
    532532        reflect.safe_repr(X()) 
    533533 
    534534 
    535     def test_unsignedID(self): 
    536         """ 
    537         L{unsignedID} is used to print ID of the object in case of error, not 
    538         standard ID value which can be negative. 
    539         """ 
    540         class X(BTBase): 
    541             breakRepr = True 
    542  
    543         ids = {X: 100} 
    544         def fakeID(obj): 
    545             try: 
    546                 return ids[obj] 
    547             except (TypeError, KeyError): 
    548                 return id(obj) 
    549         self.addCleanup(util.setIDFunction, util.setIDFunction(fakeID)) 
    550  
    551         xRepr = reflect.safe_repr(X) 
    552         self.assertIn("0x64", xRepr) 
    553  
    554  
    555535    def test_brokenClassStr(self): 
    556536        class X(BTBase): 
    557537            breakStr = True 
  • twisted/test/test_defer.py

     
    1111from twisted.internet import reactor, defer 
    1212from twisted.internet.task import Clock 
    1313from twisted.python import failure, log 
    14 from twisted.python.util import unsignedID 
    1514 
    1615class GenericError(Exception): 
    1716    pass 
     
    872871        representation of the internal Python ID. 
    873872        """ 
    874873        d = defer.Deferred() 
    875         address = hex(unsignedID(d)) 
     874        address = hex(id(d)) 
    876875        self.assertEqual( 
    877876            repr(d), '<Deferred at %s>' % (address,)) 
    878877 
     
    886885        d.callback('orange') 
    887886        self.assertEqual( 
    888887            repr(d), "<Deferred at %s current result: 'orange'>" % ( 
    889                 hex(unsignedID(d)))) 
     888                hex(id(d)))) 
    890889 
    891890 
    892891    def test_reprWithChaining(self): 
     
    900899        b.chainDeferred(a) 
    901900        self.assertEqual( 
    902901            repr(a), "<Deferred at %s waiting on Deferred at %s>" % ( 
    903                 hex(unsignedID(a)), hex(unsignedID(b)))) 
     902                hex(id(a)), hex(id(b)))) 
    904903 
    905904 
    906905    def test_boundedStackDepth(self): 
  • twisted/test/test_amp.py

     
    1111 
    1212from zope.interface.verify import verifyObject 
    1313 
    14 from twisted.python.util import setIDFunction 
    1514from twisted.python import filepath 
    1615from twisted.python.failure import Failure 
    1716from twisted.protocols import amp 
     
    13571356        otherProto = TestProto(None, "outgoing data") 
    13581357        a = amp.AMP() 
    13591358        a.innerProtocol = otherProto 
    1360         def fakeID(obj): 
    1361             return {a: 0x1234}.get(obj, id(obj)) 
    1362         self.addCleanup(setIDFunction, setIDFunction(fakeID)) 
    13631359 
    13641360        self.assertEqual( 
    1365             repr(a), "<AMP inner <TestProto #%d> at 0x1234>" % ( 
    1366                 otherProto.instanceId,)) 
     1361            repr(a), "<AMP inner <TestProto #%d> at %s>" % ( 
     1362                otherProto.instanceId,hex(id(a)))) 
    13671363 
    13681364 
    13691365    def test_innerProtocolNotInRepr(self): 
     
    13721368        is set. 
    13731369        """ 
    13741370        a = amp.AMP() 
    1375         def fakeID(obj): 
    1376             return {a: 0x4321}.get(obj, id(obj)) 
    1377         self.addCleanup(setIDFunction, setIDFunction(fakeID)) 
    1378         self.assertEqual(repr(a), "<AMP at 0x4321>") 
     1371         
     1372        self.assertEqual(repr(a), "<AMP at %s>" % hex(id(a))) 
    13791373 
    13801374 
    13811375    def test_simpleSSLRepr(self): 
  • twisted/python/util.py

     
    764764                continue 
    765765            raise 
    766766 
    767 _idFunction = id 
    768767 
     768 
    769769def setIDFunction(idFunction): 
    770770    """ 
    771771    Change the function used by L{unsignedID} to determine the integer id value 
    772772    of an object.  This is largely useful for testing to give L{unsignedID} 
    773773    deterministic, easily-controlled behavior. 
    774774 
     775    Deprecated since Twisted 12.0.0 
     776 
     777 
    775778    @param idFunction: A function with the signature of L{id}. 
    776779    @return: The previous function being used by L{unsignedID}. 
    777780    """ 
    778     global _idFunction 
    779     oldIDFunction = _idFunction 
    780     _idFunction = idFunction 
    781     return oldIDFunction 
     781    pass 
    782782 
    783783 
    784 # A value about twice as large as any Python int, to which negative values 
    785 # from id() will be added, moving them into a range which should begin just 
    786 # above where positive values from id() leave off. 
    787 _HUGEINT = (sys.maxint + 1L) * 2L 
     784 
    788785def unsignedID(obj): 
    789786    """ 
    790787    Return the id of an object as an unsigned number so that its hex 
    791788    representation makes sense. 
    792789 
    793     This is mostly necessary in Python 2.4 which implements L{id} to sometimes 
    794     return a negative value.  Python 2.3 shares this behavior, but also 
    795     implements hex and the %x format specifier to represent negative values as 
    796     though they were positive ones, obscuring the behavior of L{id}.  Python 
    797     2.5's implementation of L{id} always returns positive values. 
     790    Deprecated since Twisted 12.0.0 , use built-in function id instead 
    798791    """ 
    799     rval = _idFunction(obj) 
    800     if rval < 0: 
    801         rval += _HUGEINT 
    802     return rval 
     792    warnings.warn("twisted.python.util.unsignedID is deprecated since Twisted 12.0.0." 
     793                  "Use built-in function id instead.", 
     794                  category=DeprecationWarning, stacklevel=2) 
     795    return id(obj) 
    803796 
    804797 
     798 
    805799def mergeFunctionMetadata(f, g): 
    806800    """ 
    807801    Overwrite C{g}'s name and docstring with values from C{f}.  Update 
     
    978972    "getPassword", "println", "makeStatBar", "OrderedDict", 
    979973    "InsensitiveDict", "spewer", "searchupwards", "LineLog", 
    980974    "raises", "IntervalDifferential", "FancyStrMixin", "FancyEqMixin", 
    981     "switchUID", "SubclassableCStringIO", "unsignedID", "mergeFunctionMetadata", 
     975    "switchUID", "SubclassableCStringIO", "mergeFunctionMetadata", 
    982976    "nameToLabel", "uidFromString", "gidFromString", "runAsEffectiveUser", 
    983977] 
  • twisted/python/reflect.py

     
    2929except ImportError: 
    3030    from StringIO import StringIO 
    3131 
    32 from twisted.python.util import unsignedID 
    3332from twisted.python.deprecate import deprecated, deprecatedModuleAttribute 
    3433from twisted.python.deprecate import _fullyQualifiedName as fullyQualifiedName 
    3534from twisted.python.versions import Version 
     
    536535        try: 
    537536            return str(c) 
    538537        except: 
    539             return '<BROKEN CLASS AT 0x%x>' % unsignedID(c) 
     538            return '<BROKEN CLASS AT 0x%x>' % id(c) 
    540539 
    541540 
    542541 
     
    552551        className = _determineClassName(o) 
    553552        tbValue = io.getvalue() 
    554553        return "<%s instance at 0x%x with %s error:\n %s>" % ( 
    555             className, unsignedID(o), formatter.__name__, tbValue) 
     554            className, id(o), formatter.__name__, tbValue) 
    556555 
    557556 
    558557 
  • twisted/python/test/test_util.py

     
    777777 
    778778 
    779779 
    780 class UnsignedIDTests(unittest.TestCase): 
    781     """ 
    782     Tests for L{util.unsignedID} and L{util.setIDFunction}. 
    783     """ 
    784     def setUp(self): 
    785         """ 
    786         Save the value of L{util._idFunction} and arrange for it to be restored 
    787         after the test runs. 
    788         """ 
    789         self.addCleanup(setattr, util, '_idFunction', util._idFunction) 
    790  
    791  
    792     def test_setIDFunction(self): 
    793         """ 
    794         L{util.setIDFunction} returns the last value passed to it. 
    795         """ 
    796         value = object() 
    797         previous = util.setIDFunction(value) 
    798         result = util.setIDFunction(previous) 
    799         self.assertIdentical(value, result) 
    800  
    801  
    802     def test_unsignedID(self): 
    803         """ 
    804         L{util.unsignedID} uses the function passed to L{util.setIDFunction} to 
    805         determine the unique integer id of an object and then adjusts it to be 
    806         positive if necessary. 
    807         """ 
    808         foo = object() 
    809         bar = object() 
    810  
    811         # A fake object identity mapping 
    812         objects = {foo: 17, bar: -73} 
    813         def fakeId(obj): 
    814             return objects[obj] 
    815  
    816         util.setIDFunction(fakeId) 
    817  
    818         self.assertEqual(util.unsignedID(foo), 17) 
    819         self.assertEqual(util.unsignedID(bar), (sys.maxint + 1) * 2 - 73) 
    820  
    821  
    822     def test_defaultIDFunction(self): 
    823         """ 
    824         L{util.unsignedID} uses the built in L{id} by default. 
    825         """ 
    826         obj = object() 
    827         idValue = id(obj) 
    828         if idValue < 0: 
    829             idValue += (sys.maxint + 1) * 2 
    830  
    831         self.assertEqual(util.unsignedID(obj), idValue) 
    832  
    833  
    834  
    835780class InitGroupsTests(unittest.TestCase): 
    836781    """ 
    837782    Tests for L{util.initgroups}. 
  • twisted/protocols/amp.py

     
    182182from zope.interface import Interface, implements 
    183183 
    184184from twisted.python.compat import set 
    185 from twisted.python.util import unsignedID 
    186185from twisted.python.reflect import accumulateClassDict 
    187186from twisted.python.failure import Failure 
    188187from twisted.python import log, filepath 
     
    22742273        else: 
    22752274            innerRepr = '' 
    22762275        return '<%s%s at 0x%x>' % ( 
    2277             self.__class__.__name__, innerRepr, unsignedID(self)) 
     2276            self.__class__.__name__, innerRepr, id(self)) 
    22782277 
    22792278 
    22802279    def makeConnection(self, transport): 
  • twisted/topfiles/5544.removal

     
     1twisted.python.util.unsignedID , deprecated for it can be replaced by built-in function "id" after Python 2.5. 
  • twisted/internet/defer.py

     
    2323 
    2424# Twisted imports 
    2525from twisted.python import log, failure, lockfile 
    26 from twisted.python.util import unsignedID, mergeFunctionMetadata 
     26from twisted.python.util import mergeFunctionMetadata 
    2727 
    2828 
    2929 
     
    607607        """ 
    608608        cname = self.__class__.__name__ 
    609609        result = getattr(self, 'result', _NO_RESULT) 
    610         myID = hex(unsignedID(self)) 
     610        myID = hex(id(self)) 
    611611        if self._chainedTo is not None: 
    612             result = ' waiting on Deferred at %s' % (hex(unsignedID(self._chainedTo)),) 
     612            result = ' waiting on Deferred at %s' % (hex(id(self._chainedTo)),) 
    613613        elif result is _NO_RESULT: 
    614614            result = '' 
    615615        else: 
  • twisted/internet/tcp.py

     
    9292from twisted.internet import base, address, fdesc 
    9393from twisted.internet.task import deferLater 
    9494from twisted.python import log, failure, reflect 
    95 from twisted.python.util import unsignedID 
    9695from twisted.internet.error import CannotListenError 
    9796from twisted.internet import abstract, main, interfaces, error 
    9897 
     
    709708 
    710709 
    711710    def __repr__(self): 
    712         s = '<%s to %s at %x>' % (self.__class__, self.addr, unsignedID(self)) 
     711        s = '<%s to %s at %x>' % (self.__class__, self.addr, id(self)) 
    713712        return s 
    714713 
    715714 
  • twisted/internet/test/test_base.py

     
    1111from zope.interface import implements 
    1212 
    1313from twisted.python.threadpool import ThreadPool 
    14 from twisted.python.util import setIDFunction 
    1514from twisted.internet.interfaces import IReactorTime, IReactorThreads 
    1615from twisted.internet.error import DNSLookupError 
    1716from twisted.internet.base import ThreadedResolver, DelayedCall 
     
    188187        def nothing(): 
    189188            pass 
    190189        dc = DelayedCall(12, nothing, (3, ), {"A": 5}, None, None, lambda: 1.5) 
    191         ids = {dc: 200} 
    192         def fakeID(obj): 
    193             try: 
    194                 return ids[obj] 
    195             except (TypeError, KeyError): 
    196                 return id(obj) 
    197         self.addCleanup(setIDFunction, setIDFunction(fakeID)) 
     190         
    198191        self.assertEqual( 
    199192            str(dc), 
    200             "<DelayedCall 0xc8 [10.5s] called=0 cancelled=0 nothing(3, A=5)>") 
     193            "<DelayedCall %s [10.5s] called=0 cancelled=0 nothing(3, A=5)>" % hex(id(dc))) 
    201194 
    202195 
    203196    def test_lt(self): 
  • twisted/internet/base.py

     
    1616import traceback 
    1717 
    1818from twisted.python.compat import set 
    19 from twisted.python.util import unsignedID 
    2019from twisted.internet.interfaces import IReactorCore, IReactorTime, IReactorThreads 
    2120from twisted.internet.interfaces import IResolverSimple, IReactorPluggableResolver 
    2221from twisted.internet.interfaces import IConnector, IDelayedCall 
     
    186185 
    187186        now = self.seconds() 
    188187        L = ["<DelayedCall 0x%x [%ss] called=%s cancelled=%s" % ( 
    189                 unsignedID(self), self.time - now, self.called, 
     188                id(self), self.time - now, self.called, 
    190189                self.cancelled)] 
    191190        if func is not None: 
    192191            L.extend((" ", func, "("))