<commit_before>/*
 * A Python binding for libldap.
 *
 * Copyright (C) 2015 <NAME>
 *
 */

#include "libldap.h"


PyObject *
LDAPObject_set_option(LDAPObject *self, PyObject *args)
{
	int option;
	PyObject *value;
	int is_global = 0;
	LDAP *ctx = NULL;
	int rc;
	int integer = 0;
	char *string = NULL;
	double timeout;
	struct timeval tv;
	struct timeval *tvp = NULL;
	int i;
	Py_ssize_t size;
	char **referral_urls = NULL;
	void *ptr;

	if (self->ldap == NULL) {
		PyErr_SetString(LDAPError, "This instance has already been deallocated.");
		return NULL;
	}

	if (!PyArg_ParseTuple(args, "iO|i", &option, &value, &is_global))
		return NULL;

	if (!is_global)
		ctx = self->ldap;
	switch(option) {
		/* FIXME: SASL options are not included */
		case LDAP_OPT_CONNECT_ASYNC:
		case LDAP_OPT_REFERRALS:
		case LDAP_OPT_RESTART:
			ptr = PyObject_IsTrue(value) == 1 ? LDAP_OPT_ON : LDAP_OPT_OFF;
			break;
		case LDAP_OPT_DEBUG_LEVEL:
		case LDAP_OPT_DEREF:
		case LDAP_OPT_PROTOCOL_VERSION:
		case LDAP_OPT_RESULT_CODE:
		case LDAP_OPT_SIZELIMIT:
		case LDAP_OPT_TIMELIMIT:
		case LDAP_OPT_X_KEEPALIVE_IDLE:
		case LDAP_OPT_X_KEEPALIVE_PROBES:
		case LDAP_OPT_X_KEEPALIVE_INTERVAL:
		case LDAP_OPT_X_TLS_CRLCHECK:
		case LDAP_OPT_X_TLS_PROTOCOL_MIN:
		case LDAP_OPT_X_TLS_REQUIRE_CERT:
			integer = (int)PyLong_AsLong(value);
			ptr = &integer;
			break;
		case LDAP_OPT_DEFBASE:
		case LDAP_OPT_DIAGNOSTIC_MESSAGE:
		case LDAP_OPT_MATCHED_DN:
		case LDAP_OPT_URI:
		case LDAP_OPT_X_TLS_CACERTDIR:
		case LDAP_OPT_X_TLS_CACERTFILE:
		case LDAP_OPT_X_TLS_CERTFILE:
		case LDAP_OPT_X_TLS_CIPHER_SUITE:
		case LDAP_OPT_X_TLS_CRLFILE:
		case LDAP_OPT_X_TLS_DHFILE:
		case LDAP_OPT_X_TLS_KEYFILE:
		case LDAP_OPT_X_TLS_RANDOM_FILE:
			if ((string = PyUnicode_AsUTF8(value)) == NULL)
				return NULL;
			ptr = &string;
			break;
		case LDAP_OPT_NETWORK_TIMEOUT:
		case LDAP_OPT_TIMEOUT:
			if ((timeout = (double)PyFloat_AsDouble(value)) == -1)
				return NULL;
			if (timeout > 0) {
				tvp = &tv;
				double2timeval(tvp, timeout);
			} else {
				tvp = NULL;
			}
			ptr = tvp;
			break;
		case LDAP_OPT_REFERRAL_URLS:
			if (!PyList_Check(value))
				return NULL;
			size = PyList_GET_SIZE(value);
			referral_urls = (char **)PyMem_RawMalloc(sizeof(char *) * (size + 1));
			for (i = 0; i < size; i++) {
				referral_urls[i] = PyUnicode_AsUTF8(PyList_GET_ITEM(value, i));
			}
			referral_urls[size] = NULL;<commit_msg>Add SASL Option handling in Modules/set_option.c<commit_after>/*
 * A Python binding for libldap.
 *
 * Copyright (C) 2015 <NAME>
 *
 */

#include "libldap.h"


PyObject *
LDAPObject_set_option(LDAPObject *self, PyObject *args)
{
	int option;
	PyObject *value;
	int is_global = 0;
	LDAP *ctx = NULL;
	int rc;
	int integer = 0;
	ber_len_t bv_len;
	char *string = NULL;
	double timeout;
	struct timeval tv;
	struct timeval *tvp = NULL;
	int i;
	Py_ssize_t size;
	char **referral_urls = NULL;
	void *ptr;

	if (self->ldap == NULL) {
		PyErr_SetString(LDAPError, "This instance has already been deallocated.");
		return NULL;
	}

	if (!PyArg_ParseTuple(args, "iO|i", &option, &value, &is_global))
		return NULL;

	if (!is_global)
		ctx = self->ldap;
	switch(option) {
		/* FIXME: SASL options are not included */
		case LDAP_OPT_CONNECT_ASYNC:
		case LDAP_OPT_REFERRALS:
		case LDAP_OPT_RESTART:
			ptr = PyObject_IsTrue(value) == 1 ? LDAP_OPT_ON : LDAP_OPT_OFF;
			break;
		case LDAP_OPT_DEBUG_LEVEL:
		case LDAP_OPT_DEREF:
		case LDAP_OPT_PROTOCOL_VERSION:
		case LDAP_OPT_RESULT_CODE:
		case LDAP_OPT_SIZELIMIT:
		case LDAP_OPT_TIMELIMIT:
		case LDAP_OPT_X_KEEPALIVE_IDLE:
		case LDAP_OPT_X_KEEPALIVE_PROBES:
		case LDAP_OPT_X_KEEPALIVE_INTERVAL:
		case LDAP_OPT_X_TLS_CRLCHECK:
		case LDAP_OPT_X_TLS_PROTOCOL_MIN:
		case LDAP_OPT_X_TLS_REQUIRE_CERT:
		case LDAP_OPT_X_SASL_NOCANON:
			integer = (int)PyLong_AsLong(value);
			ptr = &integer;
			break;
		case LDAP_OPT_X_SASL_MAXBUFSIZE:
		case LDAP_OPT_X_SASL_SSF_EXTERNAL:
		case LDAP_OPT_X_SASL_SSF_MAX:
		case LDAP_OPT_X_SASL_SSF_MIN:
			bv_len = (ber_len_t)PyLong_AsLong(value);
			ptr = &bv_len;
			break;
		case LDAP_OPT_DEFBASE:
		case LDAP_OPT_DIAGNOSTIC_MESSAGE:
		case LDAP_OPT_MATCHED_DN:
		case LDAP_OPT_URI:
		case LDAP_OPT_X_TLS_CACERTDIR:
		case LDAP_OPT_X_TLS_CACERTFILE:
		case LDAP_OPT_X_TLS_CERTFILE:
		case LDAP_OPT_X_TLS_CIPHER_SUITE:
		case LDAP_OPT_X_TLS_CRLFILE:
		case LDAP_OPT_X_TLS_DHFILE:
		case LDAP_OPT_X_TLS_KEYFILE:
		case LDAP_OPT_X_TLS_RANDOM_FILE:
		case LDAP_OPT_X_SASL_SECPROPS:
			if ((string = PyUnicode_AsUTF8(value)) == NULL)
				return NULL;
			ptr = &string;
			break;
		case LDAP_OPT_NETWORK_TIMEOUT:
		case LDAP_OPT_TIMEOUT:
			if ((timeout = (double)PyFloat_AsDouble(value)) == -1)
				return NULL;
			if (timeout > 0) {
				tvp = &tv;
				double2timeval(tvp, timeout);
			} else {
				tvp = NULL;
			}
			ptr = tvp;
			break;
		case LDAP_OPT_REFERRAL_URLS:
			if (!PyList_Check(value))
				return NULL;
			size = PyList_GET_SIZE(value);
			referral_urls = (char **)PyMem_RawMalloc(sizeof(char *) * (size + 1));
			for (i = 0; i < size; i++) {
				referral_urls[i] = PyUnicode_AsUTF8(PyList_GET_ITEM(value, i));
			}
			referral_urls[size] = NULL;
{'fraction_non_alphanumeric': 0.09833204034134989, 'fraction_numerical': 0.005430566330488751, 'mean_word_length': 4.4, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '726487', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2234, 'n_words': 433}