/* * ElementTree * $Id$ * * elementtree write accelerator (work in progress) * * History: * 2005-11-21 fl created * * Copyright (c) 2005 by Secret Labs AB. All rights reserved. * Copyright (c) 2005 by Fredrik Lundh. * * info@pythonware.com * http://www.pythonware.com */ #include "Python.h" /* -------------------------------------------------------------------- */ /* configuration */ /* compiler tweaks */ #if defined(_MSC_VER) #define LOCAL(type) static __inline type __fastcall #else #define LOCAL(type) static type #endif /* compatibility macros */ #if (PY_VERSION_HEX < 0x02040000) #define PyDict_CheckExact PyDict_Check #if (PY_VERSION_HEX < 0x02020000) #define PyList_CheckExact PyList_Check #define PyString_CheckExact PyString_Check #if (PY_VERSION_HEX >= 0x01060000) #define Py_USING_UNICODE /* always enabled for 2.0 and 2.1 */ #endif #endif #endif #if !defined(Py_RETURN_NONE) #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None #endif /* -------------------------------------------------------------------- */ /* the encoder type */ typedef struct { PyObject_HEAD PyObject* encoding; } EncoderObject; staticforward PyTypeObject Encoder_Type; #define Encoder_CheckExact(op) ((op)->ob_type == &Encoder_Type) /* -------------------------------------------------------------------- */ /* encoder constructor and destructor */ static PyObject* encoder_new(PyObject* self_, PyObject* args) { EncoderObject* self; PyObject* encoding; if (!PyArg_ParseTuple(args, "O!:encode", &PyString_Type, &encoding)) return NULL; self = PyObject_New(EncoderObject, &Encoder_Type); if (self == NULL) return NULL; Py_INCREF(encoding); self->encoding = encoding; return (PyObject*) self; } #define CHECK_NONASCII 1 #define CHECK_ENTITY 2 #define CHECK_ATTRIB 4 #define CHECK_ALL 7 static PyObject* encoder_encode(EncoderObject* self, PyObject* args) { PyObject* text; if (!PyArg_ParseTuple(args, "O:encode", &text)) return NULL; if (PyString_Check(text)) { unsigned char* p = PyString_AS_STRING(text); int i, len = PyString_GET_SIZE(text); for (i = 0; i < len; i++) { if (p[i] & 0x80) break; } if (i == len) { Py_INCREF(text); return text; } } else if (PyUnicode_Check(text)) { /* FIXME */ } else { PyErr_SetString(PyExc_TypeError, "invalid string"); return NULL; } Py_RETURN_NONE; } static PyObject* encoder_encode_cdata(EncoderObject* self, PyObject* args) { PyObject* text; if (!PyArg_ParseTuple(args, "O:encode_cdata", &text)) return NULL; Py_RETURN_NONE; } static PyObject* encoder_encode_attrib(EncoderObject* self, PyObject* args) { PyObject* text; if (!PyArg_ParseTuple(args, "O:encode_attrib", &text)) return NULL; Py_RETURN_NONE; } static void encoder_dealloc(EncoderObject* self) { Py_XDECREF(self->encoding); PyObject_Del(self); } static PyMethodDef encoder_methods[] = { {"encode", (PyCFunction) encoder_encode, METH_VARARGS}, {"encode_cdata", (PyCFunction) encoder_encode_cdata, METH_VARARGS}, {"encode_attrib", (PyCFunction) encoder_encode_attrib, METH_VARARGS}, {NULL, NULL} }; static PyObject* encoder_getattr(EncoderObject* self, char* name) { return Py_FindMethod(encoder_methods, (PyObject*) self, name); } statichere PyTypeObject Encoder_Type = { PyObject_HEAD_INIT(NULL) 0, "Encoder", sizeof(EncoderObject), 0, /* methods */ (destructor)encoder_dealloc, /* tp_dealloc */ 0, /* tp_print */ (getattrfunc)encoder_getattr, /* tp_getattr */ }; static PyMethodDef _functions[] = { {"Encoder", (PyCFunction) encoder_new, METH_VARARGS}, {NULL, NULL} }; DL_EXPORT(void) initcElementTreeWrite(void) { Encoder_Type.ob_type = &PyType_Type; Py_InitModule("cElementTreeWrite", _functions); }