sys

The sys library provides access to system-specific parameters and functions. This is an extended library that must be explicitly registered.

Note: This library is enabled by default in the Scriptling CLI but must be manually registered when using the Go API.

Import

import sys

Available Constants

Constant Description
platform Operating system platform string
version Scriptling interpreter version
maxsize Maximum signed integer value
path_sep Path separator for OS
argv List of command-line arguments
stdin Standard input stream object (when stdin is available)

Constants

stdin

A file-like object connected to standard input. Only available when stdin is configured — either when running via the Scriptling CLI with piped input, or when an input is defined during registration of the sys library via the Go API.

Method Description
read() Read all remaining data from stdin
readline() Read one line including the newline character; returns "" at EOF

sys.stdin is also iterable — each iteration yields one line including its newline:

import sys

# Read all at once
data = sys.stdin.read()

# Read line by line
line = sys.stdin.readline()

# Iterate over lines
for line in sys.stdin:
    print("Got:", line.rstrip())

platform

A string identifying the operating system platform.

import sys
print(sys.platform)  # "darwin", "linux", or "win32"

version

A string containing the version of the Scriptling interpreter.

import sys
print(sys.version)  # "Scriptling 1.0"

maxsize

The maximum value of a signed integer (int64).

import sys
print(sys.maxsize)  # 9223372036854775807

path_sep

The path separator used by the operating system.

import sys
print(sys.path_sep)  # "/" on Unix, "\" on Windows

argv

A list of command-line arguments passed to the script.

import sys
print(sys.argv)  # ["script.py", "arg1", "arg2"]

Functions

input([prompt])

Read a line from stdin, stripping the trailing newline. Only available when the environment is configured with a stdin reader.

line = input()
print("You typed:", line)

Note: The optional prompt argument is accepted but not printed (no interactive terminal in remote environments).

exit([code])

Raise a SystemExit exception to exit the interpreter.

Parameters:

  • code - Exit status (default: 0). Can be an integer or a string message.

Behavior:

  • Raises a SystemExit exception that can be caught with try/except
  • If not caught, terminates the script execution with an error
  • Integer codes produce exception message: “SystemExit:
  • String messages produce exception with that message directly

Examples:

import sys

# Exit successfully (raises SystemExit: 0)
sys.exit()

# Exit with error code (raises SystemExit: 1)
sys.exit(1)

# Exit with custom message
sys.exit("Fatal error occurred")

# Catching sys.exit to prevent termination
try:
    sys.exit(42)
    print("This won't run")
except Exception as e:
    print("Caught:", str(e))  # "Caught: SystemExit: 42"
    # Continue execution instead of exiting

Note: SystemExit exceptions have an ExceptionType of “SystemExit” and can be detected by the caller via the returned error.

Go API Integration

When using the Go API, sys.exit() returns a *extlibs.SysExitCode error that contains the exit code. The caller can decide how to handle it:

package main

import (
    "fmt"
    "os"
    "github.com/paularlott/scriptling"
    "github.com/paularlott/scriptling/extlibs"
)

func main() {
    p := scriptling.New()

    // Register the sys library with argv
    extlibs.RegisterSysLibrary(p, []string{"script.py", "arg1", "arg2"})

    // Evaluate code that calls sys.exit()
    result, err := p.Eval(`
import sys
sys.exit(42)
    `)

    // Check for SystemExit error using helper
    if sysExit, ok := extlibs.GetSysExitCode(err); ok {
        fmt.Printf("Script exited with code: %d\n", sysExit.Code)
        os.Exit(sysExit.Code)  // Optionally exit the Go process
    } else if err != nil {
        fmt.Printf("Error: %v\n", err)
        os.Exit(1)
    }
}

Helper functions:

  • extlibs.IsSysExitCode(err error) bool - Check if an error is a SysExitCode
  • extlibs.GetSysExitCode(err error) (*SysExitCode, bool) - Extract the SysExitCode from an error

Key change from previous behavior: sys.exit() no longer terminates the Go process directly. Instead, it returns an error that the caller can handle. This prevents scripts from unexpectedly terminating the host application.

Examples

Check Platform

import sys

if sys.platform == "darwin":
    print("Running on macOS")
elif sys.platform == "linux":
    print("Running on Linux")
elif sys.platform == "win32":
    print("Running on Windows")

Process Arguments

import sys

if len(sys.argv) < 2:
    print("Usage: script.py <input_file>")
    sys.exit(1)

input_file = sys.argv[1]
print(f"Processing {input_file}")

Python Compatibility

This library implements a subset of Python’s sys module:

Feature Supported
argv
exit()
platform
version ✅ (simplified)
maxsize
path
modules
stdin/stdout/stderr stdin ✅ (when available), stdout/stderr ❌
executable
version_info