Beispiel #1
0
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)
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
def _escape(estr) -> str:
    s = unpack_ailobj(estr)

    if not isinstance(s, str):
        return AILRuntimeError('escape() needs a string.', 'TypeError')

    return eval('"""%s"""' % s)
Beispiel #5
0
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)
Beispiel #6
0
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)
Beispiel #7
0
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
Beispiel #8
0
def _mode_to_flag(self, mode_str: str) -> int:
    flags = 0

    rwa = False
    plus = False

    for s in mode_str:
        if s == 'x':
            if rwa:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_created = True
            self._file_writeable = True
            flags |= os.O_EXCL | os.O_CREAT
        elif s == 'r':
            if rwa:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_readable = True
        elif s == 'w':
            if rwa:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_writeable = True
            flags |= os.O_TRUNC | os.O_CREAT
        elif s == 'a':
            if rwa:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_readable = True
            self._file_appending = True
            flags |= os.O_CREAT | os.O_APPEND
        elif s == '+':
            if plus:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_readable = True
            self._file_writeable = True
            plus = 1
        elif s == 'b':
            pass
        else:
            return AILRuntimeError('invaild mode %s' % mode_str, 'ValueError')

    if not rwa:
        return _ERROR_MODE_AGAIN

    if self._file_readable and self._file_writeable:
        flags |= os.O_RDWR
    elif self._file_readable:
        flags |= os.O_RDONLY
    else:
        flags |= os.O_WRONLY

    return flags
Beispiel #9
0
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')
Beispiel #10
0
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()
Beispiel #11
0
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))
Beispiel #12
0
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)))
Beispiel #13
0
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)
Beispiel #14
0
import os

from ail.core.aobjects import convert_to_ail_object, unpack_ailobj
from ail.core.error import AILRuntimeError

from ail.objects.class_object import (
    new_class, new_object,
    object_getattr_with_default,
    object_setattr,
)


_ERROR_CLOSED = AILRuntimeError('file closed', 'OSError')
_ERROR_MODE_AGAIN = AILRuntimeError('Must have exactly one of create/read/write/append'
                                    'mode and at most one plus', 'ValueError')


def _mode_to_flag(self, mode_str: str) -> int:
    flags = 0

    rwa = False
    plus = False

    for s in mode_str:
        if s == 'x':
            if rwa:
                return _ERROR_MODE_AGAIN
            rwa = True
            self._file_created = True
            self._file_writeable = True
Beispiel #15
0
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)