static mrb_value
hs_regexp_get_match_data(mrb_state *mrb, mrb_value self, const char *str)
{
    mrb_value hs_match_data_cls, match_data;
    struct mrb_hs_regexp *reg;

    reg = (struct mrb_hs_regexp *)DATA_PTR(self);

    hs_match_data_cls = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), INTERN("HsMatchData"));
    match_data = mrb_funcall_argv(mrb, hs_match_data_cls, INTERN("new"), 0, NULL);

    mrb_iv_set(mrb, match_data, INTERN("@string"), mrb_str_new_cstr(mrb, str));

    {
        mrb_value args[2];
        mrb_sym sym_push;
        size_t i;

        sym_push = INTERN("push");
        for (i = 0; i < sizeof(reg->reg->startp)/sizeof(reg->reg->startp[0]); i++){
            if (reg->reg->startp[i] && reg->reg->endp[i]){
                args[0] = mrb_fixnum_value(reg->reg->startp[i] - str);
                args[1] = mrb_fixnum_value(reg->reg->endp[i] - reg->reg->startp[i]);
                mrb_funcall_argv(mrb, match_data, sym_push, sizeof(args)/sizeof(args[0]), &args[0]);
            }else{
                mrb_funcall_argv(mrb, match_data, sym_push, 0, NULL);
            }
        }
    }

    return match_data;
}
Example #2
0
void SGMLName::init()
{
#define INTERN(n) intern(#n, NAMECASE);
  INTERN(IMPLIED);
  INTERN(CDATA);
  INTERN(NOTATION);
  INTERN(TOKEN);
  INTERN(ENTITY);
}
static void
hs_regexp_init(mrb_state *mrb, mrb_value self, mrb_value str, unsigned char flag)
{
    struct mrb_hs_regexp *reg;
    regexp_info ri = { mrb, flag };

    if (flag & ~REGEXP_FLAG_ALL){
        mrb_raise(mrb, E_ARGUMENT_ERROR, "Invalid flag.");
    }

    if (!DATA_PTR(self)){
        DATA_PTR(self) = mrb_malloc(mrb, sizeof(struct mrb_hs_regexp));
        DATA_TYPE(self) = &mrb_hs_regexp_type;
        reg = (struct mrb_hs_regexp *)DATA_PTR(self);
    }else{
        Data_Get_Struct(mrb, self, &mrb_hs_regexp_type, reg);
        mrb_free(mrb, reg->reg);
    }

    reg->reg = hs_regcomp(&ri, RSTRING_PTR(str));
    if (!reg->reg){
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "'%s' is an invalid regular expression because %s.",
                   RSTRING_PTR(str), ri.error_msg);
    }
    reg->flag = flag;
    mrb_iv_set(mrb, self, INTERN("@source"), str);
}
static mrb_value
hs_regexp_equal(mrb_state *mrb, mrb_value self)
{
    mrb_value other;
    struct mrb_hs_regexp *self_reg, *other_reg;

    mrb_get_args(mrb, "o", &other);

    if (mrb_obj_equal(mrb, self, other)){
        return mrb_true_value();
    }

    if (mrb_type(other) != MRB_TT_DATA || DATA_TYPE(other) != &mrb_hs_regexp_type){
        return mrb_false_value();
    }

    self_reg = (struct mrb_hs_regexp *)DATA_PTR(self);
    other_reg = (struct mrb_hs_regexp *)DATA_PTR(other);
    if (!self_reg || !other_reg){
        mrb_raise(mrb, E_RUNTIME_ERROR, "Invalid HsRegexp");
    }

    if (self_reg->flag != other_reg->flag){
        return mrb_false_value();
    }

    return mrb_str_equal(mrb, mrb_iv_get(mrb, self, INTERN("@source")), mrb_iv_get(mrb, other, INTERN("@source"))) ?
        mrb_true_value() : mrb_false_value();
}
static mrb_value
hs_regexp_match(mrb_state *mrb, mrb_value self)
{
    const char *str;
    struct mrb_hs_regexp *reg;
    mrb_value m;
    regexp_info ri = { mrb };

    mrb_get_args(mrb, "z", &str);

    Data_Get_Struct(mrb, self, &mrb_hs_regexp_type, reg);
    if (!reg->reg){
        mrb_raise(mrb, E_ARGUMENT_ERROR, "HsRegexp is not initialized.");
    }

    ri.flag = reg->flag;
    if (hs_regexec(&ri, reg->reg, str)){
        m = hs_regexp_get_match_data(mrb, self, str);
    }else{
        m = mrb_nil_value();
    }

    mrb_obj_iv_set(mrb, (struct RObject *)mrb_class_real(RDATA(self)->c), INTERN("@last_match"), m);
    return m;
}
Example #6
0
static int
changed(cPersistentObject *self)
{
    if ((self->state == cPersistent_UPTODATE_STATE ||
            self->state == cPersistent_STICKY_STATE)
            && self->jar)
    {
        PyObject *meth, *arg, *result;
        static PyObject *s_register;

        if (s_register == NULL)
            s_register = INTERN("register");
        meth = PyObject_GetAttr((PyObject *)self->jar, s_register);
        if (meth == NULL)
            return -1;
        arg = PyTuple_New(1);
        if (arg == NULL)
        {
            Py_DECREF(meth);
            return -1;
        }
        Py_INCREF(self);
        PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
        result = PyEval_CallObject(meth, arg);
        Py_DECREF(arg);
        Py_DECREF(meth);
        if (result == NULL)
            return -1;
        Py_DECREF(result);

        self->state = cPersistent_CHANGED_STATE;
    }

    return 0;
}
Example #7
0
dbus_bool_t
dbus_py_init_abstract(void)
{
    _dbus_py_variant_levels = PyDict_New();
    if (!_dbus_py_variant_levels) return 0;

    dbus_py__dbus_object_path__const = INTERN("__dbus_object_path__");
    if (!dbus_py__dbus_object_path__const) return 0;

    dbus_py_variant_level_const = INTERN("variant_level");
    if (!dbus_py_variant_level_const) return 0;

    dbus_py_signature_const = INTERN("signature");
    if (!dbus_py_signature_const) return 0;

#ifdef PY3
    DBusPyBytesBase_Type.tp_base = &PyBytes_Type;
    if (PyType_Ready(&DBusPyBytesBase_Type) < 0) return 0;
    DBusPyBytesBase_Type.tp_print = NULL;
#else
    DBusPyIntBase_Type.tp_base = &PyInt_Type;
    if (PyType_Ready(&DBusPyIntBase_Type) < 0) return 0;
    /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
    desired */
    DBusPyIntBase_Type.tp_print = NULL;
#endif

    DBusPyFloatBase_Type.tp_base = &PyFloat_Type;
    if (PyType_Ready(&DBusPyFloatBase_Type) < 0) return 0;
    DBusPyFloatBase_Type.tp_print = NULL;

    DBusPyLongBase_Type.tp_base = &PyLong_Type;
    if (PyType_Ready(&DBusPyLongBase_Type) < 0) return 0;
    DBusPyLongBase_Type.tp_print = NULL;

    DBusPyStrBase_Type.tp_base = &NATIVESTR_TYPE;
    if (PyType_Ready(&DBusPyStrBase_Type) < 0) return 0;
    DBusPyStrBase_Type.tp_print = NULL;

    return 1;
}
static mrb_value
hs_regexp_initialize_copy(mrb_state *mrb, mrb_value copy)
{
    mrb_value src;
    struct mrb_hs_regexp *reg;

    mrb_get_args(mrb, "o", &src);
    if (mrb_obj_equal(mrb, copy, src)){
        return copy;
    }
    if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))){
        mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
    }

    Data_Get_Struct(mrb, src, &mrb_hs_regexp_type, reg);
    hs_regexp_init(mrb, copy, mrb_funcall_argv(mrb, src, INTERN("source"), 0, NULL), reg->flag);
    return copy;
}
Example #9
0
static int
readCurrent(cPersistentObject *self)
{
    if ((self->state == cPersistent_UPTODATE_STATE ||
            self->state == cPersistent_STICKY_STATE)
            && self->jar && self->oid)
    {
        static PyObject *s_readCurrent=NULL;
        PyObject *r;

        if (s_readCurrent == NULL)
            s_readCurrent = INTERN("readCurrent");

        r = PyObject_CallMethodObjArgs(self->jar, s_readCurrent, self, NULL);
        if (r == NULL)
            return -1;

        Py_DECREF(r);
    }

    return 0;
}
Example #10
0
static void
ghostify(cPersistentObject *self)
{
    PyObject **dictptr, *slotnames;

    /* are we already a ghost? */
    if (self->state == cPersistent_GHOST_STATE)
        return;

    /* Is it ever possible to not have a cache? */
    if (self->cache == NULL) 
    {
        self->state = cPersistent_GHOST_STATE;
        return;
    }

    if (self->ring.r_next == NULL)
    {
        /* There's no way to raise an error in this routine. */
#ifdef Py_DEBUG
        fatal_1350(self, "ghostify", "claims to be in a cache but isn't");
#else
        return;
#endif
    }

    /* If we're ghostifying an object, we better have some non-ghosts. */
    assert(self->cache->non_ghost_count > 0);
    self->cache->non_ghost_count--;
    self->cache->total_estimated_size -= 
        _estimated_size_in_bytes(self->estimated_size);
    ring_del(&self->ring);
    self->state = cPersistent_GHOST_STATE;

    /* clear __dict__ */
    dictptr = _PyObject_GetDictPtr((PyObject *)self);
    if (dictptr && *dictptr)
    {
        Py_DECREF(*dictptr);
        *dictptr = NULL;
    }

    /* clear all slots besides _p_*
     * ( for backward-compatibility reason we do this only if class does not
     *   override __new__ ) */
    if (Py_TYPE(self)->tp_new == Pertype.tp_new)
    {
        slotnames = pickle_slotnames(Py_TYPE(self));
        if (slotnames && slotnames != Py_None)
        {
            int i;

            for (i = 0; i < PyList_GET_SIZE(slotnames); i++)
            {
                PyObject *name;
                char *cname;
                int is_special;

                name = PyList_GET_ITEM(slotnames, i);
#ifdef PY3K
                if (PyUnicode_Check(name))
                {
                    PyObject *converted = convert_name(name);
                    cname = PyBytes_AS_STRING(converted);
#else
                if (PyBytes_Check(name))
                {
                    cname = PyBytes_AS_STRING(name);
#endif
                    is_special = !strncmp(cname, "_p_", 3);
#ifdef PY3K
                    Py_DECREF(converted);
#endif
                    if (is_special) /* skip persistent */
                    {
                        continue;
                    }
                }

                /* NOTE: this skips our delattr hook */
                if (PyObject_GenericSetAttr((PyObject *)self, name, NULL) < 0)
                    /* delattr of non-set slot will raise AttributeError - we
                     * simply ignore. */
                    PyErr_Clear();
            }
        }
        Py_XDECREF(slotnames);
    }

    /* We remove the reference to the just ghosted object that the ring
    * holds.  Note that the dictionary of oids->objects has an uncounted
    * reference, so if the ring's reference was the only one, this frees
    * the ghost object.  Note further that the object's dealloc knows to
    * inform the dictionary that it is going away.
    */
    Py_DECREF(self);
}

static int
changed(cPersistentObject *self)
{
    if ((self->state == cPersistent_UPTODATE_STATE ||
        self->state == cPersistent_STICKY_STATE)
        && self->jar)
    {
        PyObject *meth, *arg, *result;
        static PyObject *s_register;

        if (s_register == NULL)
            s_register = INTERN("register");
        meth = PyObject_GetAttr((PyObject *)self->jar, s_register);
        if (meth == NULL)
            return -1;
        arg = PyTuple_New(1);
        if (arg == NULL) 
        {
            Py_DECREF(meth);
            return -1;
        }
        Py_INCREF(self);
        PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
        result = PyEval_CallObject(meth, arg);
        Py_DECREF(arg);
        Py_DECREF(meth);
        if (result == NULL)
            return -1;
        Py_DECREF(result);

        self->state = cPersistent_CHANGED_STATE;
    }

    return 0;
}

static int
readCurrent(cPersistentObject *self)
{
    if ((self->state == cPersistent_UPTODATE_STATE ||
        self->state == cPersistent_STICKY_STATE)
        && self->jar && self->oid)
    {
        static PyObject *s_readCurrent=NULL;
        PyObject *r;

        if (s_readCurrent == NULL)
            s_readCurrent = INTERN("readCurrent");

        r = PyObject_CallMethodObjArgs(self->jar, s_readCurrent, self, NULL);
        if (r == NULL)
            return -1;

        Py_DECREF(r);
    }

    return 0;
}

static PyObject *
Per__p_deactivate(cPersistentObject *self)
{
    if (self->state == cPersistent_UPTODATE_STATE && self->jar)
    {
        PyObject **dictptr = _PyObject_GetDictPtr((PyObject *)self);
        if (dictptr && *dictptr)
        {
            Py_DECREF(*dictptr);
            *dictptr = NULL;
        }
        /* Note that we need to set to ghost state unless we are
            called directly. Methods that override this need to
            do the same! */
        ghostify(self);
        if (PyErr_Occurred())
            return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
Per__p_activate(cPersistentObject *self)
{
    if (unghostify(self) < 0)
        return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}

static int Per_set_changed(cPersistentObject *self, PyObject *v);

static PyObject *
Per__p_invalidate(cPersistentObject *self)
{
    signed char old_state = self->state;

    if (old_state != cPersistent_GHOST_STATE)
    {
        if (Per_set_changed(self, NULL) < 0)
            return NULL;
        ghostify(self);
        if (PyErr_Occurred())
            return NULL;
    }
    Py_INCREF(Py_None);
    return Py_None;
}
static void init_builtin_keywords()
{
#define STRING(s)    SG_MAKE_STRING(s)
#define INTERN(s, i) \
  Sg_BuiltinKeywords[i].name = STRING(s); \
  Sg_HashTableSet(keywords.table, Sg_BuiltinKeywords[i].name, SG_OBJ(&Sg_BuiltinKeywords[i]), 0)
  INTERN("lambda-list", 0);
  INTERN("qualifier", 1);
  INTERN("specializers", 2);
  INTERN("generic", 3);
  INTERN("procedure", 4);
  INTERN("definition-name", 5);
  INTERN("primary", 6);
  INTERN("before", 7);
  INTERN("after", 8);
  INTERN("around", 9);
  INTERN("init-value", 10);
  INTERN("init-keyword", 11);
  INTERN("init-thunk", 12);
  INTERN("all", 13);
  INTERN("export-reader-macro", 14);
  INTERN("export-reader", 15);
  INTERN("pipe", 16);
  INTERN("stdin", 17);
  INTERN("stdout", 18);
  INTERN("stderr", 19);
  INTERN("null", 20);
#undef INTERN
}
Example #12
0
static PyObject*
module_init(void)
{
    PyObject *module, *mod_dict, *interfaces, *conflicterr;

#ifdef KEY_TYPE_IS_PYOBJECT
    object_ = PyTuple_GetItem(Py_TYPE(Py_None)->tp_bases, 0);
    if (object_ == NULL)
      return NULL;
#endif

    sort_str = INTERN("sort");
    if (!sort_str)
        return NULL;
    reverse_str = INTERN("reverse");
    if (!reverse_str)
        return NULL;
    __setstate___str = INTERN("__setstate__");
    if (!__setstate___str)
        return NULL;
    _bucket_type_str = INTERN("_bucket_type");
    if (!_bucket_type_str)
        return NULL;

    /* Grab the ConflictError class */
    interfaces = PyImport_ImportModule("BTrees.Interfaces");
    if (interfaces != NULL)
    {
        conflicterr = PyObject_GetAttrString(interfaces, "BTreesConflictError");
        if (conflicterr != NULL)
            ConflictError = conflicterr;
        Py_DECREF(interfaces);
    }

    if (ConflictError == NULL)
    {
        Py_INCREF(PyExc_ValueError);
        ConflictError=PyExc_ValueError;
    }

    /* Initialize the PyPersist_C_API and the type objects. */
#ifdef PY3K
    cPersistenceCAPI = (cPersistenceCAPIstruct *)PyCapsule_Import(
                "persistent.cPersistence.CAPI", 0);
#else
    cPersistenceCAPI = (cPersistenceCAPIstruct *)PyCObject_Import(
                "persistent.cPersistence", "CAPI");
#endif
    if (cPersistenceCAPI == NULL)
        return NULL;

#ifdef PY3K
#define _SET_TYPE(typ) ((PyObject*)(&typ))->ob_type = &PyType_Type
#else
#define _SET_TYPE(typ) (typ).ob_type = &PyType_Type
#endif
    _SET_TYPE(BTreeItemsType);
    _SET_TYPE(BTreeIter_Type);
    BTreeIter_Type.tp_getattro = PyObject_GenericGetAttr;
    BucketType.tp_new = PyType_GenericNew;
    SetType.tp_new = PyType_GenericNew;
    BTreeType.tp_new = PyType_GenericNew;
    TreeSetType.tp_new = PyType_GenericNew;
    if (!init_persist_type(&BucketType))
	    return NULL;
    if (!init_persist_type(&BTreeType))
	    return NULL;
    if (!init_persist_type(&SetType))
	    return NULL;
    if (!init_persist_type(&TreeSetType))
	    return NULL;

    if (PyDict_SetItem(BTreeType.tp_dict, _bucket_type_str,
		       (PyObject *)&BucketType) < 0)
    {
        fprintf(stderr, "btree failed\n");
        return NULL;
    }
    if (PyDict_SetItem(TreeSetType.tp_dict, _bucket_type_str,
		       (PyObject *)&SetType) < 0)
    {
        fprintf(stderr, "bucket failed\n");
        return NULL;
    }

    /* Create the module and add the functions */
#ifdef PY3K
    module = PyModule_Create(&moduledef);
#else
    module = Py_InitModule4("_" MOD_NAME_PREFIX "BTree",
		       module_methods, BTree_module_documentation,
		       (PyObject *)NULL, PYTHON_API_VERSION);
#endif

    /* Add some symbolic constants to the module */
    mod_dict = PyModule_GetDict(module);
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Bucket",
			     (PyObject *)&BucketType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "BTree",
			     (PyObject *)&BTreeType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Set",
			     (PyObject *)&SetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeSet",
			     (PyObject *)&TreeSetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeIterator",
			     (PyObject *)&BTreeIter_Type) < 0)
        return NULL;
	/* We also want to be able to access these constants without the prefix
	 * so that code can more easily exchange modules (particularly the integer
	 * and long modules, but also others).  The TreeIterator is only internal,
	 * so we don't bother to expose that.
     */
    if (PyDict_SetItemString(mod_dict, "Bucket",
			     (PyObject *)&BucketType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "BTree",
			     (PyObject *)&BTreeType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "Set",
			     (PyObject *)&SetType) < 0)
        return NULL;
    if (PyDict_SetItemString(mod_dict, "TreeSet",
			     (PyObject *)&TreeSetType) < 0)
        return NULL;
#if defined(ZODB_64BIT_INTS) && defined(NEED_LONG_LONG_SUPPORT)
    if (PyDict_SetItemString(mod_dict, "using64bits", Py_True) < 0)
        return NULL;
#else
    if (PyDict_SetItemString(mod_dict, "using64bits", Py_False) < 0)
        return NULL;
#endif
    return module;
}