Exemplo n.º 1
0
def func(interp, s_frame, argument_count):
    # argument_count does NOT include the receiver.
    # This means that for argument_count == 3 the stack looks like:
    #  3      2       1      Top
    #  Rcvr | Arg 0 | Arg1 | Arg 2
    #
    # Validate that we have a block on the stack and that it received
    # the proper number of arguments:
    w_block_ctx = s_frame.peek(argument_count)

    # XXX need to check this since VALUE is called on all sorts of objects.
    if not w_block_ctx.getclass(interp.space).is_same_object(
            interp.space.w_BlockContext):
        raise PrimitiveFailedError()

    w_block_ctx = assert_pointers(w_block_ctx)
    s_block_ctx = w_block_ctx.as_context_get_shadow(interp.space)

    exp_arg_cnt = s_block_ctx.expected_argument_count()
    if argument_count != exp_arg_cnt:  # exp_arg_cnt doesn't count self
        raise PrimitiveFailedError()

    # Initialize the block stack with the arguments that were
    # pushed.  Also pop the receiver.
    block_args = s_frame.pop_and_return_n(exp_arg_cnt)

    # Reset stack of blockcontext to []
    s_block_ctx.reset_stack()
    s_block_ctx.push_all(block_args)

    s_frame.pop()
    s_block_ctx.reset_pc()
    return s_block_ctx
Exemplo n.º 2
0
def primitiveSQLNextObject(interp, s_frame, w_rcvr, cursor_handle):
    if CConfig is None:
        raise PrimitiveFailedError('sqpyte not found')
    space = interp.space
    cursor = dbm.cursor(cursor_handle)
    row = cursor.raw_next(space)
    if not row:
        return space.w_nil
    w_id = None
    num_cols = len(row)
    cache = [None] * num_cols
    for i in range(num_cols):
        name = cursor.column_names[i]
        if name == 'id':
            w_id = row[i]
        else:
            n0 = int(name[9:])  # strip 'inst_var_'
            class_name = w_rcvr.classname(interp.space).split(' ')[0]
            if W_DBObject.state.get_column_type(class_name, n0) is BLOB:
                db_id = space.unwrap_int(row[i])
                cache[n0] = W_DBObject.state.db_objects[db_id]
            else:
                cache[n0] = row[i]
    if w_id is None:
        raise PrimitiveFailedError('Could not find w_id')
    return W_DBObject(space, w_rcvr, num_cols, w_id=w_id, cache=cache)
Exemplo n.º 3
0
def func(interp, s_frame, w_rcvr, args_w, w_cm):
    if not isinstance(w_cm, W_CompiledMethod):
        raise PrimitiveFailedError()
    code = w_cm.primitive()
    if code:
        raise PrimitiveFailedError("withArgs:executeMethod: not support with primitive method")
    return w_cm.create_frame(interp.space, w_rcvr, args_w)
Exemplo n.º 4
0
    def execute(self, space, sql, args=None):
        if self.column_count > 0:
            raise PrimitiveFailedError(
                'execute() cannot be called twice on same cursor')
            # otherwise we can't assume that column_{count,names} are immutable

        jit.promote(self.connection)
        jit.promote(self.statement)
        cache = self.connection.statement_cache
        self.statement = cache.get_or_make(sql)
        query = self.statement.query

        if args is not None:
            if len(args) != query.bind_parameter_count():
                raise PrimitiveFailedError('wrong # of arguments for query')
            for i, w_value in enumerate(args):
                self.bind_query_argument(space, w_value, query, i + 1)

        self._step()

        self.column_count = query.data_count()
        self.column_names = [rffi.charp2strn(query.column_name(i), 255)
                             for i in range(self.column_count)]

        return self
Exemplo n.º 5
0
def func(interp, s_frame, w_cls, size):
    w_cls = assert_pointers(w_cls)
    s_class = w_cls.as_class_get_shadow(interp.space)
    if not s_class.isvariable() and size != 0:
        raise PrimitiveFailedError()
    if size < 0:
        raise PrimitiveFailedError()
    try:
        return s_class.new(size)
    except MemoryError:
        raise PrimitiveFailedError
Exemplo n.º 6
0
def func(interp, s_frame, w_class):
    # This primitive returns some instance of the class on the stack.
    # If no class is given, it returns some object.
    if w_class.is_same_object(interp.space.w_SmallInteger):
        raise PrimitiveFailedError()

    match_w = get_instances_array(interp,
                                  s_frame,
                                  w_class=w_class,
                                  some_instance=True)
    try:
        return match_w[0]
    except IndexError:
        raise PrimitiveFailedError()
Exemplo n.º 7
0
def func(interp, s_frame, w_rcvr, w_arg):
    w_arg_class = w_arg.getclass(interp.space)
    w_rcvr_class = w_rcvr.getclass(interp.space)

    # We should fail if:

    # 1. Rcvr or arg are SmallIntegers
    if (w_arg_class.is_same_object(interp.space.w_SmallInteger) or
        w_rcvr_class.is_same_object(interp.space.w_SmallInteger)):
        raise PrimitiveFailedError()

    # 2. Rcvr is an instance of a compact class and argument isn't
    # or vice versa XXX we don't have to fail here, but for squeak it's a problem

    # 3. Format of rcvr is different from format of argument
    if ((isinstance(w_arg, W_PointersObject) and
         isinstance(w_rcvr, W_PointersObject)) or
        (isinstance(w_arg, W_BytesObject) and
         isinstance(w_rcvr, W_BytesObject)) or
        (isinstance(w_arg, W_WordsObject) and
         isinstance(w_rcvr, W_WordsObject))):
        w_rcvr.change_class(interp.space, w_arg_class)
        return w_rcvr
    elif isinstance(w_rcvr, W_WordsObject) and isinstance(w_arg, W_BytesObject):
        wordsize = constants.BYTES_PER_WORD
        w_rcvr.convert_to_bytes_layout(wordsize)
        w_rcvr.change_class(interp.space, w_arg_class)
        return w_rcvr
    else:
        # TODO: this should also work to change bytes to words and such
        raise PrimitiveNotYetWrittenError
Exemplo n.º 8
0
 def _fetch_one_row(self, space):
     query = jit.promote(self.statement).query
     num_cols = query.data_count()
     jit.promote(num_cols)
     cols = [None] * num_cols
     for i in range(num_cols):
         tid = query.column_type(i)
         if tid == CConfig.SQLITE_TEXT or tid == CConfig.SQLITE_BLOB:
             textlen = query.column_bytes(i)
             result = rffi.charpsize2str(rffi.cast(rffi.CCHARP,
                                                   query.column_text(i)),
                                         textlen)
             w_result = space.wrap_string(result)  # no encoding
         elif tid == CConfig.SQLITE_INTEGER:
             result = query.column_int64(i)
             w_result = space.wrap_int(result)
         elif tid == CConfig.SQLITE_FLOAT:
             result = query.column_double(i)
             w_result = space.wrap_float(result)
         elif tid == CConfig.SQLITE_NULL:
             w_result = space.w_nil
         else:
             raise PrimitiveFailedError('read_row [tid: %s' % tid)
         cols[i] = w_result
     return cols
Exemplo n.º 9
0
def primitiveSQLColumnName(interp, s_frame, w_rcvr, cursor_handle, index):
    if index < 1:
        raise PrimitiveFailedError('Index must be >= 1')

    # Smalltalk counts from 1, rest of world from 0
    return interp.space.wrap_string(
        dbm.cursor(cursor_handle).column_names[index - 1])
Exemplo n.º 10
0
def func(interp, s_frame, w_rcvr):
    if not isinstance(w_rcvr, W_CompiledMethod):
        raise PrimitiveFailedError()
    w_class = w_rcvr.compiled_in()
    if w_class:
        w_class = assert_pointers(w_class)
        w_class.as_class_get_shadow(interp.space).flush_method_caches()
    return w_rcvr
Exemplo n.º 11
0
def primitiveSQLConnect(interp, s_frame, w_rcvr, filename, sqpyte):
    if interpreter is None:
        raise PrimitiveFailedError('sqpyte not found')
    if sqpyte:
        db_handle = dbm.connect(interpreter.SQPyteDB, filename)
    else:
        db_handle = dbm.connect(interpreter.SQLite3DB, filename)
    return interp.space.wrap_int(db_handle)
Exemplo n.º 12
0
def func(interp, s_frame, w_obj, n0, w_val):
    val = interp.space.unwrap_char_as_byte(w_val)
    n0 = assert_valid_index(interp.space, n0, w_obj)
    if not (isinstance(w_obj, W_CompiledMethod) or isinstance(
            w_obj, W_BytesObject) or isinstance(w_obj, W_WordsObject)):
        raise PrimitiveFailedError()
    w_obj.setchar(n0, val)
    return w_val
Exemplo n.º 13
0
 def __init__(self, w_connection, sql):
     assert isinstance(w_connection, SQLConnection)
     self.w_connection = w_connection
     self.sql = sql
     try:
         self.query = w_connection.db.execute(sql)
     except Exception as e:
         raise PrimitiveFailedError(str(e))
Exemplo n.º 14
0
 def _step(self):
     rc = self.statement.query.mainloop()
     if rc == CConfig.SQLITE_ROW:
         pass
     elif rc == CConfig.SQLITE_DONE:
         self._reset()
     else:
         raise PrimitiveFailedError('strange result: %s' % rc)
Exemplo n.º 15
0
def func(interp, s_frame, argcount):
    if interp.space.headless.is_set():
        w_message = None
        if s_frame.w_method().lookup_selector == 'doesNotUnderstand:':
            w_arguments = s_frame.w_arguments()
            if len(w_arguments) >= 1:
                w_message = w_arguments[0]
        s_frame.exitFromHeadlessExecution(w_message=w_message)
    raise PrimitiveFailedError()
Exemplo n.º 16
0
def primitiveSQLModeSwitch(interp, s_frame, w_rcvr, mode):
    if interpreter is None:
        raise PrimitiveFailedError('sqpyte not found')
    if mode == 1:
        dbm.driver = interpreter.SQLite3DB
    elif mode == 2:
        dbm.driver = interpreter.SQPyteDB
    else:
        dbm.driver = None
    return interp.space.w_nil
Exemplo n.º 17
0
 def connection(self):
     if self.db_connection is not None:
         return self.db_connection
     if self.driver is None:
         raise PrimitiveFailedError('connection [driver is None]')
     print "DBMode: %s" % self.driver
     connection = SQLConnection(self.driver, self.db_file_name)
     assert connection is not None
     self.db_connection = connection
     return connection
Exemplo n.º 18
0
def func(interp, s_frame, receiver, argument):
    guard_nonnull(argument)
    if isinstance(receiver, rbigint):
        if receiver.mod(argument) != NULLRBIGINT:
            raise PrimitiveFailedError
        return interp.space.wrap_rbigint(receiver.div(argument))
    else:
        if int_ovfcheck_mod(receiver, argument) != 0:
            raise PrimitiveFailedError()
        return interp.space.wrap_int(int_ovfcheck_div(receiver, argument))
Exemplo n.º 19
0
def activateClosure(interp, w_block, args_w):
    space = interp.space
    if not isinstance(w_block, W_BlockClosure):
        raise PrimitiveFailedError
    blockNumArgs = w_block.numArgs()
    if not blockNumArgs == len(args_w):
        raise PrimitiveFailedError()
    # additionally to the smalltalk implementation, this also pushes
    # args and copiedValues
    s_new_frame = w_block.create_frame(space, arguments=args_w)
    return s_new_frame
Exemplo n.º 20
0
def _become(space, w_rcvr, w_new):
    w_lefts = []
    w_rights = []
    for i in range(w_rcvr.size()):
        w_left = w_rcvr.at0(space, i)
        w_right = w_new.at0(space, i)
        if w_left.become(w_right):
            w_lefts.append(w_left)
            w_rights.append(w_right)
        else:
            for i in range(len(w_lefts)):
                w_lefts[i].become(w_rights[i])
            raise PrimitiveFailedError()
Exemplo n.º 21
0
 def bind_query_argument(self, space, w_value, query, i):
     cls = w_value.getclass(space)
     if (cls.is_same_object(space.w_String)):
         query.bind_str(i, space.unwrap_string(w_value))
     elif cls.is_same_object(space.w_SmallInteger):
         query.bind_int64(i, space.unwrap_int(w_value))
     elif cls.is_same_object(space.w_Float):
         query.bind_double(i, space.unwrap_float(w_value))
     elif cls.is_same_object(space.w_nil):
         query.bind_null(i)
     else:
         raise PrimitiveFailedError(
             'unable to unwrap %s' % w_value.getclass(space))
Exemplo n.º 22
0
def next_instance(space, list_of_objects, w_obj):
    retval = None
    try:
        idx = list_of_objects.index(w_obj)
    except ValueError:
        idx = -1
    try:
        retval = list_of_objects[idx + 1]
    except IndexError:
        raise PrimitiveFailedError()
    # just in case, that one of the objects in the list changes its class
    if retval.getclass(space).is_same_object(w_obj.getclass(space)):
        return retval
    else:
        list_of_objects.pop(idx + 1)
        return next_instance(space, list_of_objects, w_obj)
Exemplo n.º 23
0
def func(interp, s_frame, w_block_ctx, args_w):

    w_block_ctx = assert_pointers(w_block_ctx)
    s_block_ctx = w_block_ctx.as_context_get_shadow(interp.space)
    exp_arg_cnt = s_block_ctx.expected_argument_count()

    if len(args_w) != exp_arg_cnt:
        raise PrimitiveFailedError()

    # Push all the items from the array
    for i in range(exp_arg_cnt):
        s_block_ctx.push(args_w[i])

    # XXX Check original logic. Image does not test this anyway
    # because falls back to value + internal implementation
    s_block_ctx.reset_pc()
    return s_block_ctx
Exemplo n.º 24
0
def primitiveSQLExecute(interp, s_frame, argcount):
    if not 2 <= argcount <= 3:
        raise PrimitiveFailedError(
            'wrong number of arguments: %s' % argcount)
    space = interp.space
    args = None
    if argcount == 3:
        args = interp.space.unwrap_array(s_frame.pop())

    arg3_w = s_frame.pop()
    sql = space.unwrap_string(arg3_w)
    arg2_w = s_frame.pop()
    db_handle = space.unwrap_int64(arg2_w)
    connection = dbm.get_connection(db_handle)
    cursor_handle = dbm.execute(interp.space, connection, sql, args)

    return space.wrap_int(cursor_handle)
Exemplo n.º 25
0
def primitiveFileOpen(interp, s_frame, w_rcvr, file_path, w_writeable_flag):
    space = interp.space
    file_missing = not os.path.exists(file_path)
    if w_writeable_flag is space.w_true:
        if file_missing:
            mode = os.O_RDWR | os.O_CREAT
        else:
            mode = os.O_RDWR
    else:
        mode = os.O_RDONLY
        if file_missing:
            return space.w_nil
    mode |= os.O_BINARY
    try:
        file_descriptor = os.open(file_path, mode, 0666)
    except OSError:
        raise PrimitiveFailedError()
    return space.wrap_int(file_descriptor)
Exemplo n.º 26
0
 def close(self, db_pointer):
     db = self._dbs.get(db_pointer, None)
     if db is None:
         raise PrimitiveFailedError('close [db is None]')
     return db.close()
Exemplo n.º 27
0
 def cursor(self, cursor_handle):
     cursor = self._cursors.get(cursor_handle, None)
     if cursor is None:
         raise PrimitiveFailedError('cursor not found')
     return cursor
Exemplo n.º 28
0
 def get_connection(self, db_handle):
     db = self._dbs.get(db_handle, None)
     if db is None:
         raise PrimitiveFailedError('execute [db is None]')
     return db
Exemplo n.º 29
0
def primitiveFileClose(interp, s_frame, w_rcvr, fd):
    try:
        os.close(fd)
    except OSError:
        raise PrimitiveFailedError()
    return w_rcvr
Exemplo n.º 30
0
def func(interp, s_frame, w_class):
    match_w = get_instances_array(interp, s_frame, some_instance=True)
    try:
        return match_w[0]
    except IndexError:
        raise PrimitiveFailedError()