Implement PEP 3121: new module initialization and finalization API.

This commit is contained in:
Martin v. Löwis 2008-06-11 05:26:20 +00:00
parent cdf94635d7
commit 1a21451b1d
113 changed files with 2230 additions and 855 deletions

View file

@ -71,6 +71,7 @@ static PyMemberDef DB_members[] = {
/* forward declaration */
static PyTypeObject UCD_Type;
#define UCD_Check(o) (Py_TYPE(o)==&UCD_Type)
static PyObject*
new_previous_version(const char*name, const change_record* (*getrecord)(Py_UCS4),
@ -128,7 +129,7 @@ unicodedata_decimal(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) {
/* unassigned */
@ -213,7 +214,7 @@ unicodedata_numeric(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) {
/* unassigned */
@ -261,7 +262,7 @@ unicodedata_category(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
index = (int) _getrecord_ex(c)->category;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed != 0xFF)
index = old->category_changed;
@ -290,7 +291,7 @@ unicodedata_bidirectional(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
index = (int) _getrecord_ex(c)->bidirectional;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0)
index = 0; /* unassigned */
@ -321,7 +322,7 @@ unicodedata_combining(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
index = (int) _getrecord_ex(c)->combining;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0)
index = 0; /* unassigned */
@ -350,7 +351,7 @@ unicodedata_mirrored(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
index = (int) _getrecord_ex(c)->mirrored;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0)
index = 0; /* unassigned */
@ -378,7 +379,7 @@ unicodedata_east_asian_width(PyObject *self, PyObject *args)
if (c == (Py_UCS4)-1)
return NULL;
index = (int) _getrecord_ex(c)->east_asian_width;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0)
index = 0; /* unassigned */
@ -411,7 +412,7 @@ unicodedata_decomposition(PyObject *self, PyObject *args)
code = (int)c;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, c);
if (old->category_changed == 0)
return PyUnicode_FromString(""); /* unassigned */
@ -461,7 +462,8 @@ get_decomp_record(PyObject *self, Py_UCS4 code, int *index, int *prefix, int *co
{
if (code >= 0x110000) {
*index = 0;
} else if (self && get_old_record(self, code)->category_changed==0) {
} else if (self && UCD_Check(self) &&
get_old_record(self, code)->category_changed==0) {
/* unassigned in old version */
*index = 0;
}
@ -540,7 +542,7 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
continue;
}
/* normalization changes */
if (self) {
if (self && UCD_Check(self)) {
Py_UCS4 value = ((PreviousDBVersion*)self)->normalization(code);
if (value != 0) {
stack[stackptr++] = value;
@ -828,7 +830,7 @@ _getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen)
if (code >= 0x110000)
return 0;
if (self) {
if (self && UCD_Check(self)) {
const change_record *old = get_old_record(self, code);
if (old->category_changed == 0) {
/* unassigned */
@ -1183,17 +1185,29 @@ The module uses the same names and symbols as defined by the\n\
UnicodeData File Format 4.1.0 (see\n\
http://www.unicode.org/Public/4.1.0/ucd/UCD.html).");
static struct PyModuleDef unicodedatamodule = {
PyModuleDef_HEAD_INIT,
"unicodedata",
unicodedata_docstring,
-1,
unicodedata_functions,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
initunicodedata(void)
PyInit_unicodedata(void)
{
PyObject *m, *v;
Py_TYPE(&UCD_Type) = &PyType_Type;
m = Py_InitModule3(
"unicodedata", unicodedata_functions, unicodedata_docstring);
m = PyModule_Create(&unicodedatamodule);
if (!m)
return;
return NULL;
PyModule_AddStringConstant(m, "unidata_version", UNIDATA_VERSION);
Py_INCREF(&UCD_Type);
@ -1208,6 +1222,7 @@ initunicodedata(void)
v = PyCObject_FromVoidPtr((void *) &hashAPI, NULL);
if (v != NULL)
PyModule_AddObject(m, "ucnhash_CAPI", v);
return m;
}
/*