From e2b70bcf7401477936fba99a8bf4a1f759ecc8a3 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 18 Aug 2006 22:13:04 +0000 Subject: [PATCH] Get rid of dict.has_key(). Boy this has a lot of repercussions! Not all code has been fixed yet; this is just a checkpoint... The C API still has PyDict_HasKey() and _HasKeyString(); not sure if I want to change those just yet. --- Include/abstract.h | 4 +-- Lib/DocXMLRPCServer.py | 2 +- Lib/SimpleXMLRPCServer.py | 2 +- Lib/UserDict.py | 9 +++---- Lib/asyncore.py | 2 +- Lib/bdb.py | 7 +++--- Lib/bsddb/__init__.py | 2 ++ Lib/bsddb/dbobj.py | 4 ++- Lib/bsddb/dbshelve.py | 6 ++++- Lib/bsddb/dbutils.py | 2 +- Lib/bsddb/test/test_all.py | 8 ++++-- Lib/bsddb/test/test_associate.py | 2 +- Lib/bsddb/test/test_basics.py | 2 +- Lib/bsddb/test/test_compare.py | 3 ++- Lib/bsddb/test/test_compat.py | 2 +- Lib/bsddb/test/test_dbshelve.py | 2 +- Lib/bsddb/test/test_dbtables.py | 2 +- Lib/bsddb/test/test_env_close.py | 2 +- Lib/bsddb/test/test_get_none.py | 2 +- Lib/bsddb/test/test_join.py | 2 +- Lib/bsddb/test/test_lock.py | 2 +- Lib/bsddb/test/test_queue.py | 2 +- Lib/bsddb/test/test_recno.py | 2 +- Lib/bsddb/test/test_sequence.py | 2 +- Lib/bsddb/test/test_thread.py | 2 +- Lib/cgi.py | 8 ------ Lib/difflib.py | 8 +++--- Lib/distutils/archive_util.py | 2 +- Lib/distutils/ccompiler.py | 2 +- Lib/distutils/command/build_ext.py | 2 +- Lib/distutils/core.py | 6 ++--- Lib/distutils/dist.py | 8 +++--- Lib/distutils/fancy_getopt.py | 8 +++--- Lib/distutils/sysconfig.py | 22 ++++++++--------- Lib/distutils/text_file.py | 4 +-- Lib/distutils/util.py | 6 ++--- Lib/dumbdbm.py | 3 --- Lib/mailbox.py | 11 +++------ Lib/modulefinder.py | 4 +-- Lib/optparse.py | 16 ++++++------ Lib/os.py | 2 -- Lib/pickle.py | 14 +++++------ Lib/pickletools.py | 33 +++++++++---------------- Lib/platform.py | 2 +- Lib/profile.py | 2 +- Lib/pstats.py | 2 +- Lib/rfc822.py | 4 --- Lib/sets.py | 14 +++++------ Lib/shelve.py | 9 +++---- Lib/test/mapping_tests.py | 14 +---------- Lib/test/regrtest.py | 1 - Lib/test/test___all__.py | 2 +- Lib/test/test_bool.py | 6 ++--- Lib/test/test_bsddb.py | 5 ---- Lib/test/test_builtin.py | 10 ++++---- Lib/test/test_call.py | 18 +++++++------- Lib/test/test_cgi.py | 4 +-- Lib/test/test_dbm.py | 2 +- Lib/test/test_dict.py | 14 ++--------- Lib/test/test_gdbm.py | 2 +- Lib/test/test_grammar.py | 6 ++--- Lib/test/test_mailbox.py | 11 ++------- Lib/test/test_multibytecodec_support.py | 2 +- Lib/test/test_operations.py | 1 - Lib/test/test_pkgimport.py | 6 ++--- Lib/test/test_pyclbr.py | 6 ++--- Lib/test/test_rfc822.py | 2 +- Lib/test/test_sax.py | 10 ++++---- Lib/test/test_scope.py | 2 +- Lib/test/test_site.py | 2 +- Lib/test/test_struct.py | 4 +-- Lib/test/test_time.py | 2 +- Lib/test/test_urllib2.py | 2 +- Lib/test/test_userdict.py | 10 ++------ Lib/test/test_weakref.py | 20 +++++++-------- Lib/test/test_wsgiref.py | 8 +++--- Lib/trace.py | 2 +- Lib/unittest.py | 2 +- Lib/urllib.py | 2 +- Lib/urllib2.py | 2 +- Lib/weakref.py | 14 ----------- Lib/wsgiref/handlers.py | 6 ++--- Lib/wsgiref/headers.py | 4 +-- Lib/wsgiref/util.py | 2 +- Lib/wsgiref/validate.py | 2 +- Lib/xmlrpclib.py | 4 +-- Modules/bsddbmodule.c | 4 +-- Modules/dbmmodule.c | 8 +++--- Modules/gdbmmodule.c | 10 ++++---- Objects/descrobject.c | 13 +--------- Objects/dictobject.c | 9 ++----- Objects/typeobject.c | 1 - setup.py | 2 +- 93 files changed, 215 insertions(+), 313 deletions(-) diff --git a/Include/abstract.h b/Include/abstract.h index 1e69614f1ac..eed23c12489 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -1115,7 +1115,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ /* On success, return 1 if the mapping object has the key, key, and 0 otherwise. This is equivalent to the Python expression: - o.has_key(key). + key in o. This function always succeeds. */ @@ -1125,7 +1125,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ /* Return 1 if the mapping object has the key, key, and 0 otherwise. This is equivalent to the Python expression: - o.has_key(key). + key in o. This function always succeeds. diff --git a/Lib/DocXMLRPCServer.py b/Lib/DocXMLRPCServer.py index 86ed32b6fc1..ddd56047d4d 100644 --- a/Lib/DocXMLRPCServer.py +++ b/Lib/DocXMLRPCServer.py @@ -174,7 +174,7 @@ class XMLRPCDocGenerator: methods = {} for method_name in self.system_listMethods(): - if self.funcs.has_key(method_name): + if method_name in self.funcs: method = self.funcs[method_name] elif self.instance is not None: method_info = [None, None] # argspec, documentation diff --git a/Lib/SimpleXMLRPCServer.py b/Lib/SimpleXMLRPCServer.py index 0846a68ce6a..3b0a6a52aa3 100644 --- a/Lib/SimpleXMLRPCServer.py +++ b/Lib/SimpleXMLRPCServer.py @@ -313,7 +313,7 @@ class SimpleXMLRPCDispatcher: Returns a string containing documentation for the specified method.""" method = None - if self.funcs.has_key(method_name): + if method_name in self.funcs: method = self.funcs[method_name] elif self.instance is not None: # Instance can implement _methodHelp to return help for a method diff --git a/Lib/UserDict.py b/Lib/UserDict.py index 5e97817f061..b560a11c5b3 100644 --- a/Lib/UserDict.py +++ b/Lib/UserDict.py @@ -41,7 +41,6 @@ class UserDict: def iterkeys(self): return self.data.iterkeys() def itervalues(self): return self.data.itervalues() def values(self): return self.data.values() - def has_key(self, key): return self.data.has_key(key) def update(self, dict=None, **kwargs): if dict is None: pass @@ -55,11 +54,11 @@ class UserDict: if len(kwargs): self.data.update(kwargs) def get(self, key, failobj=None): - if not self.has_key(key): + if key not in self: return failobj return self[key] def setdefault(self, key, failobj=None): - if not self.has_key(key): + if key not in self: self[key] = failobj return self[key] def pop(self, key, *args): @@ -91,14 +90,12 @@ class DictMixin: def __iter__(self): for k in self.keys(): yield k - def has_key(self, key): + def __contains__(self, key): try: value = self[key] except KeyError: return False return True - def __contains__(self, key): - return self.has_key(key) # third level takes advantage of second level definitions def iteritems(self): diff --git a/Lib/asyncore.py b/Lib/asyncore.py index 886c84545b1..7b2f301340f 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -247,7 +247,7 @@ class dispatcher: fd = self._fileno if map is None: map = self._map - if map.has_key(fd): + if fd in map: #self.log_info('closing channel %d:%s' % (fd, self)) del map[fd] self._fileno = None diff --git a/Lib/bdb.py b/Lib/bdb.py index 0c56b63fa11..1e81c449e9e 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -133,8 +133,7 @@ class Bdb: raise NotImplementedError, "subclass of bdb must implement do_clear()" def break_anywhere(self, frame): - return self.breaks.has_key( - self.canonic(frame.f_code.co_filename)) + return self.canonic(frame.f_code.co_filename) in self.breaks # Derived classes should override the user_* methods # to gain control. @@ -245,7 +244,7 @@ class Bdb: # pair, then remove the breaks entry for bp in Breakpoint.bplist[filename, lineno][:]: bp.deleteMe() - if not Breakpoint.bplist.has_key((filename, lineno)): + if (filename, lineno) not in Breakpoint.bplist: self.breaks[filename].remove(lineno) if not self.breaks[filename]: del self.breaks[filename] @@ -453,7 +452,7 @@ class Breakpoint: Breakpoint.next = Breakpoint.next + 1 # Build the two lists self.bpbynumber.append(self) - if self.bplist.has_key((file, line)): + if (file, line) in self.bplist: self.bplist[file, line].append(self) else: self.bplist[file, line] = [self] diff --git a/Lib/bsddb/__init__.py b/Lib/bsddb/__init__.py index cf32668862e..0eeefd17485 100644 --- a/Lib/bsddb/__init__.py +++ b/Lib/bsddb/__init__.py @@ -255,6 +255,8 @@ class _DBWithCursor(_iter_mixin): self._checkOpen() return _DeadlockWrap(self.db.has_key, key) + __contains__ = has_key + def set_location(self, key): self._checkOpen() self._checkCursor() diff --git a/Lib/bsddb/dbobj.py b/Lib/bsddb/dbobj.py index 40a51ec2380..346c1adc6fe 100644 --- a/Lib/bsddb/dbobj.py +++ b/Lib/bsddb/dbobj.py @@ -21,7 +21,7 @@ # added to _bsddb.c. # -import db +from . import db try: from UserDict import DictMixin @@ -161,6 +161,8 @@ class DB(DictMixin): return self._cobj.key_range(*args, **kwargs) def has_key(self, *args, **kwargs): return self._cobj.has_key(*args, **kwargs) + def __contains__(self, key): + return self._cobj.has_key(key) def items(self, *args, **kwargs): return self._cobj.items(*args, **kwargs) def keys(self, *args, **kwargs): diff --git a/Lib/bsddb/dbshelve.py b/Lib/bsddb/dbshelve.py index 5cd4a53e818..afc1a1ac37f 100644 --- a/Lib/bsddb/dbshelve.py +++ b/Lib/bsddb/dbshelve.py @@ -35,7 +35,7 @@ try: except ImportError: # DictMixin is new in Python 2.3 class DictMixin: pass -import db +from . import db #------------------------------------------------------------------------ @@ -197,6 +197,10 @@ class DBShelf(DictMixin): raise NotImplementedError + def __contains__(self, key): + return self.has_key(key) + + #---------------------------------------------- # Methods allowed to pass-through to self.db # diff --git a/Lib/bsddb/dbutils.py b/Lib/bsddb/dbutils.py index 30b3cc7e9fe..0c4c1cbd0aa 100644 --- a/Lib/bsddb/dbutils.py +++ b/Lib/bsddb/dbutils.py @@ -55,7 +55,7 @@ def DeadlockWrap(function, *_args, **_kwargs): """ sleeptime = _deadlock_MinSleepTime max_retries = _kwargs.get('max_retries', -1) - if _kwargs.has_key('max_retries'): + if 'max_tries' in _kwargs: del _kwargs['max_retries'] while True: try: diff --git a/Lib/bsddb/test/test_all.py b/Lib/bsddb/test/test_all.py index ad8b1e9e94e..0b132f49cc4 100644 --- a/Lib/bsddb/test/test_all.py +++ b/Lib/bsddb/test/test_all.py @@ -41,8 +41,12 @@ class PrintInfoFakeTest(unittest.TestCase): # This little hack is for when this module is run as main and all the # other modules import it so they will still be able to get the right # verbose setting. It's confusing but it works. -import test_all -test_all.verbose = verbose +try: + import test_all +except ImportError: + pass +else: + test_all.verbose = verbose def suite(): diff --git a/Lib/bsddb/test/test_associate.py b/Lib/bsddb/test/test_associate.py index 05ef83cb6fa..33a78372450 100644 --- a/Lib/bsddb/test/test_associate.py +++ b/Lib/bsddb/test/test_associate.py @@ -14,7 +14,7 @@ except ImportError: have_threads = 0 import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_basics.py b/Lib/bsddb/test/test_basics.py index d6d507f8061..f7f4c2dc534 100644 --- a/Lib/bsddb/test/test_basics.py +++ b/Lib/bsddb/test/test_basics.py @@ -20,7 +20,7 @@ except ImportError: # For Python 2.3 from bsddb import db -from test_all import verbose +from .test_all import verbose DASH = '-' diff --git a/Lib/bsddb/test/test_compare.py b/Lib/bsddb/test/test_compare.py index 59a45ec502a..ccf8b839101 100644 --- a/Lib/bsddb/test/test_compare.py +++ b/Lib/bsddb/test/test_compare.py @@ -3,9 +3,10 @@ TestCases for python DB Btree key comparison function. """ import sys, os, re -import test_all from cStringIO import StringIO +from . import test_all + import unittest try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_compat.py b/Lib/bsddb/test/test_compat.py index b108db4c6a9..841e01c8a68 100644 --- a/Lib/bsddb/test/test_compat.py +++ b/Lib/bsddb/test/test_compat.py @@ -7,7 +7,7 @@ import sys, os, string import unittest import tempfile -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_dbshelve.py b/Lib/bsddb/test/test_dbshelve.py index 722ee5b407d..bb85bf7211b 100644 --- a/Lib/bsddb/test/test_dbshelve.py +++ b/Lib/bsddb/test/test_dbshelve.py @@ -15,7 +15,7 @@ except ImportError: # For Python 2.3 from bsddb import db, dbshelve -from test_all import verbose +from .test_all import verbose #---------------------------------------------------------------------- diff --git a/Lib/bsddb/test/test_dbtables.py b/Lib/bsddb/test/test_dbtables.py index 26e3d3650bc..2ff93a3ef12 100644 --- a/Lib/bsddb/test/test_dbtables.py +++ b/Lib/bsddb/test/test_dbtables.py @@ -28,7 +28,7 @@ except ImportError: import pickle import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_env_close.py b/Lib/bsddb/test/test_env_close.py index c1129417d1d..43dcabe92b8 100644 --- a/Lib/bsddb/test/test_env_close.py +++ b/Lib/bsddb/test/test_env_close.py @@ -15,7 +15,7 @@ except ImportError: # For Python 2.3 from bsddb import db -from test_all import verbose +from .test_all import verbose # We're going to get warnings in this module about trying to close the db when # its env is already closed. Let's just ignore those. diff --git a/Lib/bsddb/test/test_get_none.py b/Lib/bsddb/test/test_get_none.py index 5f09cecc6a3..d1b69c7d743 100644 --- a/Lib/bsddb/test/test_get_none.py +++ b/Lib/bsddb/test/test_get_none.py @@ -14,7 +14,7 @@ except ImportError: # For Python 2.3 from bsddb import db -from test_all import verbose +from .test_all import verbose #---------------------------------------------------------------------- diff --git a/Lib/bsddb/test/test_join.py b/Lib/bsddb/test/test_join.py index 69a1e9deb88..6e98b0b621a 100644 --- a/Lib/bsddb/test/test_join.py +++ b/Lib/bsddb/test/test_join.py @@ -13,7 +13,7 @@ except ImportError: have_threads = 0 import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_lock.py b/Lib/bsddb/test/test_lock.py index 7d7779805d8..53f11a82ff8 100644 --- a/Lib/bsddb/test/test_lock.py +++ b/Lib/bsddb/test/test_lock.py @@ -15,7 +15,7 @@ except ImportError: import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_queue.py b/Lib/bsddb/test/test_queue.py index 95cf20d0d2b..4226c9e1e8d 100644 --- a/Lib/bsddb/test/test_queue.py +++ b/Lib/bsddb/test/test_queue.py @@ -14,7 +14,7 @@ except ImportError: # For Python 2.3 from bsddb import db -from test_all import verbose +from .test_all import verbose #---------------------------------------------------------------------- diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py index f1ea56afdba..170448e6007 100644 --- a/Lib/bsddb/test/test_recno.py +++ b/Lib/bsddb/test/test_recno.py @@ -8,7 +8,7 @@ import tempfile from pprint import pprint import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/bsddb/test/test_sequence.py b/Lib/bsddb/test/test_sequence.py index 979f858c4a0..48631a37e2e 100644 --- a/Lib/bsddb/test/test_sequence.py +++ b/Lib/bsddb/test/test_sequence.py @@ -10,7 +10,7 @@ try: except ImportError: from bsddb import db -from test_all import verbose +from .test_all import verbose class DBSequenceTest(unittest.TestCase): diff --git a/Lib/bsddb/test/test_thread.py b/Lib/bsddb/test/test_thread.py index 31964f0b223..f18741323d9 100644 --- a/Lib/bsddb/test/test_thread.py +++ b/Lib/bsddb/test/test_thread.py @@ -31,7 +31,7 @@ except NameError: pass import unittest -from test_all import verbose +from .test_all import verbose try: # For Pythons w/distutils pybsddb diff --git a/Lib/cgi.py b/Lib/cgi.py index 41dc433cd8d..4c617a6e13c 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -608,14 +608,6 @@ class FieldStorage: if item.name not in keys: keys.append(item.name) return keys - def has_key(self, key): - """Dictionary style has_key() method.""" - if self.list is None: - raise TypeError, "not indexable" - for item in self.list: - if item.name == key: return True - return False - def __contains__(self, key): """Dictionary style __contains__ method.""" if self.list is None: diff --git a/Lib/difflib.py b/Lib/difflib.py index 3e28b18bf42..7ab682dee7b 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -199,7 +199,7 @@ class SequenceMatcher: # DON'T USE! Only __chain_b uses this. Use isbjunk. # isbjunk # for x in b, isbjunk(x) == isjunk(x) but much faster; - # it's really the has_key method of a hidden dict. + # it's really the __contains__ method of a hidden dict. # DOES NOT WORK for x in a! # isbpopular # for x in b, isbpopular(x) is true iff b is reasonably long @@ -341,8 +341,8 @@ class SequenceMatcher: # lot of junk in the sequence, the number of *unique* junk # elements is probably small. So the memory burden of keeping # this dict alive is likely trivial compared to the size of b2j. - self.isbjunk = junkdict.has_key - self.isbpopular = populardict.has_key + self.isbjunk = junkdict.__contains__ + self.isbpopular = populardict.__contains__ def find_longest_match(self, alo, ahi, blo, bhi): """Find longest matching block in a[alo:ahi] and b[blo:bhi]. @@ -674,7 +674,7 @@ class SequenceMatcher: # avail[x] is the number of times x appears in 'b' less the # number of times we've seen it in 'a' so far ... kinda avail = {} - availhas, matches = avail.has_key, 0 + availhas, matches = avail.__contains__, 0 for elt in self.a: if availhas(elt): numb = avail[elt] diff --git a/Lib/distutils/archive_util.py b/Lib/distutils/archive_util.py index b725a14b410..059d5447f68 100644 --- a/Lib/distutils/archive_util.py +++ b/Lib/distutils/archive_util.py @@ -124,7 +124,7 @@ ARCHIVE_FORMATS = { def check_archive_formats (formats): for format in formats: - if not ARCHIVE_FORMATS.has_key(format): + if format not in ARCHIVE_FORMATS: return format else: return None diff --git a/Lib/distutils/ccompiler.py b/Lib/distutils/ccompiler.py index 1349abeb65f..0ed9a40a35e 100644 --- a/Lib/distutils/ccompiler.py +++ b/Lib/distutils/ccompiler.py @@ -159,7 +159,7 @@ class CCompiler: # basically the same things with Unix C compilers. for key in args.keys(): - if not self.executables.has_key(key): + if key not in self.executables: raise ValueError, \ "unknown executable '%s' for class %s" % \ (key, self.__class__.__name__) diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py index 9626710b0c9..cd67544b7aa 100644 --- a/Lib/distutils/command/build_ext.py +++ b/Lib/distutils/command/build_ext.py @@ -341,7 +341,7 @@ class build_ext (Command): # Medium-easy stuff: same syntax/semantics, different names. ext.runtime_library_dirs = build_info.get('rpath') - if build_info.has_key('def_file'): + if 'def_file' in build_info: log.warn("'def_file' element of build info dict " "no longer supported") diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py index c9c6f037a73..85a28fe7953 100644 --- a/Lib/distutils/core.py +++ b/Lib/distutils/core.py @@ -101,9 +101,9 @@ def setup (**attrs): else: klass = Distribution - if not attrs.has_key('script_name'): + if 'script_name' not in attrs: attrs['script_name'] = os.path.basename(sys.argv[0]) - if not attrs.has_key('script_args'): + if 'script_args' not in attrs: attrs['script_args'] = sys.argv[1:] # Create the Distribution instance, using the remaining arguments @@ -111,7 +111,7 @@ def setup (**attrs): try: _setup_distribution = dist = klass(attrs) except DistutilsSetupError, msg: - if attrs.has_key('name'): + if 'name' not in attrs: raise SystemExit, "error in %s setup command: %s" % \ (attrs['name'], msg) else: diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py index ff49886d97f..d098cb9713b 100644 --- a/Lib/distutils/dist.py +++ b/Lib/distutils/dist.py @@ -239,7 +239,7 @@ Common commands: (see '--help-commands' for more) for (opt, val) in cmd_options.items(): opt_dict[opt] = ("setup script", val) - if attrs.has_key('licence'): + if 'licence' in attrs: attrs['license'] = attrs['licence'] del attrs['licence'] msg = "'licence' distribution option is deprecated; use 'license'" @@ -343,7 +343,7 @@ Common commands: (see '--help-commands' for more) user_filename = "pydistutils.cfg" # And look for the user config file - if os.environ.has_key('HOME'): + if 'HOME' in os.environ: user_file = os.path.join(os.environ.get('HOME'), user_filename) if os.path.isfile(user_file): files.append(user_file) @@ -388,7 +388,7 @@ Common commands: (see '--help-commands' for more) # If there was a "global" section in the config file, use it # to set Distribution options. - if self.command_options.has_key('global'): + if 'global' in self.command_options: for (opt, (src, val)) in self.command_options['global'].items(): alias = self.negative_opt.get(opt) try: @@ -907,7 +907,7 @@ Common commands: (see '--help-commands' for more) try: is_string = type(value) is StringType - if neg_opt.has_key(option) and is_string: + if option in neg_opt and is_string: setattr(command_obj, neg_opt[option], not strtobool(value)) elif option in bool_opts and is_string: setattr(command_obj, option, strtobool(value)) diff --git a/Lib/distutils/fancy_getopt.py b/Lib/distutils/fancy_getopt.py index 218ed73f987..31cf0c5c3b9 100644 --- a/Lib/distutils/fancy_getopt.py +++ b/Lib/distutils/fancy_getopt.py @@ -97,7 +97,7 @@ class FancyGetopt: self._build_index() def add_option (self, long_option, short_option=None, help_string=None): - if self.option_index.has_key(long_option): + if long_option in self.option_index: raise DistutilsGetoptError, \ "option conflict: already an option '%s'" % long_option else: @@ -109,7 +109,7 @@ class FancyGetopt: def has_option (self, long_option): """Return true if the option table for this parser has an option with long name 'long_option'.""" - return self.option_index.has_key(long_option) + return long_option in self.option_index def get_attr_name (self, long_option): """Translate long option name 'long_option' to the form it @@ -121,11 +121,11 @@ class FancyGetopt: def _check_alias_dict (self, aliases, what): assert type(aliases) is DictionaryType for (alias, opt) in aliases.items(): - if not self.option_index.has_key(alias): + if alias not in self.option_index: raise DistutilsGetoptError, \ ("invalid %s '%s': " "option '%s' not defined") % (what, alias, alias) - if not self.option_index.has_key(opt): + if opt not in self.option_index: raise DistutilsGetoptError, \ ("invalid %s '%s': " "aliased option '%s' not defined") % (what, alias, opt) diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py index 76fe256c001..0ea4bb7334a 100644 --- a/Lib/distutils/sysconfig.py +++ b/Lib/distutils/sysconfig.py @@ -150,22 +150,22 @@ def customize_compiler(compiler): get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', 'CCSHARED', 'LDSHARED', 'SO') - if os.environ.has_key('CC'): + if 'CC' in os.environ: cc = os.environ['CC'] - if os.environ.has_key('CXX'): + if 'CXX' in os.environ: cxx = os.environ['CXX'] - if os.environ.has_key('LDSHARED'): + if 'LDSHARED' in os.environ: ldshared = os.environ['LDSHARED'] - if os.environ.has_key('CPP'): + if 'CPP' in os.environ: cpp = os.environ['CPP'] else: cpp = cc + " -E" # not always - if os.environ.has_key('LDFLAGS'): + if 'LDFLAGS' in os.environ: ldshared = ldshared + ' ' + os.environ['LDFLAGS'] - if os.environ.has_key('CFLAGS'): + if 'CFLAGS' in os.environ: cflags = opt + ' ' + os.environ['CFLAGS'] ldshared = ldshared + ' ' + os.environ['CFLAGS'] - if os.environ.has_key('CPPFLAGS'): + if 'CPPFLAGS' in os.environ: cpp = cpp + ' ' + os.environ['CPPFLAGS'] cflags = cflags + ' ' + os.environ['CPPFLAGS'] ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] @@ -277,12 +277,12 @@ def parse_makefile(fn, g=None): if m: n = m.group(1) found = True - if done.has_key(n): + if n in done: item = str(done[n]) - elif notdone.has_key(n): + elif n in notdone: # get it on a subsequent round found = False - elif os.environ.has_key(n): + elif n in os.environ: # do it like make: fall back to environment item = os.environ[n] else: @@ -366,7 +366,7 @@ def _init_posix(): # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so # it needs to be compatible. # If it isn't set we set it to the configure-time value - if sys.platform == 'darwin' and g.has_key('MACOSX_DEPLOYMENT_TARGET'): + if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g: cfg_target = g['MACOSX_DEPLOYMENT_TARGET'] cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '') if cur_target == '': diff --git a/Lib/distutils/text_file.py b/Lib/distutils/text_file.py index 67efd65e36d..ff2878de1b6 100644 --- a/Lib/distutils/text_file.py +++ b/Lib/distutils/text_file.py @@ -89,7 +89,7 @@ class TextFile: # set values for all options -- either from client option hash # or fallback to default_options for opt in self.default_options.keys(): - if options.has_key (opt): + if opt in options: setattr (self, opt, options[opt]) else: @@ -97,7 +97,7 @@ class TextFile: # sanity check client option hash for opt in options.keys(): - if not self.default_options.has_key (opt): + if opt not in self.default_options: raise KeyError, "invalid TextFile option '%s'" % opt if file is None: diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py index 1265f4c4e33..1bcda93fe0f 100644 --- a/Lib/distutils/util.py +++ b/Lib/distutils/util.py @@ -200,11 +200,11 @@ def check_environ (): if _environ_checked: return - if os.name == 'posix' and not os.environ.has_key('HOME'): + if os.name == 'posix' and 'HOME' not in os.environ: import pwd os.environ['HOME'] = pwd.getpwuid(os.getuid())[5] - if not os.environ.has_key('PLAT'): + if 'PLAT' not in os.environ: os.environ['PLAT'] = get_platform() _environ_checked = 1 @@ -222,7 +222,7 @@ def subst_vars (s, local_vars): check_environ() def _subst (match, local_vars=local_vars): var_name = match.group(1) - if local_vars.has_key(var_name): + if var_name in local_vars: return str(local_vars[var_name]) else: return os.environ[var_name] diff --git a/Lib/dumbdbm.py b/Lib/dumbdbm.py index 84a76658908..e00d9e8382e 100644 --- a/Lib/dumbdbm.py +++ b/Lib/dumbdbm.py @@ -195,9 +195,6 @@ class _Database(UserDict.DictMixin): def keys(self): return self._index.keys() - def has_key(self, key): - return key in self._index - def __contains__(self, key): return key in self._index diff --git a/Lib/mailbox.py b/Lib/mailbox.py index b72128b4db4..ed7c7d1eaa6 100755 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -120,13 +120,10 @@ class Mailbox: """Return a list of (key, message) tuples. Memory intensive.""" return list(self.iteritems()) - def has_key(self, key): + def __contains__(self, key): """Return True if the keyed message exists, False otherwise.""" raise NotImplementedError('Method must be implemented by subclass') - def __contains__(self, key): - return self.has_key(key) - def __len__(self): """Return a count of messages in the mailbox.""" raise NotImplementedError('Method must be implemented by subclass') @@ -330,7 +327,7 @@ class Maildir(Mailbox): continue yield key - def has_key(self, key): + def __contains__(self, key): """Return True if the keyed message exists, False otherwise.""" self._refresh() return key in self._toc @@ -515,7 +512,7 @@ class _singlefileMailbox(Mailbox): for key in self._toc.keys(): yield key - def has_key(self, key): + def __contains__(self, key): """Return True if the keyed message exists, False otherwise.""" self._lookup() return key in self._toc @@ -902,7 +899,7 @@ class MH(Mailbox): return iter(sorted(int(entry) for entry in os.listdir(self._path) if entry.isdigit())) - def has_key(self, key): + def __contains__(self, key): """Return True if the keyed message exists, False otherwise.""" return os.path.exists(os.path.join(self._path, str(key))) diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index 25e14827c3c..2a31dbfa689 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -242,7 +242,7 @@ class ModuleFinder: else: self.msgout(3, "import_module ->", m) return m - if self.badmodules.has_key(fqname): + if fqname in self.badmodules: self.msgout(3, "import_module -> None") return None if parent and parent.__path__ is None: @@ -388,7 +388,7 @@ class ModuleFinder: return m def add_module(self, fqname): - if self.modules.has_key(fqname): + if fqname in self.modules: return self.modules[fqname] self.modules[fqname] = m = Module(fqname) return m diff --git a/Lib/optparse.py b/Lib/optparse.py index 62d2f7e034f..a02f79a0988 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -602,7 +602,7 @@ class Option: def _set_attrs(self, attrs): for attr in self.ATTRS: - if attrs.has_key(attr): + if attr in attrs: setattr(self, attr, attrs[attr]) del attrs[attr] else: @@ -854,7 +854,7 @@ class Values: are silently ignored. """ for attr in dir(self): - if dict.has_key(attr): + if attr in dict: dval = dict[attr] if dval is not None: setattr(self, attr, dval) @@ -974,10 +974,10 @@ class OptionContainer: def _check_conflict(self, option): conflict_opts = [] for opt in option._short_opts: - if self._short_opt.has_key(opt): + if opt in self._short_opt: conflict_opts.append((opt, self._short_opt[opt])) for opt in option._long_opts: - if self._long_opt.has_key(opt): + if opt in self._long_opt: conflict_opts.append((opt, self._long_opt[opt])) if conflict_opts: @@ -1023,7 +1023,7 @@ class OptionContainer: if option.dest is not None: # option has a dest, we need a default if option.default is not NO_DEFAULT: self.defaults[option.dest] = option.default - elif not self.defaults.has_key(option.dest): + elif option.dest not in self.defaults: self.defaults[option.dest] = None return option @@ -1039,8 +1039,8 @@ class OptionContainer: self._long_opt.get(opt_str)) def has_option(self, opt_str): - return (self._short_opt.has_key(opt_str) or - self._long_opt.has_key(opt_str)) + return (opt_str in self._short_opt or + opt_str) in self._long_opt def remove_option(self, opt_str): option = self._short_opt.get(opt_str) @@ -1658,7 +1658,7 @@ def _match_abbrev(s, wordmap): 'words', raise BadOptionError. """ # Is there an exact match? - if wordmap.has_key(s): + if s in wordmap: return s else: # Isolate all words with s as a prefix. diff --git a/Lib/os.py b/Lib/os.py index 2d1b29b57bf..5ee3d4bc7ca 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -436,8 +436,6 @@ else: def __delitem__(self, key): unsetenv(key) del self.data[key.upper()] - def has_key(self, key): - return key.upper() in self.data def __contains__(self, key): return key.upper() in self.data def get(self, key, failobj=None): diff --git a/Lib/pickle.py b/Lib/pickle.py index ccda3e7e266..b0b3aa12819 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1287,19 +1287,19 @@ def decode_long(data): r"""Decode a long from a two's complement little-endian binary string. >>> decode_long('') - 0L + 0 >>> decode_long("\xff\x00") - 255L + 255 >>> decode_long("\xff\x7f") - 32767L + 32767 >>> decode_long("\x00\xff") - -256L + -256 >>> decode_long("\x00\x80") - -32768L + -32768 >>> decode_long("\x80") - -128L + -128 >>> decode_long("\x7f") - 127L + 127 """ nbytes = len(data) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index ab5e2475795..941ca4273f4 100644 --- a/Lib/pickletools.py +++ b/Lib/pickletools.py @@ -517,23 +517,14 @@ def read_decimalnl_long(f): r""" >>> import StringIO - >>> read_decimalnl_long(StringIO.StringIO("1234\n56")) - Traceback (most recent call last): - ... - ValueError: trailing 'L' required in '1234' - - Someday the trailing 'L' will probably go away from this output. - >>> read_decimalnl_long(StringIO.StringIO("1234L\n56")) - 1234L + 1234 >>> read_decimalnl_long(StringIO.StringIO("123456789012345678901234L\n6")) - 123456789012345678901234L + 123456789012345678901234 """ s = read_stringnl(f, decode=False, stripquotes=False) - if not s.endswith("L"): - raise ValueError("trailing 'L' required in %r" % s) return long(s) @@ -625,15 +616,15 @@ def read_long1(f): r""" >>> import StringIO >>> read_long1(StringIO.StringIO("\x00")) - 0L + 0 >>> read_long1(StringIO.StringIO("\x02\xff\x00")) - 255L + 255 >>> read_long1(StringIO.StringIO("\x02\xff\x7f")) - 32767L + 32767 >>> read_long1(StringIO.StringIO("\x02\x00\xff")) - -256L + -256 >>> read_long1(StringIO.StringIO("\x02\x00\x80")) - -32768L + -32768 """ n = read_uint1(f) @@ -657,15 +648,15 @@ def read_long4(f): r""" >>> import StringIO >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\xff\x00")) - 255L + 255 >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\xff\x7f")) - 32767L + 32767 >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\xff")) - -256L + -256 >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\x80")) - -32768L + -32768 >>> read_long1(StringIO.StringIO("\x00\x00\x00\x00")) - 0L + 0 """ n = read_int4(f) diff --git a/Lib/platform.py b/Lib/platform.py index 288bc956196..5fd13a3d5a1 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -877,7 +877,7 @@ def architecture(executable=sys.executable,bits='',linkage=''): executable == sys.executable: # "file" command did not return anything; we'll try to provide # some sensible defaults then... - if _default_architecture.has_key(sys.platform): + if sys.platform in _default_architecture: b,l = _default_architecture[sys.platform] if b: bits = b diff --git a/Lib/profile.py b/Lib/profile.py index b6048aa0c37..27d68ba4838 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -318,7 +318,7 @@ class Profile: fn = ("", 0, self.c_func_name) self.cur = (t, 0, 0, fn, frame, self.cur) timings = self.timings - if timings.has_key(fn): + if fn in timings: cc, ns, tt, ct, callers = timings[fn] timings[fn] = cc, ns+1, tt, ct, callers else: diff --git a/Lib/pstats.py b/Lib/pstats.py index 4e94b0ce0ee..20edd70f5ab 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -140,7 +140,7 @@ class Stats: self.total_calls += nc self.prim_calls += cc self.total_tt += tt - if callers.has_key(("jprofile", 0, "profiler")): + if ("jprofile", 0, "profiler") in callers: self.top_level[func] = None if len(func_std_string(func)) > self.max_name_len: self.max_name_len = len(func_std_string(func)) diff --git a/Lib/rfc822.py b/Lib/rfc822.py index d6d5e471297..0ef0d974f22 100644 --- a/Lib/rfc822.py +++ b/Lib/rfc822.py @@ -428,10 +428,6 @@ class Message: self.dict[lowername] = default return default - def has_key(self, name): - """Determine whether a message contains the named header.""" - return name.lower() in self.dict - def __contains__(self, name): """Determine whether a message contains the named header.""" return name.lower() in self.dict diff --git a/Lib/sets.py b/Lib/sets.py index 32a0dd64ff8..55f93a6d1ff 100644 --- a/Lib/sets.py +++ b/Lib/sets.py @@ -231,7 +231,7 @@ class BaseSet(object): little, big = self, other else: little, big = other, self - common = ifilter(big._data.has_key, little) + common = ifilter(big._data.__contains__, little) return self.__class__(common) def __xor__(self, other): @@ -256,9 +256,9 @@ class BaseSet(object): otherdata = other._data except AttributeError: otherdata = Set(other)._data - for elt in ifilterfalse(otherdata.has_key, selfdata): + for elt in ifilterfalse(otherdata.__contains__, selfdata): data[elt] = value - for elt in ifilterfalse(selfdata.has_key, otherdata): + for elt in ifilterfalse(selfdata.__contains__, otherdata): data[elt] = value return result @@ -283,7 +283,7 @@ class BaseSet(object): except AttributeError: otherdata = Set(other)._data value = True - for elt in ifilterfalse(otherdata.has_key, self): + for elt in ifilterfalse(otherdata.__contains__, self): data[elt] = value return result @@ -309,7 +309,7 @@ class BaseSet(object): self._binary_sanity_check(other) if len(self) > len(other): # Fast check for obvious cases return False - for elt in ifilterfalse(other._data.has_key, self): + for elt in ifilterfalse(other._data.__contains__, self): return False return True @@ -318,7 +318,7 @@ class BaseSet(object): self._binary_sanity_check(other) if len(self) < len(other): # Fast check for obvious cases return False - for elt in ifilterfalse(self._data.has_key, other): + for elt in ifilterfalse(self._data.__contains__, other): return False return True @@ -501,7 +501,7 @@ class Set(BaseSet): other = Set(other) if self is other: self.clear() - for elt in ifilter(data.has_key, other): + for elt in ifilter(data.__contains__, other): del data[elt] # Python dict-like mass mutations: update, clear diff --git a/Lib/shelve.py b/Lib/shelve.py index 7a75445b0bf..2971354af0d 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -20,7 +20,7 @@ object): # access returns a *copy* of the entry! del d[key] # delete data stored at key (raises KeyError # if no such key) - flag = d.has_key(key) # true if the key exists; same as "key in d" + flag = key in d # true if the key exists list = d.keys() # a list of all existing keys (slow!) d.close() # close it @@ -94,14 +94,11 @@ class Shelf(UserDict.DictMixin): def __len__(self): return len(self.dict) - def has_key(self, key): - return self.dict.has_key(key) - def __contains__(self, key): - return self.dict.has_key(key) + return key in self.dict def get(self, key, default=None): - if self.dict.has_key(key): + if key in self.dict: return self[key] return default diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py index 4b0f797fbf5..1c570b6d349 100644 --- a/Lib/test/mapping_tests.py +++ b/Lib/test/mapping_tests.py @@ -54,12 +54,10 @@ class BasicTestMappingProtocol(unittest.TestCase): #len self.assertEqual(len(p), 0) self.assertEqual(len(d), len(self.reference)) - #has_key + #__contains__ for k in self.reference: - self.assert_(d.has_key(k)) self.assert_(k in d) for k in self.other: - self.failIf(d.has_key(k)) self.failIf(k in d) #cmp self.assertEqual(cmp(p,p), 0) @@ -333,16 +331,6 @@ class TestMappingProtocol(BasicTestMappingProtocol): d = self._full_mapping({1:2}) self.assertEqual(d.items(), [(1, 2)]) - def test_has_key(self): - d = self._empty_mapping() - self.assert_(not d.has_key('a')) - d = self._full_mapping({'a': 1, 'b': 2}) - k = d.keys() - k.sort() - self.assertEqual(k, ['a', 'b']) - - self.assertRaises(TypeError, d.has_key) - def test_contains(self): d = self._empty_mapping() self.assert_(not ('a' in d)) diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index a6e146b7e35..8c0f2e4a0eb 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -1034,7 +1034,6 @@ _expectations = { 'darwin': """ test_al - test_bsddb3 test_cd test_cl test_gdbm diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py index c45e13997ab..dba9161c9dd 100644 --- a/Lib/test/test___all__.py +++ b/Lib/test/test___all__.py @@ -24,7 +24,7 @@ class AllTest(unittest.TestCase): "%s has no __all__ attribute" % modname) names = {} exec "from %s import *" % modname in names - if names.has_key("__builtins__"): + if "__builtins__" in names: del names["__builtins__"] keys = set(names) all = set(sys.modules[modname].__all__) diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index 663417d4b31..15e1ef7a446 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -183,9 +183,9 @@ class BoolTest(unittest.TestCase): self.assertIs(issubclass(bool, int), True) self.assertIs(issubclass(int, bool), False) - def test_haskey(self): - self.assertIs({}.has_key(1), False) - self.assertIs({1:1}.has_key(1), True) + def test_contains(self): + self.assertIs(1 in {}, False) + self.assertIs(1 in {1:1}, True) def test_string(self): self.assertIs("xyz".endswith("z"), True) diff --git a/Lib/test/test_bsddb.py b/Lib/test/test_bsddb.py index 474f3da42b2..91c1cca7a02 100755 --- a/Lib/test/test_bsddb.py +++ b/Lib/test/test_bsddb.py @@ -135,11 +135,6 @@ class TestBSDDB(unittest.TestCase): self.assert_(k in self.f) self.assert_('not here' not in self.f) - def test_has_key(self): - for k in self.d: - self.assert_(self.f.has_key(k)) - self.assert_(not self.f.has_key('not here')) - def test_clear(self): self.f.clear() self.assertEqual(len(self.f), 0) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index c7e439457de..5797aeff5c9 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -630,9 +630,9 @@ class BuiltinTest(unittest.TestCase): def test_hex(self): self.assertEqual(hex(16), '0x10') - self.assertEqual(hex(16L), '0x10L') + self.assertEqual(hex(16L), '0x10') self.assertEqual(hex(-16), '-0x10') - self.assertEqual(hex(-16L), '-0x10L') + self.assertEqual(hex(-16L), '-0x10') self.assertRaises(TypeError, hex, {}) def test_id(self): @@ -1240,9 +1240,9 @@ class BuiltinTest(unittest.TestCase): def test_oct(self): self.assertEqual(oct(100), '0144') - self.assertEqual(oct(100L), '0144L') + self.assertEqual(oct(100L), '0144') self.assertEqual(oct(-100), '-0144') - self.assertEqual(oct(-100L), '-0144L') + self.assertEqual(oct(-100L), '-0144') self.assertRaises(TypeError, oct, ()) def write_testfile(self): @@ -1441,7 +1441,7 @@ class BuiltinTest(unittest.TestCase): def test_repr(self): self.assertEqual(repr(''), '\'\'') self.assertEqual(repr(0), '0') - self.assertEqual(repr(0L), '0L') + self.assertEqual(repr(0L), '0') self.assertEqual(repr(()), '()') self.assertEqual(repr([]), '[]') self.assertEqual(repr({}), '{}') diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index f3c7c8c394f..2652343ee13 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -9,39 +9,39 @@ from test import test_support class CFunctionCalls(unittest.TestCase): def test_varargs0(self): - self.assertRaises(TypeError, {}.has_key) + self.assertRaises(TypeError, {}.__contains__) def test_varargs1(self): - {}.has_key(0) + {}.__contains__(0) def test_varargs2(self): - self.assertRaises(TypeError, {}.has_key, 0, 1) + self.assertRaises(TypeError, {}.__contains__, 0, 1) def test_varargs0_ext(self): try: - {}.has_key(*()) + {}.__contains__(*()) except TypeError: pass def test_varargs1_ext(self): - {}.has_key(*(0,)) + {}.__contains__(*(0,)) def test_varargs2_ext(self): try: - {}.has_key(*(1, 2)) + {}.__contains__(*(1, 2)) except TypeError: pass else: raise RuntimeError def test_varargs0_kw(self): - self.assertRaises(TypeError, {}.has_key, x=2) + self.assertRaises(TypeError, {}.__contains__, x=2) def test_varargs1_kw(self): - self.assertRaises(TypeError, {}.has_key, x=2) + self.assertRaises(TypeError, {}.__contains__, x=2) def test_varargs2_kw(self): - self.assertRaises(TypeError, {}.has_key, x=2, y=2) + self.assertRaises(TypeError, {}.__contains__, x=2, y=2) def test_oldargs0_0(self): {}.keys() diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 130b19d969c..8b0b482b0c1 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -158,10 +158,10 @@ def main(): # test individual fields for key in expect.keys(): expect_val = expect[key] - verify(fcd.has_key(key)) + verify(key in fcd) verify(norm(fcd[key]) == norm(expect[key])) verify(fcd.get(key, "default") == fcd[key]) - verify(fs.has_key(key)) + verify(key in fs) if len(expect_val) > 1: single_value = 0 else: diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index e5757c9501a..8fdd2628669 100755 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -28,7 +28,7 @@ def test_keys(): d['a'] = 'b' d['12345678910'] = '019237410982340912840198242' d.keys() - if d.has_key('a'): + if 'a' in d: if verbose: print 'Test dbm keys: ', d.keys() diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index bbca79886c8..f16884607ed 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -21,8 +21,8 @@ class DictTest(unittest.TestCase): self.assertEqual(d.keys(), []) d = {'a': 1, 'b': 2} k = d.keys() - self.assert_(d.has_key('a')) - self.assert_(d.has_key('b')) + self.assert_('a' in d) + self.assert_('b' in d) self.assertRaises(TypeError, d.keys, None) @@ -43,16 +43,6 @@ class DictTest(unittest.TestCase): self.assertRaises(TypeError, d.items, None) - def test_has_key(self): - d = {} - self.assert_(not d.has_key('a')) - d = {'a': 1, 'b': 2} - k = d.keys() - k.sort() - self.assertEqual(k, ['a', 'b']) - - self.assertRaises(TypeError, d.has_key) - def test_contains(self): d = {} self.assert_(not ('a' in d)) diff --git a/Lib/test/test_gdbm.py b/Lib/test/test_gdbm.py index 03a47d9c245..e76539af76c 100755 --- a/Lib/test/test_gdbm.py +++ b/Lib/test/test_gdbm.py @@ -17,7 +17,7 @@ a = g.keys() if verbose: print 'Test gdbm file keys: ', a -g.has_key('a') +'a' in g g.close() try: g['a'] diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index f1608673437..296dc9ba585 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -472,7 +472,7 @@ def f(): f() g = {} exec 'z = 1' in g -if g.has_key('__builtins__'): del g['__builtins__'] +if '__builtins__' in g: del g['__builtins__'] if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' g = {} l = {} @@ -480,8 +480,8 @@ l = {} import warnings warnings.filterwarnings("ignore", "global statement", module="") exec 'global a; a = 1; b = 2' in g, l -if g.has_key('__builtins__'): del g['__builtins__'] -if l.has_key('__builtins__'): del l['__builtins__'] +if '__builtins__' in g: del g['__builtins__'] +if '__builtins__' in l: del l['__builtins__'] if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 45dd118fbe1..04e856fcfe7 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -229,16 +229,9 @@ class TestMailbox(TestBase): count += 1 self.assert_(len(values) == count) - def test_has_key(self): - # Check existence of keys using has_key() - self._test_has_key_or_contains(self._box.has_key) - def test_contains(self): # Check existence of keys using __contains__() - self._test_has_key_or_contains(self._box.__contains__) - - def _test_has_key_or_contains(self, method): - # (Used by test_has_key() and test_contains().) + method = self._box.__contains__ self.assert_(not method('foo')) key0 = self._box.add(self._template % 0) self.assert_(method(key0)) @@ -442,7 +435,7 @@ class TestMailboxSuperclass(TestBase): self.assertRaises(NotImplementedError, lambda: box.get_message('')) self.assertRaises(NotImplementedError, lambda: box.get_string('')) self.assertRaises(NotImplementedError, lambda: box.get_file('')) - self.assertRaises(NotImplementedError, lambda: box.has_key('')) + self.assertRaises(NotImplementedError, lambda: '' in box) self.assertRaises(NotImplementedError, lambda: box.__contains__('')) self.assertRaises(NotImplementedError, lambda: box.__len__()) self.assertRaises(NotImplementedError, lambda: box.clear()) diff --git a/Lib/test/test_multibytecodec_support.py b/Lib/test/test_multibytecodec_support.py index bec32de1d56..50ed4818624 100644 --- a/Lib/test/test_multibytecodec_support.py +++ b/Lib/test/test_multibytecodec_support.py @@ -297,7 +297,7 @@ class TestBase_Mapping(unittest.TestCase): continue unich = unichrs(data[1]) - if ord(unich) == 0xfffd or urt_wa.has_key(unich): + if ord(unich) == 0xfffd or unich in urt_wa: continue urt_wa[unich] = csetch diff --git a/Lib/test/test_operations.py b/Lib/test/test_operations.py index fafc0622119..0b558de4d6e 100644 --- a/Lib/test/test_operations.py +++ b/Lib/test/test_operations.py @@ -25,7 +25,6 @@ d[x1] = 1 for stmt in ['d[x2] = 2', 'z = d[x2]', 'x2 in d', - 'd.has_key(x2)', 'd.get(x2)', 'd.setdefault(x2, 42)', 'd.pop(x2)', diff --git a/Lib/test/test_pkgimport.py b/Lib/test/test_pkgimport.py index c87c342b75c..8b5e3adf282 100644 --- a/Lib/test/test_pkgimport.py +++ b/Lib/test/test_pkgimport.py @@ -6,14 +6,14 @@ class TestImport(unittest.TestCase): def __init__(self, *args, **kw): self.package_name = 'PACKAGE_' - while sys.modules.has_key(self.package_name): + while self.package_name in sys.modules: self.package_name += random.choose(string.letters) self.module_name = self.package_name + '.foo' unittest.TestCase.__init__(self, *args, **kw) def remove_modules(self): for module_name in (self.package_name, self.module_name): - if sys.modules.has_key(module_name): + if module_name in sys.modules: del sys.modules[module_name] def setUp(self): @@ -52,7 +52,7 @@ class TestImport(unittest.TestCase): try: __import__(self.module_name) except SyntaxError: pass else: raise RuntimeError, 'Failed to induce SyntaxError' - self.assert_(not sys.modules.has_key(self.module_name) and + self.assert_(self.module_name not in sys.modules and not hasattr(sys.modules[self.package_name], 'foo')) # ...make up a variable name that isn't bound in __builtins__ diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 2410b03f6c3..01703b592de 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -40,11 +40,11 @@ class PyclbrTest(TestCase): def assertHaskey(self, obj, key, ignore): - ''' succeed iff obj.has_key(key) or key in ignore. ''' + ''' succeed iff key in obj or key in ignore. ''' if key in ignore: return - if not obj.has_key(key): + if key not in obj: print >>sys.stderr, "***",key - self.failUnless(obj.has_key(key)) + self.failUnless(key) in obj def assertEqualsOrIgnored(self, a, b, ignore): ''' succeed iff a == b or a in ignore or b in ignore ''' diff --git a/Lib/test/test_rfc822.py b/Lib/test/test_rfc822.py index 6d22825ddf6..de577da6360 100644 --- a/Lib/test/test_rfc822.py +++ b/Lib/test/test_rfc822.py @@ -25,7 +25,7 @@ class MessageTestCase(unittest.TestCase): def test_setdefault(self): msg = self.create_message( 'To: "last, first" \n\ntest\n') - self.assert_(not msg.has_key("New-Header")) + self.assert_("New-Header" not in msg) self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value") self.assert_(msg.setdefault("New-Header", "Different-Value") == "New-Value") diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index af4c7dd8f41..83ffcf119aa 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -357,7 +357,7 @@ def test_expat_nsattrs_wattr(): attrs.getNames() == [(ns_uri, "attr")] and \ (attrs.getQNames() == [] or attrs.getQNames() == ["ns:attr"]) and \ len(attrs) == 1 and \ - attrs.has_key((ns_uri, "attr")) and \ + (ns_uri, "attr") in attrs and \ attrs.keys() == [(ns_uri, "attr")] and \ attrs.get((ns_uri, "attr")) == "val" and \ attrs.get((ns_uri, "attr"), 25) == "val" and \ @@ -571,7 +571,7 @@ def verify_empty_attrs(attrs): attrs.getNames() == [] and \ attrs.getQNames() == [] and \ len(attrs) == 0 and \ - not attrs.has_key("attr") and \ + "attr" not in attrs and \ attrs.keys() == [] and \ attrs.get("attrs") is None and \ attrs.get("attrs", 25) == 25 and \ @@ -584,7 +584,7 @@ def verify_attrs_wattr(attrs): attrs.getNames() == ["attr"] and \ attrs.getQNames() == ["attr"] and \ len(attrs) == 1 and \ - attrs.has_key("attr") and \ + "attr" in attrs and \ attrs.keys() == ["attr"] and \ attrs.get("attr") == "val" and \ attrs.get("attr", 25) == "val" and \ @@ -639,7 +639,7 @@ def verify_empty_nsattrs(attrs): attrs.getNames() == [] and \ attrs.getQNames() == [] and \ len(attrs) == 0 and \ - not attrs.has_key((ns_uri, "attr")) and \ + (ns_uri, "attr") not in attrs and \ attrs.keys() == [] and \ attrs.get((ns_uri, "attr")) is None and \ attrs.get((ns_uri, "attr"), 25) == 25 and \ @@ -658,7 +658,7 @@ def test_nsattrs_wattr(): attrs.getNames() == [(ns_uri, "attr")] and \ attrs.getQNames() == ["ns:attr"] and \ len(attrs) == 1 and \ - attrs.has_key((ns_uri, "attr")) and \ + (ns_uri, "attr") in attrs and \ attrs.keys() == [(ns_uri, "attr")] and \ attrs.get((ns_uri, "attr")) == "val" and \ attrs.get((ns_uri, "attr"), 25) == "val" and \ diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py index 239745c613f..98b7ef3d587 100644 --- a/Lib/test/test_scope.py +++ b/Lib/test/test_scope.py @@ -472,7 +472,7 @@ def f(x): return g d = f(2)(4) -verify(d.has_key('h')) +verify('h' in d) del d['h'] vereq(d, {'x': 2, 'y': 7, 'w': 6}) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 3a8b9d31445..05a4ac45017 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -216,7 +216,7 @@ class ImportSideEffectTests(unittest.TestCase): def test_sitecustomize_executed(self): # If sitecustomize is available, it should have been imported. - if not sys.modules.has_key("sitecustomize"): + if "sitecustomize" not in sys.modules: try: import sitecustomize except ImportError: diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 66fd6672f07..302698ba3a1 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -266,7 +266,7 @@ class IntTester: if x < 0: expected += 1L << self.bitsize assert expected > 0 - expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' + expected = hex(expected)[2:] # chop "0x" if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) @@ -322,7 +322,7 @@ class IntTester: # Try big-endian. format = ">" + code expected = long(x) - expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' + expected = hex(expected)[2:] # chop "0x" if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index f4be7590f1c..4fd783489b6 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -180,7 +180,7 @@ class TimeTestCase(unittest.TestCase): # rely on it. if org_TZ is not None: environ['TZ'] = org_TZ - elif environ.has_key('TZ'): + elif 'TZ' in environ: del environ['TZ'] time.tzset() diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 67218b81c19..4df854e3599 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -653,7 +653,7 @@ class HandlerTests(unittest.TestCase): r.info; r.geturl # addinfourl methods r.code, r.msg == 200, "OK" # added from MockHTTPClass.getreply() hdrs = r.info() - hdrs.get; hdrs.has_key # r.info() gives dict from .getreply() + hdrs.get; hdrs.__contains__ # r.info() gives dict from .getreply() self.assertEqual(r.geturl(), url) self.assertEqual(http.host, "example.com") diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py index a4b7de406bf..ecb33d1e652 100644 --- a/Lib/test/test_userdict.py +++ b/Lib/test/test_userdict.py @@ -94,13 +94,10 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): self.assertEqual(u2.items(), d2.items()) self.assertEqual(u2.values(), d2.values()) - # Test has_key and "in". + # Test "in". for i in u2.keys(): - self.assert_(u2.has_key(i)) self.assert_(i in u2) - self.assertEqual(u1.has_key(i), d1.has_key(i)) self.assertEqual(i in u1, i in d1) - self.assertEqual(u0.has_key(i), d0.has_key(i)) self.assertEqual(i in u0, i in d0) # Test update @@ -132,7 +129,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol): # Test setdefault t = UserDict.UserDict() self.assertEqual(t.setdefault("x", 42), 42) - self.assert_(t.has_key("x")) + self.assert_("x" in t) self.assertEqual(t.setdefault("x", 23), 42) # Test pop @@ -269,9 +266,6 @@ class UserDictMixinTest(mapping_tests.TestMappingProtocol): self.assertEqual(s.keys(), [10, 30]) ## Now, test the DictMixin methods one by one - # has_key - self.assert_(s.has_key(10)) - self.assert_(not s.has_key(20)) # __contains__ self.assert_(10 in s) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 1f65010c5bd..a4fb7f333cf 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -739,7 +739,7 @@ class MappingTestCase(TestBase): def test_weak_keys(self): # # This exercises d.copy(), d.items(), d[] = v, d[], del d[], - # len(d), d.has_key(). + # len(d), k in d. # dict, objects = self.make_weak_keyed_dict() for o in objects: @@ -761,8 +761,8 @@ class MappingTestCase(TestBase): "deleting the keys did not clear the dictionary") o = Object(42) dict[o] = "What is the meaning of the universe?" - self.assert_(dict.has_key(o)) - self.assert_(not dict.has_key(34)) + self.assert_(o in dict) + self.assert_(34 not in dict) def test_weak_keyed_iters(self): dict, objects = self.make_weak_keyed_dict() @@ -774,7 +774,7 @@ class MappingTestCase(TestBase): objects2 = list(objects) for wr in refs: ob = wr() - self.assert_(dict.has_key(ob)) + self.assert_(ob in dict) self.assert_(ob in dict) self.assertEqual(ob.arg, dict[ob]) objects2.remove(ob) @@ -785,7 +785,7 @@ class MappingTestCase(TestBase): self.assertEqual(len(list(dict.iterkeyrefs())), len(objects)) for wr in dict.iterkeyrefs(): ob = wr() - self.assert_(dict.has_key(ob)) + self.assert_(ob in dict) self.assert_(ob in dict) self.assertEqual(ob.arg, dict[ob]) objects2.remove(ob) @@ -900,13 +900,13 @@ class MappingTestCase(TestBase): weakdict = klass() o = weakdict.setdefault(key, value1) self.assert_(o is value1) - self.assert_(weakdict.has_key(key)) + self.assert_(key in weakdict) self.assert_(weakdict.get(key) is value1) self.assert_(weakdict[key] is value1) o = weakdict.setdefault(key, value2) self.assert_(o is value1) - self.assert_(weakdict.has_key(key)) + self.assert_(key in weakdict) self.assert_(weakdict.get(key) is value1) self.assert_(weakdict[key] is value1) @@ -920,20 +920,20 @@ class MappingTestCase(TestBase): def check_update(self, klass, dict): # - # This exercises d.update(), len(d), d.keys(), d.has_key(), + # This exercises d.update(), len(d), d.keys(), k in d, # d.get(), d[]. # weakdict = klass() weakdict.update(dict) self.assert_(len(weakdict) == len(dict)) for k in weakdict.keys(): - self.assert_(dict.has_key(k), + self.assert_(k in dict, "mysterious new key appeared in weak dict") v = dict.get(k) self.assert_(v is weakdict[k]) self.assert_(v is weakdict.get(k)) for k in dict.keys(): - self.assert_(weakdict.has_key(k), + self.assert_(k in weakdict, "original key disappeared in weak dict") v = dict[k] self.assert_(v is weakdict[k]) diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py index 1ec271b81a8..b42f4376017 100755 --- a/Lib/test/test_wsgiref.py +++ b/Lib/test/test_wsgiref.py @@ -341,7 +341,7 @@ class HeaderTests(TestCase): del h['foo'] # should not raise an error h['Foo'] = 'bar' - for m in h.has_key, h.__contains__, h.get, h.get_all, h.__getitem__: + for m in h.__contains__, h.get, h.get_all, h.__getitem__: self.failUnless(m('foo')) self.failUnless(m('Foo')) self.failUnless(m('FOO')) @@ -424,10 +424,10 @@ class HandlerTests(TestCase): env = handler.environ from os import environ for k,v in environ.items(): - if not empty.has_key(k): + if k not in empty: self.assertEqual(env[k],v) for k,v in empty.items(): - self.failUnless(env.has_key(k)) + self.failUnless(k in env) def testEnviron(self): h = TestHandler(X="Y") @@ -440,7 +440,7 @@ class HandlerTests(TestCase): h = BaseCGIHandler(None,None,None,{}) h.setup_environ() for key in 'wsgi.url_scheme', 'wsgi.input', 'wsgi.errors': - self.assert_(h.environ.has_key(key)) + self.assert_(key in h.environ) def testScheme(self): h=TestHandler(HTTPS="on"); h.setup_environ() diff --git a/Lib/trace.py b/Lib/trace.py index db36e1d21dd..e70869c2fc0 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -120,7 +120,7 @@ class Ignore: self._ignore = { '': 1 } def names(self, filename, modulename): - if self._ignore.has_key(modulename): + if modulename in self._ignore: return self._ignore[modulename] # haven't seen this one before, so see if the module name is diff --git a/Lib/unittest.py b/Lib/unittest.py index cde23d8a083..b34b389b78d 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -153,7 +153,7 @@ class TestResult: return ''.join(traceback.format_exception(exctype, value, tb)) def _is_relevant_tb_level(self, tb): - return tb.tb_frame.f_globals.has_key('__unittest') + return '__unittest' in tb.tb_frame.f_globals def _count_relevant_tb_levels(self, tb): length = 0 diff --git a/Lib/urllib.py b/Lib/urllib.py index d2a4c4807fa..e01f4215f6d 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -114,7 +114,7 @@ class URLopener: def __init__(self, proxies=None, **x509): if proxies is None: proxies = getproxies() - assert hasattr(proxies, 'has_key'), "proxies must be a mapping" + assert hasattr(proxies, 'keys'), "proxies must be a mapping" self.proxies = proxies self.key_file = x509.get('key_file') self.cert_file = x509.get('cert_file') diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 6ee9e2c8ee7..93cadd7d872 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -660,7 +660,7 @@ class ProxyHandler(BaseHandler): def __init__(self, proxies=None): if proxies is None: proxies = getproxies() - assert hasattr(proxies, 'has_key'), "proxies must be a mapping" + assert hasattr(proxies, 'keys'), "proxies must be a mapping" self.proxies = proxies for type, url in proxies.items(): setattr(self, '%s_open' % type, diff --git a/Lib/weakref.py b/Lib/weakref.py index 4f6d757fe3b..44cf9a7c1a1 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -64,13 +64,6 @@ class WeakValueDictionary(UserDict.UserDict): return False return o is not None - def has_key(self, key): - try: - o = self.data[key]() - except KeyError: - return False - return o is not None - def __repr__(self): return "" % id(self) @@ -259,13 +252,6 @@ class WeakKeyDictionary(UserDict.UserDict): def get(self, key, default=None): return self.data.get(ref(key),default) - def has_key(self, key): - try: - wr = ref(key) - except TypeError: - return 0 - return wr in self.data - def __contains__(self, key): try: wr = ref(key) diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index cc3a8058f7b..fb81bf3dd2c 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -159,7 +159,7 @@ class BaseHandler: Subclasses can extend this to add other defaults. """ - if not self.headers.has_key('Content-Length'): + if 'Content-Length' not in self.headers: self.set_content_length() def start_response(self, status, headers,exc_info=None): @@ -194,11 +194,11 @@ class BaseHandler: if self.origin_server: if self.client_is_modern(): self._write('HTTP/%s %s\r\n' % (self.http_version,self.status)) - if not self.headers.has_key('Date'): + if 'Date' not in self.headers: self._write( 'Date: %s\r\n' % format_date_time(time.time()) ) - if self.server_software and not self.headers.has_key('Server'): + if self.server_software and 'Server' not in self.headers: self._write('Server: %s\r\n' % self.server_software) else: self._write('Status: %s\r\n' % self.status) diff --git a/Lib/wsgiref/headers.py b/Lib/wsgiref/headers.py index 016eb86f9ee..934a645f3f3 100644 --- a/Lib/wsgiref/headers.py +++ b/Lib/wsgiref/headers.py @@ -80,12 +80,10 @@ class Headers: - def has_key(self, name): + def __contains__(self, name): """Return true if the message contains the header.""" return self.get(name) is not None - __contains__ = has_key - def get_all(self, name): """Return a list of all the values for the named field. diff --git a/Lib/wsgiref/util.py b/Lib/wsgiref/util.py index 9009b876e8e..17fdff637fb 100644 --- a/Lib/wsgiref/util.py +++ b/Lib/wsgiref/util.py @@ -166,7 +166,7 @@ _hoppish = { 'connection':1, 'keep-alive':1, 'proxy-authenticate':1, 'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1, 'upgrade':1 -}.has_key +}.__contains__ def is_hop_by_hop(header_name): """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header""" diff --git a/Lib/wsgiref/validate.py b/Lib/wsgiref/validate.py index 23ab9f83fa9..43784f9e660 100644 --- a/Lib/wsgiref/validate.py +++ b/Lib/wsgiref/validate.py @@ -345,7 +345,7 @@ def check_environ(environ): "Invalid CONTENT_LENGTH: %r" % environ['CONTENT_LENGTH']) if not environ.get('SCRIPT_NAME'): - assert_(environ.has_key('PATH_INFO'), + assert_('PATH_INFO' in environ, "One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO " "should at least be '/' if SCRIPT_NAME is empty)") assert_(environ.get('SCRIPT_NAME') != '/', diff --git a/Lib/xmlrpclib.py b/Lib/xmlrpclib.py index 2938f2991bb..d1bd2d81c68 100644 --- a/Lib/xmlrpclib.py +++ b/Lib/xmlrpclib.py @@ -686,7 +686,7 @@ class Marshaller: def dump_array(self, value, write): i = id(value) - if self.memo.has_key(i): + if i in self.memo: raise TypeError, "cannot marshal recursive sequences" self.memo[i] = None dump = self.__dump @@ -700,7 +700,7 @@ class Marshaller: def dump_struct(self, value, write, escape=escape): i = id(value) - if self.memo.has_key(i): + if i in self.memo: raise TypeError, "cannot marshal recursive dictionaries" self.memo[i] = None dump = self.__dump diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c index 61c656437e3..cccdf2a8290 100644 --- a/Modules/bsddbmodule.c +++ b/Modules/bsddbmodule.c @@ -461,7 +461,7 @@ bsddb_keys(bsddbobject *dp) } static PyObject * -bsddb_has_key(bsddbobject *dp, PyObject *args) +bsddb_contains(bsddbobject *dp, PyObject *args) { DBT krec, drec; int status; @@ -640,7 +640,7 @@ bsddb_sync(bsddbobject *dp) static PyMethodDef bsddb_methods[] = { {"close", (PyCFunction)bsddb_close, METH_NOARGS}, {"keys", (PyCFunction)bsddb_keys, METH_NOARGS}, - {"has_key", (PyCFunction)bsddb_has_key, METH_VARARGS}, + {"__contains__", (PyCFunction)bsddb_contains, METH_VARARGS}, {"set_location", (PyCFunction)bsddb_set_location, METH_VARARGS}, {"next", (PyCFunction)bsddb_next, METH_NOARGS}, {"previous", (PyCFunction)bsddb_previous, METH_NOARGS}, diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c index 9086c847702..a75a9bd0c90 100644 --- a/Modules/dbmmodule.c +++ b/Modules/dbmmodule.c @@ -206,12 +206,12 @@ dbm_keys(register dbmobject *dp, PyObject *unused) } static PyObject * -dbm_has_key(register dbmobject *dp, PyObject *args) +dbm_contains(register dbmobject *dp, PyObject *args) { datum key, val; int tmp_size; - if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &tmp_size)) + if (!PyArg_ParseTuple(args, "s#:__contains__", &key.dptr, &tmp_size)) return NULL; key.dsize = tmp_size; check_dbmobject_open(dp); @@ -277,8 +277,8 @@ static PyMethodDef dbm_methods[] = { "close()\nClose the database."}, {"keys", (PyCFunction)dbm_keys, METH_NOARGS, "keys() -> list\nReturn a list of all keys in the database."}, - {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, - "has_key(key} -> boolean\nReturn true iff key is in the database."}, + {"__contains__",(PyCFunction)dbm_contains, METH_VARARGS, + "__contains__(key} -> boolean\True iff key is in the database."}, {"get", (PyCFunction)dbm_get, METH_VARARGS, "get(key[, default]) -> value\n" "Return the value for key if present, otherwise default."}, diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c index cfc6abc37f5..0da93ec36d9 100644 --- a/Modules/gdbmmodule.c +++ b/Modules/gdbmmodule.c @@ -241,16 +241,16 @@ dbm_keys(register dbmobject *dp, PyObject *unused) return v; } -PyDoc_STRVAR(dbm_has_key__doc__, -"has_key(key) -> boolean\n\ +PyDoc_STRVAR(dbm_contains__doc__, +"__contains__(key) -> bool\n\ Find out whether or not the database contains a given key."); static PyObject * -dbm_has_key(register dbmobject *dp, PyObject *args) +dbm_contains(register dbmobject *dp, PyObject *args) { datum key; - if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize)) + if (!PyArg_ParseTuple(args, "s#:contains", &key.dptr, &key.dsize)) return NULL; check_dbmobject_open(dp); return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key)); @@ -355,7 +355,7 @@ dbm_sync(register dbmobject *dp, PyObject *unused) static PyMethodDef dbm_methods[] = { {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__}, {"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__}, - {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, dbm_has_key__doc__}, + {"__contains__",(PyCFunction)dbm_contains,METH_VARARGS, dbm_contains__doc__}, {"firstkey", (PyCFunction)dbm_firstkey,METH_NOARGS, dbm_firstkey__doc__}, {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__}, {"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__}, diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 914b6d35a12..d6b789da976 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -700,15 +700,6 @@ static PySequenceMethods proxy_as_sequence = { 0, /* sq_inplace_repeat */ }; -static PyObject * -proxy_has_key(proxyobject *pp, PyObject *key) -{ - int res = PyDict_Contains(pp->dict, key); - if (res < 0) - return NULL; - return PyBool_FromLong(res); -} - static PyObject * proxy_get(proxyobject *pp, PyObject *args) { @@ -761,10 +752,8 @@ proxy_copy(proxyobject *pp) } static PyMethodDef proxy_methods[] = { - {"has_key", (PyCFunction)proxy_has_key, METH_O, - PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")}, {"get", (PyCFunction)proxy_get, METH_VARARGS, - PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d." + PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d." " d defaults to None.")}, {"keys", (PyCFunction)proxy_keys, METH_NOARGS, PyDoc_STR("D.keys() -> list of D's keys")}, diff --git a/Objects/dictobject.c b/Objects/dictobject.c index f4c185dc63f..b3fdbf1a0b6 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1621,7 +1621,7 @@ dict_richcompare(PyObject *v, PyObject *w, int op) } static PyObject * -dict_has_key(register dictobject *mp, PyObject *key) +dict_contains(register dictobject *mp, PyObject *key) { long hash; dictentry *ep; @@ -1856,9 +1856,6 @@ dict_iteritems(dictobject *dict) } -PyDoc_STRVAR(has_key__doc__, -"D.has_key(k) -> True if D has a key k, else False"); - PyDoc_STRVAR(contains__doc__, "D.__contains__(k) -> True if D has a key k, else False"); @@ -1911,12 +1908,10 @@ PyDoc_STRVAR(iteritems__doc__, "D.iteritems() -> an iterator over the (key, value) items of D"); static PyMethodDef mapp_methods[] = { - {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST, + {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, contains__doc__}, {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, getitem__doc__}, - {"has_key", (PyCFunction)dict_has_key, METH_O, - has_key__doc__}, {"get", (PyCFunction)dict_get, METH_VARARGS, get__doc__}, {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 7b15925ebbe..02d45bd724a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4432,7 +4432,6 @@ slot_tp_hash(PyObject *self) } if (func == NULL) { - PyErr_Clear(); PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", self->ob_type->tp_name); return -1; diff --git a/setup.py b/setup.py index 5d711819e0f..1e58a2f81a8 100644 --- a/setup.py +++ b/setup.py @@ -674,7 +674,7 @@ class PyBuildExt(build_ext): db_minor = int(m.group(1)) db_ver = (db_major, db_minor) - if ( (not db_ver_inc_map.has_key(db_ver)) and + if ( (db_ver not in db_ver_inc_map) and (db_ver <= max_db_ver and db_ver >= min_db_ver) ): # save the include directory with the db.h version # (first occurrance only)