def _fileio_seek(self, pos, whence=os.SEEK_SET): """ seek(pos [, whence=SEEK_SET]) -> integer move the cursor of a file. @param whence: SEEK_SET: cursor directly set to pos. SEEK_CUR: cursor will go forward 'pos' step from current position. SEEK_END: cursor will go forward 'posl' step from EOF, in this case, the 'pos' can less than 0. @returns the offset of cursor to the cursor after seek(). """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED pos = unpack_ailobj(pos) if not isinstance(pos, int): return AILRuntimeError('\'pos\' must be integer', 'TypeError') whence = unpack_ailobj(whence) if not isinstance(whence, int): return AILRuntimeError('\'whence\' must be integer', 'TypeError') if whence > 2 or whence < 0: return AILRuntimeError('invalid \'whence\' argument', 'TypeError') if pos < 0 and whence != os.SEEK_END: return AILRuntimeError( 'not support negative offset in this whence', 'TypeError') return os.lseek(fd, pos, whence)
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)
def func_range(a, b, step=1): if not (compare_type(a, INTEGER_TYPE) and compare_type(b, INTEGER_TYPE)) \ and compare_type(step, INTEGER_TYPE): return AILRuntimeError('a and b must be an integer!') va = unpack_ailobj(a) vb = unpack_ailobj(b) vs = unpack_ailobj(step) if va > vb: return convert_to_array([]) return convert_to_array(list(range(va, vb, vs)))
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)
def _fileio_read(self, n): """ read(n: integer) -> bytes read n bytes from a readable file, if reach the EOF or reach EOF while reading, the length of the bytes which it returns will less than n or equals 0. """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED if not self._file_readable: return AILRuntimeError('file is not readable', 'OSError') n = unpack_ailobj(n) if not isinstance(n, int): return AILRuntimeError('Invalid arguments', 'OSError') if n < 0: return _fileio_readall(self) b = os.read(fd, n) return b
def _fileio_readall(self, block_size=1024): """ readAll([blockSize=1024]) -> bytes read a readable file from current cursor to EOF. """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED if not self._file_readable: return AILRuntimeError('file is not readable', 'OSError') block_size = unpack_ailobj(block_size) if not isinstance(block_size, int): return AILRuntimeError( '\'blockSize\' must be integer', 'TypeError') b = os.read(fd, block_size) total_b = b while b: b = os.read(fd, block_size) total_b += b return total_b
def _escape(estr) -> str: s = unpack_ailobj(estr) if not isinstance(s, str): return AILRuntimeError('escape() needs a string.', 'TypeError') return eval('"""%s"""' % s)
def _class_version___str__(self): main_v = object_getattr_with_default(self, '_main_version') sub_v = object_getattr_with_default(self, '_sub_version') v_state = object_getattr_with_default(self, '_version_state') return '%s.%s %s' % ( main_v, '.'.join([str(x) for x in unpack_ailobj(sub_v)]), v_state)
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 _map_put(this, k, v): this = convert_to_pyobj(this) m = this.__this___base_map k_val = unpack_ailobj(k) try: m[k_val] = v except TypeError: return AILRuntimeError(str(k['__class__']), 'UnhashableError')
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 _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 _fileio_write(self, b): """ write(data: bytes) -> integer write data to a writeable file. @returns the size of wrote data. """ fd = self._file_object_fd if fd < 0: return _ERROR_CLOSED if not self._file_writeable: return AILRuntimeError('file is not writeable', 'OSError') b = unpack_ailobj(b) if not isinstance(b, bytes): return AILRuntimeError('can only write bytes', 'TypeError') return os.write(fd, b)
def _printf(x, *format): xs = unpack_ailobj(x) fs = tuple([unpack_ailobj(x) for x in format]) print(xs % fs, end='')
def print_help(x: AILObject = None): if compare_type(x, STRUCT_TYPE): print('[name]') print(' %s' % x['__name__']) print('[functions]') for m in x['__bind_functions__']: print(' %s' % m) print('[protected]') for m in x.protected: if not m.startswith('_'): print(' %s' % m) print('[member]') for m in x.members.keys(): if not m.startswith('_'): print(' %s' % m) elif compare_type(x, STRUCT_OBJ_TYPE): if unpack_ailobj(x['__type__']) is not None: print('[type]') print(' %s' % x['__type__']['__name__']) print('[name]') print(' %s' % x['__name__']) print('[protected]') for m in x.protected: if not m.startswith('_'): print(' %s' % m) print('[member]') for m in x.members.keys(): if not m.startswith('_'): print(' %s' % m) elif compare_type(x, CLASS_TYPE): print('[name]') print(' %s' % x['__name__']) print('[bases]') print(' %s' % x['__bases__']) print('[mro]') print(' %s' % x['__mro__']) class_dict = x['__dict__'] class_var = [] class_methods = [] doc_string = x['__doc__'] for k, v in class_dict.items(): if compare_type(v, PY_FUNCTION_TYPE, FUNCTION_TYPE): if k[:1] == '_' and not k[:2] == k[-2:] == '__': class_methods.append(k) else: class_var.append(k) if class_var: print('[class var]') print('\n '.join(class_var)) if class_methods: print('[methods]') print('\n '.join(class_methods)) if doc_string: print('\n[doc]') print(doc_string) elif compare_type(x, MODULE_TYPE): print('[type]') print(' module') print('[name]') print(' %s' % x['__name__']) print('[path]') print(' %s' % x['__path__']) print('[namespace]') for m in x['__namespace__'].keys(): if not m.startswith('_'): print(' %s' % m) elif compare_type(x, FUNCTION_TYPE): print('[signature]') print(' %s' % x['__signature__']) doc_string = x['__doc__'] if doc_string: print('\n[doc]') print(doc_string) elif compare_type(x, PY_FUNCTION_TYPE): doc_string = x['__doc__'] if doc_string: print('\n[doc]') print(doc_string) elif isinstance(x, AILObject): methods = _DEFAULT_HELPER.list_methods(x) meta = _DEFAULT_HELPER.list_meta(x) print('[type]') print(' %s (AIL Built-in type)' % x['__class__'].name) if methods: print('[methods]') for m in methods.keys(): print(' %s' % m) if meta: print('[meta]') for m in meta.keys(): print(' %s' % m) else: print('no help info provided.')