[Python-checkins] r78757 - in python/trunk/Lib: _pyio.py calendar.py ctypes/test/test_callbacks.py distutils/util.py shutil.py test/test_calendar.py test/test_memoryio.py unittest/case.py warnings.py

florent.xicluna python-checkins at python.org
Sun Mar 7 13:14:25 CET 2010


Author: florent.xicluna
Date: Sun Mar 7 13:14:25 2010
New Revision: 78757
Log:
Fix some py3k warnings in the standard library.
Modified:
 python/trunk/Lib/_pyio.py
 python/trunk/Lib/calendar.py
 python/trunk/Lib/ctypes/test/test_callbacks.py
 python/trunk/Lib/distutils/util.py
 python/trunk/Lib/shutil.py
 python/trunk/Lib/test/test_calendar.py
 python/trunk/Lib/test/test_memoryio.py
 python/trunk/Lib/unittest/case.py
 python/trunk/Lib/warnings.py
Modified: python/trunk/Lib/_pyio.py
==============================================================================
--- python/trunk/Lib/_pyio.py	(original)
+++ python/trunk/Lib/_pyio.py	Sun Mar 7 13:14:25 2010
@@ -839,8 +839,8 @@
 if self.closed:
 raise ValueError("seek on closed file")
 try:
- pos = pos.__index__()
- except AttributeError as err:
+ pos.__index__
+ except AttributeError:
 raise TypeError("an integer is required")
 if whence == 0:
 if pos < 0:
@@ -864,8 +864,13 @@
 raise ValueError("truncate on closed file")
 if pos is None:
 pos = self._pos
- elif pos < 0:
- raise ValueError("negative truncate position %r" % (pos,))
+ else:
+ try:
+ pos.__index__
+ except AttributeError:
+ raise TypeError("an integer is required")
+ if pos < 0:
+ raise ValueError("negative truncate position %r" % (pos,))
 del self._buffer[pos:]
 return pos
 
@@ -1813,6 +1818,10 @@
 if n is None:
 n = -1
 decoder = self._decoder or self._get_decoder()
+ try:
+ n.__index__
+ except AttributeError:
+ raise TypeError("an integer is required")
 if n < 0:
 # Read everything.
 result = (self._get_decoded_chars() +
Modified: python/trunk/Lib/calendar.py
==============================================================================
--- python/trunk/Lib/calendar.py	(original)
+++ python/trunk/Lib/calendar.py	Sun Mar 7 13:14:25 2010
@@ -564,6 +564,10 @@
 firstweekday = c.getfirstweekday
 
 def setfirstweekday(firstweekday):
+ try:
+ firstweekday.__index__
+ except AttributeError:
+ raise IllegalWeekdayError(firstweekday)
 if not MONDAY <= firstweekday <= SUNDAY:
 raise IllegalWeekdayError(firstweekday)
 c.firstweekday = firstweekday
Modified: python/trunk/Lib/ctypes/test/test_callbacks.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_callbacks.py	(original)
+++ python/trunk/Lib/ctypes/test/test_callbacks.py	Sun Mar 7 13:14:25 2010
@@ -132,7 +132,7 @@
 gc.collect()
 live = [x for x in gc.get_objects()
 if isinstance(x, X)]
- self.failUnlessEqual(len(live), 0)
+ self.assertEqual(len(live), 0)
 
 try:
 WINFUNCTYPE
@@ -164,7 +164,7 @@
 result = integrate(0.0, 1.0, CALLBACK(func), 10)
 diff = abs(result - 1./3.)
 
- self.assertTrue(diff < 0.01, "%s not less than 0.01" % diff)
+ self.assertLess(diff, 0.01, "%s not less than 0.01" % diff)
 
 ################################################################
 
Modified: python/trunk/Lib/distutils/util.py
==============================================================================
--- python/trunk/Lib/distutils/util.py	(original)
+++ python/trunk/Lib/distutils/util.py	Sun Mar 7 13:14:25 2010
@@ -406,7 +406,7 @@
 
 log.info(msg)
 if not dry_run:
- apply(func, args)
+ func(*args)
 
 
 def strtobool (val):
Modified: python/trunk/Lib/shutil.py
==============================================================================
--- python/trunk/Lib/shutil.py	(original)
+++ python/trunk/Lib/shutil.py	Sun Mar 7 13:14:25 2010
@@ -10,6 +10,7 @@
 from os.path import abspath
 import fnmatch
 from warnings import warn
+import collections
 
 try:
 from pwd import getpwnam
@@ -500,7 +501,7 @@
 """
 if extra_args is None:
 extra_args = []
- if not callable(function):
+ if not isinstance(function, collections.Callable):
 raise TypeError('The %s object is not callable' % function)
 if not isinstance(extra_args, (tuple, list)):
 raise TypeError('extra_args needs to be a sequence')
Modified: python/trunk/Lib/test/test_calendar.py
==============================================================================
--- python/trunk/Lib/test/test_calendar.py	(original)
+++ python/trunk/Lib/test/test_calendar.py	Sun Mar 7 13:14:25 2010
@@ -212,11 +212,9 @@
 self.assertEqual(calendar.isleap(2003), 0)
 
 def test_setfirstweekday(self):
- # Silence a py3k warning claiming to affect Lib/calendar.py
- with test_support.check_warnings():
- self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber')
- self.assertRaises(ValueError, calendar.setfirstweekday, -1)
- self.assertRaises(ValueError, calendar.setfirstweekday, 200)
+ self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber')
+ self.assertRaises(ValueError, calendar.setfirstweekday, -1)
+ self.assertRaises(ValueError, calendar.setfirstweekday, 200)
 orig = calendar.firstweekday()
 calendar.setfirstweekday(calendar.SUNDAY)
 self.assertEqual(calendar.firstweekday(), calendar.SUNDAY)
Modified: python/trunk/Lib/test/test_memoryio.py
==============================================================================
--- python/trunk/Lib/test/test_memoryio.py	(original)
+++ python/trunk/Lib/test/test_memoryio.py	Sun Mar 7 13:14:25 2010
@@ -133,9 +133,7 @@
 pos = memio.tell()
 self.assertEqual(memio.truncate(None), pos)
 self.assertEqual(memio.tell(), pos)
- # Silence a py3k warning
- with support.check_warnings():
- self.assertRaises(TypeError, memio.truncate, '0')
+ self.assertRaises(TypeError, memio.truncate, '0')
 memio.close()
 self.assertRaises(ValueError, memio.truncate, 0)
 
@@ -172,9 +170,7 @@
 self.assertEqual(type(memio.read()), type(buf))
 memio.seek(0)
 self.assertEqual(memio.read(None), buf)
- # Silence a py3k warning
- with support.check_warnings():
- self.assertRaises(TypeError, memio.read, '')
+ self.assertRaises(TypeError, memio.read, '')
 memio.close()
 self.assertRaises(ValueError, memio.read)
 
Modified: python/trunk/Lib/unittest/case.py
==============================================================================
--- python/trunk/Lib/unittest/case.py	(original)
+++ python/trunk/Lib/unittest/case.py	Sun Mar 7 13:14:25 2010
@@ -774,26 +774,23 @@
 set(actual))`` but it works with sequences of unhashable objects as
 well.
 """
- try:
- expected = set(expected_seq)
- actual = set(actual_seq)
- missing = list(expected.difference(actual))
- unexpected = list(actual.difference(expected))
- missing.sort()
- unexpected.sort()
- except TypeError:
- # Fall back to slower list-compare if any of the objects are
- # not hashable.
- expected = list(expected_seq)
- actual = list(actual_seq)
- with warnings.catch_warnings():
- if sys.py3kwarning:
- # Silence Py3k warning
- warnings.filterwarnings("ignore",
- "dict inequality comparisons "
- "not supported", DeprecationWarning)
- expected.sort()
- actual.sort()
+ with warnings.catch_warnings():
+ if sys.py3kwarning:
+ # Silence Py3k warning raised during the sorting
+ for msg in ["dict inequality comparisons",
+ "builtin_function_or_method order comparisons",
+ "comparing unequal types"]:
+ warnings.filterwarnings("ignore", msg, DeprecationWarning)
+ try:
+ expected = set(expected_seq)
+ actual = set(actual_seq)
+ missing = sorted(expected.difference(actual))
+ unexpected = sorted(actual.difference(expected))
+ except TypeError:
+ # Fall back to slower list-compare if any of the objects are
+ # not hashable.
+ expected = sorted(expected_seq)
+ actual = sorted(actual_seq)
 missing, unexpected = sorted_list_difference(expected, actual)
 errors = []
 if missing:
Modified: python/trunk/Lib/warnings.py
==============================================================================
--- python/trunk/Lib/warnings.py	(original)
+++ python/trunk/Lib/warnings.py	Sun Mar 7 13:14:25 2010
@@ -29,7 +29,7 @@
 file.write(formatwarning(message, category, filename, lineno, line))
 except IOError:
 pass # the file (probably stderr) is invalid - this warning gets lost.
-# Keep a worrking version around in case the deprecation of the old API is
+# Keep a working version around in case the deprecation of the old API is
 # triggered.
 showwarning = _show_warning
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /