Exemplo n.º 1
0
PyObject* JySync_Init_PyMethodDescr_From_JyMethodDescr(jobject src, PyTypeObject* nonNativeSubtype)
{
//	jputs(__FUNCTION__);
	env(NULL);
	jint max = (*env)->CallIntMethod(env, src, pyBuiltinCallableInfo_getMaxargs);
	//mdef->ml_flags = (max ? (METH_KEYWORDS | METH_VARARGS) : METH_NOARGS) | METH_JYTHON;

	jstring jtmp = (*env)->CallObjectMethod(env, src, pyBuiltinCallableInfo_getName);
	global_cstr_from_jstring(cName, jtmp);
	//mdef->ml_name = cName;

	jobject dtype = (*env)->GetObjectField(env, src, pyDescr_dtypeField);

	PyMethodDef* mdef = malloc(sizeof(PyMethodDef));
	mdef->ml_flags = (max ? (METH_KEYWORDS | METH_VARARGS) : METH_NOARGS)
			| METH_JYTHON | METH_JYTHON_CDEF;
	mdef->ml_name = cName;

	jtmp = (*env)->CallObjectMethod(env, src, pyMethodDescr_getDoc);
	if (jtmp)
	{
		global_cstr_from_jstring2(cDoc, jtmp);
		mdef->ml_doc = cDoc;
	} else mdef->ml_doc = NULL;
	mdef->ml_meth = (PyCFunctionWithKeywords) jyBuiltinCallWithKeywords;

	PyObject* result = PyDescr_NewMethod(JyNI_PyObject_FromJythonPyObject(dtype), mdef);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodName,
			cName, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	if (mdef->ml_doc)
		JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodDoc,
				mdef->ml_doc, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodDef,
			mdef, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	return result;
}
Exemplo n.º 2
0
static int
PyExtensionClass_Export_(PyObject *dict, char *name, PyTypeObject *typ)
{
  long ecflags = 0;
  PyMethodDef *pure_methods = NULL, *mdef = NULL;
  PyObject *m;

  if (typ->tp_flags == 0) 
    { 
      /* Old-style EC */

      if (typ->tp_traverse) 
        { 
          /* ExtensionClasses stick there methods in the tp_traverse slot */
          mdef = (PyMethodDef *)typ->tp_traverse;

          if (typ->tp_basicsize <= sizeof(_emptyobject))
            /* Pure mixin. We want rebindable methods */
            pure_methods = mdef;
          else
            typ->tp_methods = mdef;

          typ->tp_traverse = NULL; 

          /* Look for __init__ method  */
          for (; mdef->ml_name; mdef++)
            {
              if (strcmp(mdef->ml_name, "__init__") == 0)
                {
                  /* we have an old-style __init__, install a special slot */
                  typ->tp_init = ec_init;
                  break;
                }
            }
        } 

      if (typ->tp_clear)
        {
          /* ExtensionClasses stick there flags in the tp_clear slot */
          ecflags = (long)(typ->tp_clear);

          /* Some old-style flags were set */

          if ((ecflags & EXTENSIONCLASS_BINDABLE_FLAG)
              && typ->tp_descr_get == NULL)
            /* We have __of__-style binding */
            typ->tp_descr_get = of_get; 
        }
      typ->tp_clear = NULL; 
      typ->tp_flags = Py_TPFLAGS_DEFAULT 
                    | Py_TPFLAGS_BASETYPE;

      if (typ->tp_dealloc != NULL)
          typ->tp_new = ec_new_for_custom_dealloc;
    }

  typ->ob_type = ECExtensionClassType; 

  if (ecflags & EXTENSIONCLASS_NOINSTDICT_FLAG)
    typ->tp_base = &NoInstanceDictionaryBaseType;
  else
    typ->tp_base = &BaseType;
  typ->tp_basicsize += typ->tp_base->tp_basicsize;

  if (typ->tp_new == NULL)
    typ->tp_new = PyType_GenericNew; 

  if (PyType_Ready(typ) < 0) 
    return -1; 

  if (pure_methods)
    {
      /* We had pure methods. We want to be able to rebind these, so
         we'll make them ordinary method wrappers around method descrs
      */
      for (; pure_methods->ml_name; pure_methods++)
        {
          m = PyDescr_NewMethod(ECBaseType, pure_methods);
          if (! m)
            return -1;
          m = PyMethod_New((PyObject *)m, NULL, (PyObject *)ECBaseType);
          if (! m)
            return -1;
          if (PyDict_SetItemString(typ->tp_dict, pure_methods->ml_name, m) 
              < 0)
            return -1;
        }      
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
      PyType_Modified(typ);
#endif
    }
  else if (mdef && mdef->ml_name)
    {
      /* Blast, we have to stick __init__ in the dict ourselves
         because PyType_Ready probably stuck a wrapper for ec_init in
         instead.
      */
      m = PyDescr_NewMethod(typ, mdef);
      if (! m)
        return -1;
      if (PyDict_SetItemString(typ->tp_dict, mdef->ml_name, m) < 0)
        return -1;
#ifdef Py_TPFLAGS_HAVE_VERSION_TAG
      PyType_Modified(typ);
#endif
    }

  if (PyMapping_SetItemString(dict, name, (PyObject*)typ) < 0)  
    return -1; 

  return 0;
}
Exemplo n.º 3
0
static int
psyco_errors_init(void)
{
    /* the names of the exceptions here reflect the oranization of the
       psycopg2 module and not the fact the the original error objects
       live in _psycopg */

    int i;
    PyObject *dict = NULL;
    PyObject *base;
    PyObject *str = NULL;
    PyObject *descr = NULL;
    int rv = -1;

#if PY_VERSION_HEX >= 0x02050000
    static PyMethodDef psyco_error_reduce_ex_def =
        {"__reduce_ex__", psyco_error_reduce_ex, METH_VARARGS, "pickle helper"};
#endif

    for (i=0; exctable[i].name; i++) {
        if (!(dict = PyDict_New())) { goto exit; }

        if (exctable[i].docstr) {
            if (!(str = Text_FromUTF8(exctable[i].docstr))) { goto exit; }
            if (0 != PyDict_SetItemString(dict, "__doc__", str)) { goto exit; }
            Py_CLEAR(str);
        }

        if (exctable[i].base == 0) {
            #if PY_MAJOR_VERSION < 3
            base = PyExc_StandardError;
            #else
            /* StandardError is gone in 3.0 */
            base = NULL;
            #endif
        }
        else
            base = *exctable[i].base;

        if (!(*exctable[i].exc = PyErr_NewException(
                exctable[i].name, base, dict))) {
            goto exit;
        }
        Py_CLEAR(dict);
    }

    /* Make pgerror, pgcode and cursor default to None on psycopg
       error objects.  This simplifies error handling code that checks
       these attributes. */
    PyObject_SetAttrString(Error, "pgerror", Py_None);
    PyObject_SetAttrString(Error, "pgcode", Py_None);
    PyObject_SetAttrString(Error, "cursor", Py_None);

    /* install __reduce_ex__ on Error to make all the subclasses picklable.
     *
     * Don't install it on Py 2.4: it is not used by the pickle
     * protocol, and if called manually fails in an unsettling way,
     * probably because the exceptions were old-style classes. */
#if PY_VERSION_HEX >= 0x02050000
    if (!(descr = PyDescr_NewMethod((PyTypeObject *)Error,
            &psyco_error_reduce_ex_def))) {
        goto exit;
    }
    if (0 != PyObject_SetAttrString(Error,
            psyco_error_reduce_ex_def.ml_name, descr)) {
        goto exit;
    }
#endif

    rv = 0;

exit:
    Py_XDECREF(descr);
    Py_XDECREF(str);
    Py_XDECREF(dict);
    return rv;
}