def _fileio___init__(self, file_path, mode, buf_size=1024): path = unpack_ailobj(file_path) if not isinstance(path, str): return AILRuntimeError('\'path\' must be string', 'TypeError') if not os.access(path, 0): return AILRuntimeError( 'No such file or directory: \'%s\'' % path, 'FileNotFoundError') mode_str = unpack_ailobj(mode) if not isinstance(mode_str, str): return AILRuntimeError('\'mode\' must be string', 'TypeError') bufsize = unpack_ailobj(buf_size) if not isinstance(buf_size, int): return AILRuntimeError('\'bufferSize\' must be integer', 'TypeError') object_setattr(self, 'path', file_path) object_setattr(self, 'mode', mode) object_setattr(self, 'bufferSize', buf_size) self._file_writeable = False self._file_created = False self._file_appending = False self._file_readable = False flags = _mode_to_flag(self, mode_str) if not isinstance(flags, int): assert isinstance(flags, AILRuntimeError) return flags self._file_flags = flags fd = os.open(path, flags, bufsize) self._file_object_fd = fd object_setattr(self, 'readable', convert_to_ail_object(self._file_readable)) object_setattr(self, 'writeable', convert_to_ail_object(self._file_writeable)) object_setattr(self, 'created', convert_to_ail_object(self._file_created)) object_setattr(self, 'appending', convert_to_ail_object(self._file_appending)) object_setattr(self, 'closed', convert_to_ail_object(False))
def _get_special_methods(_type: AILObjectType, wrapper=None, convert=True): methods = dict() for m in _SPECIAL_METHODS: if m in _type.required: _req = _type.required[m] if wrapper is not None: _req = wrapper(_req) if convert: _req = convert_to_ail_object(_req) methods[m] = _req return methods
def _fileio_close(self): """ close() close a file. """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED os.close(fd) self._file_object_fd = -1 object_setattr(self, 'closed', convert_to_ail_object(True))
def _version_new_version(): return new_object( _CLASS_VERSION, convert_to_ail_object(AIL_MAIN_VERSION), convert_to_ail_object(AIL_SUB_VERSION), convert_to_ail_object(AIL_VERSION_STATE),)
return 'AILVersion(%s, %s, %s)' % (repr(main_v), repr(sub_v), repr(v_state)) def _class_version___init__(self, main_v, sub_v, v_state): object_setattr(self, '_main_version', main_v) object_setattr(self, '_sub_version', sub_v) object_setattr(self, '_version_state', v_state) _CLASS_VERSION = new_class( '_AILVersion', [], { '__init__': convert_to_ail_object(_class_version___init__), '__str__': convert_to_ail_object(_class_version___str__), '__repr__': convert_to_ail_object(_class_version___repr__), } ) def _version_new_version(): return new_object( _CLASS_VERSION, convert_to_ail_object(AIL_MAIN_VERSION), convert_to_ail_object(AIL_SUB_VERSION), convert_to_ail_object(AIL_VERSION_STATE),) _IS_AIL_MODULE_ = True
close a file. """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED os.close(fd) self._file_object_fd = -1 object_setattr(self, 'closed', convert_to_ail_object(True)) CLASS_FILEIO = new_class( '_FileIO', [], { '__init__': convert_to_ail_object(_fileio___init__), 'readAll': convert_to_ail_object(_fileio_readall), 'read': convert_to_ail_object(_fileio_read), 'write': convert_to_ail_object(_fileio_write), 'seek': convert_to_ail_object(_fileio_seek), 'close': convert_to_ail_object(_fileio_close), } ) _IS_AIL_MODULE_ = True _AIL_NAMESPACE_ = { 'FileIO': CLASS_FILEIO, 'SEEK_SET': convert_to_ail_object(os.SEEK_SET), 'SEEK_CUR': convert_to_ail_object(os.SEEK_CUR), 'SEEK_END': convert_to_ail_object(os.SEEK_END),
from ail.core.aobjects import unpack_ailobj, convert_to_ail_object from ail.core.error import AILRuntimeError _IS_AIL_MODULE_ = True def _printf(x, *format): xs = unpack_ailobj(x) fs = tuple([unpack_ailobj(x) for x in format]) print(xs % fs, end='') _AIL_NAMESPACE_ = { 'printf': convert_to_ail_object(_printf) }
from ail.core.modules._error import make_err_struct_object, get_err_struct from ail.core.aobjects import AILObject, unpack_ailobj, convert_to_ail_object from ail.core.astate import MAIN_INTERPRETER_STATE from ail.core.error import AILRuntimeError def error(err_type: AILObject, err_msg: AILObject) -> AILObject: """ error(err_type: string, err_msg: string) -> Error @returns an Error object which can throw to AIL Runtime """ err_type = unpack_ailobj(err_type) err_msg = unpack_ailobj(err_msg) if not isinstance(err_type, str) or not isinstance(err_msg, str): return AILRuntimeError('\'err_type\' and \'err_msg\' must be string', 'TypeError') return MAIN_INTERPRETER_STATE.global_interpreter.make_runtime_error_obj( err_msg, err_type) _IS_AIL_MODULE_ = True _AIL_NAMESPACE_ = { 'error': convert_to_ail_object(error), 'Error': get_err_struct(), }
def _map_get(this, k, default=None): this = convert_to_pyobj(this) k_val = unpack_ailobj(k) try: v = this.__this___base_map.get(k_val, default) except TypeError: return AILRuntimeError(str(k['__class__']), 'UnhashableError') return v def _new_map(): new_dict = map_obj_dict.copy() new_dict['__base_map'] = dict() return new_struct_object('hash_map', null, new_dict, map_obj_dict.keys()) map_obj_dict = { '__base_map': None, 'get': convert_to_ail_object(_map_get), 'put': convert_to_ail_object(_map_put), } _IS_AIL_MODULE_ = True _AIL_NAMESPACE_ = {'map': _new_map}
def inspect_see_function(ailfunc): if not compare_type(ailfunc, PY_FUNCTION_TYPE): return AILRuntimeError('py_function_wrapper required') pyfunc: FunctionType = ailfunc['__pyfunction__'] return _new_pyfunc_info(pyfunc) def _new_pyfunc_info(func: FunctionType): name = func.__name__ addr = id(func) _PYFUNC_INFO_STRUCT_DICT['name'] = name _PYFUNC_INFO_STRUCT_DICT['addr'] = addr return new_struct_object('<PYFUNC_INFO>', None, _PYFUNC_INFO_STRUCT_DICT, _PYFUNC_INFO_STRUCT_DICT.keys()) _PYFUNC_INFO_STRUCT_DICT = { 'name': None, 'addr': None, } _IS_AIL_MODULE_ = True _AIL_NAMESPACE_ = { 'see_pyfunction': convert_to_ail_object(inspect_see_function) }
@wraps(func) def integer_method_wrapper(self, *args): val = self.members['__value'] return func(val, *args) return integer_method_wrapper def _integer_init(self, value): real_int = ObjectCreater.new_object(integer.INTEGER_TYPE, value) self.members['__value'] = real_int _INTEGER_STRUCT_MEMBER = ['__value'] _INTEGER_STRUCT_METHOD = { n: convert_to_ail_object(_integer_wrapper(m)) for n, m in integer.INTEGER_TYPE.methods.items() } _INTEGER_STRUCT_METHOD.update( _get_special_methods(integer.INTEGER_TYPE, _integer_wrapper)) _INTEGER_STRUCT_METHOD['__init__'] = convert_to_ail_object(_integer_init) def get_integer_struct(): i = struct.new_struct('Integer', _INTEGER_STRUCT_MEMBER, _INTEGER_STRUCT_MEMBER, _INTEGER_STRUCT_METHOD) return i _IS_AIL_MODULE_ = True _AIL_NAMESPACE_ = {
_IS_AIL_MODULE_ = True def _random_random(seed=None): if seed is None: seed = time.time() seed = unpack_ailobj(seed) if type(seed) not in (int, float): return AILRuntimeError('seed must be integer or float' 'TypeError') random.seed(seed) return random.random() def _random_randint(a, b): a = unpack_ailobj(a) b = unpack_ailobj(b) if not isinstance(a, int) or not isinstance(b, int): return AILRuntimeError('a, b must be an integer', 'TypeError') return random.randint(a, b) _AIL_NAMESPACE_ = { 'random': convert_to_ail_object(_random_random), 'randint': convert_to_ail_object(_random_randint) }
from ail.core.aobjects import unpack_ailobj, convert_to_ail_object from ail.core.error import AILRuntimeError _IS_AIL_MODULE_ = True def _escape(estr) -> str: s = unpack_ailobj(estr) if not isinstance(s, str): return AILRuntimeError('escape() needs a string.', 'TypeError') return eval('"""%s"""' % s) _AIL_NAMESPACE_ = {'escape': convert_to_ail_object(_escape)}
# A info of a movie NEZHA from ail.core.aobjects import convert_to_ail_object from ail.objects.struct import new_struct_object from ail.objects.null import null _IS_AIL_MODULE_ = True _aobing_info = { 'name': convert_to_ail_object('AoBing'), 'age': convert_to_ail_object('3'), 'location': convert_to_ail_object('龙宫') } _nezha_info = { 'name': convert_to_ail_object('Nezha'), 'age': convert_to_ail_object(3), 'friend': convert_to_ail_object( new_struct_object('Aobing', null, _aobing_info, _aobing_info.keys()), ), 'location': convert_to_ail_object('陈塘关') } _AIL_NAMESPACE_ = { 'NAME': 'Nezha', 'DATE': '2019/7/26', 'BOX_OFFICE_IN_CHINA': '50.13亿',