<commit_before>// Copyright 2011 <NAME>.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

// #include <Python.h>
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
// static inline void xdecref(PyObject *obj) { Py_XDECREF(obj); }
import "C"

import (
	"fmt"
	"os"
	"unsafe"
)

type Error struct {
	kind *C.PyObject
	tb   *C.PyObject
	msg  string
}

func (e *Error) String() string {
	return fmt.Sprintf("<TYPE>: %s", e.msg)
}

func exception() os.Error {
	if C.PyErr_Occurred() == nil {
		return nil
	}

	var t, v, tb *C.PyObject

	defer C.xdecref(v)

	C.PyErr_Fetch(&t, &v, &tb)

	s := C.PyObject_Str(v)
	defer C.decref(s)

	return &Error{t, tb, C.GoString(C.PyString_AsString(s))}
}

func raise(err os.Error) {
	var msg string
	var exc = C.PyExc_Exception

	e, ok := err.(*Error)
	if ok {
		exc = e.kind
		msg = e.msg
	} else {
		msg = err.String()
	}

	s := C.CString(msg)
	defer C.free(unsafe.Pointer(s))

	C.PyErr_SetString(exc, s)
}

func Err_Format(f string, args ...interface{}) {
	s := C.CString(fmt.Sprintf(f, args))
	defer C.free(unsafe.Pointer(s))
	C.PyErr_SetString(C.PyExc_Exception, s)
}

func KeyError(format string, args ...interface{}) os.Error {
	msg := fmt.Sprintf(format, args...)
	return &Error{C.PyExc_KeyError, nil, msg}
}

func int2Err(i C.int) os.Error {
	if i < 0 {
		return exception()
	}
	return nil
}
<commit_msg>Incref the exception type when we use it<commit_after>// Copyright 2011 <NAME>.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

// #include <Python.h>
// static inline void incref(PyObject *obj) { Py_INCREF(obj); }
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
// static inline void xdecref(PyObject *obj) { Py_XDECREF(obj); }
import "C"

import (
	"fmt"
	"os"
	"unsafe"
)

type Error struct {
	kind *C.PyObject
	tb   *C.PyObject
	msg  string
}

func (e *Error) String() string {
	return fmt.Sprintf("<TYPE>: %s", e.msg)
}

func exception() os.Error {
	if C.PyErr_Occurred() == nil {
		return nil
	}

	var t, v, tb *C.PyObject

	defer C.xdecref(v)

	C.PyErr_Fetch(&t, &v, &tb)

	s := C.PyObject_Str(v)
	defer C.decref(s)

	return &Error{t, tb, C.GoString(C.PyString_AsString(s))}
}

func raise(err os.Error) {
	var msg string
	var exc = C.PyExc_Exception

	e, ok := err.(*Error)
	if ok {
		exc = e.kind
		msg = e.msg
	} else {
		msg = err.String()
	}

	s := C.CString(msg)
	defer C.free(unsafe.Pointer(s))

	C.PyErr_SetString(exc, s)
}

func Err_Format(f string, args ...interface{}) {
	s := C.CString(fmt.Sprintf(f, args))
	defer C.free(unsafe.Pointer(s))
	C.PyErr_SetString(C.PyExc_Exception, s)
}

func KeyError(format string, args ...interface{}) os.Error {
	msg := fmt.Sprintf(format, args...)
	C.incref(C.PyExc_KeyError)
	return &Error{C.PyExc_KeyError, nil, msg}
}

func int2Err(i C.int) os.Error {
	if i < 0 {
		return exception()
	}
	return nil
}

{'fraction_non_alphanumeric': 0.15675855197608768, 'fraction_numerical': 0.003985386914646297, 'mean_word_length': 3.502242152466368, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '3766848', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1215, 'n_words': 324}