<s> __author__ = "<STR_LIT>" <EOL> import datetime <EOL> from datetime import timedelta <EOL> from pg8000 import ( <EOL> Interval , min_int2 , max_int2 , min_int4 , max_int4 , min_int8 , max_int8 , <EOL> Bytea , NotSupportedError , ProgrammingError , InternalError , IntegrityError , <EOL> OperationalError , DatabaseError , InterfaceError , Error , <EOL> ArrayContentNotHomogenousError , ArrayContentEmptyError , <EOL> ArrayDimensionsNotConsistentError , ArrayContentNotSupportedError , Warning , <EOL> i_unpack , ii_unpack , iii_unpack , h_pack , d_unpack , q_unpack , d_pack , <EOL> f_unpack , q_pack , i_pack , h_unpack , dii_unpack , qii_unpack , ci_unpack , <EOL> bh_unpack , ihihih_unpack , cccc_unpack , ii_pack , iii_pack , dii_pack , <EOL> qii_pack ) <EOL> from warnings import warn <EOL> import socket <EOL> import threading <EOL> from struct import pack <EOL> from hashlib import md5 <EOL> from decimal import Decimal <EOL> import pg8000 <EOL> from collections import deque , defaultdict <EOL> from itertools import count , islice <EOL> from pg8000 . six . moves import map <EOL> from pg8000 . six import b , PY2 , integer_types , next , PRE_26 , text_type , u <EOL> from sys import exc_info <EOL> from uuid import UUID <EOL> from copy import deepcopy <EOL> from calendar import timegm <EOL> import os <EOL> from distutils . version import LooseVersion <EOL> ZERO = timedelta ( <NUM_LIT:0> ) <EOL> class UTC ( datetime . tzinfo ) : <EOL> def utcoffset ( self , dt ) : <EOL> return ZERO <EOL> def tzname ( self , dt ) : <EOL> return "<STR_LIT>" <EOL> def dst ( self , dt ) : <EOL> return ZERO <EOL> utc = UTC ( ) <EOL> if PRE_26 : <EOL> bytearray = list <EOL> FC_TEXT = <NUM_LIT:0> <EOL> FC_BINARY = <NUM_LIT:1> <EOL> BINARY_SPACE = b ( "<STR_LIT:U+0020>" ) <EOL> DDL_COMMANDS = b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) <EOL> def convert_paramstyle ( style , query ) : <EOL> OUTSIDE = <NUM_LIT:0> <EOL> INSIDE_SQ = <NUM_LIT:1> <EOL> INSIDE_QI = <NUM_LIT:2> <EOL> INSIDE_ES = <NUM_LIT:3> <EOL> INSIDE_PN = <NUM_LIT:4> <EOL> in_quote_escape = False <EOL> in_param_escape = False <EOL> placeholders = [ ] <EOL> output_query = [ ] <EOL> param_idx = map ( lambda x : "<STR_LIT:$>" + str ( x ) , count ( <NUM_LIT:1> ) ) <EOL> state = OUTSIDE <EOL> prev_c = None <EOL> for i , c in enumerate ( query ) : <EOL> if i + <NUM_LIT:1> < len ( query ) : <EOL> next_c = query [ i + <NUM_LIT:1> ] <EOL> else : <EOL> next_c = None <EOL> if state == OUTSIDE : <EOL> if c == "<STR_LIT:'>" : <EOL> output_query . append ( c ) <EOL> if prev_c == '<STR_LIT:E>' : <EOL> state = INSIDE_ES <EOL> else : <EOL> state = INSIDE_SQ <EOL> elif c == '<STR_LIT:">' : <EOL> output_query . append ( c ) <EOL> state = INSIDE_QI <EOL> elif style == "<STR_LIT>" and c == "<STR_LIT:?>" : <EOL> output_query . append ( next ( param_idx ) ) <EOL> elif style == "<STR_LIT>" and c == "<STR_LIT::>" : <EOL> output_query . append ( "<STR_LIT:$>" ) <EOL> elif style == "<STR_LIT>" and c == "<STR_LIT::>" : <EOL> state = INSIDE_PN <EOL> placeholders . append ( '<STR_LIT>' ) <EOL> elif style == "<STR_LIT>" and c == '<STR_LIT:%>' and next_c == "<STR_LIT:(>" : <EOL> state = INSIDE_PN <EOL> placeholders . append ( '<STR_LIT>' ) <EOL> elif style in ( "<STR_LIT>" , "<STR_LIT>" ) and c == "<STR_LIT:%>" : <EOL> style = "<STR_LIT>" <EOL> if in_param_escape : <EOL> in_param_escape = False <EOL> output_query . append ( c ) <EOL> else : <EOL> if next_c == "<STR_LIT:%>" : <EOL> in_param_escape = True <EOL> elif next_c == "<STR_LIT:s>" : <EOL> state = INSIDE_PN <EOL> output_query . append ( next ( param_idx ) ) <EOL> else : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> output_query . append ( c ) <EOL> elif state == INSIDE_SQ : <EOL> if c == "<STR_LIT:'>" : <EOL> output_query . append ( c ) <EOL> if in_quote_escape : <EOL> in_quote_escape = False <EOL> else : <EOL> if next_c == "<STR_LIT:'>" : <EOL> in_quote_escape = True <EOL> else : <EOL> state = OUTSIDE <EOL> elif style in ( "<STR_LIT>" , "<STR_LIT>" ) and c == "<STR_LIT:%>" : <EOL> if in_param_escape : <EOL> in_param_escape = False <EOL> output_query . append ( c ) <EOL> else : <EOL> if next_c == "<STR_LIT:%>" : <EOL> in_param_escape = True <EOL> else : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" + next_c + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> output_query . append ( c ) <EOL> elif state == INSIDE_QI : <EOL> if c == '<STR_LIT:">' : <EOL> state = OUTSIDE <EOL> output_query . append ( c ) <EOL> elif style in ( "<STR_LIT>" , "<STR_LIT>" ) and c == "<STR_LIT:%>" : <EOL> if in_param_escape : <EOL> in_param_escape = False <EOL> output_query . append ( c ) <EOL> else : <EOL> if next_c == "<STR_LIT:%>" : <EOL> in_param_escape = True <EOL> else : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" + next_c + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> output_query . append ( c ) <EOL> elif state == INSIDE_ES : <EOL> if c == "<STR_LIT:'>" and prev_c != "<STR_LIT:\\>" : <EOL> output_query . append ( c ) <EOL> state = OUTSIDE <EOL> elif style in ( "<STR_LIT>" , "<STR_LIT>" ) and c == "<STR_LIT:%>" : <EOL> if in_param_escape : <EOL> in_param_escape = False <EOL> output_query . append ( c ) <EOL> else : <EOL> if next_c == "<STR_LIT:%>" : <EOL> in_param_escape = True <EOL> else : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" + next_c + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> output_query . append ( c ) <EOL> elif state == INSIDE_PN : <EOL> if style == '<STR_LIT>' : <EOL> placeholders [ - <NUM_LIT:1> ] += c <EOL> if next_c is None or ( not next_c . isalnum ( ) and next_c != '<STR_LIT:_>' ) : <EOL> state = OUTSIDE <EOL> try : <EOL> pidx = placeholders . index ( placeholders [ - <NUM_LIT:1> ] , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> output_query . append ( "<STR_LIT:$>" + str ( pidx + <NUM_LIT:1> ) ) <EOL> del placeholders [ - <NUM_LIT:1> ] <EOL> except ValueError : <EOL> output_query . append ( "<STR_LIT:$>" + str ( len ( placeholders ) ) ) <EOL> elif style == '<STR_LIT>' : <EOL> if prev_c == '<STR_LIT:)>' and c == "<STR_LIT:s>" : <EOL> state = OUTSIDE <EOL> try : <EOL> pidx = placeholders . index ( placeholders [ - <NUM_LIT:1> ] , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> output_query . append ( "<STR_LIT:$>" + str ( pidx + <NUM_LIT:1> ) ) <EOL> del placeholders [ - <NUM_LIT:1> ] <EOL> except ValueError : <EOL> output_query . append ( "<STR_LIT:$>" + str ( len ( placeholders ) ) ) <EOL> elif c in "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> placeholders [ - <NUM_LIT:1> ] += c <EOL> elif style == '<STR_LIT>' : <EOL> state = OUTSIDE <EOL> prev_c = c <EOL> if style in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> def make_args ( vals ) : <EOL> return vals <EOL> else : <EOL> def make_args ( vals ) : <EOL> return tuple ( vals [ p ] for p in placeholders ) <EOL> return '<STR_LIT>' . join ( output_query ) , make_args <EOL> EPOCH = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> EPOCH_TZ = EPOCH . replace ( tzinfo = utc ) <EOL> EPOCH_SECONDS = timegm ( EPOCH . timetuple ( ) ) <EOL> utcfromtimestamp = datetime . datetime . utcfromtimestamp <EOL> INFINITY_MICROSECONDS = <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> <EOL> MINUS_INFINITY_MICROSECONDS = - <NUM_LIT:1> * INFINITY_MICROSECONDS - <NUM_LIT:1> <EOL> def timestamp_recv_integer ( data , offset , length ) : <EOL> micros = q_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> try : <EOL> return EPOCH + timedelta ( microseconds = micros ) <EOL> except OverflowError : <EOL> if micros == INFINITY_MICROSECONDS : <EOL> return datetime . datetime . max <EOL> elif micros == MINUS_INFINITY_MICROSECONDS : <EOL> return datetime . datetime . min <EOL> else : <EOL> raise exc_info ( ) [ <NUM_LIT:1> ] <EOL> def timestamp_recv_float ( data , offset , length ) : <EOL> return utcfromtimestamp ( EPOCH_SECONDS + d_unpack ( data , offset ) [ <NUM_LIT:0> ] ) <EOL> def timestamp_send_integer ( v ) : <EOL> if v == datetime . datetime . max : <EOL> micros = INFINITY_MICROSECONDS <EOL> elif v == datetime . datetime . min : <EOL> micros = MINUS_INFINITY_MICROSECONDS <EOL> else : <EOL> micros = int ( <EOL> ( timegm ( v . timetuple ( ) ) - EPOCH_SECONDS ) * <NUM_LIT> ) + v . microsecond <EOL> return q_pack ( micros ) <EOL> def timestamp_send_float ( v ) : <EOL> return d_pack ( timegm ( v . timetuple ) + v . microsecond / <NUM_LIT> - EPOCH_SECONDS ) <EOL> def timestamptz_send_integer ( v ) : <EOL> return timestamp_send_integer ( v . astimezone ( utc ) . replace ( tzinfo = None ) ) <EOL> def timestamptz_send_float ( v ) : <EOL> return timestamp_send_float ( v . astimezone ( utc ) . replace ( tzinfo = None ) ) <EOL> DATETIME_MAX_TZ = datetime . datetime . max . replace ( tzinfo = utc ) <EOL> DATETIME_MIN_TZ = datetime . datetime . min . replace ( tzinfo = utc ) <EOL> def timestamptz_recv_integer ( data , offset , length ) : <EOL> micros = q_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> try : <EOL> return EPOCH_TZ + timedelta ( microseconds = micros ) <EOL> except OverflowError : <EOL> if micros == INFINITY_MICROSECONDS : <EOL> return DATETIME_MAX_TZ <EOL> elif micros == MINUS_INFINITY_MICROSECONDS : <EOL> return DATETIME_MIN_TZ <EOL> else : <EOL> raise exc_info ( ) [ <NUM_LIT:1> ] <EOL> def timestamptz_recv_float ( data , offset , length ) : <EOL> return timestamp_recv_float ( data , offset , length ) . replace ( tzinfo = utc ) <EOL> def interval_send_integer ( v ) : <EOL> microseconds = v . microseconds <EOL> try : <EOL> microseconds += int ( v . seconds * <NUM_LIT> ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> months = v . months <EOL> except AttributeError : <EOL> months = <NUM_LIT:0> <EOL> return qii_pack ( microseconds , v . days , months ) <EOL> def interval_send_float ( v ) : <EOL> seconds = v . microseconds / <NUM_LIT> / <NUM_LIT> <EOL> try : <EOL> seconds += v . seconds <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> months = v . months <EOL> except AttributeError : <EOL> months = <NUM_LIT:0> <EOL> return dii_pack ( seconds , v . days , months ) <EOL> def interval_recv_integer ( data , offset , length ) : <EOL> microseconds , days , months = qii_unpack ( data , offset ) <EOL> if months == <NUM_LIT:0> : <EOL> seconds , micros = divmod ( microseconds , <NUM_LIT> ) <EOL> return datetime . timedelta ( days , seconds , micros ) <EOL> else : <EOL> return Interval ( microseconds , days , months ) <EOL> def interval_recv_float ( data , offset , length ) : <EOL> seconds , days , months = dii_unpack ( data , offset ) <EOL> if months == <NUM_LIT:0> : <EOL> secs , microseconds = divmod ( seconds , <NUM_LIT> ) <EOL> return datetime . timedelta ( days , secs , microseconds ) <EOL> else : <EOL> return Interval ( int ( seconds * <NUM_LIT:1000> * <NUM_LIT:1000> ) , days , months ) <EOL> def int8_recv ( data , offset , length ) : <EOL> return q_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> def int2_recv ( data , offset , length ) : <EOL> return h_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> def int4_recv ( data , offset , length ) : <EOL> return i_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> def float4_recv ( data , offset , length ) : <EOL> return f_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> def float8_recv ( data , offset , length ) : <EOL> return d_unpack ( data , offset ) [ <NUM_LIT:0> ] <EOL> def bytea_send ( v ) : <EOL> return v <EOL> if PY2 : <EOL> def bytea_recv ( data , offset , length ) : <EOL> return Bytea ( data [ offset : offset + length ] ) <EOL> else : <EOL> def bytea_recv ( data , offset , length ) : <EOL> return data [ offset : offset + length ] <EOL> def uuid_send ( v ) : <EOL> return v . bytes <EOL> def uuid_recv ( data , offset , length ) : <EOL> return UUID ( bytes = data [ offset : offset + length ] ) <EOL> TRUE = b ( "<STR_LIT>" ) <EOL> FALSE = b ( "<STR_LIT:\x00>" ) <EOL> def bool_send ( v ) : <EOL> return TRUE if v else FALSE <EOL> NULL = i_pack ( - <NUM_LIT:1> ) <EOL> NULL_BYTE = b ( '<STR_LIT:\x00>' ) <EOL> def null_send ( v ) : <EOL> return NULL <EOL> def int_in ( data , offset , length ) : <EOL> return int ( data [ offset : offset + length ] ) <EOL> class Cursor ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection ) : <EOL> self . _c = connection <EOL> self . arraysize = <NUM_LIT:1> <EOL> self . ps = None <EOL> self . _row_count = - <NUM_LIT:1> <EOL> self . _cached_rows = deque ( ) <EOL> self . portal_name = None <EOL> self . portal_suspended = False <EOL> @ property <EOL> def connection ( self ) : <EOL> warn ( "<STR_LIT>" , stacklevel = <NUM_LIT:3> ) <EOL> return self . _c <EOL> @ property <EOL> def rowcount ( self ) : <EOL> return self . _row_count <EOL> description = property ( lambda self : self . _getDescription ( ) ) <EOL> def _getDescription ( self ) : <EOL> if self . ps is None : <EOL> return None <EOL> row_desc = self . ps [ '<STR_LIT>' ] <EOL> if len ( row_desc ) == <NUM_LIT:0> : <EOL> return None <EOL> columns = [ ] <EOL> for col in row_desc : <EOL> columns . append ( <EOL> ( col [ "<STR_LIT:name>" ] , col [ "<STR_LIT>" ] , None , None , None , None , None ) ) <EOL> return columns <EOL> def execute ( self , operation , args = None , stream = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _c . _lock . acquire ( ) <EOL> self . stream = stream <EOL> if not self . _c . in_transaction and not self . _c . autocommit : <EOL> self . _c . execute ( self , "<STR_LIT>" , None ) <EOL> self . _c . execute ( self , operation , args ) <EOL> except AttributeError : <EOL> if self . _c is None : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> elif self . _c . _sock is None : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> else : <EOL> raise exc_info ( ) [ <NUM_LIT:1> ] <EOL> finally : <EOL> self . _c . _lock . release ( ) <EOL> def executemany ( self , operation , param_sets ) : <EOL> """<STR_LIT>""" <EOL> rowcounts = [ ] <EOL> for parameters in param_sets : <EOL> self . execute ( operation , parameters ) <EOL> rowcounts . append ( self . _row_count ) <EOL> self . _row_count = - <NUM_LIT:1> if - <NUM_LIT:1> in rowcounts else sum ( rowcounts ) <EOL> def fetchone ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return next ( self ) <EOL> except StopIteration : <EOL> return None <EOL> except TypeError : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> except AttributeError : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> def fetchmany ( self , num = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return tuple ( <EOL> islice ( self , self . arraysize if num is None else num ) ) <EOL> except TypeError : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> def fetchall ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return tuple ( self ) <EOL> except TypeError : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _c = None <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def setinputsizes ( self , sizes ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def setoutputsize ( self , size , column = None ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def __next__ ( self ) : <EOL> try : <EOL> self . _c . _lock . acquire ( ) <EOL> return self . _cached_rows . popleft ( ) <EOL> except IndexError : <EOL> if self . portal_suspended : <EOL> self . _c . send_EXECUTE ( self ) <EOL> self . _c . _write ( SYNC_MSG ) <EOL> self . _c . _flush ( ) <EOL> self . _c . handle_messages ( self ) <EOL> if not self . portal_suspended : <EOL> self . _c . close_portal ( self ) <EOL> try : <EOL> return self . _cached_rows . popleft ( ) <EOL> except IndexError : <EOL> if self . ps is None : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> elif len ( self . ps [ '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> else : <EOL> raise StopIteration ( ) <EOL> finally : <EOL> self . _c . _lock . release ( ) <EOL> if PY2 : <EOL> Cursor . next = Cursor . __next__ <EOL> NOTICE_RESPONSE = b ( "<STR_LIT:N>" ) <EOL> AUTHENTICATION_REQUEST = b ( "<STR_LIT:R>" ) <EOL> PARAMETER_STATUS = b ( "<STR_LIT:S>" ) <EOL> BACKEND_KEY_DATA = b ( "<STR_LIT>" ) <EOL> READY_FOR_QUERY = b ( "<STR_LIT>" ) <EOL> ROW_DESCRIPTION = b ( "<STR_LIT:T>" ) <EOL> ERROR_RESPONSE = b ( "<STR_LIT:E>" ) <EOL> DATA_ROW = b ( "<STR_LIT:D>" ) <EOL> COMMAND_COMPLETE = b ( "<STR_LIT:C>" ) <EOL> PARSE_COMPLETE = b ( "<STR_LIT:1>" ) <EOL> BIND_COMPLETE = b ( "<STR_LIT:2>" ) <EOL> CLOSE_COMPLETE = b ( "<STR_LIT:3>" ) <EOL> PORTAL_SUSPENDED = b ( "<STR_LIT:s>" ) <EOL> NO_DATA = b ( "<STR_LIT:n>" ) <EOL> PARAMETER_DESCRIPTION = b ( "<STR_LIT:t>" ) <EOL> NOTIFICATION_RESPONSE = b ( "<STR_LIT:A>" ) <EOL> COPY_DONE = b ( "<STR_LIT:c>" ) <EOL> COPY_DATA = b ( "<STR_LIT:d>" ) <EOL> COPY_IN_RESPONSE = b ( "<STR_LIT>" ) <EOL> COPY_OUT_RESPONSE = b ( "<STR_LIT:H>" ) <EOL> BIND = b ( "<STR_LIT:B>" ) <EOL> PARSE = b ( "<STR_LIT:P>" ) <EOL> EXECUTE = b ( "<STR_LIT:E>" ) <EOL> FLUSH = b ( '<STR_LIT:H>' ) <EOL> SYNC = b ( '<STR_LIT:S>' ) <EOL> PASSWORD = b ( '<STR_LIT:p>' ) <EOL> DESCRIBE = b ( '<STR_LIT:D>' ) <EOL> TERMINATE = b ( '<STR_LIT:X>' ) <EOL> CLOSE = b ( '<STR_LIT:C>' ) <EOL> FLUSH_MSG = FLUSH + i_pack ( <NUM_LIT:4> ) <EOL> SYNC_MSG = SYNC + i_pack ( <NUM_LIT:4> ) <EOL> TERMINATE_MSG = TERMINATE + i_pack ( <NUM_LIT:4> ) <EOL> COPY_DONE_MSG = COPY_DONE + i_pack ( <NUM_LIT:4> ) <EOL> STATEMENT = b ( '<STR_LIT:S>' ) <EOL> PORTAL = b ( '<STR_LIT:P>' ) <EOL> RESPONSE_SEVERITY = b ( "<STR_LIT:S>" ) <EOL> RESPONSE_CODE = b ( "<STR_LIT:C>" ) <EOL> RESPONSE_MSG = b ( "<STR_LIT:M>" ) <EOL> RESPONSE_DETAIL = b ( "<STR_LIT:D>" ) <EOL> RESPONSE_HINT = b ( "<STR_LIT:H>" ) <EOL> RESPONSE_POSITION = b ( "<STR_LIT:P>" ) <EOL> RESPONSE__POSITION = b ( "<STR_LIT:p>" ) <EOL> RESPONSE__QUERY = b ( "<STR_LIT:q>" ) <EOL> RESPONSE_WHERE = b ( "<STR_LIT>" ) <EOL> RESPONSE_FILE = b ( "<STR_LIT:F>" ) <EOL> RESPONSE_LINE = b ( "<STR_LIT:L>" ) <EOL> RESPONSE_ROUTINE = b ( "<STR_LIT:R>" ) <EOL> IDLE = b ( "<STR_LIT:I>" ) <EOL> IDLE_IN_TRANSACTION = b ( "<STR_LIT:T>" ) <EOL> IDLE_IN_FAILED_TRANSACTION = b ( "<STR_LIT:E>" ) <EOL> def data_into_dict ( data ) : <EOL> return dict ( ( s [ <NUM_LIT:0> : <NUM_LIT:1> ] , s [ <NUM_LIT:1> : ] ) for s in data . split ( NULL_BYTE ) ) <EOL> arr_trans = dict ( zip ( map ( ord , u ( "<STR_LIT>" ) ) , list ( u ( '<STR_LIT:{}>' ) ) + [ None ] * <NUM_LIT:3> ) ) <EOL> class MulticastDelegate ( object ) : <EOL> def __init__ ( self ) : <EOL> self . delegates = [ ] <EOL> def __iadd__ ( self , delegate ) : <EOL> self . add ( delegate ) <EOL> return self <EOL> def add ( self , delegate ) : <EOL> self . delegates . append ( delegate ) <EOL> def __isub__ ( self , delegate ) : <EOL> self . delegates . remove ( delegate ) <EOL> return self <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> for d in self . delegates : <EOL> d ( * args , ** kwargs ) <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> Warning = property ( lambda self : self . _getError ( Warning ) ) <EOL> Error = property ( lambda self : self . _getError ( Error ) ) <EOL> InterfaceError = property ( lambda self : self . _getError ( InterfaceError ) ) <EOL> DatabaseError = property ( lambda self : self . _getError ( DatabaseError ) ) <EOL> OperationalError = property ( lambda self : self . _getError ( OperationalError ) ) <EOL> IntegrityError = property ( lambda self : self . _getError ( IntegrityError ) ) <EOL> InternalError = property ( lambda self : self . _getError ( InternalError ) ) <EOL> ProgrammingError = property ( lambda self : self . _getError ( ProgrammingError ) ) <EOL> NotSupportedError = property ( <EOL> lambda self : self . _getError ( NotSupportedError ) ) <EOL> _row_cache_size = <NUM_LIT:100> <EOL> _row_cache_size_bin = i_pack ( _row_cache_size ) <EOL> def _getError ( self , error ) : <EOL> warn ( <EOL> "<STR_LIT>" % <EOL> error . __name__ , stacklevel = <NUM_LIT:3> ) <EOL> return error <EOL> def __init__ ( self , user , host , unix_sock , port , database , password , ssl ) : <EOL> self . _client_encoding = "<STR_LIT:ascii>" <EOL> self . _commands_with_count = ( <EOL> b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , <EOL> b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) ) <EOL> self . _lock = threading . Lock ( ) <EOL> if user is None : <EOL> try : <EOL> self . user = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> try : <EOL> self . user = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . user = user <EOL> self . password = password <EOL> self . autocommit = False <EOL> self . _xid = None <EOL> self . _caches = defaultdict ( lambda : defaultdict ( dict ) ) <EOL> self . statement_number = <NUM_LIT:0> <EOL> self . portal_number = <NUM_LIT:0> <EOL> try : <EOL> if unix_sock is None and host is not None : <EOL> self . _usock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> elif unix_sock is not None : <EOL> if not hasattr ( socket , "<STR_LIT>" ) : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _usock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> else : <EOL> raise ProgrammingError ( <EOL> "<STR_LIT>" ) <EOL> if unix_sock is None and host is not None : <EOL> self . _usock . connect ( ( host , port ) ) <EOL> elif unix_sock is not None : <EOL> self . _usock . connect ( unix_sock ) <EOL> if ssl : <EOL> try : <EOL> self . _lock . acquire ( ) <EOL> import ssl as sslmodule <EOL> self . _usock . sendall ( ii_pack ( <NUM_LIT:8> , <NUM_LIT> ) ) <EOL> resp = self . _usock . recv ( <NUM_LIT:1> ) <EOL> if resp == b ( '<STR_LIT:S>' ) : <EOL> self . _usock = sslmodule . wrap_socket ( self . _usock ) <EOL> else : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> except ImportError : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> self . _sock = self . _usock . makefile ( mode = "<STR_LIT>" ) <EOL> except socket . error : <EOL> self . _usock . close ( ) <EOL> raise InterfaceError ( "<STR_LIT>" , exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> self . _flush = self . _sock . flush <EOL> self . _read = self . _sock . read <EOL> if PRE_26 : <EOL> self . _write = self . _sock . writelines <EOL> else : <EOL> self . _write = self . _sock . write <EOL> self . _backend_key_data = None <EOL> self . NoticeReceived = MulticastDelegate ( ) <EOL> self . ParameterStatusReceived = MulticastDelegate ( ) <EOL> self . NotificationReceived = MulticastDelegate ( ) <EOL> self . ParameterStatusReceived += self . handle_PARAMETER_STATUS <EOL> def text_out ( v ) : <EOL> return v . encode ( self . _client_encoding ) <EOL> def time_out ( v ) : <EOL> return v . isoformat ( ) . encode ( self . _client_encoding ) <EOL> def date_out ( v ) : <EOL> if v == datetime . date . max : <EOL> return '<STR_LIT>' . encode ( self . _client_encoding ) <EOL> elif v == datetime . date . min : <EOL> return '<STR_LIT>' . encode ( self . _client_encoding ) <EOL> else : <EOL> return v . isoformat ( ) . encode ( self . _client_encoding ) <EOL> def unknown_out ( v ) : <EOL> return str ( v ) . encode ( self . _client_encoding ) <EOL> trans_tab = dict ( zip ( map ( ord , u ( '<STR_LIT:{}>' ) ) , u ( '<STR_LIT>' ) ) ) <EOL> glbls = { '<STR_LIT>' : Decimal } <EOL> def array_in ( data , idx , length ) : <EOL> arr = [ ] <EOL> prev_c = None <EOL> for c in data [ idx : idx + length ] . decode ( <EOL> self . _client_encoding ) . translate ( <EOL> trans_tab ) . replace ( u ( '<STR_LIT>' ) , u ( '<STR_LIT:None>' ) ) : <EOL> if c not in ( '<STR_LIT:[>' , '<STR_LIT:]>' , '<STR_LIT:U+002C>' , '<STR_LIT:N>' ) and prev_c in ( '<STR_LIT:[>' , '<STR_LIT:U+002C>' ) : <EOL> arr . extend ( "<STR_LIT>" ) <EOL> elif c in ( '<STR_LIT:]>' , '<STR_LIT:U+002C>' ) and prev_c not in ( '<STR_LIT:[>' , '<STR_LIT:]>' , '<STR_LIT:U+002C>' , '<STR_LIT:e>' ) : <EOL> arr . extend ( "<STR_LIT>" ) <EOL> arr . append ( c ) <EOL> prev_c = c <EOL> return eval ( '<STR_LIT>' . join ( arr ) , glbls ) <EOL> def array_recv ( data , idx , length ) : <EOL> final_idx = idx + length <EOL> dim , hasnull , typeoid = iii_unpack ( data , idx ) <EOL> idx += <NUM_LIT:12> <EOL> conversion = self . pg_types [ typeoid ] [ <NUM_LIT:1> ] <EOL> dim_lengths = [ ] <EOL> for i in range ( dim ) : <EOL> dim_lengths . append ( ii_unpack ( data , idx ) [ <NUM_LIT:0> ] ) <EOL> idx += <NUM_LIT:8> <EOL> values = [ ] <EOL> while idx < final_idx : <EOL> element_len , = i_unpack ( data , idx ) <EOL> idx += <NUM_LIT:4> <EOL> if element_len == - <NUM_LIT:1> : <EOL> values . append ( None ) <EOL> else : <EOL> values . append ( conversion ( data , idx , element_len ) ) <EOL> idx += element_len <EOL> for length in reversed ( dim_lengths [ <NUM_LIT:1> : ] ) : <EOL> values = list ( map ( list , zip ( * [ iter ( values ) ] * length ) ) ) <EOL> return values <EOL> def vector_in ( data , idx , length ) : <EOL> return eval ( '<STR_LIT:[>' + data [ idx : idx + length ] . decode ( <EOL> self . _client_encoding ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:U+002C>' ) + '<STR_LIT:]>' ) <EOL> if PY2 : <EOL> def text_recv ( data , offset , length ) : <EOL> return unicode ( <EOL> data [ offset : offset + length ] , self . _client_encoding ) <EOL> def bool_recv ( d , o , l ) : <EOL> return d [ o ] == "<STR_LIT>" <EOL> else : <EOL> def text_recv ( data , offset , length ) : <EOL> return str ( <EOL> data [ offset : offset + length ] , self . _client_encoding ) <EOL> def bool_recv ( data , offset , length ) : <EOL> return data [ offset ] == <NUM_LIT:1> <EOL> def time_in ( data , offset , length ) : <EOL> hour = int ( data [ offset : offset + <NUM_LIT:2> ] ) <EOL> minute = int ( data [ offset + <NUM_LIT:3> : offset + <NUM_LIT:5> ] ) <EOL> sec = Decimal ( <EOL> data [ offset + <NUM_LIT:6> : offset + length ] . decode ( self . _client_encoding ) ) <EOL> return datetime . time ( <EOL> hour , minute , int ( sec ) , int ( ( sec - int ( sec ) ) * <NUM_LIT> ) ) <EOL> def date_in ( data , offset , length ) : <EOL> year_str = data [ offset : offset + <NUM_LIT:4> ] . decode ( self . _client_encoding ) <EOL> if year_str == '<STR_LIT>' : <EOL> return datetime . date . max <EOL> elif year_str == '<STR_LIT>' : <EOL> return datetime . date . min <EOL> else : <EOL> return datetime . date ( <EOL> int ( year_str ) , int ( data [ offset + <NUM_LIT:5> : offset + <NUM_LIT:7> ] ) , <EOL> int ( data [ offset + <NUM_LIT:8> : offset + <NUM_LIT:10> ] ) ) <EOL> def numeric_in ( data , offset , length ) : <EOL> return Decimal ( <EOL> data [ offset : offset + length ] . decode ( self . _client_encoding ) ) <EOL> def numeric_out ( d ) : <EOL> return str ( d ) . encode ( self . _client_encoding ) <EOL> self . pg_types = defaultdict ( <EOL> lambda : ( FC_TEXT , text_recv ) , { <EOL> <NUM_LIT:16> : ( FC_BINARY , bool_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , bytea_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT:20> : ( FC_BINARY , int8_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , int2_recv ) , <EOL> <NUM_LIT> : ( FC_TEXT , vector_in ) , <EOL> <NUM_LIT> : ( FC_BINARY , int4_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT> : ( FC_TEXT , int_in ) , <EOL> <NUM_LIT> : ( FC_TEXT , int_in ) , <EOL> <NUM_LIT> : ( FC_BINARY , float4_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , float8_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT> : ( FC_TEXT , text_recv ) , <EOL> <NUM_LIT:1000> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT> : ( FC_TEXT , date_in ) , <EOL> <NUM_LIT> : ( FC_TEXT , time_in ) , <EOL> <NUM_LIT> : ( FC_BINARY , timestamp_recv_float ) , <EOL> <NUM_LIT> : ( FC_BINARY , timestamptz_recv_float ) , <EOL> <NUM_LIT> : ( FC_BINARY , interval_recv_integer ) , <EOL> <NUM_LIT> : ( FC_TEXT , array_in ) , <EOL> <NUM_LIT> : ( FC_BINARY , array_recv ) , <EOL> <NUM_LIT> : ( FC_TEXT , numeric_in ) , <EOL> <NUM_LIT> : ( FC_BINARY , text_recv ) , <EOL> <NUM_LIT> : ( FC_BINARY , uuid_recv ) , <EOL> } ) <EOL> self . py_types = { <EOL> type ( None ) : ( - <NUM_LIT:1> , FC_BINARY , null_send ) , <EOL> bool : ( <NUM_LIT:16> , FC_BINARY , bool_send ) , <EOL> int : ( <NUM_LIT> , FC_TEXT , unknown_out ) , <EOL> float : ( <NUM_LIT> , FC_BINARY , d_pack ) , <EOL> str : ( <NUM_LIT> , FC_TEXT , text_out ) , <EOL> datetime . date : ( <NUM_LIT> , FC_TEXT , date_out ) , <EOL> datetime . time : ( <NUM_LIT> , FC_TEXT , time_out ) , <EOL> <NUM_LIT> : ( <NUM_LIT> , FC_BINARY , timestamp_send_integer ) , <EOL> <NUM_LIT> : ( <NUM_LIT> , FC_BINARY , timestamptz_send_integer ) , <EOL> datetime . timedelta : ( <NUM_LIT> , FC_BINARY , interval_send_integer ) , <EOL> Interval : ( <NUM_LIT> , FC_BINARY , interval_send_integer ) , <EOL> Decimal : ( <NUM_LIT> , FC_TEXT , numeric_out ) , <EOL> UUID : ( <NUM_LIT> , FC_BINARY , uuid_send ) , <EOL> } <EOL> self . inspect_funcs = { <EOL> datetime . datetime : self . inspect_datetime , <EOL> list : self . array_inspect , <EOL> tuple : self . array_inspect , <EOL> } <EOL> if PY2 : <EOL> self . py_types [ pg8000 . Bytea ] = ( <NUM_LIT> , FC_BINARY , bytea_send ) <EOL> self . py_types [ text_type ] = ( <NUM_LIT> , FC_TEXT , text_out ) <EOL> self . py_types [ long ] = ( <NUM_LIT> , FC_TEXT , unknown_out ) <EOL> else : <EOL> self . py_types [ bytes ] = ( <NUM_LIT> , FC_BINARY , bytea_send ) <EOL> try : <EOL> from ipaddress import ( <EOL> ip_address , IPv4Address , IPv6Address , ip_network , IPv4Network , <EOL> IPv6Network ) <EOL> def inet_out ( v ) : <EOL> return str ( v ) . encode ( self . _client_encoding ) <EOL> def inet_in ( data , offset , length ) : <EOL> inet_str = data [ offset : offset + length ] . decode ( <EOL> self . _client_encoding ) <EOL> if '<STR_LIT:/>' in inet_str : <EOL> return ip_network ( inet_str , False ) <EOL> else : <EOL> return ip_address ( inet_str ) <EOL> self . py_types [ IPv4Address ] = ( <NUM_LIT> , FC_TEXT , inet_out ) <EOL> self . py_types [ IPv6Address ] = ( <NUM_LIT> , FC_TEXT , inet_out ) <EOL> self . py_types [ IPv4Network ] = ( <NUM_LIT> , FC_TEXT , inet_out ) <EOL> self . py_types [ IPv6Network ] = ( <NUM_LIT> , FC_TEXT , inet_out ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_TEXT , inet_in ) <EOL> except ImportError : <EOL> pass <EOL> self . message_types = { <EOL> NOTICE_RESPONSE : self . handle_NOTICE_RESPONSE , <EOL> AUTHENTICATION_REQUEST : self . handle_AUTHENTICATION_REQUEST , <EOL> PARAMETER_STATUS : self . handle_PARAMETER_STATUS , <EOL> BACKEND_KEY_DATA : self . handle_BACKEND_KEY_DATA , <EOL> READY_FOR_QUERY : self . handle_READY_FOR_QUERY , <EOL> ROW_DESCRIPTION : self . handle_ROW_DESCRIPTION , <EOL> ERROR_RESPONSE : self . handle_ERROR_RESPONSE , <EOL> DATA_ROW : self . handle_DATA_ROW , <EOL> COMMAND_COMPLETE : self . handle_COMMAND_COMPLETE , <EOL> PARSE_COMPLETE : self . handle_PARSE_COMPLETE , <EOL> BIND_COMPLETE : self . handle_BIND_COMPLETE , <EOL> CLOSE_COMPLETE : self . handle_CLOSE_COMPLETE , <EOL> PORTAL_SUSPENDED : self . handle_PORTAL_SUSPENDED , <EOL> NO_DATA : self . handle_NO_DATA , <EOL> PARAMETER_DESCRIPTION : self . handle_PARAMETER_DESCRIPTION , <EOL> NOTIFICATION_RESPONSE : self . handle_NOTIFICATION_RESPONSE , <EOL> COPY_DONE : self . handle_COPY_DONE , <EOL> COPY_DATA : self . handle_COPY_DATA , <EOL> COPY_IN_RESPONSE : self . handle_COPY_IN_RESPONSE , <EOL> COPY_OUT_RESPONSE : self . handle_COPY_OUT_RESPONSE } <EOL> protocol = <NUM_LIT> <EOL> val = bytearray ( i_pack ( protocol ) + b ( "<STR_LIT>" ) ) <EOL> val . extend ( user . encode ( "<STR_LIT:ascii>" ) + NULL_BYTE ) <EOL> if database is not None : <EOL> val . extend ( <EOL> b ( "<STR_LIT>" ) + database . encode ( "<STR_LIT:ascii>" ) + NULL_BYTE ) <EOL> val . append ( <NUM_LIT:0> ) <EOL> self . _write ( i_pack ( len ( val ) + <NUM_LIT:4> ) ) <EOL> self . _write ( val ) <EOL> self . _flush ( ) <EOL> self . _cursor = self . cursor ( ) <EOL> try : <EOL> self . _lock . acquire ( ) <EOL> code = self . error = None <EOL> while code not in ( READY_FOR_QUERY , ERROR_RESPONSE ) : <EOL> code , data_len = ci_unpack ( self . _read ( <NUM_LIT:5> ) ) <EOL> self . message_types [ code ] ( self . _read ( data_len - <NUM_LIT:4> ) , None ) <EOL> if self . error is not None : <EOL> raise self . error <EOL> except : <EOL> self . _close ( ) <EOL> raise <EOL> finally : <EOL> self . _lock . release ( ) <EOL> self . in_transaction = False <EOL> self . notifies = [ ] <EOL> self . notifies_lock = threading . Lock ( ) <EOL> def handle_ERROR_RESPONSE ( self , data , ps ) : <EOL> msg_dict = data_into_dict ( data ) <EOL> if msg_dict [ RESPONSE_CODE ] == "<STR_LIT>" : <EOL> self . error = InterfaceError ( "<STR_LIT>" ) <EOL> else : <EOL> self . error = ProgrammingError ( <EOL> msg_dict [ RESPONSE_SEVERITY ] , msg_dict [ RESPONSE_CODE ] , <EOL> msg_dict [ RESPONSE_MSG ] ) <EOL> def handle_CLOSE_COMPLETE ( self , data , ps ) : <EOL> pass <EOL> def handle_PARSE_COMPLETE ( self , data , ps ) : <EOL> pass <EOL> def handle_BIND_COMPLETE ( self , data , ps ) : <EOL> pass <EOL> def handle_PORTAL_SUSPENDED ( self , data , cursor ) : <EOL> cursor . portal_suspended = True <EOL> def handle_PARAMETER_DESCRIPTION ( self , data , ps ) : <EOL> pass <EOL> def handle_COPY_DONE ( self , data , ps ) : <EOL> self . _copy_done = True <EOL> def handle_COPY_OUT_RESPONSE ( self , data , ps ) : <EOL> is_binary , num_cols = bh_unpack ( data ) <EOL> if ps . stream is None : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" ) <EOL> def handle_COPY_DATA ( self , data , ps ) : <EOL> ps . stream . write ( data ) <EOL> def handle_COPY_IN_RESPONSE ( self , data , ps ) : <EOL> is_binary , num_cols = bh_unpack ( data ) <EOL> assert self . _lock . locked ( ) <EOL> if ps . stream is None : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" ) <EOL> if PY2 : <EOL> while True : <EOL> data = ps . stream . read ( <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> self . _write ( COPY_DATA + i_pack ( len ( data ) + <NUM_LIT:4> ) ) <EOL> self . _write ( data ) <EOL> self . _flush ( ) <EOL> else : <EOL> bffr = bytearray ( <NUM_LIT> ) <EOL> while True : <EOL> bytes_read = ps . stream . readinto ( bffr ) <EOL> if bytes_read == <NUM_LIT:0> : <EOL> break <EOL> self . _write ( COPY_DATA + i_pack ( bytes_read + <NUM_LIT:4> ) ) <EOL> self . _write ( bffr [ : bytes_read ] ) <EOL> self . _flush ( ) <EOL> self . _write ( COPY_DONE_MSG ) <EOL> self . _write ( SYNC_MSG ) <EOL> self . _flush ( ) <EOL> def handle_NOTIFICATION_RESPONSE ( self , data , ps ) : <EOL> self . NotificationReceived ( data ) <EOL> backend_pid = i_unpack ( data ) [ <NUM_LIT:0> ] <EOL> idx = <NUM_LIT:4> <EOL> null = data . find ( NULL_BYTE , idx ) - idx <EOL> condition = data [ idx : idx + null ] . decode ( "<STR_LIT:ascii>" ) <EOL> idx += null + <NUM_LIT:1> <EOL> null = data . find ( NULL_BYTE , idx ) - idx <EOL> try : <EOL> self . notifies_lock . acquire ( ) <EOL> self . notifies . append ( ( backend_pid , condition ) ) <EOL> finally : <EOL> self . notifies_lock . release ( ) <EOL> def cursor ( self ) : <EOL> """<STR_LIT>""" <EOL> return Cursor ( self ) <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _lock . acquire ( ) <EOL> self . execute ( self . _cursor , "<STR_LIT>" , None ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def rollback ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _lock . acquire ( ) <EOL> self . execute ( self . _cursor , "<STR_LIT>" , None ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def _close ( self ) : <EOL> try : <EOL> self . _write ( TERMINATE_MSG ) <EOL> self . _flush ( ) <EOL> self . _sock . close ( ) <EOL> self . _usock . close ( ) <EOL> self . _sock = None <EOL> except AttributeError : <EOL> raise pg8000 . InterfaceError ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> raise pg8000 . InterfaceError ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _lock . acquire ( ) <EOL> self . _close ( ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def handle_AUTHENTICATION_REQUEST ( self , data , cursor ) : <EOL> assert self . _lock . locked ( ) <EOL> auth_code = i_unpack ( data ) [ <NUM_LIT:0> ] <EOL> if auth_code == <NUM_LIT:0> : <EOL> pass <EOL> elif auth_code == <NUM_LIT:3> : <EOL> if self . password is None : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _send_message ( <EOL> PASSWORD , self . password . encode ( "<STR_LIT:ascii>" ) + NULL_BYTE ) <EOL> self . _flush ( ) <EOL> elif auth_code == <NUM_LIT:5> : <EOL> salt = b ( "<STR_LIT>" ) . join ( cccc_unpack ( data , <NUM_LIT:4> ) ) <EOL> if self . password is None : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> pwd = b ( "<STR_LIT>" ) + md5 ( <EOL> md5 ( <EOL> self . password . encode ( "<STR_LIT:ascii>" ) + <EOL> self . user . encode ( "<STR_LIT:ascii>" ) ) . hexdigest ( ) . encode ( "<STR_LIT:ascii>" ) + <EOL> salt ) . hexdigest ( ) . encode ( "<STR_LIT:ascii>" ) <EOL> self . _send_message ( PASSWORD , pwd + NULL_BYTE ) <EOL> self . _flush ( ) <EOL> elif auth_code in ( <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ) : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" + str ( auth_code ) + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" + str ( auth_code ) + <EOL> "<STR_LIT>" ) <EOL> def handle_READY_FOR_QUERY ( self , data , ps ) : <EOL> self . in_transaction = data != IDLE <EOL> def handle_BACKEND_KEY_DATA ( self , data , ps ) : <EOL> self . _backend_key_data = data <EOL> def inspect_datetime ( self , value ) : <EOL> if value . tzinfo is None : <EOL> return self . py_types [ <NUM_LIT> ] <EOL> else : <EOL> return self . py_types [ <NUM_LIT> ] <EOL> def make_params ( self , values ) : <EOL> params = [ ] <EOL> for value in values : <EOL> typ = type ( value ) <EOL> try : <EOL> params . append ( self . py_types [ typ ] ) <EOL> except KeyError : <EOL> try : <EOL> params . append ( self . inspect_funcs [ typ ] ( value ) ) <EOL> except KeyError : <EOL> raise NotSupportedError ( <EOL> "<STR_LIT>" + str ( exc_info ( ) [ <NUM_LIT:1> ] ) + <EOL> "<STR_LIT>" ) <EOL> return params <EOL> def handle_ROW_DESCRIPTION ( self , data , cursor ) : <EOL> count = h_unpack ( data ) [ <NUM_LIT:0> ] <EOL> idx = <NUM_LIT:2> <EOL> for i in range ( count ) : <EOL> field = { '<STR_LIT:name>' : data [ idx : data . find ( NULL_BYTE , idx ) ] } <EOL> idx += len ( field [ '<STR_LIT:name>' ] ) + <NUM_LIT:1> <EOL> field . update ( <EOL> dict ( zip ( ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ihihih_unpack ( data , idx ) ) ) ) <EOL> idx += <NUM_LIT> <EOL> cursor . ps [ '<STR_LIT>' ] . append ( field ) <EOL> try : <EOL> field [ '<STR_LIT>' ] , field [ '<STR_LIT>' ] = self . pg_types [ <EOL> field [ '<STR_LIT>' ] ] <EOL> except KeyError : <EOL> raise NotSupportedError ( <EOL> "<STR_LIT>" + exc_info ( ) [ <NUM_LIT:1> ] + "<STR_LIT>" ) <EOL> def execute ( self , cursor , operation , vals ) : <EOL> if vals is None : <EOL> vals = ( ) <EOL> paramstyle = pg8000 . paramstyle <EOL> cache = self . _caches [ paramstyle ] <EOL> try : <EOL> statement , make_args = cache [ '<STR_LIT>' ] [ operation ] <EOL> except KeyError : <EOL> statement , make_args = convert_paramstyle ( paramstyle , operation ) <EOL> cache [ '<STR_LIT>' ] [ operation ] = statement , make_args <EOL> args = make_args ( vals ) <EOL> params = self . make_params ( args ) <EOL> key = tuple ( oid for oid , x , y in params ) , operation <EOL> try : <EOL> ps = cache [ '<STR_LIT>' ] [ key ] <EOL> cursor . ps = ps <EOL> except KeyError : <EOL> statement_name = "<STR_LIT>" + str ( self . statement_number ) <EOL> self . statement_number += <NUM_LIT:1> <EOL> statement_name_bin = statement_name . encode ( '<STR_LIT:ascii>' ) + NULL_BYTE <EOL> ps = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : tuple ( x [ <NUM_LIT:2> ] for x in params ) , <EOL> } <EOL> cursor . ps = ps <EOL> param_fcs = tuple ( x [ <NUM_LIT:1> ] for x in params ) <EOL> val = bytearray ( statement_name_bin ) <EOL> val . extend ( statement . encode ( self . _client_encoding ) + NULL_BYTE ) <EOL> val . extend ( h_pack ( len ( params ) ) ) <EOL> for oid , fc , send_func in params : <EOL> val . extend ( i_pack ( <NUM_LIT> if oid == - <NUM_LIT:1> else oid ) ) <EOL> self . _send_message ( PARSE , val ) <EOL> self . _send_message ( DESCRIBE , STATEMENT + statement_name_bin ) <EOL> self . _write ( SYNC_MSG ) <EOL> try : <EOL> self . _flush ( ) <EOL> except AttributeError : <EOL> if self . _sock is None : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> else : <EOL> raise exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . handle_messages ( cursor ) <EOL> output_fc = tuple ( <EOL> self . pg_types [ f [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] for f in ps [ '<STR_LIT>' ] ) <EOL> ps [ '<STR_LIT>' ] = tuple ( f [ '<STR_LIT>' ] for f in ps [ '<STR_LIT>' ] ) <EOL> ps [ '<STR_LIT>' ] = statement_name_bin + h_pack ( len ( params ) ) + pack ( "<STR_LIT:!>" + "<STR_LIT:h>" * len ( param_fcs ) , * param_fcs ) + h_pack ( len ( params ) ) <EOL> ps [ '<STR_LIT>' ] = h_pack ( len ( output_fc ) ) + pack ( "<STR_LIT:!>" + "<STR_LIT:h>" * len ( output_fc ) , * output_fc ) <EOL> cache [ '<STR_LIT>' ] [ key ] = ps <EOL> cursor . _cached_rows . clear ( ) <EOL> cursor . _row_count = - <NUM_LIT:1> <EOL> cursor . portal_name = "<STR_LIT>" + str ( self . portal_number ) <EOL> self . portal_number += <NUM_LIT:1> <EOL> cursor . portal_name_bin = cursor . portal_name . encode ( '<STR_LIT:ascii>' ) + NULL_BYTE <EOL> cursor . execute_msg = cursor . portal_name_bin + Connection . _row_cache_size_bin <EOL> retval = bytearray ( cursor . portal_name_bin + ps [ '<STR_LIT>' ] ) <EOL> for value , send_func in zip ( args , ps [ '<STR_LIT>' ] ) : <EOL> if value is None : <EOL> val = NULL <EOL> else : <EOL> val = send_func ( value ) <EOL> retval . extend ( i_pack ( len ( val ) ) ) <EOL> retval . extend ( val ) <EOL> retval . extend ( ps [ '<STR_LIT>' ] ) <EOL> self . _send_message ( BIND , retval ) <EOL> self . send_EXECUTE ( cursor ) <EOL> self . _write ( SYNC_MSG ) <EOL> self . _flush ( ) <EOL> self . handle_messages ( cursor ) <EOL> if cursor . portal_suspended : <EOL> if self . autocommit : <EOL> raise InterfaceError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . close_portal ( cursor ) <EOL> def _send_message ( self , code , data ) : <EOL> try : <EOL> self . _write ( code ) <EOL> self . _write ( i_pack ( len ( data ) + <NUM_LIT:4> ) ) <EOL> self . _write ( data ) <EOL> self . _write ( FLUSH_MSG ) <EOL> except ValueError : <EOL> if str ( exc_info ( ) [ <NUM_LIT:1> ] ) == "<STR_LIT>" : <EOL> raise pg8000 . InterfaceError ( "<STR_LIT>" ) <EOL> else : <EOL> raise exc_info ( ) [ <NUM_LIT:1> ] <EOL> except AttributeError : <EOL> raise pg8000 . InterfaceError ( "<STR_LIT>" ) <EOL> def send_EXECUTE ( self , cursor ) : <EOL> cursor . portal_suspended = False <EOL> self . _send_message ( EXECUTE , cursor . execute_msg ) <EOL> def handle_NO_DATA ( self , msg , ps ) : <EOL> pass <EOL> def handle_COMMAND_COMPLETE ( self , data , cursor ) : <EOL> values = data [ : - <NUM_LIT:1> ] . split ( BINARY_SPACE ) <EOL> command = values [ <NUM_LIT:0> ] <EOL> if command in self . _commands_with_count : <EOL> row_count = int ( values [ - <NUM_LIT:1> ] ) <EOL> if cursor . _row_count == - <NUM_LIT:1> : <EOL> cursor . _row_count = row_count <EOL> else : <EOL> cursor . _row_count += row_count <EOL> if command in DDL_COMMANDS : <EOL> for k in self . _caches : <EOL> self . _caches [ k ] [ '<STR_LIT>' ] . clear ( ) <EOL> def handle_DATA_ROW ( self , data , cursor ) : <EOL> data_idx = <NUM_LIT:2> <EOL> row = [ ] <EOL> for func in cursor . ps [ '<STR_LIT>' ] : <EOL> vlen = i_unpack ( data , data_idx ) [ <NUM_LIT:0> ] <EOL> data_idx += <NUM_LIT:4> <EOL> if vlen == - <NUM_LIT:1> : <EOL> row . append ( None ) <EOL> else : <EOL> row . append ( func ( data , data_idx , vlen ) ) <EOL> data_idx += vlen <EOL> cursor . _cached_rows . append ( row ) <EOL> def handle_messages ( self , cursor ) : <EOL> code = self . error = None <EOL> try : <EOL> while code != READY_FOR_QUERY : <EOL> code , data_len = ci_unpack ( self . _read ( <NUM_LIT:5> ) ) <EOL> self . message_types [ code ] ( self . _read ( data_len - <NUM_LIT:4> ) , cursor ) <EOL> except : <EOL> self . _close ( ) <EOL> raise <EOL> if self . error is not None : <EOL> raise self . error <EOL> def close_portal ( self , cursor ) : <EOL> self . _send_message ( CLOSE , PORTAL + cursor . portal_name_bin ) <EOL> self . _write ( SYNC_MSG ) <EOL> self . _flush ( ) <EOL> self . handle_messages ( cursor ) <EOL> def handle_NOTICE_RESPONSE ( self , data , ps ) : <EOL> resp = data_into_dict ( data ) <EOL> self . NoticeReceived ( resp ) <EOL> def handle_PARAMETER_STATUS ( self , data , ps ) : <EOL> pos = data . find ( NULL_BYTE ) <EOL> key , value = data [ : pos ] , data [ pos + <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if key == b ( "<STR_LIT>" ) : <EOL> encoding = value . decode ( "<STR_LIT:ascii>" ) . lower ( ) <EOL> self . _client_encoding = pg_to_py_encodings . get ( encoding , encoding ) <EOL> elif key == b ( "<STR_LIT>" ) : <EOL> if value == b ( '<STR_LIT>' ) : <EOL> self . py_types [ <NUM_LIT> ] = ( <NUM_LIT> , FC_BINARY , timestamp_send_integer ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , timestamp_recv_integer ) <EOL> self . py_types [ <NUM_LIT> ] = ( <EOL> <NUM_LIT> , FC_BINARY , timestamptz_send_integer ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , timestamptz_recv_integer ) <EOL> self . py_types [ Interval ] = ( <EOL> <NUM_LIT> , FC_BINARY , interval_send_integer ) <EOL> self . py_types [ datetime . timedelta ] = ( <EOL> <NUM_LIT> , FC_BINARY , interval_send_integer ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , interval_recv_integer ) <EOL> else : <EOL> self . py_types [ <NUM_LIT> ] = ( <NUM_LIT> , FC_BINARY , timestamp_send_float ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , timestamp_recv_float ) <EOL> self . py_types [ <NUM_LIT> ] = ( <NUM_LIT> , FC_BINARY , timestamptz_send_float ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , timestamptz_recv_float ) <EOL> self . py_types [ Interval ] = ( <EOL> <NUM_LIT> , FC_BINARY , interval_send_float ) <EOL> self . py_types [ datetime . timedelta ] = ( <EOL> <NUM_LIT> , FC_BINARY , interval_send_float ) <EOL> self . pg_types [ <NUM_LIT> ] = ( FC_BINARY , interval_recv_float ) <EOL> elif key == b ( "<STR_LIT>" ) : <EOL> self . _server_version = LooseVersion ( value . decode ( '<STR_LIT:ascii>' ) ) <EOL> if self . _server_version < LooseVersion ( '<STR_LIT>' ) : <EOL> self . _commands_with_count = ( <EOL> b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , <EOL> b ( "<STR_LIT>" ) ) <EOL> elif self . _server_version < LooseVersion ( '<STR_LIT>' ) : <EOL> self . _commands_with_count = ( <EOL> b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) , <EOL> b ( "<STR_LIT>" ) , b ( "<STR_LIT>" ) ) <EOL> def array_inspect ( self , value ) : <EOL> first_element = array_find_first_element ( value ) <EOL> if first_element is None : <EOL> raise ArrayContentEmptyError ( "<STR_LIT>" ) <EOL> typ = type ( first_element ) <EOL> if issubclass ( typ , integer_types ) : <EOL> typ = integer_types <EOL> int2_ok , int4_ok , int8_ok = True , True , True <EOL> for v in array_flatten ( value ) : <EOL> if v is None : <EOL> continue <EOL> if min_int2 < v < max_int2 : <EOL> continue <EOL> int2_ok = False <EOL> if min_int4 < v < max_int4 : <EOL> continue <EOL> int4_ok = False <EOL> if min_int8 < v < max_int8 : <EOL> continue <EOL> int8_ok = False <EOL> if int2_ok : <EOL> array_oid = <NUM_LIT> <EOL> oid , fc , send_func = ( <NUM_LIT> , FC_BINARY , h_pack ) <EOL> elif int4_ok : <EOL> array_oid = <NUM_LIT> <EOL> oid , fc , send_func = ( <NUM_LIT> , FC_BINARY , i_pack ) <EOL> elif int8_ok : <EOL> array_oid = <NUM_LIT> <EOL> oid , fc , send_func = ( <NUM_LIT:20> , FC_BINARY , q_pack ) <EOL> else : <EOL> raise ArrayContentNotSupportedError ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> oid , fc , send_func = self . make_params ( ( first_element , ) ) [ <NUM_LIT:0> ] <EOL> if oid == <NUM_LIT> : <EOL> oid = <NUM_LIT> <EOL> array_oid = pg_array_types [ oid ] <EOL> except KeyError : <EOL> raise ArrayContentNotSupportedError ( <EOL> "<STR_LIT>" + str ( oid ) + "<STR_LIT>" ) <EOL> except NotSupportedError : <EOL> raise ArrayContentNotSupportedError ( <EOL> "<STR_LIT>" + str ( typ ) + "<STR_LIT>" ) <EOL> if fc == FC_BINARY : <EOL> def send_array ( arr ) : <EOL> for a , i , v in walk_array ( arr ) : <EOL> if not isinstance ( v , ( typ , type ( None ) ) ) : <EOL> raise ArrayContentNotHomogenousError ( <EOL> "<STR_LIT>" + str ( typ ) ) <EOL> array_check_dimensions ( arr ) <EOL> has_null = array_has_null ( arr ) <EOL> dim_lengths = array_dim_lengths ( arr ) <EOL> data = bytearray ( iii_pack ( len ( dim_lengths ) , has_null , oid ) ) <EOL> for i in dim_lengths : <EOL> data . extend ( ii_pack ( i , <NUM_LIT:1> ) ) <EOL> for v in array_flatten ( arr ) : <EOL> if v is None : <EOL> data += i_pack ( - <NUM_LIT:1> ) <EOL> else : <EOL> inner_data = send_func ( v ) <EOL> data += i_pack ( len ( inner_data ) ) <EOL> data += inner_data <EOL> return data <EOL> else : <EOL> def send_array ( arr ) : <EOL> for a , i , v in walk_array ( arr ) : <EOL> if not isinstance ( v , ( typ , type ( None ) ) ) : <EOL> raise ArrayContentNotHomogenousError ( <EOL> "<STR_LIT>" + str ( typ ) ) <EOL> array_check_dimensions ( arr ) <EOL> ar = deepcopy ( arr ) <EOL> for a , i , v in walk_array ( ar ) : <EOL> if v is None : <EOL> a [ i ] = '<STR_LIT>' <EOL> else : <EOL> a [ i ] = send_func ( v ) . decode ( '<STR_LIT:ascii>' ) <EOL> return u ( str ( ar ) ) . translate ( arr_trans ) . encode ( '<STR_LIT:ascii>' ) <EOL> return ( array_oid , fc , send_array ) <EOL> def xid ( self , format_id , global_transaction_id , branch_qualifier ) : <EOL> """<STR_LIT>""" <EOL> return ( format_id , global_transaction_id , branch_qualifier ) <EOL> def tpc_begin ( self , xid ) : <EOL> """<STR_LIT>""" <EOL> self . _xid = xid <EOL> if self . autocommit : <EOL> self . execute ( self . _cursor , "<STR_LIT>" , None ) <EOL> def tpc_prepare ( self ) : <EOL> """<STR_LIT>""" <EOL> q = "<STR_LIT>" % ( self . _xid [ <NUM_LIT:1> ] , ) <EOL> self . execute ( self . _cursor , q , None ) <EOL> def tpc_commit ( self , xid = None ) : <EOL> """<STR_LIT>""" <EOL> if xid is None : <EOL> xid = self . _xid <EOL> if xid is None : <EOL> raise ProgrammingError ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> previous_autocommit_mode = self . autocommit <EOL> self . autocommit = True <EOL> if xid in self . tpc_recover ( ) : <EOL> self . execute ( <EOL> self . _cursor , "<STR_LIT>" % ( xid [ <NUM_LIT:1> ] , ) , <EOL> None ) <EOL> else : <EOL> self . commit ( ) <EOL> finally : <EOL> self . autocommit = previous_autocommit_mode <EOL> self . _xid = None <EOL> def tpc_rollback ( self , xid = None ) : <EOL> """<STR_LIT>""" <EOL> if xid is None : <EOL> xid = self . _xid <EOL> if xid is None : <EOL> raise ProgrammingError ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> previous_autocommit_mode = self . autocommit <EOL> self . autocommit = True <EOL> if xid in self . tpc_recover ( ) : <EOL> self . execute ( <EOL> self . _cursor , "<STR_LIT>" % ( xid [ <NUM_LIT:1> ] , ) , <EOL> None ) <EOL> else : <EOL> self . rollback ( ) <EOL> finally : <EOL> self . autocommit = previous_autocommit_mode <EOL> self . _xid = None <EOL> def tpc_recover ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> previous_autocommit_mode = self . autocommit <EOL> self . autocommit = True <EOL> curs = self . cursor ( ) <EOL> curs . execute ( "<STR_LIT>" ) <EOL> return [ self . xid ( <NUM_LIT:0> , row [ <NUM_LIT:0> ] , '<STR_LIT>' ) for row in curs ] <EOL> finally : <EOL> self . autocommit = previous_autocommit_mode <EOL> pg_array_types = { <EOL> <NUM_LIT:16> : <NUM_LIT:1000> , <EOL> <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <EOL> } <EOL> pg_to_py_encodings = { <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:ascii>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:utf-8>" , <EOL> } <EOL> def walk_array ( arr ) : <EOL> for i , v in enumerate ( arr ) : <EOL> if isinstance ( v , list ) : <EOL> for a , i2 , v2 in walk_array ( v ) : <EOL> yield a , i2 , v2 <EOL> else : <EOL> yield arr , i , v <EOL> def array_find_first_element ( arr ) : <EOL> for v in array_flatten ( arr ) : <EOL> if v is not None : <EOL> return v <EOL> return None <EOL> def array_flatten ( arr ) : <EOL> for v in arr : <EOL> if isinstance ( v , list ) : <EOL> for v2 in array_flatten ( v ) : <EOL> yield v2 <EOL> else : <EOL> yield v <EOL> def array_check_dimensions ( arr ) : <EOL> v0 = arr [ <NUM_LIT:0> ] <EOL> if isinstance ( v0 , list ) : <EOL> req_len = len ( v0 ) <EOL> req_inner_lengths = array_check_dimensions ( v0 ) <EOL> for v in arr : <EOL> inner_lengths = array_check_dimensions ( v ) <EOL> if len ( v ) != req_len or inner_lengths != req_inner_lengths : <EOL> raise ArrayDimensionsNotConsistentError ( <EOL> "<STR_LIT>" ) <EOL> retval = [ req_len ] <EOL> retval . extend ( req_inner_lengths ) <EOL> return retval <EOL> else : <EOL> for v in arr : <EOL> if isinstance ( v , list ) : <EOL> raise ArrayDimensionsNotConsistentError ( <EOL> "<STR_LIT>" ) <EOL> return [ ] <EOL> def array_has_null ( arr ) : <EOL> for v in array_flatten ( arr ) : <EOL> if v is None : <EOL> return True <EOL> return False <EOL> def array_dim_lengths ( arr ) : <EOL> v0 = arr [ <NUM_LIT:0> ] <EOL> if isinstance ( v0 , list ) : <EOL> retval = [ len ( v0 ) ] <EOL> retval . extend ( array_dim_lengths ( v0 ) ) <EOL> else : <EOL> return [ len ( arr ) ] <EOL> return retval </s>
