X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/c1756f78cbf3007438b3eb2fbfbd632d070be6ca..c80de12d8d0827e0553fed2e4d392cb9bf3a378f:/pyke.c diff --git a/pyke.c b/pyke.c index 883223a..330f15c 100644 --- a/pyke.c +++ b/pyke.c @@ -46,16 +46,18 @@ PyObject *getbool(int b) int convulong(PyObject *o, void *pp) { - long i; unsigned long *p = pp; PyObject *t; if (!o) VALERR("can't delete"); +#ifdef PY2 if (PyInt_Check(o)) { - i = PyInt_AS_LONG(o); + long i = PyInt_AS_LONG(o); if (i < 0) VALERR("must be nonnegative"); *p = i; - } else { + } else +#endif + { if ((t = PyNumber_Long(o)) == 0) goto end; *p = PyLong_AsUnsignedLong(t); Py_DECREF(t); @@ -101,6 +103,27 @@ end: return (0); } +int convbin(PyObject *o, void *pp) +{ + struct bin *r = pp; + + if (BIN_CHECK(o)) { + r->p = BIN_PTR(o); + r->sz = BIN_LEN(o); + return (1); + } +#ifdef PY2 + if (PyUnicode_Check(o)) { + o = _PyUnicode_AsDefaultEncodedString(o, 0); + if (!o) return (0); + r->p = PyString_AS_STRING(o); + r->sz = PyString_GET_SIZE(o); + return (1); + } +#endif + return (PyObject_AsReadBuffer(o, &r->p, &r->sz) ? 0 : 1); +} + /*----- Miscellaneous utilities -------------------------------------------*/ PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) @@ -109,6 +132,22 @@ PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) return (0); } +PyObject *enrich_compare(int op, int cmp) +{ + int r = -1; + + switch (op) { + case Py_LT: r = cmp < 0; break; + case Py_LE: r = cmp <= 0; break; + case Py_EQ: r = cmp == 0; break; + case Py_NE: r = cmp != 0; break; + case Py_GE: r = cmp >= 0; break; + case Py_GT: r = cmp > 0; break; + default: assert(0); + } + return (getbool(r)); +} + /*----- Saving and restoring exceptions ----------------------------------*/ void report_lost_exception_v(struct excinfo *exc, @@ -124,7 +163,7 @@ void report_lost_exception_v(struct excinfo *exc, assert(!PyErr_Occurred()); /* Format the explanation. */ - if (why) whyobj = PyString_FromFormatV(why, ap); + if (why) whyobj = TEXT_VFORMAT(why, ap); else { whyobj = Py_None; Py_INCREF(whyobj); } /* Find our home module's `lostexchook' function. This won't work if @@ -153,8 +192,7 @@ ouch: * `sys.excepthook'. */ sys: - PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", - PyString_AS_STRING(whyobj)); + PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj)); RESTORE_EXCINFO(exc); PyErr_Print(); /* drop through... */ @@ -218,7 +256,6 @@ void *newtype(PyTypeObject *metaty, (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0)); if (!skel) skel = &emptytype; memcpy(ty, skel, sizeof(*skel)); - if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base); #define COPY(blah) do { \ if (ty->ht_type.tp_as_##blah) { \ memcpy(&ty->as_##blah, \ @@ -233,11 +270,17 @@ void *newtype(PyTypeObject *metaty, COPY(buffer); #undef COPY if (name) - ty->ht_name = PyString_FromString(name); + ty->ht_name = TEXT_FROMSTR(name); else if (ty->ht_type.tp_name) - ty->ht_name = PyString_FromString(ty->ht_type.tp_name); + ty->ht_name = TEXT_FROMSTR(ty->ht_type.tp_name); + else + ty->ht_name = 0; if (ty->ht_name) - ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name); + ty->ht_type.tp_name = TEXT_STR(ty->ht_name); + ty->ht_slots = 0; +#ifdef PY3 + ty->ht_qualname = 0; +#endif (void)PyObject_INIT(&ty->ht_type, metaty); Py_INCREF(metaty); return (ty); @@ -245,13 +288,19 @@ void *newtype(PyTypeObject *metaty, void typeready(PyTypeObject *ty) { +#ifdef PY3 + PyHeapTypeObject *hty = (PyHeapTypeObject *)ty; + hty->ht_qualname = hty->ht_name; +#endif PyType_Ready(ty); PyDict_SetItemString(ty->tp_dict, "__module__", modname); } -PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta) +PyTypeObject *inittype(const PyTypeObject *tyskel, + PyTypeObject *base, PyTypeObject *meta) { PyTypeObject *ty = newtype(meta, tyskel, 0); + if (base) { ty->tp_base = base; Py_INCREF(base); } ty->tp_flags |= Py_TPFLAGS_HEAPTYPE; typeready(ty); return (ty); @@ -260,7 +309,7 @@ PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta) /*----- Populating modules ------------------------------------------------*/ PyObject *mkexc(PyObject *mod, PyObject *base, - const char *name, PyMethodDef *mm) + const char *name, const PyMethodDef *mm) { PyObject *nameobj = 0; PyObject *dict = 0; @@ -272,8 +321,10 @@ PyObject *mkexc(PyObject *mod, PyObject *base, if (mm) { while (mm->ml_name) { - if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 || - (meth = PyMethod_New(func, 0, exc)) == 0 || + if ((func = PyCFunction_NewEx((/*unconst*/ PyMethodDef *)mm, + 0, mod)) == 0 || + (meth = PY23(PyMethod_New(func, 0, exc), + PyInstanceMethod_New(func))) == 0 || PyDict_SetItemString(dict, mm->ml_name, meth)) goto fail; Py_DECREF(func); func = 0; @@ -282,11 +333,8 @@ PyObject *mkexc(PyObject *mod, PyObject *base, } } - if ((nameobj = PyString_FromFormat("%s.%s", - PyModule_GetName(mod), - name)) == 0 || - (exc = PyErr_NewException(PyString_AS_STRING(nameobj), - base, dict)) == 0) + if ((nameobj = TEXT_FORMAT("%s.%s", PyModule_GetName(mod), name)) == 0 || + (exc = PyErr_NewException(TEXT_STR(nameobj), base, dict)) == 0) goto fail; done: