Browse Source

PythonUtil: Fix Python 3 next() for SerialNumGen, AlphabetCounter

rdb 4 years ago
parent
commit
3732d3f1e6
2 changed files with 31 additions and 0 deletions
  1. 9 0
      direct/src/showbase/PythonUtil.py
  2. 22 0
      tests/showbase/test_PythonUtil.py

+ 9 - 0
direct/src/showbase/PythonUtil.py

@@ -1245,18 +1245,24 @@ class SerialNumGen:
         if start is None:
             start = 0
         self.__counter = start-1
+
     def next(self):
         self.__counter += 1
         return self.__counter
 
+    __next__ = next
+
 class SerialMaskedGen(SerialNumGen):
     def __init__(self, mask, start=None):
         self._mask = mask
         SerialNumGen.__init__(self, start)
+
     def next(self):
         v = SerialNumGen.next(self)
         return v & self._mask
 
+    __next__ = next
+
 _serialGen = SerialNumGen()
 def serialNum():
     global _serialGen
@@ -2517,6 +2523,7 @@ class AlphabetCounter:
     # object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
     def __init__(self):
         self._curCounter = ['A']
+
     def next(self):
         result = ''.join([c for c in self._curCounter])
         index = -1
@@ -2540,6 +2547,8 @@ class AlphabetCounter:
                 break
         return result
 
+    __next__ = next
+
 if __debug__ and __name__ == '__main__':
     def testAlphabetCounter():
         tempList = []

+ 22 - 0
tests/showbase/test_PythonUtil.py

@@ -157,3 +157,25 @@ def test_weighted_choice():
     # When subtracting that number by each weight, it will reach 0
     # by the time it hits 'item6' in the iteration.
     assert item == items[5]
+
+
+def test_serial():
+    gen = PythonUtil.SerialNumGen()
+    assert gen.next() == 0
+    assert next(gen) == 1
+    assert next(gen) == 2
+    assert gen.next() == 3
+
+
+def test_alphabet_counter():
+    counter = PythonUtil.AlphabetCounter()
+    assert next(counter) == 'A'
+    assert counter.next() == 'B'
+    assert counter.next() == 'C'
+    assert next(counter) == 'D'
+
+    for i in range(26 - 4):
+        next(counter)
+
+    assert next(counter) == 'AA'
+    assert next(counter) == 'AB'