exceptions.c File Reference

Definitions and native method bindings for error types. More...

#include <string.h>
#include <kuroko/vm.h>
#include <kuroko/value.h>
#include <kuroko/memory.h>
#include <kuroko/util.h>
#include "private.h"
#include "opcode_enum.h"
Include dependency graph for exceptions.c:

Go to the source code of this file.

Macros

#define ADD_EXCEPTION_CLASS(obj, name, baseClass)
 
#define IS_BaseException(o)   (likely(krk_isInstanceOf(o,vm.exceptions->baseException)))
 
#define AS_BaseException(o)   (AS_INSTANCE(o))
 
#define IS_KeyError(o)   (likely(krk_isInstanceOf(o,vm.exceptions->keyError)))
 
#define AS_KeyError(o)   (AS_INSTANCE(o))
 
#define IS_SyntaxError(o)   (likely(krk_isInstanceOf(o,vm.exceptions->syntaxError)))
 
#define AS_SyntaxError(o)   (AS_INSTANCE(o))
 
#define CURRENT_CTYPE   KrkInstance*
 
#define CURRENT_NAME   self
 

Functions

KrkValue _BaseException___init__ (int, KrkValue *, int)
 Initialize an exception object. More...
 
KrkValue _BaseException___repr__ (int, KrkValue *, int)
 Create a string representation of an BaseException. More...
 
KrkValue _BaseException___str__ (int, KrkValue *, int)
 Obtain a descriptive string from an exception. More...
 
KrkValue _KeyError___str__ (int, KrkValue *, int)
 
KrkValue _SyntaxError___str__ (int, KrkValue *, int)
 Generate printable text for a syntax error. More...
 
_noexport void _createAndBind_exceptions (void)
 Bind native methods and classes for exceptions. More...
 
void krk_dumpTraceback (void)
 If there is an active exception, print a traceback to stderr. More...
 
void krk_attachInnerException (KrkValue innerException)
 Attach an inner exception to the current exception object. More...
 
void krk_raiseException (KrkValue base, KrkValue cause)
 Raise an exception value. More...
 
KrkValue krk_runtimeError (KrkClass *type, const char *fmt,...)
 Produce and raise an exception with a formatted message. More...
 

Detailed Description

Definitions and native method bindings for error types.

Definition in file exceptions.c.

Macro Definition Documentation

◆ ADD_EXCEPTION_CLASS

#define ADD_EXCEPTION_CLASS (   obj,
  name,
  baseClass 
)
Value:
KrkClass * name; do { \
ADD_BASE_CLASS(obj,#name,baseClass); \
krk_finalizeClass(obj); \
name = obj; \
(void)name; \
} while (0)
Type object.
Definition: object.h:205

Convenience macro for creating exception types.

Definition at line 19 of file exceptions.c.

Function Documentation

◆ _BaseException___init__()

KrkValue _BaseException___init__ ( int  ,
KrkValue ,
int   
)

Initialize an exception object.

Native binding for BaseException.__init__

Parameters
argOptional string to attach to the exception object.

Definition at line 42 of file exceptions.c.

◆ _BaseException___repr__()

KrkValue _BaseException___repr__ ( int  ,
KrkValue ,
int   
)

Create a string representation of an BaseException.

Native binding for BaseException.__repr__

Generates a string representation of the form "BaseException(arg)" .

Definition at line 58 of file exceptions.c.

◆ _BaseException___str__()

KrkValue _BaseException___str__ ( int  ,
KrkValue ,
int   
)

Obtain a descriptive string from an exception.

Native binding for BaseException.__str__

For most exceptions, this is the 'arg' value attached at initialization and is printed during a traceback after the name of the exception type.

Definition at line 85 of file exceptions.c.

◆ _createAndBind_exceptions()

_noexport void _createAndBind_exceptions ( void  )

Bind native methods and classes for exceptions.

Called on VM initialization to create the base classes for exception types and bind the native methods for exception objects.

Definition at line 184 of file exceptions.c.

◆ _SyntaxError___str__()

KrkValue _SyntaxError___str__ ( int  ,
KrkValue ,
int   
)

Generate printable text for a syntax error.

Native binding for SyntaxError.__str__

Syntax errors are handled specially by the traceback generator so that they can print the original source line containing the erroneous input, so instead of printing {BaseException.__class__.__name__}: {str(BaseException)} we just print {str(BaseException)} for syntax errors and they handle the rest. This is a bit of a kludge, but it works for now.

Definition at line 125 of file exceptions.c.

◆ krk_attachInnerException()

void krk_attachInnerException ( KrkValue  innerException)

Attach an inner exception to the current exception object.

Sets the __context__ of the current exception object.

There must be a current exception, and it must be an instance object.

Parameters
innerException__context__ to set.

Definition at line 423 of file exceptions.c.

◆ krk_dumpTraceback()

void krk_dumpTraceback ( void  )

If there is an active exception, print a traceback to stderr.

Display a traceback by scanning up the stack / call frames. The format of the output here is modeled after the output given by CPython, so we display the outermost call first and then move inwards; on each call frame we try to open the source file and print the corresponding line.

Definition at line 366 of file exceptions.c.

◆ krk_raiseException()

void krk_raiseException ( KrkValue  base,
KrkValue  cause 
)

Raise an exception value.

Implementation of the OP_RAISE and OP_RAISE_FROM instructions.

If either of base or cause is a class, the class will be called to produce an instance, so exception classes may be used directly if desired.

If cause is not None it will be attached as __cause__ to the resulting exception object.

A traceback is automatically attached.

Parameters
baseException object or class to raise.
causeException cause object or class to attach.

Definition at line 435 of file exceptions.c.

◆ krk_runtimeError()

KrkValue krk_runtimeError ( KrkClass type,
const char *  fmt,
  ... 
)

Produce and raise an exception with a formatted message.

Raise an exception. Creates an exception object of the requested type and formats a message string to attach to it. Exception classes are found in vm.exceptions and are initialized on startup.

Definition at line 460 of file exceptions.c.