2626 ConfigurationError ,
2727 InvalidName ,
2828 OperationFailure )
29- from pymongo import read_preferences as rp
29+ from pymongo .read_preferences import (modes ,
30+ secondary_ok_commands ,
31+ ReadPreference )
3032
3133
3234def _check_name (name ):
@@ -282,7 +284,7 @@ def _command(self, command, value=1,
282284
283285 command_name = command .keys ()[0 ].lower ()
284286 must_use_master = kwargs .pop ('_use_master' , False )
285- if command_name not in rp . secondary_ok_commands :
287+ if command_name not in secondary_ok_commands :
286288 must_use_master = True
287289
288290 # Special-case: mapreduce can go to secondaries only if inline
@@ -323,13 +325,13 @@ def _command(self, command, value=1,
323325 command .update (kwargs )
324326
325327 # Warn if must_use_master will override read_preference.
326- if (extra_opts ['read_preference' ] != rp . ReadPreference .PRIMARY and
328+ if (extra_opts ['read_preference' ] != ReadPreference .PRIMARY and
327329 extra_opts ['_must_use_master' ]):
328330 warnings .warn ("%s does not support %s read preference "
329331 "and will be routed to the primary instead." %
330332 (command_name ,
331- rp . modes [extra_opts ['read_preference' ]]),
332- UserWarning )
333+ modes [extra_opts ['read_preference' ]]),
334+ UserWarning , stacklevel = 3 )
333335
334336 cursor = self ["$cmd" ].find (command , ** extra_opts ).limit (- 1 )
335337 for doc in cursor :
@@ -466,7 +468,8 @@ def drop_collection(self, name_or_collection):
466468
467469 self .__connection ._purge_index (self .__name , name )
468470
469- self .command ("drop" , unicode (name ), allowable_errors = ["ns not found" ])
471+ self .command ("drop" , unicode (name ), allowable_errors = ["ns not found" ],
472+ read_preference = ReadPreference .PRIMARY )
470473
471474 def validate_collection (self , name_or_collection ,
472475 scandata = False , full = False ):
@@ -504,7 +507,8 @@ def validate_collection(self, name_or_collection,
504507 "%s or Collection" % (basestring .__name__ ,))
505508
506509 result = self .command ("validate" , unicode (name ),
507- scandata = scandata , full = full )
510+ scandata = scandata , full = full ,
511+ read_preference = ReadPreference .PRIMARY )
508512
509513 valid = True
510514 # Pre 1.9 results
@@ -553,7 +557,8 @@ def profiling_level(self):
553557
554558 .. mongodoc:: profiling
555559 """
556- result = self .command ("profile" , - 1 )
560+ result = self .command ("profile" , - 1 ,
561+ read_preference = ReadPreference .PRIMARY )
557562
558563 assert result ["was" ] >= 0 and result ["was" ] <= 2
559564 return result ["was" ]
@@ -593,9 +598,11 @@ def set_profiling_level(self, level, slow_ms=None):
593598 raise TypeError ("slow_ms must be an integer" )
594599
595600 if slow_ms is not None :
596- self .command ("profile" , level , slowms = slow_ms )
601+ self .command ("profile" , level , slowms = slow_ms ,
602+ read_preference = ReadPreference .PRIMARY )
597603 else :
598- self .command ("profile" , level )
604+ self .command ("profile" , level ,
605+ read_preference = ReadPreference .PRIMARY )
599606
600607 def profiling_info (self ):
601608 """Returns a list containing current profiling information.
@@ -610,7 +617,8 @@ def error(self):
610617 Return None if the last operation was error-free. Otherwise return the
611618 error that occurred.
612619 """
613- error = self .command ("getlasterror" )
620+ error = self .command ("getlasterror" ,
621+ read_preference = ReadPreference .PRIMARY )
614622 error_msg = error .get ("err" , "" )
615623 if error_msg is None :
616624 return None
@@ -623,7 +631,8 @@ def last_status(self):
623631
624632 Returns a SON object with status information.
625633 """
626- return self .command ("getlasterror" )
634+ return self .command ("getlasterror" ,
635+ read_preference = ReadPreference .PRIMARY )
627636
628637 def previous_error (self ):
629638 """Get the most recent error to have occurred on this database.
@@ -632,7 +641,8 @@ def previous_error(self):
632641 `Database.reset_error_history`. Returns None if no such errors have
633642 occurred.
634643 """
635- error = self .command ("getpreverror" )
644+ error = self .command ("getpreverror" ,
645+ read_preference = ReadPreference .PRIMARY )
636646 if error .get ("err" , 0 ) is None :
637647 return None
638648 return error
@@ -643,7 +653,8 @@ def reset_error_history(self):
643653 Calls to `Database.previous_error` will only return errors that have
644654 occurred since the most recent call to this method.
645655 """
646- self .command ("reseterror" )
656+ self .command ("reseterror" ,
657+ read_preference = ReadPreference .PRIMARY )
647658
648659 def __iter__ (self ):
649660 return self
@@ -697,7 +708,8 @@ def _create_or_update_user(
697708 else :
698709 command_name = "updateUser"
699710
700- self .command (command_name , name , ** opts )
711+ self .command (command_name , name ,
712+ read_preference = ReadPreference .PRIMARY , ** opts )
701713
702714 def _legacy_add_user (self , name , password , read_only , ** kwargs ):
703715 """Uses v1 system to add users, i.e. saving to system.users.
@@ -763,7 +775,8 @@ def add_user(self, name, password=None, read_only=None, **kwargs):
763775 "read_only and roles together" )
764776
765777 try :
766- uinfo = self .command ("usersInfo" , name )
778+ uinfo = self .command ("usersInfo" , name ,
779+ read_preference = ReadPreference .PRIMARY )
767780 except OperationFailure , exc :
768781 # MongoDB >= 2.5.3 requires the use of commands to manage
769782 # users. "No such command" error didn't return an error
@@ -793,6 +806,7 @@ def remove_user(self, name):
793806
794807 try :
795808 self .command ("dropUser" , name ,
809+ read_preference = ReadPreference .PRIMARY ,
796810 writeConcern = self ._get_wc_override ())
797811 except OperationFailure , exc :
798812 # See comment in add_user try / except above.
@@ -930,7 +944,9 @@ def eval(self, code, *args):
930944 if not isinstance (code , Code ):
931945 code = Code (code )
932946
933- result = self .command ("$eval" , code , args = args )
947+ result = self .command ("$eval" , code ,
948+ read_preference = ReadPreference .PRIMARY ,
949+ args = args )
934950 return result .get ("retval" , None )
935951
936952 def __call__ (self , * args , ** kwargs ):
0 commit comments