2 * This is a Python interface over Simon Josefsson's libidn
3 * <URL:http://josefsson.org/libidn/>.
5 * Stephane Bortzmeyer <bortzmeyer@nic.fr>
14 #define MESSAGE_SIZE 512
16 static PyObject
*IDNError
;
17 static PyObject
*IDNInvLengthError
;
19 #define onError(message) { PyErr_SetString(IDNError, message); free(message); return NULL; }
22 idn2ace (PyObject
* self
, PyObject
* args
)
27 if (!PyArg_ParseTuple (args
, "s", &instr
))
28 onError ("Invalid argument");
29 rc
= idna_utf8_to_ace (instr
, &result
);
30 if (rc
!= IDNA_SUCCESS
)
34 case IDNA_INVALID_LENGTH
:
35 result
= malloc (MESSAGE_SIZE
);
36 sprintf (result
, "%d bytes", strlen (instr
));
37 PyErr_SetString (IDNInvLengthError
, result
);
42 result
= malloc (MESSAGE_SIZE
);
43 sprintf (result
, "IDN error: %d (see idna.h)", rc
);
47 outstr
= Py_BuildValue ("s", result
);
52 ace2idn (PyObject
* self
, PyObject
* args
)
57 if (!PyArg_ParseTuple (args
, "s", &instr
))
58 onError ("Invalid argument");
59 rc
= idna_utf8ace_to_utf8 (instr
, &result
);
60 if (rc
!= IDNA_SUCCESS
)
62 result
= malloc (MESSAGE_SIZE
);
63 sprintf (result
, "IDN error: %d (see idna.h)", rc
);
66 outstr
= Py_BuildValue ("s", result
);
70 static struct PyMethodDef methods
[] = {
71 {"idn2ace", idn2ace
, 1},
72 {"ace2idn", ace2idn
, 1},
79 Py_InitModule ("idn", methods
);
80 IDNError
= PyErr_NewException ("idn.error", NULL
, NULL
);
81 IDNInvLengthError
= PyErr_NewException ("idn.invalidLength", NULL
, NULL
);