@@ -480,22 +480,21 @@ def _purge_index(self, database_name,
480480 if index_name in self .__index_cache [database_name ][collection_name ]:
481481 del self .__index_cache [database_name ][collection_name ][index_name ]
482482
483- def _server_property (self , attr_name , default = None ):
483+ def _server_property (self , attr_name ):
484484 """An attribute of the current server's description.
485485
486- Returns "default" while there is no current server, primary, or mongos.
486+ If the client is not connected, this will block until a connection is
487+ established or raise ServerSelectionTimeoutError if no server is
488+ available.
487489
488490 Not threadsafe if used multiple times in a single method, since
489491 the server may change. In such cases, store a local reference to a
490492 ServerDescription first, then use its properties.
491493 """
492- try :
493- server = self ._topology .select_server (
494- writable_server_selector , server_selection_timeout = 0 )
494+ server = self ._topology .select_server (
495+ writable_server_selector )
495496
496- return getattr (server .description , attr_name )
497- except ConnectionFailure :
498- return default
497+ return getattr (server .description , attr_name )
499498
500499 @property
501500 def event_listeners (self ):
@@ -513,15 +512,21 @@ def address(self):
513512 the client is load-balancing among mongoses, since there is no single
514513 address. Use :attr:`nodes` instead.
515514
515+ If the client is not connected, this will block until a connection is
516+ established or raise ServerSelectionTimeoutError if no server is
517+ available.
518+
516519 .. versionadded:: 3.0
517520 """
518- try :
519- return self ._topology .get_direct_or_primary ()
520- except InvalidOperation :
521- # Only one case where Topology throws InvalidOperation.
521+ topology_type = self ._topology ._description .topology_type
522+ if topology_type == TOPOLOGY_TYPE .Sharded :
522523 raise InvalidOperation (
523524 'Cannot use "address" property when load balancing among'
524525 ' mongoses, use "nodes" instead.' )
526+ if topology_type not in (TOPOLOGY_TYPE .ReplicaSetWithPrimary ,
527+ TOPOLOGY_TYPE .Single ):
528+ return None
529+ return self ._server_property ('address' )
525530
526531 @property
527532 def primary (self ):
@@ -563,16 +568,20 @@ def arbiters(self):
563568
564569 @property
565570 def is_primary (self ):
566- """If this client if connected to a server that can accept writes.
571+ """If this client is connected to a server that can accept writes.
567572
568573 True if the current server is a standalone, mongos, or the primary of
569- a replica set.
574+ a replica set. If the client is not connected, this will block until a
575+ connection is established or raise ServerSelectionTimeoutError if no
576+ server is available.
570577 """
571- return self ._server_property ('is_writable' , False )
578+ return self ._server_property ('is_writable' )
572579
573580 @property
574581 def is_mongos (self ):
575- """If this client is connected to mongos.
582+ """If this client is connected to mongos. If the client is not
583+ connected, this will block until a connection is established or raise
584+ ServerSelectionTimeoutError if no server is available..
576585 """
577586 return self ._server_property ('server_type' ) == SERVER_TYPE .Mongos
578587
@@ -605,28 +614,34 @@ def nodes(self):
605614 def max_bson_size (self ):
606615 """The largest BSON object the connected server accepts in bytes.
607616
608- Defaults to 16MB if not connected to a server.
617+ If the client is not connected, this will block until a connection is
618+ established or raise ServerSelectionTimeoutError if no server is
619+ available.
609620 """
610- return self ._server_property ('max_bson_size' , common . MAX_BSON_SIZE )
621+ return self ._server_property ('max_bson_size' )
611622
612623 @property
613624 def max_message_size (self ):
614625 """The largest message the connected server accepts in bytes.
615626
616- Defaults to 32MB if not connected to a server.
627+ If the client is not connected, this will block until a connection is
628+ established or raise ServerSelectionTimeoutError if no server is
629+ available.
617630 """
618- return self ._server_property (
619- 'max_message_size' , common .MAX_MESSAGE_SIZE )
631+ return self ._server_property ('max_message_size' )
620632
621633 @property
622634 def max_write_batch_size (self ):
623635 """The maxWriteBatchSize reported by the server.
624636
637+ If the client is not connected, this will block until a connection is
638+ established or raise ServerSelectionTimeoutError if no server is
639+ available.
640+
625641 Returns a default value when connected to server versions prior to
626642 MongoDB 2.6.
627643 """
628- return self ._server_property (
629- 'max_write_batch_size' , common .MAX_WRITE_BATCH_SIZE )
644+ return self ._server_property ('max_write_batch_size' )
630645
631646 @property
632647 def local_threshold_ms (self ):
0 commit comments