<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals , absolute_import <EOL> from base64 import standard_b64encode , standard_b64decode <EOL> from decimal import Decimal <EOL> from py4j . compat import ( <EOL> long , basestring , unicode , bytearray2 , <EOL> bytestr , isbytestr , isbytearray , ispython3bytestr , <EOL> bytetoint , bytetostr , strtobyte ) <EOL> JAVA_MAX_INT = <NUM_LIT> <EOL> JAVA_INFINITY = "<STR_LIT>" <EOL> JAVA_NEGATIVE_INFINITY = "<STR_LIT>" <EOL> JAVA_NAN = "<STR_LIT>" <EOL> ESCAPE_CHAR = "<STR_LIT:\\>" <EOL> ENTRY_POINT_OBJECT_ID = "<STR_LIT:t>" <EOL> CONNECTION_PROPERTY_OBJECT_ID = "<STR_LIT:c>" <EOL> STATIC_PREFIX = "<STR_LIT>" <EOL> DEFAULT_JVM_ID = "<STR_LIT>" <EOL> DEFAULT_JVM_NAME = "<STR_LIT:default>" <EOL> BYTES_TYPE = "<STR_LIT>" <EOL> INTEGER_TYPE = "<STR_LIT:i>" <EOL> LONG_TYPE = "<STR_LIT:L>" <EOL> BOOLEAN_TYPE = "<STR_LIT:b>" <EOL> DOUBLE_TYPE = "<STR_LIT:d>" <EOL> DECIMAL_TYPE = "<STR_LIT:D>" <EOL> STRING_TYPE = "<STR_LIT:s>" <EOL> REFERENCE_TYPE = "<STR_LIT:r>" <EOL> ARRAY_TYPE = "<STR_LIT:t>" <EOL> SET_TYPE = "<STR_LIT:h>" <EOL> LIST_TYPE = "<STR_LIT:l>" <EOL> MAP_TYPE = "<STR_LIT:a>" <EOL> NULL_TYPE = "<STR_LIT:n>" <EOL> PACKAGE_TYPE = "<STR_LIT:p>" <EOL> CLASS_TYPE = "<STR_LIT:c>" <EOL> METHOD_TYPE = "<STR_LIT:m>" <EOL> NO_MEMBER = "<STR_LIT:o>" <EOL> VOID_TYPE = "<STR_LIT:v>" <EOL> ITERATOR_TYPE = "<STR_LIT:g>" <EOL> PYTHON_PROXY_TYPE = "<STR_LIT:f>" <EOL> END = "<STR_LIT:e>" <EOL> ERROR = "<STR_LIT:x>" <EOL> SUCCESS = "<STR_LIT:y>" <EOL> SUCCESS_PACKAGE = SUCCESS + PACKAGE_TYPE <EOL> SUCCESS_CLASS = SUCCESS + CLASS_TYPE <EOL> CLASS_FQN_START = <NUM_LIT:2> <EOL> END_COMMAND_PART = END + "<STR_LIT:\n>" <EOL> NO_MEMBER_COMMAND = SUCCESS + NO_MEMBER <EOL> CALL_COMMAND_NAME = "<STR_LIT>" <EOL> FIELD_COMMAND_NAME = "<STR_LIT>" <EOL> CONSTRUCTOR_COMMAND_NAME = "<STR_LIT>" <EOL> SHUTDOWN_GATEWAY_COMMAND_NAME = "<STR_LIT>" <EOL> LIST_COMMAND_NAME = "<STR_LIT>" <EOL> REFLECTION_COMMAND_NAME = "<STR_LIT>" <EOL> MEMORY_COMMAND_NAME = "<STR_LIT>" <EOL> HELP_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_COMMAND_NAME = "<STR_LIT>" <EOL> JVMVIEW_COMMAND_NAME = "<STR_LIT>" <EOL> EXCEPTION_COMMAND_NAME = "<STR_LIT>" <EOL> DIR_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_GET_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_SET_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_SLICE_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_LEN_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> ARRAY_CREATE_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> REFL_GET_UNKNOWN_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> REFL_GET_MEMBER_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> LIST_SORT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_REVERSE_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_SLICE_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_CONCAT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_MULT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_IMULT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> LIST_COUNT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> FIELD_GET_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> FIELD_SET_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> MEMORY_DEL_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> MEMORY_ATTACH_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> HELP_OBJECT_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> HELP_CLASS_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> JVM_CREATE_VIEW_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> JVM_IMPORT_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> JVM_SEARCH_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> REMOVE_IMPORT_SUB_COMMAND_NAME = "<STR_LIT>" <EOL> PYTHON_PROXY_PREFIX = "<STR_LIT:p>" <EOL> ERROR_RETURN_MESSAGE = ERROR + "<STR_LIT:\n>" <EOL> CALL_PROXY_COMMAND_NAME = "<STR_LIT:c>" <EOL> GARBAGE_COLLECT_PROXY_COMMAND_NAME = "<STR_LIT:g>" <EOL> DIR_FIELDS_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> DIR_METHODS_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> DIR_STATIC_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> DIR_JVMVIEW_SUBCOMMAND_NAME = "<STR_LIT>" <EOL> OUTPUT_CONVERTER = { <EOL> NULL_TYPE : ( lambda x , y : None ) , <EOL> BOOLEAN_TYPE : ( lambda value , y : value . lower ( ) == "<STR_LIT:true>" ) , <EOL> LONG_TYPE : ( lambda value , y : long ( value ) ) , <EOL> DECIMAL_TYPE : ( lambda value , y : Decimal ( value ) ) , <EOL> INTEGER_TYPE : ( lambda value , y : int ( value ) ) , <EOL> BYTES_TYPE : ( lambda value , y : decode_bytearray ( value ) ) , <EOL> DOUBLE_TYPE : ( lambda value , y : float ( value ) ) , <EOL> STRING_TYPE : ( lambda value , y : unescape_new_line ( value ) ) , <EOL> } <EOL> INPUT_CONVERTER = [ ] <EOL> def escape_new_line ( original ) : <EOL> """<STR_LIT>""" <EOL> return smart_decode ( original ) . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\r>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> def unescape_new_line ( escaped ) : <EOL> """<STR_LIT>""" <EOL> return ESCAPE_CHAR . join ( <EOL> "<STR_LIT:\n>" . join ( <EOL> ( "<STR_LIT:\r>" . join ( p . split ( ESCAPE_CHAR + "<STR_LIT:r>" ) ) ) <EOL> . split ( ESCAPE_CHAR + "<STR_LIT:n>" ) ) <EOL> for p in escaped . split ( ESCAPE_CHAR + ESCAPE_CHAR ) ) <EOL> def smart_decode ( s ) : <EOL> if isinstance ( s , unicode ) : <EOL> return s <EOL> elif isinstance ( s , bytestr ) : <EOL> return unicode ( s , "<STR_LIT:utf-8>" ) <EOL> else : <EOL> return unicode ( s ) <EOL> def encode_float ( float_value ) : <EOL> float_str = smart_decode ( float_value ) <EOL> if float_str == "<STR_LIT>" : <EOL> float_str = JAVA_NEGATIVE_INFINITY <EOL> elif float_str == "<STR_LIT>" : <EOL> float_str = JAVA_INFINITY <EOL> elif float_str == "<STR_LIT>" : <EOL> float_str = JAVA_NAN <EOL> return float_str <EOL> def encode_bytearray ( barray ) : <EOL> if isbytestr ( barray ) : <EOL> return bytetostr ( standard_b64encode ( barray ) ) <EOL> else : <EOL> newbytestr = bytestr ( barray ) <EOL> return bytetostr ( standard_b64encode ( newbytestr ) ) <EOL> def decode_bytearray ( encoded ) : <EOL> new_bytes = strtobyte ( encoded ) <EOL> return bytearray2 ( [ bytetoint ( b ) for b in standard_b64decode ( new_bytes ) ] ) <EOL> def is_python_proxy ( parameter ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> is_proxy = len ( parameter . Java . implements ) > <NUM_LIT:0> <EOL> except Exception : <EOL> is_proxy = False <EOL> return is_proxy <EOL> def get_command_part ( parameter , python_proxy_pool = None ) : <EOL> """<STR_LIT>""" <EOL> command_part = "<STR_LIT>" <EOL> if parameter is None : <EOL> command_part = NULL_TYPE <EOL> elif isinstance ( parameter , bool ) : <EOL> command_part = BOOLEAN_TYPE + smart_decode ( parameter ) <EOL> elif isinstance ( parameter , Decimal ) : <EOL> command_part = DECIMAL_TYPE + smart_decode ( parameter ) <EOL> elif isinstance ( parameter , int ) and parameter <= JAVA_MAX_INT : <EOL> command_part = INTEGER_TYPE + smart_decode ( parameter ) <EOL> elif isinstance ( parameter , long ) or isinstance ( parameter , int ) : <EOL> command_part = LONG_TYPE + smart_decode ( parameter ) <EOL> elif isinstance ( parameter , float ) : <EOL> command_part = DOUBLE_TYPE + encode_float ( parameter ) <EOL> elif isbytearray ( parameter ) : <EOL> command_part = BYTES_TYPE + encode_bytearray ( parameter ) <EOL> elif ispython3bytestr ( parameter ) : <EOL> command_part = BYTES_TYPE + encode_bytearray ( parameter ) <EOL> elif isinstance ( parameter , basestring ) : <EOL> command_part = STRING_TYPE + escape_new_line ( parameter ) <EOL> elif is_python_proxy ( parameter ) : <EOL> command_part = PYTHON_PROXY_TYPE + python_proxy_pool . put ( parameter ) <EOL> for interface in parameter . Java . implements : <EOL> command_part += "<STR_LIT:;>" + interface <EOL> else : <EOL> command_part = REFERENCE_TYPE + parameter . _get_object_id ( ) <EOL> command_part += "<STR_LIT:\n>" <EOL> return command_part <EOL> def get_return_value ( answer , gateway_client , target_id = None , name = None ) : <EOL> """<STR_LIT>""" <EOL> if is_error ( answer ) [ <NUM_LIT:0> ] : <EOL> if len ( answer ) > <NUM_LIT:1> : <EOL> type = answer [ <NUM_LIT:1> ] <EOL> value = OUTPUT_CONVERTER [ type ] ( answer [ <NUM_LIT:2> : ] , gateway_client ) <EOL> if answer [ <NUM_LIT:1> ] == REFERENCE_TYPE : <EOL> raise Py4JJavaError ( <EOL> "<STR_LIT>" . <EOL> format ( target_id , "<STR_LIT:.>" , name ) , value ) <EOL> else : <EOL> raise Py4JError ( <EOL> "<STR_LIT>" . <EOL> format ( target_id , "<STR_LIT:.>" , name , value ) ) <EOL> else : <EOL> raise Py4JError ( <EOL> "<STR_LIT>" . <EOL> format ( target_id , "<STR_LIT:.>" , name ) ) <EOL> else : <EOL> type = answer [ <NUM_LIT:1> ] <EOL> if type == VOID_TYPE : <EOL> return <EOL> else : <EOL> return OUTPUT_CONVERTER [ type ] ( answer [ <NUM_LIT:2> : ] , gateway_client ) <EOL> def is_error ( answer ) : <EOL> if len ( answer ) == <NUM_LIT:0> or answer [ <NUM_LIT:0> ] != SUCCESS : <EOL> return ( True , None ) <EOL> else : <EOL> return ( False , None ) <EOL> def register_output_converter ( output_type , converter ) : <EOL> """<STR_LIT>""" <EOL> global OUTPUT_CONVERTER <EOL> OUTPUT_CONVERTER [ output_type ] = converter <EOL> def register_input_converter ( converter , prepend = False ) : <EOL> """<STR_LIT>""" <EOL> global INPUT_CONVERTER <EOL> if prepend : <EOL> INPUT_CONVERTER . insert ( <NUM_LIT:0> , converter ) <EOL> else : <EOL> INPUT_CONVERTER . append ( converter ) <EOL> class Py4JError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args = None , cause = None ) : <EOL> super ( Py4JError , self ) . __init__ ( args ) <EOL> self . cause = cause <EOL> class Py4JNetworkError ( Py4JError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Py4JJavaError ( Py4JError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg , java_exception ) : <EOL> self . args = ( msg , java_exception ) <EOL> self . errmsg = msg <EOL> self . java_exception = java_exception <EOL> self . exception_cmd = EXCEPTION_COMMAND_NAME + REFERENCE_TYPE + java_exception . _target_id + "<STR_LIT:\n>" + END_COMMAND_PART <EOL> def __str__ ( self ) : <EOL> gateway_client = self . java_exception . _gateway_client <EOL> answer = gateway_client . send_command ( self . exception_cmd ) <EOL> return_value = get_return_value ( answer , gateway_client , None , None ) <EOL> return "<STR_LIT>" . format ( self . errmsg , return_value ) </s>
