diff --git a/pymongo/auth.py b/pymongo/auth.py index 452129e93..77dd14227 100644 --- a/pymongo/auth.py +++ b/pymongo/auth.py @@ -14,11 +14,6 @@ """Authentication helpers.""" -# TODO: We may not want to do this. It causes issues -# with passing the string 'sha1' to hashlib.pbkdf2_hmac -# in cpython 2.x >= 2.8 and pypy >= 2.4.x. -from __future__ import unicode_literals - import hmac HAVE_KERBEROS = True @@ -104,7 +99,7 @@ try: from backports.pbkdf2 import pbkdf2_hmac def _hi(data, salt, iterations): - return pbkdf2_hmac(str('sha1'), data, salt, iterations) + return pbkdf2_hmac('sha1', data, salt, iterations) except ImportError: try: @@ -112,7 +107,7 @@ except ImportError: from hashlib import pbkdf2_hmac def _hi(data, salt, iterations): - return pbkdf2_hmac(str('sha1'), data, salt, iterations) + return pbkdf2_hmac('sha1', data, salt, iterations) except ImportError: diff --git a/pymongo/bulk.py b/pymongo/bulk.py index aee600e75..f3a01484d 100644 --- a/pymongo/bulk.py +++ b/pymongo/bulk.py @@ -17,9 +17,8 @@ .. versionadded:: 2.7 """ -from __future__ import unicode_literals - from bson.objectid import ObjectId +from bson.py3compat import u from bson.son import SON from pymongo.common import (validate_is_mapping, validate_is_mutable_mapping, @@ -45,6 +44,16 @@ _WRITE_CONCERN_ERROR = 64 _COMMANDS = ('insert', 'update', 'delete') +# These string literals are used when we create fake server return +# documents client side. We use unicode literals in python 2.x to +# match the actual return values from the server. +_UID = u("_id") +_UCODE = u("code") +_UERRMSG = u("errmsg") +_UINDEX = u("index") +_UOP = u("op") + + class _Run(object): """Represents a batch of write operations. """ @@ -79,10 +88,10 @@ def _make_error(index, code, errmsg, operation): """Create and return an error document. """ return { - "index": index, - "code": code, - "errmsg": errmsg, - "op": operation + _UINDEX: index, + _UCODE: code, + _UERRMSG: errmsg, + _UOP: operation } @@ -109,7 +118,7 @@ def _merge_legacy(run, full_result, result, index): full_result['nInserted'] += 1 elif run.op_type == _UPDATE: if "upserted" in result: - doc = {"index": run.index(index), "_id": result["upserted"]} + doc = {_UINDEX: run.index(index), _UID: result["upserted"]} full_result["upserted"].append(doc) full_result['nUpserted'] += affected # Versions of MongoDB before 2.6 don't return the _id for an @@ -119,7 +128,7 @@ def _merge_legacy(run, full_result, result, index): # If _id is in both the update document *and* the query spec # the update document _id takes precedence. _id = op['u'].get('_id', op['q'].get('_id')) - doc = {"index": run.index(index), "_id": _id} + doc = {_UINDEX: run.index(index), _UID: _id} full_result["upserted"].append(doc) full_result['nUpserted'] += affected else: @@ -153,7 +162,7 @@ def _merge_command(run, full_result, results): else: n_upserted = 1 index = run.index(offset) - doc = {"index": index, "_id": upserted} + doc = {_UINDEX: index, _UID: upserted} full_result["upserted"].append(doc) full_result["nUpserted"] += n_upserted full_result["nMatched"] += (affected - n_upserted) @@ -175,7 +184,7 @@ def _merge_command(run, full_result, results): idx = doc["index"] + offset doc["index"] = run.index(idx) # Add the failed operation to the error document. - doc["op"] = run.ops[idx] + doc[_UOP] = run.ops[idx] full_result["writeErrors"].extend(write_errors) wc_error = result.get("writeConcernError") diff --git a/pymongo/collection.py b/pymongo/collection.py index d864679cd..dfb67eb75 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -14,8 +14,6 @@ """Collection level utilities for Mongo.""" -from __future__ import unicode_literals - import collections import warnings @@ -23,7 +21,8 @@ from bson.code import Code from bson.objectid import ObjectId from bson.py3compat import (_unicode, integer_types, - string_type) + string_type, + u) from bson.codec_options import CodecOptions from bson.son import SON from pymongo import (common, @@ -51,6 +50,7 @@ except ImportError: _ORDERED_TYPES = (SON,) _NO_OBJ_ERROR = "No matching object found" +_UJOIN = u("%s.%s") class ReturnDocument(object): @@ -149,7 +149,7 @@ class Collection(common.BaseObject): self.__database = database self.__name = _unicode(name) - self.__full_name = "%s.%s" % (self.__database.name, self.__name) + self.__full_name = _UJOIN % (self.__database.name, self.__name) if create or kwargs: self.__create(kwargs) @@ -211,7 +211,7 @@ class Collection(common.BaseObject): - `name`: the name of the collection to get """ if name.startswith('_'): - full_name = '%s.%s' % (self.__name, name) + full_name = _UJOIN % (self.__name, name) raise AttributeError( "Collection has no attribute %r. To access the %s" " collection, use database['%s']." % ( @@ -219,7 +219,7 @@ class Collection(common.BaseObject): return self.__getitem__(name) def __getitem__(self, name): - return Collection(self.__database, "%s.%s" % (self.__name, name)) + return Collection(self.__database, _UJOIN % (self.__name, name)) def __repr__(self): return "Collection(%r, %r)" % (self.__database, self.__name) @@ -1262,7 +1262,7 @@ class Collection(common.BaseObject): ReadPreference.PRIMARY, CodecOptions(SON))["cursor"] else: - namespace = "%s.%s" % (self.__database.name, "system.indexes") + namespace = _UJOIN % (self.__database.name, "system.indexes") res = helpers._first_batch( sock_info, namespace, {"ns": self.__full_name}, 0, slave_ok, CodecOptions(SON), ReadPreference.PRIMARY) diff --git a/pymongo/helpers.py b/pymongo/helpers.py index b2debc971..820ff1f79 100644 --- a/pymongo/helpers.py +++ b/pymongo/helpers.py @@ -21,7 +21,7 @@ from pymongo.message import _Query import bson import pymongo from bson.codec_options import CodecOptions -from bson.py3compat import itervalues, string_type, iteritems +from bson.py3compat import itervalues, string_type, iteritems, u from bson.son import SON from pymongo.errors import (CursorNotFound, DuplicateKeyError, @@ -33,9 +33,12 @@ from pymongo.errors import (CursorNotFound, WTimeoutError) +_UUNDER = u("_") + + def _gen_index_name(keys): """Generate an index name from the set of fields it is over.""" - return "_".join(["%s_%s" % item for item in keys]) + return _UUNDER.join(["%s_%s" % item for item in keys]) def _index_list(key_or_list, direction=None):