<s> """<STR_LIT>""" <EOL> from __future__ import print_function , absolute_import <EOL> import inspect <EOL> from functools import wraps , partial <EOL> from llvmlite import ir <EOL> from numba import types , cgutils <EOL> from numba . pythonapi import box , unbox , NativeValue <EOL> from numba import njit <EOL> from numba . six import exec_ <EOL> from . import _box <EOL> _getter_code_template = """<STR_LIT>""" <EOL> _setter_code_template = """<STR_LIT>""" <EOL> _method_code_template = """<STR_LIT>""" <EOL> def _generate_property ( field , template , fname ) : <EOL> """<STR_LIT>""" <EOL> source = template . format ( field ) <EOL> glbls = { } <EOL> exec_ ( source , glbls ) <EOL> return njit ( glbls [ fname ] ) <EOL> _generate_getter = partial ( _generate_property , template = _getter_code_template , <EOL> fname = '<STR_LIT>' ) <EOL> _generate_setter = partial ( _generate_property , template = _setter_code_template , <EOL> fname = '<STR_LIT>' ) <EOL> def _generate_method ( name , func ) : <EOL> """<STR_LIT>""" <EOL> argspec = inspect . getargspec ( func ) <EOL> assert not argspec . varargs , '<STR_LIT>' <EOL> assert not argspec . keywords , '<STR_LIT>' <EOL> assert not argspec . defaults , '<STR_LIT>' <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( argspec . args [ <NUM_LIT:1> : ] ) <EOL> source = _method_code_template . format ( method = name , args = args ) <EOL> glbls = { } <EOL> exec_ ( source , glbls ) <EOL> method = njit ( glbls [ '<STR_LIT>' ] ) <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return method ( * args , ** kwargs ) <EOL> return wrapper <EOL> _cache_specialized_box = { } <EOL> def _specialize_box ( typ ) : <EOL> """<STR_LIT>""" <EOL> if typ in _cache_specialized_box : <EOL> return _cache_specialized_box [ typ ] <EOL> dct = { '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : typ } <EOL> for field in typ . struct : <EOL> if not field . startswith ( '<STR_LIT:_>' ) : <EOL> getter = _generate_getter ( field ) <EOL> setter = _generate_setter ( field ) <EOL> dct [ field ] = property ( getter , setter ) <EOL> for field , impdct in typ . jitprops . items ( ) : <EOL> if not field . startswith ( '<STR_LIT:_>' ) : <EOL> getter = None <EOL> setter = None <EOL> if '<STR_LIT>' in impdct : <EOL> getter = _generate_getter ( field ) <EOL> if '<STR_LIT>' in impdct : <EOL> setter = _generate_setter ( field ) <EOL> dct [ field ] = property ( getter , setter ) <EOL> for name , func in typ . methods . items ( ) : <EOL> if not name . startswith ( '<STR_LIT:_>' ) : <EOL> getter = _generate_method ( name , func ) <EOL> dct [ name ] = getter <EOL> subcls = type ( typ . classname , ( _box . Box , ) , dct ) <EOL> _cache_specialized_box [ typ ] = subcls <EOL> for k , v in dct . items ( ) : <EOL> if isinstance ( v , property ) : <EOL> prop = getattr ( subcls , k ) <EOL> if prop . fget is not None : <EOL> fget = prop . fget <EOL> fast_fget = fget . compile ( ( typ , ) ) <EOL> fget . disable_compile ( ) <EOL> setattr ( subcls , k , <EOL> property ( fast_fget , prop . fset , prop . fdel ) ) <EOL> return subcls <EOL> @ box ( types . ClassInstanceType ) <EOL> def _box_class_instance ( typ , val , c ) : <EOL> meminfo , dataptr = cgutils . unpack_tuple ( c . builder , val ) <EOL> box_subclassed = _specialize_box ( typ ) <EOL> int_addr_boxcls = c . context . get_constant ( types . uintp , id ( box_subclassed ) ) <EOL> box_cls = c . builder . inttoptr ( int_addr_boxcls , c . pyapi . pyobj ) <EOL> box = c . pyapi . call_function_objargs ( box_cls , ( ) ) <EOL> llvoidptr = ir . IntType ( <NUM_LIT:8> ) . as_pointer ( ) <EOL> addr_meminfo = c . builder . bitcast ( meminfo , llvoidptr ) <EOL> addr_data = c . builder . bitcast ( dataptr , llvoidptr ) <EOL> def set_member ( member_offset , value ) : <EOL> offset = c . context . get_constant ( types . uintp , member_offset ) <EOL> ptr = cgutils . pointer_add ( c . builder , box , offset ) <EOL> casted = c . builder . bitcast ( ptr , llvoidptr . as_pointer ( ) ) <EOL> c . builder . store ( value , casted ) <EOL> set_member ( _box . box_meminfoptr_offset , addr_meminfo ) <EOL> set_member ( _box . box_dataptr_offset , addr_data ) <EOL> return box <EOL> @ unbox ( types . ClassInstanceType ) <EOL> def _unbox_class_instance ( typ , val , c ) : <EOL> def access_member ( member_offset ) : <EOL> offset = c . context . get_constant ( types . uintp , member_offset ) <EOL> llvoidptr = ir . IntType ( <NUM_LIT:8> ) . as_pointer ( ) <EOL> ptr = cgutils . pointer_add ( c . builder , val , offset ) <EOL> casted = c . builder . bitcast ( ptr , llvoidptr . as_pointer ( ) ) <EOL> return c . builder . load ( casted ) <EOL> struct_cls = cgutils . create_struct_proxy ( typ ) <EOL> inst = struct_cls ( c . context , c . builder ) <EOL> ptr_meminfo = access_member ( _box . box_meminfoptr_offset ) <EOL> ptr_dataptr = access_member ( _box . box_dataptr_offset ) <EOL> inst . meminfo = c . builder . bitcast ( ptr_meminfo , inst . meminfo . type ) <EOL> inst . data = c . builder . bitcast ( ptr_dataptr , inst . data . type ) <EOL> ret = inst . _getvalue ( ) <EOL> c . context . nrt_incref ( c . builder , typ , ret ) <EOL> return NativeValue ( ret , is_error = c . pyapi . c_api_error ( ) ) </s>
