wings 0.2.1
An embeddable header-only Python interpreter for C/C++.
wings.h File Reference

The wings API. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  Wg_Config
 The configuration used to initialise an interpreter. More...
 

Typedefs

typedef struct Wg_Context Wg_Context
 An opaque type representing the state of an interpreter. More...
 
typedef struct Wg_Obj Wg_Obj
 An opaque type representing an object in the interpreter. More...
 
typedef int64_t Wg_int
 The underlying data type of an integer object. More...
 
typedef uint64_t Wg_uint
 The unsigned version of Wg_int. More...
 
typedef double Wg_float
 The underlying data type of a float object. More...
 
typedef Wg_Obj *(* Wg_Function) (Wg_Context *context, Wg_Obj **argv, int argc)
 The signature of a native function registered into the interpreter as a function object. More...
 
typedef void(* Wg_Finalizer) (void *userdata)
 The signature of an object finalizer. More...
 
typedef void(* Wg_PrintFunction) (const char *message, int len, void *userdata)
 The signature of the print function used by the interpreter for printing. More...
 
typedef void(* Wg_ErrorCallback) (const char *message)
 The signature of the fatal error callback. More...
 
typedef bool(* Wg_IterationCallback) (Wg_Obj *obj, void *userdata)
 The signature of an iteration callback used by Wg_Iterate(). More...
 
typedef bool(* Wg_ModuleLoader) (Wg_Context *context)
 The signature of a module loader. More...
 
typedef struct Wg_Config Wg_Config
 The configuration used to initialise an interpreter. More...
 
typedef enum Wg_UnOp Wg_UnOp
 The unary operation to be used Wg_UnaryOp. More...
 
typedef enum Wg_BinOp Wg_BinOp
 The binary operation to be used by Wg_BinaryOp. More...
 
typedef enum Wg_Exc Wg_Exc
 The type exception to be raised by Wg_RaiseException. More...
 

Enumerations

enum  Wg_UnOp {
  WG_UOP_POS , WG_UOP_NEG , WG_UOP_BITNOT , WG_UOP_NOT ,
  WG_UOP_HASH , WG_UOP_LEN , WG_UOP_BOOL , WG_UOP_INT ,
  WG_UOP_FLOAT , WG_UOP_STR , WG_UOP_REPR , WG_UOP_INDEX
}
 The unary operation to be used Wg_UnaryOp. More...
 
enum  Wg_BinOp {
  WG_BOP_ADD , WG_BOP_SUB , WG_BOP_MUL , WG_BOP_DIV ,
  WG_BOP_FLOORDIV , WG_BOP_MOD , WG_BOP_POW , WG_BOP_BITAND ,
  WG_BOP_BITOR , WG_BOP_BITXOR , WG_BOP_AND , WG_BOP_OR ,
  WG_BOP_SHL , WG_BOP_SHR , WG_BOP_IN , WG_BOP_NOTIN ,
  WG_BOP_EQ , WG_BOP_NE , WG_BOP_LT , WG_BOP_LE ,
  WG_BOP_GT , WG_BOP_GE
}
 The binary operation to be used by Wg_BinaryOp. More...
 
enum  Wg_Exc {
  WG_EXC_BASEEXCEPTION , WG_EXC_SYSTEMEXIT , WG_EXC_EXCEPTION , WG_EXC_STOPITERATION ,
  WG_EXC_ARITHMETICERROR , WG_EXC_OVERFLOWERROR , WG_EXC_ZERODIVISIONERROR , WG_EXC_ATTRIBUTEERROR ,
  WG_EXC_IMPORTERROR , WG_EXC_LOOKUPERROR , WG_EXC_INDEXERROR , WG_EXC_KEYERROR ,
  WG_EXC_MEMORYERROR , WG_EXC_NAMEERROR , WG_EXC_OSERROR , WG_EXC_ISADIRECTORYERROR ,
  WG_EXC_RUNTIMEERROR , WG_EXC_NOTIMPLEMENTEDERROR , WG_EXC_RECURSIONERROR , WG_EXC_SYNTAXERROR ,
  WG_EXC_TYPEERROR , WG_EXC_VALUEERROR
}
 The type exception to be raised by Wg_RaiseException. More...
 

Functions

Wg_ContextWg_CreateContext (const Wg_Config *config)
 Create an instance of an interpreter. More...
 
void Wg_DestroyContext (Wg_Context *context)
 Free a context created with Wg_CreateContext(). More...
 
void Wg_DefaultConfig (Wg_Config *config)
 Get the default configuration. More...
 
bool Wg_Execute (Wg_Context *context, const char *script, const char *prettyName)
 Execute a script. More...
 
Wg_ObjWg_ExecuteExpression (Wg_Context *context, const char *script, const char *prettyName)
 Execute a script containing an expression. More...
 
Wg_ObjWg_Compile (Wg_Context *context, const char *script, const char *prettyName)
 Compile a script into a function object. More...
 
Wg_ObjWg_CompileExpression (Wg_Context *context, const char *script, const char *prettyName)
 Compile an expression into a function object. More...
 
void Wg_SetErrorCallback (Wg_ErrorCallback callback)
 Set a callback for programmer errors. More...
 
const char * Wg_GetErrorMessage (Wg_Context *context)
 Get the current exception message. More...
 
Wg_ObjWg_GetException (Wg_Context *context)
 Get the current exception object. More...
 
void Wg_RaiseException (Wg_Context *context, Wg_Exc type, const char *message)
 Create and raise an exception. More...
 
void Wg_RaiseExceptionClass (Wg_Obj *klass, const char *message)
 Create and raise an exception using a class object. More...
 
void Wg_RaiseExceptionObject (Wg_Obj *obj)
 Raise an existing exception object. More...
 
void Wg_ReraiseExceptionObject (Wg_Obj *obj)
 Raise an existing exception object without affecting the stack trace. More...
 
void Wg_RaiseArgumentCountError (Wg_Context *context, int given, int expected)
 Raise a TypeError with a formatted message. More...
 
void Wg_RaiseArgumentTypeError (Wg_Context *context, int index, const char *expected)
 Raise a TypeError with a formatted message. More...
 
void Wg_RaiseAttributeError (const Wg_Obj *obj, const char *attribute)
 Raise a AttributeError with a formatted message. More...
 
void Wg_RaiseKeyError (Wg_Context *context, Wg_Obj *key)
 Raise a KeyError with a formatted message. More...
 
void Wg_RaiseNameError (Wg_Context *context, const char *name)
 Raise a NameError with a formatted message. More...
 
void Wg_ClearException (Wg_Context *context)
 Clear the current exception. More...
 
Wg_ContextWg_GetContextFromObject (Wg_Obj *obj)
 Get the context associated with an object. More...
 
Wg_ObjWg_IsInstance (const Wg_Obj *instance, Wg_Obj *const *types, int typesLen)
 Check if an object's class derives from any of the specified classes. More...
 
Wg_ObjWg_GetGlobal (Wg_Context *context, const char *name)
 Get a global variable in the current module namespace. More...
 
void Wg_SetGlobal (Wg_Context *context, const char *name, Wg_Obj *value)
 Set a global variable in the current module namespace. More...
 
void Wg_Print (const Wg_Context *context, const char *message, int len)
 Print a message. More...
 
void Wg_PrintString (const Wg_Context *context, const char *message)
 Print a string message. More...
 
void Wg_CollectGarbage (Wg_Context *context)
 Force run the garbage collector and free all unreachable objects. More...
 
void Wg_IncRef (Wg_Obj *obj)
 Increment the reference count of an object. A positive reference count prevents the object from being garbage collected. More...
 
void Wg_DecRef (Wg_Obj *obj)
 Decrement the reference count of an object. A positive reference count prevents the object from being garbage collected. More...
 
Wg_ObjWg_None (Wg_Context *context)
 Get the None singleton value. More...
 
Wg_ObjWg_NewBool (Wg_Context *context, bool value)
 Instantiate a boolean object. More...
 
Wg_ObjWg_NewInt (Wg_Context *context, Wg_int value)
 Instantiate an integer object. More...
 
Wg_ObjWg_NewFloat (Wg_Context *context, Wg_float value)
 Instantiate a float object. More...
 
Wg_ObjWg_NewString (Wg_Context *context, const char *value)
 Instantiate a string object. More...
 
Wg_ObjWg_NewStringBuffer (Wg_Context *context, const char *buffer, int len)
 Instantiate a string object from a buffer. More...
 
Wg_ObjWg_NewTuple (Wg_Context *context, Wg_Obj **argv, int argc)
 Instantiate a tuple object. More...
 
Wg_ObjWg_NewList (Wg_Context *context, Wg_Obj **argv, int argc)
 Instantiate a list object. More...
 
Wg_ObjWg_NewDictionary (Wg_Context *context, Wg_Obj **keys, Wg_Obj **values, int len)
 Instantiate a dictionary object. More...
 
Wg_ObjWg_NewSet (Wg_Context *context, Wg_Obj **argv, int argc)
 Instantiate a set object. More...
 
Wg_ObjWg_NewFunction (Wg_Context *context, Wg_Function fptr, void *userdata, const char *prettyName)
 Instantiate a function object. More...
 
Wg_ObjWg_BindMethod (Wg_Obj *klass, const char *name, Wg_Function fptr, void *userdata)
 Instantiate a function object and bind it to a class. More...
 
Wg_ObjWg_NewClass (Wg_Context *context, const char *name, Wg_Obj **bases, int basesLen)
 Instantiate a class object. More...
 
bool Wg_IsNone (const Wg_Obj *obj)
 Check if an object is None. More...
 
bool Wg_IsBool (const Wg_Obj *obj)
 Check if an object is a boolean. More...
 
bool Wg_IsInt (const Wg_Obj *obj)
 Check if an object is an integer. More...
 
bool Wg_IsIntOrFloat (const Wg_Obj *obj)
 Check if an object is a float. More...
 
bool Wg_IsString (const Wg_Obj *obj)
 Check if an object is a string. More...
 
bool Wg_IsTuple (const Wg_Obj *obj)
 Check if an object is a tuple. More...
 
bool Wg_IsList (const Wg_Obj *obj)
 Check if an object is a list. More...
 
bool Wg_IsDictionary (const Wg_Obj *obj)
 Check if an object is a dictionary. More...
 
bool Wg_IsSet (const Wg_Obj *obj)
 Check if an object is a set. More...
 
bool Wg_IsFunction (const Wg_Obj *obj)
 Check if an object is a function. More...
 
bool Wg_IsClass (const Wg_Obj *obj)
 Check if an object is a class. More...
 
bool Wg_GetBool (const Wg_Obj *obj)
 Get the value from a boolean object. More...
 
Wg_int Wg_GetInt (const Wg_Obj *obj)
 Get the value from an integer object. More...
 
Wg_float Wg_GetFloat (const Wg_Obj *obj)
 Get the float value from an integer or float object. More...
 
const char * Wg_GetString (const Wg_Obj *obj, int *len)
 Get the value from a string object. More...
 
void Wg_SetUserdata (Wg_Obj *obj, void *userdata)
 Set the userdata for an object. More...
 
bool Wg_TryGetUserdata (const Wg_Obj *obj, const char *type, void **userdata)
 Get the userdata from an object if it is of the expected type. More...
 
void Wg_RegisterFinalizer (Wg_Obj *obj, Wg_Finalizer finalizer, void *userdata)
 Register a finalizer to run when an object is garbage collected. More...
 
bool Wg_HasAttribute (Wg_Obj *obj, const char *attribute)
 Check if an object has an attribute. More...
 
Wg_ObjWg_GetAttribute (Wg_Obj *obj, const char *attribute)
 Get an attribute of an object. If the attribute does not exist, an AttributeError is raised. If the attribute is an unbound method object, a new method object is allocated with obj bound. If this allocation fails, a MemoryError is raised. More...
 
Wg_ObjWg_GetAttributeNoExcept (Wg_Obj *obj, const char *attribute)
 Get an attribute of an object. Unlike, Wg_GetAttribute(), this function does not raise exceptions. More...
 
void Wg_SetAttribute (Wg_Obj *obj, const char *attribute, Wg_Obj *value)
 Set an attribute of an object. More...
 
Wg_ObjWg_GetAttributeFromBase (Wg_Obj *obj, const char *attribute, Wg_Obj *baseClass)
 Get an attribute of an object, skipping attributes that belong to the most derived layer. More...
 
bool Wg_Iterate (Wg_Obj *obj, void *userdata, Wg_IterationCallback callback)
 Iterate over an iterable object. More...
 
bool Wg_Unpack (Wg_Obj *obj, int count, Wg_Obj **values)
 Helper function to unpack an iterable object into an array of objects. More...
 
Wg_ObjWg_GetKwargs (Wg_Context *context)
 Get the keyword arguments dictionary passed to the current function. More...
 
void * Wg_GetFunctionUserdata (Wg_Context *context)
 Get the userdata associated with the current function. More...
 
Wg_ObjWg_Call (Wg_Obj *callable, Wg_Obj **argv, int argc, Wg_Obj *kwargs)
 Call a callable object. More...
 
Wg_ObjWg_CallMethod (Wg_Obj *obj, const char *method, Wg_Obj **argv, int argc, Wg_Obj *kwargs)
 Call a method on a object. More...
 
Wg_ObjWg_CallMethodFromBase (Wg_Obj *obj, const char *method, Wg_Obj **argv, int argc, Wg_Obj *kwargs, Wg_Obj *baseClass)
 Call a method on a object, skipping methods which belong to the most derived layer. More...
 
bool Wg_ParseKwargs (Wg_Obj *dict, const char *const *keys, int keysLen, Wg_Obj **values)
 Get the values from a kwargs parameter. More...
 
Wg_ObjWg_GetIndex (Wg_Obj *obj, Wg_Obj *index)
 Index an object. More...
 
Wg_ObjWg_SetIndex (Wg_Obj *obj, Wg_Obj *index, Wg_Obj *value)
 Set an index of an object. More...
 
Wg_ObjWg_UnaryOp (Wg_UnOp op, Wg_Obj *arg)
 Perform a unary operation. More...
 
Wg_ObjWg_BinaryOp (Wg_BinOp op, Wg_Obj *lhs, Wg_Obj *rhs)
 Perform a binary operation. More...
 
void Wg_RegisterModule (Wg_Context *context, const char *name, Wg_ModuleLoader loader)
 Register a callback to be called when a module with the given name is imported. In the callback, new members can be added to the module. More...
 
Wg_ObjWg_ImportModule (Wg_Context *context, const char *module, const char *alias)
 Import a module. More...
 
Wg_ObjWg_ImportFromModule (Wg_Context *context, const char *module, const char *name, const char *alias)
 Import a specific name from a module. More...
 
bool Wg_ImportAllFromModule (Wg_Context *context, const char *module)
 Import all names from a module. More...
 

Detailed Description

The wings API.

Typedef Documentation

◆ Wg_BinOp

typedef enum Wg_BinOp Wg_BinOp

The binary operation to be used by Wg_BinaryOp.

◆ Wg_Config

typedef struct Wg_Config Wg_Config

The configuration used to initialise an interpreter.

The configuration cannot be changed after the interpreter is initialised.

See also
Wg_CreateContext

◆ Wg_Context

typedef struct Wg_Context Wg_Context

An opaque type representing the state of an interpreter.

See also
Wg_CreateContext, Wg_DestroyContext

◆ Wg_ErrorCallback

typedef void(* Wg_ErrorCallback) (const char *message)

The signature of the fatal error callback.

Parameters
messageA null terminated string containing the error message.
See also
Wg_SetErrorCallback

◆ Wg_Exc

typedef enum Wg_Exc Wg_Exc

The type exception to be raised by Wg_RaiseException.

◆ Wg_Finalizer

typedef void(* Wg_Finalizer) (void *userdata)

The signature of an object finalizer.

Warning
Do not perform any object allocations in this function.
Parameters
userdataThe userdata specified when this callback was registered.
See also
Wg_RegisterFinalizer

◆ Wg_float

typedef double Wg_float

The underlying data type of a float object.

See also
Wg_NewFloat, Wg_GetFloat

◆ Wg_Function

typedef Wg_Obj *(* Wg_Function) (Wg_Context *context, Wg_Obj **argv, int argc)

The signature of a native function registered into the interpreter as a function object.

To get the keyword arguments passed to the function, call Wg_GetKwargs().

Parameters
contextThe associated context.
argvAn array of objects passed to the function.
argcThe length of the argv array.
Returns
The return value of the function call. If an exception was raised, NULL should be returned.
See also
Wg_NewFunction, Wg_BindMethod

◆ Wg_int

typedef int64_t Wg_int

The underlying data type of an integer object.

See also
Wg_NewInt, Wg_GetInt

◆ Wg_IterationCallback

typedef bool(* Wg_IterationCallback) (Wg_Obj *obj, void *userdata)

The signature of an iteration callback used by Wg_Iterate().

The yielded object is protected from garbage collection for the duration of this function.

Parameters
objThe object yielded by iteration.
userdataThe userdata specified in Wg_Iterate.
Returns
A boolean indicating whether iteration should continue. A value of false does not necessarily mean an error occurred.

◆ Wg_ModuleLoader

typedef bool(* Wg_ModuleLoader) (Wg_Context *context)

The signature of a module loader.

Parameters
contextThe associated context.
Returns
A boolean indicating whether tthe module was loaded successfully.
See also
Wg_RegisterModule, Wg_ImportModule, Wg_ImportFromModule, Wg_ImportAllFromModule

◆ Wg_Obj

typedef struct Wg_Obj Wg_Obj

An opaque type representing an object in the interpreter.

◆ Wg_PrintFunction

typedef void(* Wg_PrintFunction) (const char *message, int len, void *userdata)

The signature of the print function used by the interpreter for printing.

Attention
The message may not be null terminated and may contain null bytes in the middle.
Parameters
messageAn array of bytes to be printed.
lenThe length of the message in bytes.
userdataThe userdata specified when this callback was registered.
See also
Wg_Config, Wg_Print, Wg_PrintString

◆ Wg_uint

typedef uint64_t Wg_uint

The unsigned version of Wg_int.

See also
Wg_int

◆ Wg_UnOp

typedef enum Wg_UnOp Wg_UnOp

The unary operation to be used Wg_UnaryOp.

Enumeration Type Documentation

◆ Wg_BinOp

enum Wg_BinOp

The binary operation to be used by Wg_BinaryOp.

Enumerator
WG_BOP_ADD 

The addition operator.

Calls the __add__ special method.

WG_BOP_SUB 

The subtraction operator.

Calls the __sub__ special method.

WG_BOP_MUL 

The multiplication operator.

Calls the __mul__ special method.

WG_BOP_DIV 

The division operator.

Calls the __truediv__ special method.

WG_BOP_FLOORDIV 

The floor division operator.

Calls the __floordiv__ special method.

WG_BOP_MOD 

The modulo operator.

Calls the __mod__ special method.

WG_BOP_POW 

The power operator.

Calls the __pow__ special method.

WG_BOP_BITAND 

The bitwise and operator.

Calls the __and__ special method.

WG_BOP_BITOR 

The bitwise or operator.

Calls the __or__ special method.

WG_BOP_BITXOR 

The xor operator.

Calls the __xor__ special method.

WG_BOP_AND 

The logical and operator.

Calls the __nonzero__ special method on both arguments and returns the logical and of the result. If either __nonzero__ returns a non boolean type, a TypeError is raised.

WG_BOP_OR 

The logical or operator.

Calls the __nonzero__ special method on both arguments and returns the logical or of the result. If either __nonzero__ returns a non boolean type, a TypeError is raised.

WG_BOP_SHL 

The bit left shift operator.

Calls the __lshift__ special method.

WG_BOP_SHR 

The bit right shift operator.

Calls the __rshift__ special method.

WG_BOP_IN 

The in operator.

Calls the __contains__ special method. If __contains__ returns a non boolean type, a TypeError is raised.

WG_BOP_NOTIN 

The not in operator.

Calls the __contains__ special method and inverts the result. If __contains__ returns a non boolean type, a TypeError is raised.

WG_BOP_EQ 

The equals operator.

Calls the __eq__ special method. If __eq__ returns a non boolean type, a TypeError is raised.

WG_BOP_NE 

The not equals operator.

Calls the __ne__ special method. If __ne__ returns a non boolean type, a TypeError is raised.

WG_BOP_LT 

The less than operator.

Calls the __lt__ special method. If __lt__ returns a non boolean type, a TypeError is raised.

WG_BOP_LE 

The less than or equals operator.

Calls the __le__ special method. If __le__ returns a non boolean type, a TypeError is raised.

WG_BOP_GT 

The greater than operator.

Calls the __gt__ special method. If __gt__ returns a non boolean type, a TypeError is raised.

WG_BOP_GE 

The greater than or equals operator.

Calls the __ge__ special method. If __ge__ returns a non boolean type, a TypeError is raised.

◆ Wg_Exc

enum Wg_Exc

The type exception to be raised by Wg_RaiseException.

Enumerator
WG_EXC_BASEEXCEPTION 

BaseException.

WG_EXC_SYSTEMEXIT 

SystemExit.

WG_EXC_EXCEPTION 

Exception.

WG_EXC_STOPITERATION 

StopIteration.

WG_EXC_ARITHMETICERROR 

ArithmeticError.

WG_EXC_OVERFLOWERROR 

OverflowError.

WG_EXC_ZERODIVISIONERROR 

ZeroDivisionError.

WG_EXC_ATTRIBUTEERROR 

AttributeError.

WG_EXC_IMPORTERROR 

ImportError.

WG_EXC_LOOKUPERROR 

LookupError.

WG_EXC_INDEXERROR 

IndexError.

WG_EXC_KEYERROR 

KeyError.

WG_EXC_MEMORYERROR 

MemoryError.

WG_EXC_NAMEERROR 

NameError.

WG_EXC_OSERROR 

OSError.

WG_EXC_ISADIRECTORYERROR 

IsADirectoryError.

WG_EXC_RUNTIMEERROR 

RuntimeError.

WG_EXC_NOTIMPLEMENTEDERROR 

NotImplementedError.

WG_EXC_RECURSIONERROR 

RecursionError.

WG_EXC_SYNTAXERROR 

SyntaxError.

WG_EXC_TYPEERROR 

TypeError.

WG_EXC_VALUEERROR 

ValueError.

◆ Wg_UnOp

enum Wg_UnOp

The unary operation to be used Wg_UnaryOp.

Enumerator
WG_UOP_POS 

The identity operator.

Calls the __pos__ special method.

WG_UOP_NEG 

The unary minus operator.

Calls the __neg__ special method.

WG_UOP_BITNOT 

The bitwise complement operator.

Calls the __invert__ special method.

WG_UOP_NOT 

The logical not operator.

Calls the __nonzero__ special method and inverts the result. If __nonzero__ returns a non boolean type, a TypeError is raised.

WG_UOP_HASH 

The hash operator.

Calls the __hash__ special method. If __hash__ returns a non integer type, a TypeError is raised.

WG_UOP_LEN 

The length operator.

Calls the __len__ special method. If __len__ returns a non integer type, a TypeError is raised.

WG_UOP_BOOL 

The bool conversion operator.

Calls the __bool__ special method. If __bool__ returns a non boolean type, a TypeError is raised.

WG_UOP_INT 

The integer conversion operator.

Calls the __int__ special method. If __int__ returns a non integer type, a TypeError is raised.

WG_UOP_FLOAT 

The float operator operator.

Calls the __float__ special method. If __float__ returns a non float type, a TypeError is raised.

WG_UOP_STR 

The string conversion operator.

Calls the __str__ special method. If __str__ returns a non string type, a TypeError is raised.

WG_UOP_REPR 

The string representation operator.

Calls the __repr__ special method. If __repr__ returns a non string type, a TypeError is raised.

WG_UOP_INDEX 

The index conversion operator.

Calls the __index__ special method. If __index__ returns a non integer type, a TypeError is raised.

Function Documentation

◆ Wg_BinaryOp()

Wg_Obj * Wg_BinaryOp ( Wg_BinOp  op,
Wg_Obj lhs,
Wg_Obj rhs 
)

Perform a binary operation.

Note
The logical and/or operators will short-circuit their truthy test.
Parameters
opThe binary operation to perform.
lhsThe left hand side operand.
rhsThe right hand side operand.
Returns
The result of the operation, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_BindMethod()

Wg_Obj * Wg_BindMethod ( Wg_Obj klass,
const char *  name,
Wg_Function  fptr,
void *  userdata 
)

Instantiate a function object and bind it to a class.

To get the userdata in the function, call Wg_GetFunctionUserdata(). To get the keyword arguments passed to the function, call Wg_GetKwargs().

Note
Existing instances of the class will gain the new method.
Parameters
klassThe class to bind the method to.
nameThe name of the method.
fptrThe native function to be bound.
userdataThe userdata to pass to the function when it is called.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage, Wg_BindMethod

◆ Wg_Call()

Wg_Obj * Wg_Call ( Wg_Obj callable,
Wg_Obj **  argv,
int  argc,
Wg_Obj kwargs 
)

Call a callable object.

Calling a class object will instantiate the class.

The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.

Parameters
callableThe object to call.
argvAn array of arguments to pass to the callable object. If argc is 0 then this can be NULL.
argcThe length of the argv array.
kwargsA dictionary object containing the keyword arguments or NULL if none.
Returns
The return value of the callable, or NULL on failure.
See also
Wg_CallMethod, Wg_GetException, Wg_GetErrorMessage

◆ Wg_CallMethod()

Wg_Obj * Wg_CallMethod ( Wg_Obj obj,
const char *  method,
Wg_Obj **  argv,
int  argc,
Wg_Obj kwargs 
)

Call a method on a object.

This is equivalent to calling Wg_GetAttribute() and then Wg_Call().

The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.

Parameters
objThe object to call the method on.
methodThe method to call.
argvAn array of arguments to pass to the callable object. If argc is 0 then this can be NULL.
argcThe length of the argv array.
kwargsA dictionary object containing the keyword arguments or NULL if none.
Returns
The return value of the callable, or NULL on failure.
See also
Wg_Call, Wg_GetException, Wg_GetErrorMessage

◆ Wg_CallMethodFromBase()

Wg_Obj * Wg_CallMethodFromBase ( Wg_Obj obj,
const char *  method,
Wg_Obj **  argv,
int  argc,
Wg_Obj kwargs,
Wg_Obj baseClass 
)

Call a method on a object, skipping methods which belong to the most derived layer.

This is equivalent to calling Wg_GetAttributeFromBase() and then Wg_Call().

This is useful if the method is shadowed by the derived class. If multiple bases contain this attribute, the first attribute found is returned.

The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.

Parameters
objThe object to call the method on.
methodThe method to call.
argvAn array of arguments to pass to the callable object. If argc is 0 then this can be NULL.
argcThe length of the argv array.
kwargsA dictionary object containing the keyword arguments or NULL if none.
baseClassThe base class to search in, or NULL to search in all bases.
Returns
The return value of the callable, or NULL on failure.
See also
Wg_Call, Wg_GetException, Wg_GetErrorMessage

◆ Wg_ClearException()

void Wg_ClearException ( Wg_Context context)

Clear the current exception.

Parameters
contextThe associated context.

◆ Wg_CollectGarbage()

void Wg_CollectGarbage ( Wg_Context context)

Force run the garbage collector and free all unreachable objects.

Parameters
contextThe associated context.

◆ Wg_Compile()

Wg_Obj * Wg_Compile ( Wg_Context context,
const char *  script,
const char *  prettyName 
)

Compile a script into a function object.

An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.

Parameters
contextThe associated context.
scriptThe script to compile.
prettyNameThe name to run the script under, or NULL to use a default name.
Returns
A function object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage, Wg_Call

◆ Wg_CompileExpression()

Wg_Obj * Wg_CompileExpression ( Wg_Context context,
const char *  script,
const char *  prettyName 
)

Compile an expression into a function object.

An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.

Parameters
contextThe associated context.
scriptThe expression to compile.
prettyNameThe name to run the script under, or NULL to use a default name.
Returns
A function object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage, Wg_Call

◆ Wg_CreateContext()

Wg_Context * Wg_CreateContext ( const Wg_Config config)

Create an instance of an interpreter.

The returned context must be freed with Wg_DestroyContext(). This function does not fail.

Parameters
configThe configuration to use, or NULL to use the default configuration.
Returns
A newly created context.

◆ Wg_DecRef()

void Wg_DecRef ( Wg_Obj obj)

Decrement the reference count of an object. A positive reference count prevents the object from being garbage collected.

The reference count must not be negative.

Parameters
objThe object whose reference count is to be decremented.
See also
Wg_IncRef

◆ Wg_DefaultConfig()

void Wg_DefaultConfig ( Wg_Config config)

Get the default configuration.

Parameters
[out]configThe returned default configuration.
See also
Wg_CreateContext

◆ Wg_DestroyContext()

void Wg_DestroyContext ( Wg_Context context)

Free a context created with Wg_CreateContext().

Parameters
contextThe context to free.

◆ Wg_Execute()

bool Wg_Execute ( Wg_Context context,
const char *  script,
const char *  prettyName 
)

Execute a script.

An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.

Parameters
contextThe associated context.
scriptThe script to execute.
prettyNameThe name to run the script under, or NULL to use a default name.
Returns
A boolean indicating whether the script compiled and executed successfully.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_ExecuteExpression()

Wg_Obj * Wg_ExecuteExpression ( Wg_Context context,
const char *  script,
const char *  prettyName 
)

Execute a script containing an expression.

An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.

Parameters
contextThe associated context.
scriptThe expression to execute.
prettyNameThe name to run the script under, or NULL to use a default name.
Returns
The result of executing the expression, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_GetAttribute()

Wg_Obj * Wg_GetAttribute ( Wg_Obj obj,
const char *  attribute 
)

Get an attribute of an object. If the attribute does not exist, an AttributeError is raised. If the attribute is an unbound method object, a new method object is allocated with obj bound. If this allocation fails, a MemoryError is raised.

Parameters
objThe object to get the attribute from.
attributeThe attribute to get.
Returns
The attribute value, or NULL if the attribute does not exist or there is an error.
See also
Wg_HasAttribute, Wg_GetAttributeFromBase, Wg_GetAttributeNoExcept, Wg_SetAttribute, Wg_GetException, Wg_GetErrorMessage

◆ Wg_GetAttributeFromBase()

Wg_Obj * Wg_GetAttributeFromBase ( Wg_Obj obj,
const char *  attribute,
Wg_Obj baseClass 
)

Get an attribute of an object, skipping attributes that belong to the most derived layer.

This is useful if the attribute is shadowed by the derived class. If multiple bases contain this attribute, the first attribute found is returned.

Parameters
objThe object to get the attribute from.
attributeThe attribute to get.
baseClassThe base class to search in, or NULL to search in all bases.
Returns
The attribute value, or NULL if the attribute does not exist.
See also
Wg_HasAttribute, Wg_GetAttribute, Wg_GetAttributeNoExcept, Wg_SetAttribute

◆ Wg_GetAttributeNoExcept()

Wg_Obj * Wg_GetAttributeNoExcept ( Wg_Obj obj,
const char *  attribute 
)

Get an attribute of an object. Unlike, Wg_GetAttribute(), this function does not raise exceptions.

Warning
This function will not bind unbound method objects to obj.
Parameters
objThe object to get the attribute from.
attributeThe attribute to get.
Returns
The attribute value, or NULL if the attribute does not exist.
See also
Wg_HasAttribute, Wg_GetAttribute, Wg_GetAttributeFromBase, Wg_SetAttribute

◆ Wg_GetBool()

bool Wg_GetBool ( const Wg_Obj obj)

Get the value from a boolean object.

Parameters
objThe object to get the value from.
Returns
The boolean value of the object.

◆ Wg_GetContextFromObject()

Wg_Context * Wg_GetContextFromObject ( Wg_Obj obj)

Get the context associated with an object.

Parameters
objThe object.
Returns
The associated context.

◆ Wg_GetErrorMessage()

const char * Wg_GetErrorMessage ( Wg_Context context)

Get the current exception message.

Parameters
contextThe associated context.
Returns
The current error string.
See also
Wg_GetException, Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject, Wg_ReraiseExceptionObject

◆ Wg_GetException()

Wg_Obj * Wg_GetException ( Wg_Context context)

Get the current exception object.

Parameters
contextThe associated context.
Returns
The current exception object, or NULL if there is no exception.
See also
Wg_GetErrorMessage, Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject, Wg_ReraiseExceptionObject

◆ Wg_GetFloat()

Wg_float Wg_GetFloat ( const Wg_Obj obj)

Get the float value from an integer or float object.

Parameters
objThe object to get the value from.
Returns
The float value of the object.

◆ Wg_GetFunctionUserdata()

void * Wg_GetFunctionUserdata ( Wg_Context context)

Get the userdata associated with the current function.

Note
This function must be called inside a function bound with Wg_NewFunction() or Wg_BindMethod().
Returns
The userdata associated with the current function.
See also
Wg_NewFunction, Wg_BindMethod

◆ Wg_GetGlobal()

Wg_Obj * Wg_GetGlobal ( Wg_Context context,
const char *  name 
)

Get a global variable in the current module namespace.

Parameters
contextThe associated context.
nameThe name of the global variable.
Returns
The value of the global variable, or NULL if it does not exist.
See also
Wg_SetGlobal

◆ Wg_GetIndex()

Wg_Obj * Wg_GetIndex ( Wg_Obj obj,
Wg_Obj index 
)

Index an object.

This calls the __getitem__ method.

Parameters
objThe object to index.
indexThe index.
Returns
The object at the specified index, or NULL on failure.
See also
Wg_UnaryOp, Wg_BinaryOp, Wg_SetIndex, Wg_GetException, Wg_GetErrorMessage

◆ Wg_GetInt()

Wg_int Wg_GetInt ( const Wg_Obj obj)

Get the value from an integer object.

Parameters
objThe object to get the value from.
Returns
The integer value of the object.

◆ Wg_GetKwargs()

Wg_Obj * Wg_GetKwargs ( Wg_Context context)

Get the keyword arguments dictionary passed to the current function.

Note
This function must be called inside a function bound with Wg_NewFunction() or Wg_BindMethod().
This function can return NULL to indicate an empty dictionary.
Parameters
contextThe associated context.
Returns
The keywords arguments dictionary.
See also
Wg_Call, Wg_CallMethod

◆ Wg_GetString()

const char * Wg_GetString ( const Wg_Obj obj,
int *  len 
)

Get the value from a string object.

Note
The string is always null terminated. If null bytes are expected to appear in the middle of the string, the length parameter can be used to get the true length of the string.
Parameters
objThe object to get the value from.
[out]lenThe length of the string. This parameter may be NULL.
Returns
The string value of the object.

◆ Wg_HasAttribute()

bool Wg_HasAttribute ( Wg_Obj obj,
const char *  attribute 
)

Check if an object has an attribute.

Parameters
objThe object to check.
attributeThe attribute to check.
Returns
A boolean indicating whether the object has the attribute.
See also
Wg_GetAttribute, Wg_GetAttributeFromBase, Wg_GetAttributeNoExcept, Wg_SetAttribute

◆ Wg_ImportAllFromModule()

bool Wg_ImportAllFromModule ( Wg_Context context,
const char *  module 
)

Import all names from a module.

If the names are imported successfully, the objects bound to the names are bound to global variables with the same names.

Attention
If the names in the module are rebound to different objects after importing, the changes are not visible to outside modules.
Parameters
contextThe associated context.
moduleThe name of the module to import.
Returns
A boolean indicating if the names were imported successfully.
See also
Wg_RegisterModule, Wg_ImportModule, Wg_ImportFromModule, Wg_GetException, Wg_GetErrorMessage

◆ Wg_ImportFromModule()

Wg_Obj * Wg_ImportFromModule ( Wg_Context context,
const char *  module,
const char *  name,
const char *  alias 
)

Import a specific name from a module.

If the name is imported successfully, the object bound to the name is bound to a global variable with the same name (or the alias provided).

Attention
If the names in the module are rebound to different objects after importing, the changes are not visible to outside modules.
Parameters
contextThe associated context.
moduleThe name of the module to import from.
nameThe name to import.
aliasThe alias to import the name under, or NULL to use the same name.
Returns
The imported object, or NULL on failure.
See also
Wg_RegisterModule, Wg_ImportModule, Wg_ImportAllFromModule, Wg_GetException, Wg_GetErrorMessage

◆ Wg_ImportModule()

Wg_Obj * Wg_ImportModule ( Wg_Context context,
const char *  module,
const char *  alias 
)

Import a module.

If the module has already been imported before, the module is not reloaded.

If the module is imported successfully, the module object is bound to a global variable with the same name (or the alias provided).

Attention
If the names in the module are rebound to different objects after importing, the changes are not visible to outside modules.
Parameters
contextThe associated context.
moduleThe name of the module to import.
aliasThe alias to import the module under, or NULL to use the same name.
Returns
The imported module object, or NULL on failure.
See also
Wg_RegisterModule, Wg_ImportFromModule, Wg_ImportAllFromModule, Wg_GetException, Wg_GetErrorMessage

◆ Wg_IncRef()

void Wg_IncRef ( Wg_Obj obj)

Increment the reference count of an object. A positive reference count prevents the object from being garbage collected.

Parameters
objThe object whose reference count is to be incremented.
See also
Wg_DecRef

◆ Wg_IsBool()

bool Wg_IsBool ( const Wg_Obj obj)

Check if an object is a boolean.

Parameters
objThe object to inspect.
Returns
True if the object is a boolean, otherwise false.

◆ Wg_IsClass()

bool Wg_IsClass ( const Wg_Obj obj)

Check if an object is a class.

Parameters
objThe object to inspect.
Returns
True if the object is a class, otherwise false.

◆ Wg_IsDictionary()

bool Wg_IsDictionary ( const Wg_Obj obj)

Check if an object is a dictionary.

Parameters
objThe object to inspect.
Returns
True if the object is a dictionary, otherwise false.

◆ Wg_IsFunction()

bool Wg_IsFunction ( const Wg_Obj obj)

Check if an object is a function.

Parameters
objThe object to inspect.
Returns
True if the object is a function, otherwise false.

◆ Wg_IsInstance()

Wg_Obj * Wg_IsInstance ( const Wg_Obj instance,
Wg_Obj *const *  types,
int  typesLen 
)

Check if an object's class derives from any of the specified classes.

Parameters
instanceThe object to be checked.
typesAn array of class objects to be checked against. This can be NULL if typesLen is 0.
typesLenThe length of the types array.
Returns
The first subclass matched, or NULL if the object's class does not derive from any of the specified classes.

◆ Wg_IsInt()

bool Wg_IsInt ( const Wg_Obj obj)

Check if an object is an integer.

Parameters
objThe object to inspect.
Returns
True if the object is an integer, otherwise false.

◆ Wg_IsIntOrFloat()

bool Wg_IsIntOrFloat ( const Wg_Obj obj)

Check if an object is a float.

Parameters
objThe object to inspect.
Returns
True if the object is an integer or float, otherwise false.

◆ Wg_IsList()

bool Wg_IsList ( const Wg_Obj obj)

Check if an object is a list.

Parameters
objThe object to inspect.
Returns
True if the object is a list, otherwise false.

◆ Wg_IsNone()

bool Wg_IsNone ( const Wg_Obj obj)

Check if an object is None.

Parameters
objThe object to inspect.
Returns
True if the object is None, otherwise false.

◆ Wg_IsSet()

bool Wg_IsSet ( const Wg_Obj obj)

Check if an object is a set.

Parameters
objThe object to inspect.
Returns
True if the object is a set, otherwise false.

◆ Wg_IsString()

bool Wg_IsString ( const Wg_Obj obj)

Check if an object is a string.

Parameters
objThe object to inspect.
Returns
True if the object is a string, otherwise false.

◆ Wg_IsTuple()

bool Wg_IsTuple ( const Wg_Obj obj)

Check if an object is a tuple.

Parameters
objThe object to inspect.
Returns
True if the object is a tuple, otherwise false.

◆ Wg_Iterate()

bool Wg_Iterate ( Wg_Obj obj,
void *  userdata,
Wg_IterationCallback  callback 
)

Iterate over an iterable object.

The object must be iterable, otherwise an exception is raised.

The object is protected from garbage collection until the function returns.

Parameters
objThe object to iterate over.
userdataThe userdata to be passed to the callback function.
callbackA function to be called for each value yielded by iteration. See Wg_IterationCallback for more details on this function.
Returns
True on success, or false on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewBool()

Wg_Obj * Wg_NewBool ( Wg_Context context,
bool  value 
)

Instantiate a boolean object.

Unlike the other Wg_NewXXX functions, this function always succeeds.

Parameters
contextThe associated context.
valueThe value of the object.
Returns
The instantiated object.

◆ Wg_NewClass()

Wg_Obj * Wg_NewClass ( Wg_Context context,
const char *  name,
Wg_Obj **  bases,
int  basesLen 
)

Instantiate a class object.

Methods can be added later with Wg_BindMethod() and data members can be added to instances inside the __init__ method with Wg_SetAttribute().

If no bases are specified, the object class is implicitly used as a base.

The name of the class cannot begin with two underscores.

Parameters
contextThe associated context.
nameThe name of the class.
basesAn array of class objects to be used as a base. This can be NULL if basesLen is 0.
basesLenThe length of the bases array.
Returns
The instantiated class object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewDictionary()

Wg_Obj * Wg_NewDictionary ( Wg_Context context,
Wg_Obj **  keys,
Wg_Obj **  values,
int  len 
)

Instantiate a dictionary object.

The keys must be hashable, otherwise a TypeError is raised.

Parameters
contextThe associated context.
keysAn array of keys to initialise the dictionary with. This can be NULL if argc is 0.
valuesAn array of values to initialise the dictionary with. This can be NULL if argc is 0.
lenThe length of the keys and values arrays.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewFloat()

Wg_Obj * Wg_NewFloat ( Wg_Context context,
Wg_float  value 
)

Instantiate a float object.

Parameters
contextThe associated context.
valueThe value of the object.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewFunction()

Wg_Obj * Wg_NewFunction ( Wg_Context context,
Wg_Function  fptr,
void *  userdata,
const char *  prettyName 
)

Instantiate a function object.

To get the userdata in the function, call Wg_GetFunctionUserdata(). To get the keyword arguments passed to the function, call Wg_GetKwargs().

Parameters
contextThe associated context.
fptrThe native function to be bound.
userdataThe userdata to pass to the function when it is called.
prettyNameThe name of the function, or NULL to use a default name.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage, Wg_BindMethod

◆ Wg_NewInt()

Wg_Obj * Wg_NewInt ( Wg_Context context,
Wg_int  value 
)

Instantiate an integer object.

Parameters
contextThe associated context.
valueThe value of the object.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewList()

Wg_Obj * Wg_NewList ( Wg_Context context,
Wg_Obj **  argv,
int  argc 
)

Instantiate a list object.

Parameters
contextThe associated context.
argvAn array of objects to initialise the list with. This can be NULL if argc is 0.
argcThe length of the argv array.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewSet()

Wg_Obj * Wg_NewSet ( Wg_Context context,
Wg_Obj **  argv,
int  argc 
)

Instantiate a set object.

Parameters
contextThe associated context.
argvAn array of objects to initialise the set with. This can be NULL if argc is 0.
argcThe length of the argv array.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewString()

Wg_Obj * Wg_NewString ( Wg_Context context,
const char *  value 
)

Instantiate a string object.

Parameters
contextThe associated context.
valueA null terminated string, or NULL for an empty string.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewStringBuffer()

Wg_Obj * Wg_NewStringBuffer ( Wg_Context context,
const char *  buffer,
int  len 
)

Instantiate a string object from a buffer.

Parameters
contextThe associated context.
bufferThe buffer.
lenThe length of the buffer.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_NewTuple()

Wg_Obj * Wg_NewTuple ( Wg_Context context,
Wg_Obj **  argv,
int  argc 
)

Instantiate a tuple object.

Parameters
contextThe associated context.
argvAn array of objects to initialise the tuple with. This can be NULL if argc is 0.
argcThe length of the argv array.
Returns
The instantiated object, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_None()

Wg_Obj * Wg_None ( Wg_Context context)

Get the None singleton value.

Parameters
contextThe associated context.
Returns
The None singleton object.

◆ Wg_ParseKwargs()

bool Wg_ParseKwargs ( Wg_Obj dict,
const char *const *  keys,
int  keysLen,
Wg_Obj **  values 
)

Get the values from a kwargs parameter.

Note
This function can be called with dictionaries other than the one returned by Wg_GetKwargs().
Parameters
dictThe dictionary to get the values from. If this is NULL, then an empty dictionary is assumed.
keysThe keys to look up.
keysLenThe length of the keys array.
[out]valuesThe values in the dictionary corresponding to the keys. The values are given in the order that the keys are given and will be NULL for keys that were not found.
Returns
A boolean indicating whether the operation was successful.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_Print()

void Wg_Print ( const Wg_Context context,
const char *  message,
int  len 
)

Print a message.

This function uses the print function specified in the configuration.

Parameters
contextThe associated context.
messageA byte array containing the message. This can NULL if len is 0.
lenThe length of the message byte array.
See also
Wg_PrintString, Wg_Config

◆ Wg_PrintString()

void Wg_PrintString ( const Wg_Context context,
const char *  message 
)

Print a string message.

This function uses the print function specified in the configuration.

Parameters
contextThe associated context.
messageA null terminated message string.
See also
Wg_Print, Wg_Config

◆ Wg_RaiseArgumentCountError()

void Wg_RaiseArgumentCountError ( Wg_Context context,
int  given,
int  expected 
)

Raise a TypeError with a formatted message.

If an exception is already set, the old exception will be overwritten.

Parameters
contextThe associated context.
givenThe number of arguments given.
expectedThe number of arguments expected, or -1 if this is not a fixed number.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_RaiseArgumentTypeError()

void Wg_RaiseArgumentTypeError ( Wg_Context context,
int  index,
const char *  expected 
)

Raise a TypeError with a formatted message.

If an exception is already set, the old exception will be overwritten.

Parameters
contextThe associated context.
indexThe parameter index of the invalid argument.
expectedA string describing the expected type.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_RaiseAttributeError()

void Wg_RaiseAttributeError ( const Wg_Obj obj,
const char *  attribute 
)

Raise a AttributeError with a formatted message.

If an exception is already set, the old exception will be overwritten.

Parameters
objThe object missing the attribute.
attributeThe missing attribute.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_RaiseException()

void Wg_RaiseException ( Wg_Context context,
Wg_Exc  type,
const char *  message 
)

Create and raise an exception.

If an exception is already set, the old exception will be overwritten.

Parameters
contextThe associated context.
typeThe exception type.
messageThe error message, or NULL for an empty string.
See also
Wg_RaiseExceptionClass, Wg_RaiseExceptionObject, Wg_ReraiseExceptionObject

◆ Wg_RaiseExceptionClass()

void Wg_RaiseExceptionClass ( Wg_Obj klass,
const char *  message 
)

Create and raise an exception using a class object.

The class must derive from BaseException, otherwise a TypeError is raised.

If an exception is already set, the old exception will be overwritten.

Parameters
klassThe exception class.
messageThe error message, or NULL for an empty string.
See also
Wg_RaiseException, Wg_RaiseExceptionObject, Wg_ReraiseExceptionObject

◆ Wg_RaiseExceptionObject()

void Wg_RaiseExceptionObject ( Wg_Obj obj)

Raise an existing exception object.

The object's type must derive from BaseException, otherwise a TypeError is raised.

If an exception is already set, the old exception will be overwritten.

Parameters
objThe exception object to raise. The type must be a subclass of BaseException.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_ReraiseExceptionObject

◆ Wg_RaiseKeyError()

void Wg_RaiseKeyError ( Wg_Context context,
Wg_Obj key 
)

Raise a KeyError with a formatted message.

If an exception is already set, the old exception will be overwritten.

Parameters
contextThe associated context.
keyThe key that caused the KeyError, or NULL to leave unspecified.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_RaiseNameError()

void Wg_RaiseNameError ( Wg_Context context,
const char *  name 
)

Raise a NameError with a formatted message.

If an exception is already set, the old exception will be overwritten.

Parameters
contextThe associated context.
nameThe name that was not found.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_RegisterFinalizer()

void Wg_RegisterFinalizer ( Wg_Obj obj,
Wg_Finalizer  finalizer,
void *  userdata 
)

Register a finalizer to run when an object is garbage collected.

Multiple finalizers may be registered.

Warning
Do not instantiate any objects in the finalizer.
Parameters
objThe object to register the finalizer for.
finalizerThe finalizer function.
userdataThe userdata to pass to the finalizer function.

◆ Wg_RegisterModule()

void Wg_RegisterModule ( Wg_Context context,
const char *  name,
Wg_ModuleLoader  loader 
)

Register a callback to be called when a module with the given name is imported. In the callback, new members can be added to the module.

Parameters
contextThe associated context.
nameThe name of the module to register the callback for.
loaderThe callback to register. The callback should return false on failure and otherwise true.
See also
Wg_ImportModule, Wg_ImportFromModule, Wg_ImportAllFromModule

◆ Wg_ReraiseExceptionObject()

void Wg_ReraiseExceptionObject ( Wg_Obj obj)

Raise an existing exception object without affecting the stack trace.

The object's type must derive from BaseException, otherwise a TypeError is raised.

If an exception is already set, the old exception will be overwritten.

Parameters
objThe exception object to raise. The type must be a subclass of BaseException.
See also
Wg_RaiseException, Wg_RaiseExceptionClass, Wg_RaiseExceptionObject

◆ Wg_SetAttribute()

void Wg_SetAttribute ( Wg_Obj obj,
const char *  attribute,
Wg_Obj value 
)

Set an attribute of an object.

Parameters
objThe object to set the attribute for.
attributeThe attribute to set.
valueThe attribute value.
See also
Wg_HasAttribute, Wg_GetAttribute, Wg_GetAttributeNoExcept, Wg_GetAttributeFromBase

◆ Wg_SetErrorCallback()

void Wg_SetErrorCallback ( Wg_ErrorCallback  callback)

Set a callback for programmer errors.

This function is threadsafe.

Parameters
callbackThe callback to use, or NULL to default to abort().

◆ Wg_SetGlobal()

void Wg_SetGlobal ( Wg_Context context,
const char *  name,
Wg_Obj value 
)

Set a global variable in the current module namespace.

Parameters
contextThe associated context.
nameThe name of the global variable.
valueThe value to set.
See also
Wg_GetGlobal

◆ Wg_SetIndex()

Wg_Obj * Wg_SetIndex ( Wg_Obj obj,
Wg_Obj index,
Wg_Obj value 
)

Set an index of an object.

This calls the __setitem__ method.

Parameters
objThe object to index.
indexThe index.
valueThe value to set.
Returns
The result of the operation (usually None), or NULL on failure.
See also
Wg_UnaryOp, Wg_BinaryOp, Wg_GetIndex, Wg_GetException, Wg_GetErrorMessage

◆ Wg_SetUserdata()

void Wg_SetUserdata ( Wg_Obj obj,
void *  userdata 
)

Set the userdata for an object.

If the userdata requires cleanup (e.g. freeing memory), Wg_RegisterFinalizer() may be useful for automatically performing the cleanup when it the object is no longer needed.

Warning
Only call this function on objects instantiated from user-created classes created with Wg_NewClass(). Do not set the userdata for builtin types.
Parameters
objThe object to set the userdata for.
userdataThe userdata to set.
See also
Wg_TryGetUserdata

◆ Wg_TryGetUserdata()

bool Wg_TryGetUserdata ( const Wg_Obj obj,
const char *  type,
void **  userdata 
)

Get the userdata from an object if it is of the expected type.

Parameters
objThe object to get the value from.
typeThe type to match.
[out]userdataThe userdata. This parameter may be NULL.
Returns
A boolean indicating whether obj matches type.
See also
Wg_SetUserdata, Wg_NewClass

◆ Wg_UnaryOp()

Wg_Obj * Wg_UnaryOp ( Wg_UnOp  op,
Wg_Obj arg 
)

Perform a unary operation.

Parameters
opThe unary operation to perform.
argThe operand.
Returns
The result of the operation, or NULL on failure.
See also
Wg_GetException, Wg_GetErrorMessage

◆ Wg_Unpack()

bool Wg_Unpack ( Wg_Obj obj,
int  count,
Wg_Obj **  values 
)

Helper function to unpack an iterable object into an array of objects.

If the number of objects yielded by the iterator does not match the count parameter, a ValueError is raised.

Parameters
objThe object to iterate over.
countThe expected number of values.
[out]valuesThe unpacked objects.
Returns
A boolean indicating success.