Skip to content

tux.wrappers.wandbox

Functions:

Name Description
getoutput

Compile and execute code using a specified compiler and return the output.

Classes

Functions

getoutput(code: str, compiler: str, options: str | None) -> dict[str, Any] | None

Compile and execute code using a specified compiler and return the output.

Parameters:

Name Type Description Default
code str

The source code to be compiled and executed.

required
compiler str

The identifier or name of the compiler to use.

required
options str or None

Additional compiler options or flags. If None, an empty string is used.

required

Returns:

Type Description
dict[str, Any] or None

A dictionary containing the compiler output if the request is successful, otherwise None. Returns None on HTTP errors or read timeout.

Source code in tux/wrappers/wandbox.py
Python
def getoutput(code: str, compiler: str, options: str | None) -> dict[str, Any] | None:
    """
    Compile and execute code using a specified compiler and return the output.

    Parameters
    ----------
    code : str
        The source code to be compiled and executed.
    compiler : str
        The identifier or name of the compiler to use.
    options : str or None
        Additional compiler options or flags. If None, an empty string is used.

    Returns
    -------
    dict[str, Any] or None
        A dictionary containing the compiler output if the request is successful,
        otherwise `None`. Returns `None` on HTTP errors or read timeout.
    """

    copt = options if options is not None else ""

    payload = {"compiler": compiler, "code": code, "options": copt}

    try:
        uri = client.post(url, json=payload)
        uri.raise_for_status()
    except httpx.ReadTimeout as e:
        # Changed to raise APIConnectionError for timeouts
        raise APIConnectionError(service_name="Wandbox", original_error=e) from e
    except httpx.RequestError as e:
        # General connection/request error
        raise APIConnectionError(service_name="Wandbox", original_error=e) from e
    except httpx.HTTPStatusError as e:
        # Specific HTTP status errors
        if e.response.status_code == 404:
            raise APIResourceNotFoundError(
                service_name="Wandbox",
                resource_identifier=compiler,
            ) from e  # Using compiler as resource identifier
        raise APIRequestError(service_name="Wandbox", status_code=e.response.status_code, reason=e.response.text) from e
    else:
        return uri.json() if uri.status_code == httpx.codes.OK else None