diff options
author | Collin Winter <collinw@gmail.com> | 2007-07-17 20:59:35 (GMT) |
---|---|---|
committer | Collin Winter <collinw@gmail.com> | 2007-07-17 20:59:35 (GMT) |
commit | 6f2df4d5e193d54244b0c2de91ef0ab1604b9243 (patch) | |
tree | 5e172400da7561eb4bb8fafc62c8cab511d74dad /Demo/metaclasses | |
parent | a8c360ee76fb76902a2e2140fbb38d4b06b2d9fb (diff) | |
download | cpython-6f2df4d5e193d54244b0c2de91ef0ab1604b9243.zip cpython-6f2df4d5e193d54244b0c2de91ef0ab1604b9243.tar.gz cpython-6f2df4d5e193d54244b0c2de91ef0ab1604b9243.tar.bz2 |
Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Diffstat (limited to 'Demo/metaclasses')
-rw-r--r-- | Demo/metaclasses/Enum.py | 45 | ||||
-rw-r--r-- | Demo/metaclasses/Meta.py | 18 | ||||
-rw-r--r-- | Demo/metaclasses/Simple.py | 6 | ||||
-rw-r--r-- | Demo/metaclasses/Synch.py | 41 | ||||
-rw-r--r-- | Demo/metaclasses/Trace.py | 42 |
5 files changed, 75 insertions, 77 deletions
diff --git a/Demo/metaclasses/Enum.py b/Demo/metaclasses/Enum.py index df1d814..8d402db 100644 --- a/Demo/metaclasses/Enum.py +++ b/Demo/metaclasses/Enum.py @@ -37,12 +37,12 @@ class EnumMetaClass: """ for base in bases: if base.__class__ is not EnumMetaClass: - raise TypeError, "Enumeration base class must be enumeration" - bases = filter(lambda x: x is not Enum, bases) + raise TypeError("Enumeration base class must be enumeration") + bases = [x for x in bases if x is not Enum] self.__name__ = name self.__bases__ = bases self.__dict = {} - for key, value in dict.items(): + for key, value in list(dict.items()): self.__dict[key] = EnumInstance(name, key, value) def __getattr__(self, name): @@ -61,7 +61,7 @@ class EnumMetaClass: """ if name == '__members__': - return self.__dict.keys() + return list(self.__dict.keys()) try: return self.__dict[name] @@ -72,16 +72,15 @@ class EnumMetaClass: except AttributeError: continue - raise AttributeError, name + raise AttributeError(name) def __repr__(self): s = self.__name__ if self.__bases__: - s = s + '(' + string.join(map(lambda x: x.__name__, - self.__bases__), ", ") + ')' + s = s + '(' + string.join([x.__name__ for x in self.__bases__], ", ") + ')' if self.__dict: list = [] - for key, value in self.__dict.items(): + for key, value in list(self.__dict.items()): list.append("%s: %s" % (key, int(value))) s = "%s: {%s}" % (s, string.join(list, ", ")) return s @@ -130,13 +129,13 @@ def _test(): green = 2 blue = 3 - print Color.red - print dir(Color) + print(Color.red) + print(dir(Color)) - print Color.red == Color.red - print Color.red == Color.blue - print Color.red == 1 - print Color.red == 2 + print(Color.red == Color.red) + print(Color.red == Color.blue) + print(Color.red == 1) + print(Color.red == 2) class ExtendedColor(Color): white = 0 @@ -145,10 +144,10 @@ def _test(): purple = 6 black = 7 - print ExtendedColor.orange - print ExtendedColor.red + print(ExtendedColor.orange) + print(ExtendedColor.red) - print Color.red == ExtendedColor.red + print(Color.red == ExtendedColor.red) class OtherColor(Enum): white = 4 @@ -157,13 +156,13 @@ def _test(): class MergedColor(Color, OtherColor): pass - print MergedColor.red - print MergedColor.white + print(MergedColor.red) + print(MergedColor.white) - print Color - print ExtendedColor - print OtherColor - print MergedColor + print(Color) + print(ExtendedColor) + print(OtherColor) + print(MergedColor) if __name__ == '__main__': _test() diff --git a/Demo/metaclasses/Meta.py b/Demo/metaclasses/Meta.py index 9529e0f..90bfd97 100644 --- a/Demo/metaclasses/Meta.py +++ b/Demo/metaclasses/Meta.py @@ -31,7 +31,7 @@ class MetaHelper: try: ga = self.__formalclass__.__getattr__('__usergetattr__') except (KeyError, AttributeError): - raise AttributeError, name + raise AttributeError(name) return ga(self, name) if type(raw) != types.FunctionType: return raw @@ -71,7 +71,7 @@ class MetaClass: return base.__getattr__(name) except AttributeError: pass - raise AttributeError, name + raise AttributeError(name) def __setattr__(self, name, value): if not self.__inited: @@ -96,20 +96,20 @@ Meta = MetaClass('Meta', (), {}) def _test(): class C(Meta): def __init__(self, *args): - print "__init__, args =", args + print("__init__, args =", args) def m1(self, x): - print "m1(x=%r)" % (x,) - print C + print("m1(x=%r)" % (x,)) + print(C) x = C() - print x + print(x) x.m1(12) class D(C): def __getattr__(self, name): - if name[:2] == '__': raise AttributeError, name + if name[:2] == '__': raise AttributeError(name) return "getattr:%s" % name x = D() - print x.foo - print x._foo + print(x.foo) + print(x._foo) ## print x.__foo ## print x.__foo__ diff --git a/Demo/metaclasses/Simple.py b/Demo/metaclasses/Simple.py index e3e54f7..8878ade 100644 --- a/Demo/metaclasses/Simple.py +++ b/Demo/metaclasses/Simple.py @@ -17,7 +17,7 @@ class Instance: try: value = self.__klass__.__namespace__[name] except KeyError: - raise AttributeError, name + raise AttributeError(name) if type(value) is not types.FunctionType: return value return BoundMethod(value, self) @@ -27,7 +27,7 @@ class BoundMethod: self.function = function self.instance = instance def __call__(self, *args): - print "calling", self.function, "for", self.instance, "with", args + print("calling", self.function, "for", self.instance, "with", args) return self.function(self.instance, *args) Trace = Tracing('Trace', (), {}) @@ -42,4 +42,4 @@ aninstance = MyTracedClass() aninstance.method1(10) -print aninstance.method2() +print(aninstance.method2()) diff --git a/Demo/metaclasses/Synch.py b/Demo/metaclasses/Synch.py index cd13e86..445ce83 100644 --- a/Demo/metaclasses/Synch.py +++ b/Demo/metaclasses/Synch.py @@ -106,13 +106,13 @@ def _testLock(): def f2(lock, done=done): lock.acquire() - print "f2 running in thread %d\n" % thread.get_ident(), + print("f2 running in thread %d\n" % thread.get_ident(), end=' ') lock.release() done.append(1) def f1(lock, f2=f2, done=done): lock.acquire() - print "f1 running in thread %d\n" % thread.get_ident(), + print("f1 running in thread %d\n" % thread.get_ident(), end=' ') try: f2(lock) finally: @@ -134,9 +134,9 @@ def _testLock(): lock.release() import time while len(done) < 9: - print len(done) + print(len(done)) time.sleep(0.001) - print len(done) + print(len(done)) # Now, the Locking metaclass is a piece of cake. @@ -183,22 +183,22 @@ def _test(): return # Double the buffer size # First normalize it so that first==0 and last==size-1 - print "buffer =", self.buffer - print "first = %d, last = %d, size = %d" % ( - self.first, self.last, self.size) + print("buffer =", self.buffer) + print("first = %d, last = %d, size = %d" % ( + self.first, self.last, self.size)) if self.first <= self.last: temp = self.buffer[self.first:self.last] else: temp = self.buffer[self.first:] + self.buffer[:self.last] - print "temp =", temp + print("temp =", temp) self.buffer = temp + [None]*(self.size+1) self.first = 0 self.last = self.size-1 self.size = self.size*2 - print "Buffer size doubled to", self.size - print "new buffer =", self.buffer - print "first = %d, last = %d, size = %d" % ( - self.first, self.last, self.size) + print("Buffer size doubled to", self.size) + print("new buffer =", self.buffer) + print("first = %d, last = %d, size = %d" % ( + self.first, self.last, self.size)) self.put(item) # Recursive call to test the locking def get(self): # Is the buffer empty? @@ -212,10 +212,10 @@ def _test(): import time i = 0 while i < n: - print "put", i + print("put", i) buffer.put(i) i = i+1 - print "Producer: done producing", n, "items" + print("Producer: done producing", n, "items") wait.release() def consumer(buffer, wait, n=1000): @@ -226,15 +226,14 @@ def _test(): try: x = buffer.get() if x != i: - raise AssertionError, \ - "get() returned %s, expected %s" % (x, i) - print "got", i + raise AssertionError("get() returned %s, expected %s" % (x, i)) + print("got", i) i = i+1 tout = 0.001 except EOFError: time.sleep(tout) tout = tout*2 - print "Consumer: done consuming", n, "items" + print("Consumer: done consuming", n, "items") wait.release() pwait = thread.allocate_lock() @@ -246,10 +245,10 @@ def _test(): thread.start_new_thread(consumer, (buffer, cwait, n)) thread.start_new_thread(producer, (buffer, pwait, n)) pwait.acquire() - print "Producer done" + print("Producer done") cwait.acquire() - print "All done" - print "buffer size ==", len(buffer.buffer) + print("All done") + print("buffer size ==", len(buffer.buffer)) if __name__ == '__main__': _testLock() diff --git a/Demo/metaclasses/Trace.py b/Demo/metaclasses/Trace.py index 97fda56..d211d17 100644 --- a/Demo/metaclasses/Trace.py +++ b/Demo/metaclasses/Trace.py @@ -35,7 +35,7 @@ class TraceMetaClass: return base.__getattr__(name) except AttributeError: pass - raise AttributeError, name + raise AttributeError(name) def __setattr__(self, name, value): if not self.__inited: @@ -69,7 +69,7 @@ class TracingInstance: try: raw = self.__class.__getattr__(name) except AttributeError: - raise AttributeError, name + raise AttributeError(name) if type(raw) != types.FunctionType: return raw # It's a function @@ -99,7 +99,7 @@ class TracingWrapper(NotTracingWrapper): self.inst.__trace_call__(self.inst.__trace_output__, "returning from %s with exception %s: %s", self.__name__, t, v) - raise t, v, tb + raise t(v).with_traceback(tb) else: self.inst.__trace_call__(self.inst.__trace_output__, "returning from %s with value %s", @@ -117,28 +117,28 @@ def _test(): def m2(self, y): return self.x + y __trace_output__ = sys.stdout class D(C): - def m2(self, y): print "D.m2(%r)" % (y,); return C.m2(self, y) + def m2(self, y): print("D.m2(%r)" % (y,)); return C.m2(self, y) __trace_output__ = None x = C(4321) - print x - print x.x - print x.m1(100) - print x.m1(10) - print x.m2(33) - print x.m1(5) - print x.m2(4000) - print x.x - - print C.__init__ - print C.m2 - print D.__init__ - print D.m2 + print(x) + print(x.x) + print(x.m1(100)) + print(x.m1(10)) + print(x.m2(33)) + print(x.m1(5)) + print(x.m2(4000)) + print(x.x) + + print(C.__init__) + print(C.m2) + print(D.__init__) + print(D.m2) y = D() - print y - print y.m1(10) - print y.m2(100) - print y.x + print(y) + print(y.m1(10)) + print(y.m2(100)) + print(y.x) if __name__ == '__main__': _test() |