Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/redis/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/redis/__pycache__/connection.cpython-37.opt-1.pyc

B

�aG��	@s�ddlmZddlmZddlmZddlmZmZmZddl	m
Z
mZmZddl
Z
ddlZddlZddlZddlZddlZddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$m%Z%yddl&Z&d	Z'Wne(k
r�d
Z'YnXe)e
j*iZ+e'�r6e,e&d��r,de+e&j-<de+e&j.<n
de+e&j/<e0e+�1��Z2e$�r�ddl3Z3ee3j4�Z5e5ed
�kZ6e5ed�kZ7e5ed�kZ8e7�s�de3j4Z9e�:e9�d	Z;e7�s�d
Z;dZ<dZ=dZ>dZ?dZ@eA�ZBdZCdZDdZEdZFGdd�d�ZGGdd�d�ZHGdd�d�ZIGd d!�d!eH�ZJGd"d#�d#eH�ZKe$�r&eKZLneJZLGd$d%�d%�ZMGd&d'�d'eM�ZNGd(d)�d)eM�ZOd*ZPd+d,�ZQeReSeSeQeQeReReQd-�ZTd.d/�ZUGd0d1�d1�ZVGd2d3�d3eV�ZWdS)4�)�
StrictVersion)�chain)�time)�	LifoQueue�Empty�Full)�parse_qs�unquote�urlparseN)�AuthenticationError�$AuthenticationWrongNumberOfArgsError�BusyLoadingError�ChildDeadlockedError�ConnectionError�	DataError�ExecAbortError�InvalidResponse�NoPermissionError�
NoScriptError�
ReadOnlyError�
RedisError�
ResponseError�TimeoutError�ModuleError)�HIREDIS_AVAILABLE�str_if_bytesTF�SSLWantReadError�z0.1.3z0.1.4z1.0.0z`redis-py works best with hiredis >= 0.1.4. You're running hiredis %s. Please consider upgrading.�*�$s
�zConnection closed by server.z:Error loading the extension. Please check the server logs.z5Error unloading module: no such module with that namez/Error unloading module: operation not possible.z[Error unloading module: the module exports one or more module-side data types, can't unloadc@s*eZdZdZdd�Zdd�Zd
dd�Zd	S)�Encoderz=Encode strings to bytes-like and decode bytes-like to stringscCs||_||_||_dS)N)�encoding�encoding_errors�decode_responses)�selfr"r#r$�r&�A/opt/alt/python37/lib/python3.7/site-packages/redis/connection.py�__init__]szEncoder.__init__cCs�t|ttf�r|St|t�r&td��n<t|ttf�rBt|���}n t|t	�sbt
|�j}td|��t|t	�r||�|j|j
�}|S)z=Return a bytestring or bytes-like representation of the valuezNInvalid input of type: 'bool'. Convert to a bytes, string, int or float first.zLInvalid input of type: '%s'. Convert to a bytes, string, int or float first.)�
isinstance�bytes�
memoryview�boolr�int�float�repr�encode�str�type�__name__r"r#)r%�value�typenamer&r&r'r0bs




zEncoder.encodeFcCs:|js
|r6t|t�r|��}t|t�r6|�|j|j�}|S)z:Return a unicode string from the bytes-like representation)r$r)r+�tobytesr*�decoder"r#)r%r4�forcer&r&r'r7us


zEncoder.decodeN)F)r3�
__module__�__qualname__�__doc__r(r0r7r&r&r&r'r!Zsr!c@sLeZdZdededededeeeeee	ee
ei	eee
eeed�Zdd�Zd	S)
�
BaseParserzmax number of clients reachedz(Client sent AUTH, but no password is setzinvalid passwordz,wrong number of arguments for 'auth' commandz,wrong number of arguments for 'AUTH' command)ZERRZ	EXECABORTZLOADINGZNOSCRIPTZREADONLYZNOAUTHZNOPERMcCs\|�d�d}||jkrT|t|�dd�}|j|}t|t�rL|�|t�}||�St|�S)zParse an error response� r�N)�split�EXCEPTION_CLASSES�lenr)�dict�getr)r%�responseZ
error_codeZexception_classr&r&r'�parse_error�s


zBaseParser.parse_errorN)r3r9r:rrr�MODULE_LOAD_ERRORr�MODULE_EXPORTS_DATA_TYPES_ERROR�NO_SUCH_MODULE_ERROR� MODULE_UNLOAD_NOT_POSSIBLE_ERRORrr
rrrr@rEr&r&r&r'r<s$r<c@sXeZdZdd�Zedd��Zdedfdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�ZdS)�SocketBuffercCs,||_||_||_t��|_d|_d|_dS)Nr)�_sock�socket_read_size�socket_timeout�io�BytesIO�_buffer�
bytes_written�
bytes_read)r%�socketrLrMr&r&r'r(�s
zSocketBuffer.__init__cCs|j|jS)N)rQrR)r%r&r&r'�length�szSocketBuffer.lengthNTc

Cs@|j}|j}|j}|�|j�d}|tk	}�z�y||r>|�|�xh|j�|�}	t|	t	�rjt
|	�dkrjtt��|�
|	�t
|	�}
|j|
7_||
7}|dk	r�||kr�q@dSWnxtjk
r�|r�td��dStk
�r"}z8t�|jd�}|�s|j|k�rdStd|jf��Wdd}~XYnXWd|�r:|�|j�XdS)NrTzTimeout reading from socketF���z#Error while reading from socket: %s)rKrLrP�seekrQ�SENTINEL�
settimeout�recvr)r*rAr�SERVER_CLOSED_CONNECTION_ERROR�writerS�timeoutr�NONBLOCKING_EXCEPTIONS�#NONBLOCKING_EXCEPTION_ERROR_NUMBERSrC�	__class__�errno�argsrM)
r%rTr\�raise_on_timeout�sockrL�bufZmarker�custom_timeout�dataZdata_length�ex�allowedr&r&r'�_read_from_socket�s@

"zSocketBuffer._read_from_socketcCst|j�p|j|dd�S)NF)r\rb)r,rTri)r%r\r&r&r'�can_read�s
zSocketBuffer.can_readcCsn|d}||jkr"|�||j�|j�|j�|j�|�}|jt|�7_|j|jkrb|��|dd�S)Nr���)	rTrirPrVrR�readrArQ�purge)r%rTrfr&r&r'rl�s
zSocketBuffer.readcCsx|j}|�|j�|��}x*|�t�sD|��|�|j�|��}qW|jt|�7_|j|jkrl|�	�|dd�S)Nrk)
rPrVrR�readline�endswith�SYM_CRLFrirArQrm)r%rdrfr&r&r'rn�szSocketBuffer.readlinecCs&|j�d�|j��d|_d|_dS)Nr)rPrV�truncaterQrR)r%r&r&r'rm	s
zSocketBuffer.purgecCs<y|��|j��Wntk
r*YnXd|_d|_dS)N)rmrP�close�	ExceptionrK)r%r&r&r'rrszSocketBuffer.close)
r3r9r:r(�propertyrTrWrirjrlrnrmrrr&r&r&r'rJ�s
*rJc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�PythonParserzPlain Python parsing classcCs||_d|_d|_d|_dS)N)rL�encoderrKrP)r%rLr&r&r'r( szPythonParser.__init__cCs&y|��Wntk
r YnXdS)N)�
on_disconnectrs)r%r&r&r'�__del__&szPythonParser.__del__cCs(|j|_t|j|j|j�|_|j|_dS)zCalled when the socket connectsN)rKrJrLrMrPrv)r%�
connectionr&r&r'�
on_connect,s

zPythonParser.on_connectcCs*d|_|jdk	r |j��d|_d|_dS)z"Called when the socket disconnectsN)rKrPrrrv)r%r&r&r'rw4s


zPythonParser.on_disconnectcCs|jo|j�|�S)N)rPrj)r%r\r&r&r'rj<szPythonParser.can_readcs
�j��}|stt��|dd�|dd�}}|dkrDtd|��|dkrv|jddd�}��|�}t|t�rr|�|S|dkr�nn|d	kr�t|�}n\|d
kr�t|�}|dkr�dS�j�	|�}n2|dkr�t|�}|dkr�dS�fd
d�t
|�D�}t|t��r�j�|�}|S)Nr>)�-�+�:rrzProtocol Error: %rr{zutf-8�replace)�errorsr|r}rrUrcsg|]}����qSr&)�
read_response)�.0�i)r%r&r'�
<listcomp>gsz.PythonParser.read_response.<locals>.<listcomp>)
rPrnrrZrr7rEr)r-rl�ranger*rv)r%�rawZbyterD�errorrTr&)r%r'r�?s:



zPythonParser.read_responseN)
r3r9r:r;r(rxrzrwrjr�r&r&r&r'rusruc@sNeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Ze	dfd
d�Z
dd�ZdS)�
HiredisParserz*Parser class for connections using HirediscCs$tstd��||_tr t|�|_dS)NzHiredis is not installed)rrrL�HIREDIS_USE_BYTE_BUFFER�	bytearrayrP)r%rLr&r&r'r(os
zHiredisParser.__init__cCs&y|��Wntk
r YnXdS)N)rwrs)r%r&r&r'rxwszHiredisParser.__del__cCsd|j|_|j|_t|jd�}ts(t|d<|jjr<|jj	|d<t
rL|jj|d<tj
f|�|_d|_dS)N)Z
protocolError�
replyErrorr�r"rF)rKrM�_socket_timeoutrrE� HIREDIS_SUPPORTS_CALLABLE_ERRORSrrvr$r"� HIREDIS_SUPPORTS_ENCODING_ERRORSr#�hiredis�Reader�_reader�_next_response)r%ry�kwargsr&r&r'rz}s
zHiredisParser.on_connectcCsd|_d|_d|_dS)NF)rKr�r�)r%r&r&r'rw�szHiredisParser.on_disconnectcCs@|jstt��|jdkr<|j��|_|jdkr<|j|dd�SdS)NF)r\rbT)r�rrZr��gets�read_from_socket)r%r\r&r&r'rj�s

zHiredisParser.can_readTc	
Cs$|j}|tk	}�z�y�|r"|�|�trX|j�|j�}|dkrDtt��|j�	|jd|�n8|j�
|j�}t|t
�r|t|�dkr�tt��|j�	|�dStjk
r�|r�td��dStk
�r}z4t�|jd�}|s�|j|kr�dStd|jf��Wdd}~XYnXWd|�r|�|j�XdS)NrTzTimeout reading from socketFrUz#Error while reading from socket: %s)rKrWrXr�Z	recv_intorPrrZr�ZfeedrYrLr)r*rArSr\rr]r^rCr_r`rar�)	r%r\rbrcreZbufflen�bufferrgrhr&r&r'r��s6
"zHiredisParser.read_from_socketcCs�|jstt��|jdk	r(|j}d|_|S|j��}x|dkrP|��|j��}q4Wts�t|t�rr|�	|j
d�}n4t|t�r�|r�t|dt�r�|�	|dj
d�|d<t|t�r�|�n$t|t�r�|r�t|dt�r�|d�|S)NFr)r�rrZr�r�r�r�r)rrEra�list)r%rDr&r&r'r��s,




zHiredisParser.read_responseN)r3r9r:r;r(rxrzrwrjrWr�r�r&r&r&r'r�ms&r�c@s�eZdZdZddddddddddddded	dddfd
d�Zdd
�Zdd�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd d!�Zd/d#d$�Zd%d&�Zd0d'd(�Zd)d*�Zd+d,�Zd-d.�ZdS)1�
Connectionz4Manages TCP communication to and from a Redis serverZ	localhosti�rNFzutf-8�stricticCs�t��|_||_t|�|_||_||_||_||_	||_
|p>||_||_|pNi|_
|	|_|
|_||_d|_t|||
�|_d|_||d�|_g|_d|_dS)Nr)rLip)�os�getpid�pid�hostr-�port�db�username�client_name�passwordrM�socket_connect_timeout�socket_keepalive�socket_keepalive_options�socket_type�retry_on_timeout�health_check_interval�next_health_checkr!rvrK�_parser�_connect_callbacks�_buffer_cutoff)r%r�r�r�r�rMr�r�r�r�r�r"r#r$�parser_classrLr�r�r�r&r&r'r(�s(



zConnection.__init__cCs(d�dd�|��D��}d|jj|fS)N�,cSsg|]\}}d||f�qS)z%s=%sr&)r��k�vr&r&r'r�
sz'Connection.__repr__.<locals>.<listcomp>z%s<%s>)�join�repr_piecesr_r3)r%Z	repr_argsr&r&r'�__repr__szConnection.__repr__cCs6d|jfd|jfd|jfg}|jr2|�d|jf�|S)Nr�r�r�r�)r�r�r�r��append)r%�piecesr&r&r'r�szConnection.repr_piecescCs&y|��Wntk
r YnXdS)N)�
disconnectrs)r%r&r&r'rxszConnection.__del__cCs|j�|�dS)N)r�r�)r%�callbackr&r&r'�register_connect_callback sz$Connection.register_connect_callbackcCs
g|_dS)N)r�)r%r&r&r'�clear_connect_callbacks#sz"Connection.clear_connect_callbacksc
Cs�|jr
dSy|��}WnNtjk
r4td��Yn2tjk
rd}zt|�|���Wdd}~XYnX||_y|��Wnt	k
r�|�
��YnXx|jD]}||�q�WdS)z5Connects to the Redis server if not already connectedNzTimeout connecting to server)rK�_connectrSr\rr�r�_error_messagerzrr�r�)r%rc�er�r&r&r'�connect&s  zConnection.connectcCsd}x�t�|j|j|jtj�D]�}|\}}}}}d}y�t�|||�}|�tjtjd�|j	r�|�tj
tjd�x&|j�
�D]\}	}
|�tj|	|
�qzW|�|j�|�|�|�|j�|Stk
r�}z|}|dk	r�|��Wdd}~XYqXqW|dk	�r|�td��dS)zCreate a TCP socket connectionNr>z)socket.getaddrinfo returned an empty list)rSZgetaddrinfor�r�r��SOCK_STREAMZ
setsockoptZIPPROTO_TCPZTCP_NODELAYr�Z
SOL_SOCKETZSO_KEEPALIVEr��itemsrXr�r�rM�OSErrorrr)r%�err�resZfamilyZsocktype�protoZ	canonnameZsocket_addressrcr�r��_r&r&r'r�>s.

zConnection._connectcCsJt|j�dkr&d|j|j|jdfSd|jd|j|j|jdfSdS)Nr>zError connecting to %s:%s. %s.rz!Error %s connecting to %s:%s. %s.)rArar�r�)r%�	exceptionr&r&r'r�fs
zConnection._error_messagecCs�|j�|�|js|jr�|jr0|j|jp*df}n|jf}|jd|�ddi�y|��}Wn.tk
r�|jd|jdd�|��}YnXt|�dkr�td��|j	r�|�dd	|j	�t|���dkr�t
d
��|jr�|�d|j�t|���dkr�t
d��d
S)z=Initialize the connection, authenticate and select a database��AUTH�check_healthF)r�ZOKzInvalid Username or PasswordZCLIENTZSETNAMEzError setting client nameZSELECTzInvalid DatabaseN)r�)r�rzr�r��send_commandr�rrrr�rr�)r%Z	auth_argsZ
auth_responser&r&r'rzps*zConnection.on_connectcCsb|j��|jdkrdSy*t��|jkr6|j�tj�|j�	�Wnt
k
rVYnXd|_dS)z!Disconnects from the Redis serverN)r�rwrKr�r�r�ZshutdownrSZ	SHUT_RDWRrrr�)r%r&r&r'r��s

zConnection.disconnectc	Cs�|jr�t�|jkr�y*|jddd�t|���dkr:td��WnFttfk
r�|��|jddd�t|���dkr~td��YnXdS)z3Check the health of the connection with a PING/PONGZPINGF)r�ZPONGz#Bad response from PING health checkN)	r�rr�r�rr�rrr�)r%r&r&r'r��s
zConnection.check_healthTc
Cs�|js|��|r|��y.t|t�r,|g}x|D]}|j�|�q2WWn�tjk
rn|��t	d��Yn�tj
k
r�}zP|��t|j�dkr�d|jd}}n|jd}|jd}t
d||f��Wdd}~XYntk
r�|���YnXdS)z2Send an already packed command to the Redis serverzTimeout writing to socketr>ZUNKNOWNrz%Error %s while writing to socket. %s.N)rKr�r�r)r1ZsendallrSr\r�rr�rArar�
BaseException)r%Zcommandr��itemr�r`�errmsgr&r&r'�send_packed_command�s.



zConnection.send_packed_commandcOs |j|j|�|�dd�d�dS)z+Pack and send a command to the Redis serverr�T)r�N)r��pack_commandrC)r%rar�r&r&r'r��szConnection.send_commandcCs$|j}|s|��|j}|j�|�S)z8Poll the socket to see if there's data that can be read.)rKr�r�rj)r%r\rcr&r&r'rj�s
zConnection.can_readc
Cs�y|j��}Wn�tjk
r@|��td|j|jf��Yn`tjk
r�}z$|��t	d|j|j|j
f��Wdd}~XYntk
r�|���YnX|jr�t
�|j|_t|t�r�|�|S)z0Read the response from a previously sent commandzTimeout reading from %s:%sz#Error while reading from %s:%s : %sN)r�r�rSr\r�rr�r�r�rrar�r�rr�r)r)r%rDr�r&r&r'r��s$$
zConnection.read_responsec	Gsg}t|dt�r4t|d�����|dd�}n(d|dkr\t|d���|dd�}t�ttt|����t	f�}|j
}x�t|jj|�D]|}t|�}t|�|ks�||ks�t|t
�r�t�|tt|���t	f�}|�|�|�|�t	}q�t�|tt|���t	|t	f�}q�W|�|�|S)z2Pack a series of arguments into the Redis protocolrr>N� )r)r1�tupler0r?�	SYM_EMPTYr��SYM_STARrArpr��maprvr+�
SYM_DOLLARr�)r%ra�outputZbuff�
buffer_cutoff�argZ
arg_lengthr&r&r'r��s*"



zConnection.pack_commandc	Cs�g}g}d}|j}x�|D]�}x||j|�D]n}t|�}||ksN||ksNt|t�rf|�t�|��d}g}||ksxt|t�r�|�|�q(|�|�||7}q(WqW|r�|�t�|��|S)z.Pack multiple commands into the Redis protocolr)r�r�rAr)r+r�r�r�)	r%Zcommandsr�r�Z
buffer_lengthr��cmd�chunkZchunklenr&r&r'�
pack_commandss&


zConnection.pack_commands)T)r)r3r9r:r;�
DefaultParserr(r�r�rxr�r�r�r�r�rzr�r�r�r�rjr�r�r�r&r&r&r'r��s2
(
'


"r�cs*eZdZd�fdd�	Z�fdd�Z�ZS)	�
SSLConnectionN�requiredFcs�tstd��t�jf|�||_||_|dkr6tj}n8t|t	�rntjtj
tjd�}||krftd|��||}||_||_
||_dS)Nz$Python wasn't built with SSL support)ZnoneZoptionalr�z-Invalid SSL Certificate Requirements Flag: %s)�
ssl_availabler�superr(�keyfile�certfile�sslZ	CERT_NONEr)r1Z
CERT_OPTIONALZ
CERT_REQUIRED�	cert_reqs�ca_certs�check_hostname)r%Zssl_keyfileZssl_certfileZ
ssl_cert_reqsZssl_ca_certs�ssl_check_hostnamer�Z	CERT_REQS)r_r&r'r(2s&

zSSLConnection.__init__csbt���}t��}|j|_|j|_|jr@|jr@|j	|j|jd�|j
rR|�|j
�|j||j
d�S)z Wrap the socket with SSL support)r�r�)Zserver_hostname)r�r�r�Zcreate_default_contextr�r�Zverify_moder�r�Zload_cert_chainr�Zload_verify_locationsZwrap_socketr�)r%rc�context)r_r&r'r�Ms

zSSLConnection._connect)NNr�NF)r3r9r:r(r��
__classcell__r&r&)r_r'r�0sr�c
@sHeZdZdddddddddedddf
dd	�Zd
d�Zdd
�Zdd�ZdS)�UnixDomainSocketConnectionr�rNzutf-8r�FicCspt��|_||_||_||_|
|_||_||_|	|_	||_
d|_t|||�|_
d|_|
|d�|_g|_d|_dS)Nr)rLip)r�r�r��pathr�r�r�r�rMr�r�r�r!rvrKr�r�r�)r%r�r�r�r�rMr"r#r$r�r�rLr�r�r&r&r'r(]s
z#UnixDomainSocketConnection.__init__cCs.d|jfd|jfg}|jr*|�d|jf�|S)Nr�r�r�)r�r�r�r�)r%r�r&r&r'r�ss
z&UnixDomainSocketConnection.repr_piecescCs,t�tjtj�}|�|j�|�|j�|S)z&Create a Unix domain socket connection)rSZAF_UNIXr�rXrMr�r�)r%rcr&r&r'r�|sz#UnixDomainSocketConnection._connectcCsBt|j�dkr"d|j|jdfSd|jd|j|jdfSdS)Nr>z(Error connecting to unix socket: %s. %s.rz+Error %s connecting to unix socket: %s. %s.)rArar�)r%r�r&r&r'r��s
z)UnixDomainSocketConnection._error_message)r3r9r:r�r(r�r�r�r&r&r&r'r�[s	r�)�0�FZFALSE�NZNOcCs6|dks|dkrdSt|t�r.|��tkr.dSt|�S)Nr�F)r)r1�upper�
FALSE_STRINGSr,)r4r&r&r'�to_bool�s
r�)r�rMr�r�r��max_connectionsr�r�c
Cs�t|�}i}x�t|j���D]r\}}|rt|�dkrt|d�}t�|�}|r�y||�||<Wq�tt	fk
r�t	d|��Yq�Xq|||<qW|j
r�t|j
�|d<|jr�t|j�|d<|jdkr�|j
r�t|j
�|d<t|d<n�|jdk�r||j�rt|j�|d	<|j�rt|j�|d
<|j
�rfd|k�rfytt|j
��dd
��|d<Wntt	fk
�rdYnX|jdk�r�t|d<nd}t	d|��|S)Nrz)Invalid value for `%s` in connection URL.r�r�Zunixr��connection_class)Zredis�redissr�r�r��/r�r�zredis://, rediss://, unix://z8Redis URL must specify one of the following schemes (%s))r
rZqueryr�rAr	�URL_QUERY_ARGUMENT_PARSERSrC�	TypeError�
ValueErrorr�r�Zschemer�r�Zhostnamer�r-r~�AttributeErrorr�)�urlr��namer4�parserZ
valid_schemesr&r&r'�	parse_url�sJ



r�c@steZdZdZedd��Zedfdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zddd�ZdS)�ConnectionPoola�
    Create a connection pool. ``If max_connections`` is set, then this
    object raises :py:class:`~redis.ConnectionError` when the pool's
    limit is reached.

    By default, TCP connections are created unless ``connection_class``
    is specified. Use :py:class:`~redis.UnixDomainSocketConnection` for
    unix sockets.

    Any additional keyword arguments are passed to the constructor of
    ``connection_class``.
    cKst|�}|�|�|f|�S)a
        Return a connection pool configured from the given URL.

        For example::

            redis://[[username]:[password]]@localhost:6379/0
            rediss://[[username]:[password]]@localhost:6379/0
            unix://[[username]:[password]]@/path/to/socket.sock?db=0

        Three URL schemes are supported:

        - `redis://` creates a TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/redis>
        - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/rediss>
        - ``unix://``: creates a Unix Domain Socket connection.

        The username, password, hostname, path and all querystring values
        are passed through urllib.parse.unquote in order to replace any
        percent-encoded values with their corresponding characters.

        There are several ways to specify a database number. The first value
        found will be used:
            1. A ``db`` querystring option, e.g. redis://localhost?db=0
            2. If using the redis:// or rediss:// schemes, the path argument
               of the url, e.g. redis://localhost/0
            3. A ``db`` keyword argument to this function.

        If none of these options are specified, the default db=0 is used.

        All querystring options are cast to their appropriate Python types.
        Boolean arguments can be specified with string values "True"/"False"
        or "Yes"/"No". Values that cannot be properly cast cause a
        ``ValueError`` to be raised. Once parsed, the querystring arguments
        and keyword arguments are passed to the ``ConnectionPool``'s
        class initializer. In the case of conflicting arguments, querystring
        arguments always win.
        )r��update)�clsr�r�Zurl_optionsr&r&r'�from_url�s(
zConnectionPool.from_urlNcKsJ|pd}t|t�r|dkr"td��||_||_||_t��|_|�	�dS)Nlrz,"max_connections" must be a positive integer)
r)r-r�r��connection_kwargsr��	threading�Lock�
_fork_lock�reset)r%r�r�rr&r&r'r(s

zConnectionPool.__init__cCs dt|�jt|jf|j��fS)Nz%s<%s>)r2r3r/r�r)r%r&r&r'r�(szConnectionPool.__repr__cCs,t��|_d|_g|_t�|_t��|_	dS)Nr)
rr�_lock�_created_connections�_available_connections�set�_in_use_connectionsr�r�r�)r%r&r&r'r.s

zConnectionPool.resetcCsP|jt��krL|jjdd�}|s$t�z|jt��kr<|��Wd|j��XdS)N�)r\)r�r�r�r�acquirerr�release)r%Zacquiredr&r&r'�	_checkpid?s#zConnectionPool._checkpidc	Os�|��|j�>y|j��}Wntk
r:|��}YnX|j�|�WdQRXyV|��y|�	�rnt
d��Wn4t
k
r�|��|��|�	�r�t
d��YnXWn tk
r�|�
|��YnX|S)zGet a connection from the poolNzConnection has datazConnection not ready)r
rr�pop�
IndexError�make_connectionr	�addr�rjrr�r�r)r%�command_name�keys�optionsryr&r&r'�get_connectionns*
zConnectionPool.get_connectioncCs,|j}t|�dd�|�dd�|�dd�d�S)z,Return an encoder based on encoding settingsr"zutf-8r#r�r$F)r"r#r$)rr!rC)r%r�r&r&r'�get_encoder�s


zConnectionPool.get_encodercCs0|j|jkrtd��|jd7_|jf|j�S)zCreate a new connectionzToo many connectionsr>)rr�rr�r)r%r&r&r'r�szConnectionPool.make_connectionc	Csv|��|j�^y|j�|�Wntk
r4YnX|�|�rN|j�|�n|jd8_|�	�dSWdQRXdS)z(Releases the connection back to the poolr>N)
r
rr	�remove�KeyError�owns_connectionrr�rr�)r%ryr&r&r'r�s
zConnectionPool.releasecCs|j|jkS)N)r�)r%ryr&r&r'r�szConnectionPool.owns_connectionTc	CsN|��|j�6|r$t|j|j�}n|j}x|D]}|��q0WWdQRXdS)z�
        Disconnects connections in the pool

        If ``inuse_connections`` is True, disconnect connections that are
        current in use, potentially by other threads. Otherwise only disconnect
        connections that are idle in the pool.
        N)r
rrrr	r�)r%Zinuse_connectionsZconnectionsryr&r&r'r��s

zConnectionPool.disconnect)T)r3r9r:r;�classmethodr�r�r(r�rr
rrrrrr�r&r&r&r'r��s,/!	r�csReZdZdZddeef�fdd�	Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Z�ZS)�BlockingConnectionPoola
    Thread-safe blocking connection pool::

        >>> from redis.client import Redis
        >>> client = Redis(connection_pool=BlockingConnectionPool())

    It performs the same function as the default
    :py:class:`~redis.ConnectionPool` implementation, in that,
    it maintains a pool of reusable connections that can be shared by
    multiple redis clients (safely across threads if required).

    The difference is that, in the event that a client tries to get a
    connection from the pool when all of connections are in use, rather than
    raising a :py:class:`~redis.ConnectionError` (as the default
    :py:class:`~redis.ConnectionPool` implementation does), it
    makes the client wait ("blocks") for a specified number of seconds until
    a connection becomes available.

    Use ``max_connections`` to increase / decrease the pool size::

        >>> pool = BlockingConnectionPool(max_connections=10)

    Use ``timeout`` to tell it either how many seconds to wait for a connection
    to become available, or to block forever:

        >>> # Block forever.
        >>> pool = BlockingConnectionPool(timeout=None)

        >>> # Raise a ``ConnectionError`` after five seconds if a connection is
        >>> # not available.
        >>> pool = BlockingConnectionPool(timeout=5)
    �2�cs(||_||_t�jf||d�|��dS)N)r�r�)�queue_classr\r�r()r%r�r\r�rr)r_r&r'r(�szBlockingConnectionPool.__init__cCsP|�|j�|_x,y|j�d�Wqtk
r6PYqXqWg|_t��|_dS)N)	rr��pool�
put_nowaitr�_connectionsr�r�r�)r%r&r&r'r�szBlockingConnectionPool.resetcCs|jf|j�}|j�|�|S)zMake a fresh connection.)r�rr!r�)r%ryr&r&r'rsz&BlockingConnectionPool.make_connectioncOs�|��d}y|jjd|jd�}Wntk
r>td��YnX|dkrP|��}yV|��y|��rltd��Wn4tk
r�|�	�|��|��r�td��YnXWn t
k
r�|�|��YnX|S)a7
        Get a connection, blocking for ``self.timeout`` until a connection
        is available from the pool.

        If the connection returned is ``None`` then creates a new connection.
        Because we use a last-in first-out queue, the existing connections
        (having been returned to the pool after the initial ``None`` values
        were added) will be returned before ``None`` values. This means we only
        create new connections when we need to, i.e.: the actual number of
        connections will only increase in response to demand.
        NT)�blockr\zNo connection available.zConnection has datazConnection not ready)r
rrCr\rrrr�rjr�r�r)r%rrrryr&r&r'rs,

z%BlockingConnectionPool.get_connectioncCsT|��|�|�s*|��|j�d�dSy|j�|�Wntk
rNYnXdS)z)Releases the connection back to the pool.N)r
rr�rr r)r%ryr&r&r'rIs
zBlockingConnectionPool.releasecCs$|��x|jD]}|��qWdS)z(Disconnects all connections in the pool.N)r
r!r�)r%ryr&r&r'r�^sz!BlockingConnectionPool.disconnect)
r3r9r:r;r�rr(rrrrr�r�r&r&)r_r'r�s 
4r)XZdistutils.versionr�	itertoolsrrZqueuerrrZurllib.parserr	r
r`rNr�rSr�warningsZredis.exceptionsrrr
rrrrrrrrrrrrZredis.utilsrrr�r��ImportError�BlockingIOErrorZEWOULDBLOCKr^�hasattrrZSSLWantWriteErrorZSSLErrorr�rr]r��__version__Zhiredis_versionr�ZHIREDIS_SUPPORTS_BYTE_BUFFERr��msg�warnr�r�r�rpr�rZ�objectrWrFrHrIrGr!r<rJrur�r�r�r�r�r�r�r-r.r�r�r�rr&r&r&r'�<module>s�D





%'xOzE+3	5r