bpo-15999: Clean up of handling boolean arguments. (GH-15610)
* Use the 'p' format unit instead of manually called PyObject_IsTrue().
* Pass boolean value instead 0/1 integers to functions that needs boolean.
* Convert some arguments to boolean only once.
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 0d3f974..c1bdac7 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -2295,7 +2295,7 @@
return not eof
def _pack_cookie(self, position, dec_flags=0,
- bytes_to_feed=0, need_eof=0, chars_to_skip=0):
+ bytes_to_feed=0, need_eof=False, chars_to_skip=0):
# The meaning of a tell() cookie is: seek to position, set the
# decoder flags to dec_flags, read bytes_to_feed bytes, feed them
# into the decoder with need_eof as the EOF flag, then skip
@@ -2309,7 +2309,7 @@
rest, dec_flags = divmod(rest, 1<<64)
rest, bytes_to_feed = divmod(rest, 1<<64)
need_eof, chars_to_skip = divmod(rest, 1<<64)
- return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
+ return position, dec_flags, bytes_to_feed, bool(need_eof), chars_to_skip
def tell(self):
if not self._seekable:
@@ -2383,7 +2383,7 @@
# (a point where the decoder has nothing buffered, so seek()
# can safely start from there and advance to this location).
bytes_fed = 0
- need_eof = 0
+ need_eof = False
# Chars decoded since `start_pos`
chars_decoded = 0
for i in range(skip_bytes, len(next_input)):
@@ -2400,7 +2400,7 @@
else:
# We didn't get enough decoded data; signal EOF to get more.
chars_decoded += len(decoder.decode(b'', final=True))
- need_eof = 1
+ need_eof = True
if chars_decoded < chars_to_skip:
raise OSError("can't reconstruct logical file position")
diff --git a/Lib/codeop.py b/Lib/codeop.py
index fb759da..e5c7ade 100644
--- a/Lib/codeop.py
+++ b/Lib/codeop.py
@@ -130,7 +130,7 @@
self.flags = PyCF_DONT_IMPLY_DEDENT
def __call__(self, source, filename, symbol):
- codeob = compile(source, filename, symbol, self.flags, 1)
+ codeob = compile(source, filename, symbol, self.flags, True)
for feature in _features:
if codeob.co_flags & feature.compiler_flag:
self.flags |= feature.compiler_flag
diff --git a/Lib/doctest.py b/Lib/doctest.py
index bf4889f..8fca628 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1326,7 +1326,7 @@
try:
# Don't blink! This is where the user's code gets run.
exec(compile(example.source, filename, "single",
- compileflags, 1), test.globs)
+ compileflags, True), test.globs)
self.debugger.set_continue() # ==== Example Finished ====
exception = None
except KeyboardInterrupt:
diff --git a/Lib/quopri.py b/Lib/quopri.py
index cbd979a..08899c5 100755
--- a/Lib/quopri.py
+++ b/Lib/quopri.py
@@ -204,11 +204,11 @@
print("-t: quote tabs")
print("-d: decode; default encode")
sys.exit(2)
- deco = 0
- tabs = 0
+ deco = False
+ tabs = False
for o, a in opts:
- if o == '-t': tabs = 1
- if o == '-d': deco = 1
+ if o == '-t': tabs = True
+ if o == '-d': deco = True
if tabs and deco:
sys.stdout = sys.stderr
print("-t and -d are mutually exclusive")
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index d09255d..d5de5e9 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -6160,7 +6160,7 @@
def test_date_from_date(self):
exp_date = date(1993, 8, 26)
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro):
c_api_date = _testcapi.get_date_fromdate(
macro,
@@ -6173,7 +6173,7 @@
def test_datetime_from_dateandtime(self):
exp_date = datetime(1993, 8, 26, 22, 12, 55, 99999)
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro):
c_api_date = _testcapi.get_datetime_fromdateandtime(
macro,
@@ -6191,7 +6191,7 @@
exp_date = datetime(1993, 8, 26, 22, 12, 55, 99999)
for fold in [0, 1]:
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro, fold=fold):
c_api_date = _testcapi.get_datetime_fromdateandtimeandfold(
macro,
@@ -6210,7 +6210,7 @@
def test_time_from_time(self):
exp_time = time(22, 12, 55, 99999)
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro):
c_api_time = _testcapi.get_time_fromtime(
macro,
@@ -6225,7 +6225,7 @@
exp_time = time(22, 12, 55, 99999)
for fold in [0, 1]:
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro, fold=fold):
c_api_time = _testcapi.get_time_fromtimeandfold(
macro,
@@ -6241,7 +6241,7 @@
def test_delta_from_dsu(self):
exp_delta = timedelta(26, 55, 99999)
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro):
c_api_delta = _testcapi.get_delta_fromdsu(
macro,
@@ -6254,7 +6254,7 @@
def test_date_from_timestamp(self):
ts = datetime(1995, 4, 12).timestamp()
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(macro=macro):
d = _testcapi.get_date_fromtimestamp(int(ts), macro)
@@ -6272,7 +6272,7 @@
from_timestamp = _testcapi.get_datetime_fromtimestamp
for case in cases:
- for macro in [0, 1]:
+ for macro in False, True:
with self.subTest(case=case, macro=macro):
dtup, tzinfo, usetz = case
dt_orig = datetime(*dtup, tzinfo=tzinfo)
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
index d12a86e..cd1155d 100644
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -182,7 +182,7 @@
def test_timeout(self):
lock = self.locktype()
# Can't set timeout if not blocking
- self.assertRaises(ValueError, lock.acquire, 0, 1)
+ self.assertRaises(ValueError, lock.acquire, False, 1)
# Invalid timeout values
self.assertRaises(ValueError, lock.acquire, timeout=-100)
self.assertRaises(OverflowError, lock.acquire, timeout=1e100)
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 1100c49..db2f6cd 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -320,8 +320,8 @@
bom = b'\xef\xbb\xbf'
compile(bom + b'print(1)\n', '', 'exec')
compile(source='pass', filename='?', mode='exec')
- compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
- compile('pass', '?', dont_inherit=1, mode='exec')
+ compile(dont_inherit=False, filename='tmp', source='0', mode='eval')
+ compile('pass', '?', dont_inherit=True, mode='exec')
compile(memoryview(b"text"), "name", "exec")
self.assertRaises(TypeError, compile)
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
@@ -1853,7 +1853,7 @@
self.assertEqual(data, sorted(copy, key=lambda x: -x))
self.assertNotEqual(data, copy)
random.shuffle(copy)
- self.assertEqual(data, sorted(copy, reverse=1))
+ self.assertEqual(data, sorted(copy, reverse=True))
self.assertNotEqual(data, copy)
def test_bad_arguments(self):
diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py
index d1a5db9..a287358 100644
--- a/Lib/test/test_ioctl.py
+++ b/Lib/test/test_ioctl.py
@@ -48,7 +48,7 @@
else:
buf.append(fill)
with open("/dev/tty", "rb") as tty:
- r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
+ r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, True)
rpgrp = buf[0]
self.assertEqual(r, 0)
self.assertIn(rpgrp, ids)
diff --git a/Lib/test/test_ordered_dict.py b/Lib/test/test_ordered_dict.py
index 148a9bd..f337be8 100644
--- a/Lib/test/test_ordered_dict.py
+++ b/Lib/test/test_ordered_dict.py
@@ -407,9 +407,9 @@
self.assertEqual(list(od), list('abcde'))
od.move_to_end('c')
self.assertEqual(list(od), list('abdec'))
- od.move_to_end('c', 0)
+ od.move_to_end('c', False)
self.assertEqual(list(od), list('cabde'))
- od.move_to_end('c', 0)
+ od.move_to_end('c', False)
self.assertEqual(list(od), list('cabde'))
od.move_to_end('e')
self.assertEqual(list(od), list('cabde'))
@@ -418,7 +418,7 @@
with self.assertRaises(KeyError):
od.move_to_end('x')
with self.assertRaises(KeyError):
- od.move_to_end('x', 0)
+ od.move_to_end('x', False)
def test_move_to_end_issue25406(self):
OrderedDict = self.OrderedDict
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 7bd7f51..8636f2b 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -2820,15 +2820,15 @@
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600',
'a\ud800b\udfffc', '\ud834\udd1e']:
l = len(s)
- self.assertEqual(unicode_asucs4(s, l, 1), s+'\0')
- self.assertEqual(unicode_asucs4(s, l, 0), s+'\uffff')
- self.assertEqual(unicode_asucs4(s, l+1, 1), s+'\0\uffff')
- self.assertEqual(unicode_asucs4(s, l+1, 0), s+'\0\uffff')
- self.assertRaises(SystemError, unicode_asucs4, s, l-1, 1)
- self.assertRaises(SystemError, unicode_asucs4, s, l-2, 0)
+ self.assertEqual(unicode_asucs4(s, l, True), s+'\0')
+ self.assertEqual(unicode_asucs4(s, l, False), s+'\uffff')
+ self.assertEqual(unicode_asucs4(s, l+1, True), s+'\0\uffff')
+ self.assertEqual(unicode_asucs4(s, l+1, False), s+'\0\uffff')
+ self.assertRaises(SystemError, unicode_asucs4, s, l-1, True)
+ self.assertRaises(SystemError, unicode_asucs4, s, l-2, False)
s = '\0'.join([s, s])
- self.assertEqual(unicode_asucs4(s, len(s), 1), s+'\0')
- self.assertEqual(unicode_asucs4(s, len(s), 0), s+'\uffff')
+ self.assertEqual(unicode_asucs4(s, len(s), True), s+'\0')
+ self.assertEqual(unicode_asucs4(s, len(s), False), s+'\uffff')
# Test PyUnicode_AsUTF8()
@support.cpython_only
diff --git a/Lib/threading.py b/Lib/threading.py
index c3bbb19..5932367 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -262,7 +262,7 @@
def _is_owned(self):
# Return True if lock is owned by current_thread.
# This method is called only if _lock doesn't have _is_owned().
- if self._lock.acquire(0):
+ if self._lock.acquire(False):
self._lock.release()
return False
else:
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 9258484..5d5cf90 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -2241,7 +2241,7 @@
_w = '.'
def __init__(self, screenName=None, baseName=None, className='Tk',
- useTk=1, sync=0, use=None):
+ useTk=True, sync=False, use=None):
"""Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
be created. BASENAME will be used for the identification of the profile file (see
readprofile).
@@ -2259,7 +2259,7 @@
baseName, ext = os.path.splitext(baseName)
if ext not in ('.py', '.pyc'):
baseName = baseName + ext
- interactive = 0
+ interactive = False
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
if useTk:
self._loadtk()
@@ -2361,7 +2361,7 @@
# copied into the Pack, Place or Grid class.
-def Tcl(screenName=None, baseName=None, className='Tk', useTk=0):
+def Tcl(screenName=None, baseName=None, className='Tk', useTk=False):
return Tk(screenName, baseName, className, useTk)