bson/_cbsonmodule.c
/*
 * Encode a builtin Python regular expression or our custom Regex class.
 *
 * Sets exception and returns 0 on failure.
 */
Encode a builtin Python regular expression or our custom Regex class.
Sets exception and returns 0 on failure.
Encode a builtin Python regular expression or our custom Regex class . Sets exception and returns 0 on failure .
static int _write_regex_to_buffer(
    buffer_t buffer, int type_byte, PyObject* value) {
    PyObject* py_flags;
    PyObject* py_pattern;
    PyObject* encoded_pattern;
    long int_flags;
    char flags[FLAGS_SIZE];
    char check_utf8 = 0;
    const char* pattern_data;
    int pattern_length, flags_length;
    result_t status;
    py_flags = PyObject_GetAttrString(value, "flags");
    if (!py_flags) {
        return 0;
    }
#if PY_MAJOR_VERSION >= 3
    int_flags = PyLong_AsLong(py_flags);
#else
    int_flags = PyInt_AsLong(py_flags);
#endif
    Py_DECREF(py_flags);
    py_pattern = PyObject_GetAttrString(value, "pattern");
    if (!py_pattern) {
        return 0;
    }
    if (PyUnicode_Check(py_pattern)) {
        encoded_pattern = PyUnicode_AsUTF8String(py_pattern);
        Py_DECREF(py_pattern);
        if (!encoded_pattern) {
            return 0;
        }
    } else {
        encoded_pattern = py_pattern;
        check_utf8 = 1;
    }
#if PY_MAJOR_VERSION >= 3
    if (!(pattern_data = PyBytes_AsString(encoded_pattern))) {
        Py_DECREF(encoded_pattern);
        return 0;
    }
    if ((pattern_length = _downcast_and_check(PyBytes_Size(encoded_pattern), 0)) == -1) {
        Py_DECREF(encoded_pattern);
        return 0;
    }
#else
    if (!(pattern_data = PyString_AsString(encoded_pattern))) {
        Py_DECREF(encoded_pattern);
        return 0;
    }
    if ((pattern_length = _downcast_and_check(PyString_Size(encoded_pattern), 0)) == -1) {
        Py_DECREF(encoded_pattern);
        return 0;
    }
#endif
    status = check_string((const unsigned char*)pattern_data,
                          pattern_length, check_utf8, 1);
    if (status == NOT_UTF_8) {
        PyObject* InvalidStringData = _error("InvalidStringData");
        if (InvalidStringData) {
            PyErr_SetString(InvalidStringData,
                            "regex patterns must be valid UTF-8");
            Py_DECREF(InvalidStringData);
        }
        Py_DECREF(encoded_pattern);
        return 0;
    } else if (status == HAS_NULL) {
        PyObject* InvalidDocument = _error("InvalidDocument");
        if (InvalidDocument) {
            PyErr_SetString(InvalidDocument,
                            "regex patterns must not contain the NULL byte");
            Py_DECREF(InvalidDocument);
        }
        Py_DECREF(encoded_pattern);
        return 0;
    }
    if (!buffer_write_bytes(buffer, pattern_data, pattern_length + 1)) {
        Py_DECREF(encoded_pattern);
        return 0;
    }
    Py_DECREF(encoded_pattern);
    flags[0] = 0;
    if (int_flags & 2) {
        STRCAT(flags, FLAGS_SIZE, "i");
    }
    if (int_flags & 4) {
        STRCAT(flags, FLAGS_SIZE, "l");
    }
    if (int_flags & 8) {
        STRCAT(flags, FLAGS_SIZE, "m");
    }
    if (int_flags & 16) {
        STRCAT(flags, FLAGS_SIZE, "s");
    }
    if (int_flags & 32) {
        STRCAT(flags, FLAGS_SIZE, "u");
    }
    if (int_flags & 64) {
        STRCAT(flags, FLAGS_SIZE, "x");
    }
    flags_length = (int)strlen(flags) + 1;
    if (!buffer_write_bytes(buffer, flags, flags_length)) {
        return 0;
    }
    *(buffer_get_buffer(buffer) + type_byte) = 0x0B;
    return 1;
}
static int _write_regex_to_buffer ( buffer_t buffer , int type_byte , PyObject * value ) { PyObject * py_flags ; PyObject * py_pattern ; PyObject * encoded_pattern ; long int_flags ; char flags [ FLAGS_SIZE ] ; char check_utf8 = 0 ; const char * pattern_data ; int pattern_length , flags_length ; result_t status ; py_flags = PyObject_GetAttrString ( value , " " ) ; if ( ! py_flags ) { return 0 ; } #if PY_MAJOR_VERSION >= 3 int_flags = PyLong_AsLong ( py_flags ) ; #else int_flags = PyInt_AsLong ( py_flags ) ; #endif Py_DECREF ( py_flags ) ; py_pattern = PyObject_GetAttrString ( value , " " ) ; if ( ! py_pattern ) { return 0 ; } if ( PyUnicode_Check ( py_pattern ) ) { encoded_pattern = PyUnicode_AsUTF8String ( py_pattern ) ; Py_DECREF ( py_pattern ) ; if ( ! encoded_pattern ) { return 0 ; } } else { encoded_pattern = py_pattern ; check_utf8 = 1 ; } #if PY_MAJOR_VERSION >= 3 if ( ! ( pattern_data = PyBytes_AsString ( encoded_pattern ) ) ) { Py_DECREF ( encoded_pattern ) ; return 0 ; } if ( ( pattern_length = _downcast_and_check ( PyBytes_Size ( encoded_pattern ) , 0 ) ) == -1 ) { Py_DECREF ( encoded_pattern ) ; return 0 ; } #else if ( ! ( pattern_data = PyString_AsString ( encoded_pattern ) ) ) { Py_DECREF ( encoded_pattern ) ; return 0 ; } if ( ( pattern_length = _downcast_and_check ( PyString_Size ( encoded_pattern ) , 0 ) ) == -1 ) { Py_DECREF ( encoded_pattern ) ; return 0 ; } #endif status = check_string ( ( const unsigned char * ) pattern_data , pattern_length , check_utf8 , 1 ) ; if ( status == NOT_UTF_8 ) { PyObject * InvalidStringData = _error ( " " ) ; if ( InvalidStringData ) { PyErr_SetString ( InvalidStringData , " " ) ; Py_DECREF ( InvalidStringData ) ; } Py_DECREF ( encoded_pattern ) ; return 0 ; } else if ( status == HAS_NULL ) { PyObject * InvalidDocument = _error ( " " ) ; if ( InvalidDocument ) { PyErr_SetString ( InvalidDocument , " " ) ; Py_DECREF ( InvalidDocument ) ; } Py_DECREF ( encoded_pattern ) ; return 0 ; } if ( ! buffer_write_bytes ( buffer , pattern_data , pattern_length + 1 ) ) { Py_DECREF ( encoded_pattern ) ; return 0 ; } Py_DECREF ( encoded_pattern ) ; flags [ 0 ] = 0 ; if ( int_flags & 2 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } if ( int_flags & 4 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } if ( int_flags & 8 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } if ( int_flags & 16 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } if ( int_flags & 32 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } if ( int_flags & 64 ) { STRCAT ( flags , FLAGS_SIZE , " " ) ; } flags_length = ( int ) strlen ( flags ) + 1 ; if ( ! buffer_write_bytes ( buffer , flags , flags_length ) ) { return 0 ; } * ( buffer_get_buffer ( buffer ) + type_byte ) = 0x0B ; return 1 ; }
Encode a builtin Python regular expression or our custom Regex class.
Encode a builtin Python regular expression or our custom Regex class .
/*
     * Both the builtin re type and our Regex class have attributes
     * "flags" and "pattern".
     */