<commit_before>import re
import os.path as op
from .base import (tmpl_replace, OBJCTYPE2SPEC, copy_objp_unit, ArgSpec, MethodSpec, ClassSpec,
    get_objc_signature)

TEMPLATE_UNIT = """
#define PY_SSIZE_T_CLEAN
#import <Python.h>
#import "structmember.h"
#import "ObjP.h"

%%classes%%

static PyMethodDef module_methods[] = {
    {NULL}  /* Sentinel */
};

static struct PyModuleDef %%modulename%%Def = {
    PyModuleDef_HEAD_INIT,
    "%%modulename%%",
    NULL,
    -1,
    module_methods,
    NULL,
    NULL,
    NULL,
    NULL
};

PyObject *
PyInit_%%modulename%%(void)
{
    PyObject *m;
    m = PyModule_Create(&%%modulename%%Def);
    if (m == NULL) {
        return NULL;
    }
    
    %%clsaddtomod%%
    
    return m;
}

"""

TEMPLATE_CLASS = """
%%objcinterface%%

typedef struct {
    PyObject_HEAD
    %%typedecl%%objc_ref;
} %%clsname%%_Struct;

static PyTypeObject %%clsname%%_Type; /* Forward declaration */

/* Methods */

static void
%%clsname%%_dealloc(%%clsname%%_Struct *self)
{
    [self->objc_ref release];
    Py_TYPE(self)->tp_free((PyObject *)self);
}

%%initfunc%%

%%methods%%

static PyMethodDef %%clsname%%_methods[] = {
 %%methodsdef%%
{NULL}  /* Sentinel */
};

static PyTypeObject %%clsname%%_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "%%modulename%%.%%clsname%%", /*tp_name*/
    sizeof(%%clsname%%_Struct), /*tp_basicsize*/
    0, /*tp_itemsize*/
    (destructor)%%clsname%%_dealloc, /*tp_dealloc*/
    0, /*tp_print*/
    0, /*tp_getattr*/
    0, /*tp_setattr*/
    0, /*tp_reserved*/
    0, /*tp_repr*/
    0, /*tp_as_number*/
    0, /*tp_as_sequence*/
    0, /*tp_as_mapping*/
    0, /*tp_hash */
    0, /*tp_call*/
    0, /*tp_str*/
    0, /*tp_getattro*/
    0, /*tp_setattro*/
    0, /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "%%clsname%% object", /* tp_doc */
    0, /* tp_traverse */
    0, /* tp_clear */
    0, /* tp_richcompare */
    0, /* tp_weaklistoffset */
    0, /* tp_iter */
    0, /* tp_iternext */
    %%clsname%%_methods,/* tp_methods */
    0, /* tp_members */
    0, /* tp_getset */
    0, /* tp_base */
    0, /* tp_dict */
    0, /* tp_descr_get */
    0, /* tp_descr_set */
    0, /* tp_dictoffset */
    (initproc)%%clsname%%_init,      /* tp_init */
    0, /* tp_alloc */
    0, /* tp_new */
    0, /* tp_free */
    0, /* tp_is_gcc */
    0, /* tp_bases */
    0, /* tp_mro */
    0, /* tp_cache */
    0, /* tp_subclasses */
    0  /* tp_weaklist */
};
"""

TEMPLATE_CLASS_ADD_TO_MOD = """
    %%clsname%%_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready(&%%clsname%%_Type) < 0) {
        return NULL;
    }
    Py_INCREF(&%%clsname%%_Type);
    PyModule_AddObject(m, "%%clsname%%", (PyObject *)&%%clsname%%_Type);
"""

TEMPLATE_TARGET_PROTOCOL = """
@protocol %%clsname%% <NSObject>
%%methods%%
@end
"""

TEMPLATE_TARGET_INTERFACE = """
@interface %%clsname%%: NSObject {}
%%methods%%
@end
"""

TEMPLATE_INITFUNC_CREATE = """
static int
%%clsname%%_init(%%clsname%%_Struct *self, PyObject *args, PyObject *kwds)
{
    PyObject *pRefCapsule = NULL;
    if (!PyArg_ParseTuple(args, "|O", &pRefCapsule)) {
        return -1;
    }
    
    if (pRefCapsule == NULL) {
        self->objc_ref = %%objc_create%%
    }
    else {
        self->objc_ref = PyCapsule_GetPointer(pRefCapsule, NULL);
        [self->objc_ref retain];
    }
    
    return 0;
}
"""

TEMPLATE_METHOD_NOARGS = """
static PyObject *
%%clsname%%_%%methname%%(%%clsname%%_Struct *self)
{<commit_msg>p2o now optionally doesn't retain the reference object.<commit_after>import re
import os.path as op
from .base import (tmpl_replace, OBJCTYPE2SPEC, copy_objp_unit, ArgSpec, MethodSpec, ClassSpec,
    get_objc_signature)

TEMPLATE_UNIT = """
#define PY_SSIZE_T_CLEAN
#import <Python.h>
#import "structmember.h"
#import "ObjP.h"

%%classes%%

static PyMethodDef module_methods[] = {
    {NULL}  /* Sentinel */
};

static struct PyModuleDef %%modulename%%Def = {
    PyModuleDef_HEAD_INIT,
    "%%modulename%%",
    NULL,
    -1,
    module_methods,
    NULL,
    NULL,
    NULL,
    NULL
};

PyObject *
PyInit_%%modulename%%(void)
{
    PyObject *m;
    m = PyModule_Create(&%%modulename%%Def);
    if (m == NULL) {
        return NULL;
    }
    
    %%clsaddtomod%%
    
    return m;
}

"""

TEMPLATE_CLASS = """
%%objcinterface%%

typedef struct {
    PyObject_HEAD
    %%typedecl%%objc_ref;
    unsigned char is_retained;
} %%clsname%%_Struct;

static PyTypeObject %%clsname%%_Type; /* Forward declaration */

/* Methods */

static void
%%clsname%%_dealloc(%%clsname%%_Struct *self)
{
    if (self->is_retained) {
        [self->objc_ref release];
    }
    Py_TYPE(self)->tp_free((PyObject *)self);
}

%%initfunc%%

%%methods%%

static PyMethodDef %%clsname%%_methods[] = {
 %%methodsdef%%
{NULL}  /* Sentinel */
};

static PyTypeObject %%clsname%%_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "%%modulename%%.%%clsname%%", /*tp_name*/
    sizeof(%%clsname%%_Struct), /*tp_basicsize*/
    0, /*tp_itemsize*/
    (destructor)%%clsname%%_dealloc, /*tp_dealloc*/
    0, /*tp_print*/
    0, /*tp_getattr*/
    0, /*tp_setattr*/
    0, /*tp_reserved*/
    0, /*tp_repr*/
    0, /*tp_as_number*/
    0, /*tp_as_sequence*/
    0, /*tp_as_mapping*/
    0, /*tp_hash */
    0, /*tp_call*/
    0, /*tp_str*/
    0, /*tp_getattro*/
    0, /*tp_setattro*/
    0, /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "%%clsname%% object", /* tp_doc */
    0, /* tp_traverse */
    0, /* tp_clear */
    0, /* tp_richcompare */
    0, /* tp_weaklistoffset */
    0, /* tp_iter */
    0, /* tp_iternext */
    %%clsname%%_methods,/* tp_methods */
    0, /* tp_members */
    0, /* tp_getset */
    0, /* tp_base */
    0, /* tp_dict */
    0, /* tp_descr_get */
    0, /* tp_descr_set */
    0, /* tp_dictoffset */
    (initproc)%%clsname%%_init,      /* tp_init */
    0, /* tp_alloc */
    0, /* tp_new */
    0, /* tp_free */
    0, /* tp_is_gcc */
    0, /* tp_bases */
    0, /* tp_mro */
    0, /* tp_cache */
    0, /* tp_subclasses */
    0  /* tp_weaklist */
};
"""

TEMPLATE_CLASS_ADD_TO_MOD = """
    %%clsname%%_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready(&%%clsname%%_Type) < 0) {
        return NULL;
    }
    Py_INCREF(&%%clsname%%_Type);
    PyModule_AddObject(m, "%%clsname%%", (PyObject *)&%%clsname%%_Type);
"""

TEMPLATE_TARGET_PROTOCOL = """
@protocol %%clsname%% <NSObject>
%%methods%%
@end
"""

TEMPLATE_TARGET_INTERFACE = """
@interface %%clsname%%: NSObject {}
%%methods%%
@end
"""

TEMPLATE_INITFUNC_CREATE = """
static int
%%clsname%%_init(%%clsname%%_Struct *self, PyObject *args, PyObject *kwds)
{
    PyObject *pRefCapsule = NULL;
    unsigned char should_retain = 1;
    if (!PyArg_ParseTuple(args, "|Ob", &pRefCapsule, &should_retain)) {
        return -1;
    }
    
    if (pRefCapsule == NULL) {
        self->objc_ref = %%objc_create%%
        self->is_retained = 1;
    }
    else {
        self->objc_ref = PyCapsule_GetPointer(pRefCapsule, NULL);
        self->is_retained = should_retain;
        if (should_retain) {
            [self->objc_ref retain];
        }
    }
    
    return 0;
}
"""

TEMPLATE_METHOD_NOARGS = """
static PyObject *
%%clsname%%_%%methname%%(%%clsname%%_Struct *self)
{
{'fraction_non_alphanumeric': 0.18278383225272452, 'fraction_numerical': 0.012277555524899985, 'mean_word_length': 2.8915727321524423, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, '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': '6735933', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2867, 'n_words': 509}