<commit_before>package ocio

// #include "stdlib.h"
//
// #include "cpp/ocio.h"
//
import "C"

import (
	"errors"
	"fmt"
	"reflect"
	"runtime"
	"unsafe"
)

// A Config defines all the colorspaces available at runtime.
type Config struct {
	ptr *C.Config
}

/*
Config Initialization
*/

func newConfig(p *C.Config) *Config {
	cfg := &Config{p}
	runtime.SetFinalizer(cfg, deleteConfig)
	return cfg
}

func deleteConfig(c *Config) {
	C.freeContext((*C._Context)(c.ptr))
	runtime.KeepAlive(c)
}

// Create a new empty Config
func NewConfig() *Config {
	return newConfig(C.Config_Create())
}

// Get the current configuration.
// If a current config had not yet been set, it will be automatically
// initialized from the environment.
func CurrentConfig() (*Config, error) {
	c, err := C.GetCurrentConfig()
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Set the current configuration. This will then store a copy of the specified config.
func SetCurrentConfig(config *Config) error {
	_, err := C.SetCurrentConfig(config.ptr)
	runtime.KeepAlive(config)
	return err
}

// Create a Config by checking the OCIO environment variable
func ConfigCreateFromEnv() (*Config, error) {
	c, err := C.Config_CreateFromEnv()
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Create a Config from an existing yaml Config file
func ConfigCreateFromFile(filename string) (*Config, error) {
	c_str := C.CString(filename)
	defer C.free(unsafe.Pointer(c_str))

	c, err := C.Config_CreateFromFile(c_str)
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Create a Config from a valid yaml string
func ConfigCreateFromData(data string) (*Config, error) {
	c_str := C.CString(data)
	defer C.free(unsafe.Pointer(c_str))

	c, err := C.Config_CreateFromData(c_str)
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

func (c *Config) lastError() error {
	ret := errors.New(C.GoString(c.ptr.last_error))
	runtime.KeepAlive(c)
	return ret
}<commit_msg>Fix lastError() returning a non-nil error with an empty string (false error)<commit_after>package ocio

// #include "stdlib.h"
//
// #include "cpp/ocio.h"
//
import "C"

import (
	"errors"
	"fmt"
	"reflect"
	"runtime"
	"unsafe"
)

// A Config defines all the colorspaces available at runtime.
type Config struct {
	ptr *C.Config
}

/*
Config Initialization
*/

func newConfig(p *C.Config) *Config {
	cfg := &Config{p}
	runtime.SetFinalizer(cfg, deleteConfig)
	return cfg
}

func deleteConfig(c *Config) {
	C.freeContext((*C._Context)(c.ptr))
	runtime.KeepAlive(c)
}

// Create a new empty Config
func NewConfig() *Config {
	return newConfig(C.Config_Create())
}

// Get the current configuration.
// If a current config had not yet been set, it will be automatically
// initialized from the environment.
func CurrentConfig() (*Config, error) {
	c, err := C.GetCurrentConfig()
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Set the current configuration. This will then store a copy of the specified config.
func SetCurrentConfig(config *Config) error {
	_, err := C.SetCurrentConfig(config.ptr)
	runtime.KeepAlive(config)
	return err
}

// Create a Config by checking the OCIO environment variable
func ConfigCreateFromEnv() (*Config, error) {
	c, err := C.Config_CreateFromEnv()
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Create a Config from an existing yaml Config file
func ConfigCreateFromFile(filename string) (*Config, error) {
	c_str := C.CString(filename)
	defer C.free(unsafe.Pointer(c_str))

	c, err := C.Config_CreateFromFile(c_str)
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

// Create a Config from a valid yaml string
func ConfigCreateFromData(data string) (*Config, error) {
	c_str := C.CString(data)
	defer C.free(unsafe.Pointer(c_str))

	c, err := C.Config_CreateFromData(c_str)
	if err != nil {
		return nil, getLastError((*C._Context)(c))
	}
	return newConfig(c), err
}

func (c *Config) lastError() error {
	e := C.GoString(c.ptr.last_error)
	if e == "" {
		return nil
	}
	runtime.KeepAlive(c)
	return errors.New(e)
}
{'fraction_non_alphanumeric': 0.1362048894062864, 'fraction_numerical': 0.0, 'mean_word_length': 4.284132841328414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5054970', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1476, 'n_words': 433}