瀏覽代碼

Fix py3k module importing; stick a bunch of macros in one place.

tags/v0.3
Ben Kurtovic 11 年之前
父節點
當前提交
b5ec7f3beb
共有 2 個檔案被更改,包括 22 行新增19 行删除
  1. +5
    -19
      mwparserfromhell/parser/tokenizer.c
  2. +17
    -0
      mwparserfromhell/parser/tokenizer.h

+ 5
- 19
mwparserfromhell/parser/tokenizer.c 查看文件

@@ -2311,11 +2311,7 @@ static int load_entitydefs(void)
PyObject *string;
#endif

#ifdef IS_PY3K
tempmod = PyImport_ImportModule("html.entities");
#else
tempmod = PyImport_ImportModule("htmlentitydefs");
#endif
tempmod = PyImport_ImportModule(ENTITYDEFS_MODULE);
if (!tempmod)
return -1;
defmap = PyObject_GetAttrString(tempmod, "entitydefs");
@@ -2353,7 +2349,7 @@ static int load_tokens(void)
*globals = PyEval_GetGlobals(),
*locals = PyEval_GetLocals(),
*fromlist = PyList_New(1),
*modname = PyBytes_FromString("tokens");
*modname = IMPORT_NAME_FUNC("tokens");
char *name = "mwparserfromhell.parser";

if (!fromlist || !modname)
@@ -2413,7 +2409,7 @@ static int load_tag_defs(void)
*globals = PyEval_GetGlobals(),
*locals = PyEval_GetLocals(),
*fromlist = PyList_New(1),
*modname = PyBytes_FromString("tag_defs");
*modname = IMPORT_NAME_FUNC("tag_defs");
char *name = "mwparserfromhell";

if (!fromlist || !modname)
@@ -2428,24 +2424,14 @@ static int load_tag_defs(void)
return 0;
}

#ifdef IS_PY3K
#define INIT_ERROR return NULL
PyMODINIT_FUNC PyInit__tokenizer(void)
#else
#define INIT_ERROR return
PyMODINIT_FUNC init_tokenizer(void)
#endif
PyMODINIT_FUNC INIT_FUNC_NAME(void)
{
PyObject *module;

TokenizerType.tp_new = PyType_GenericNew;
if (PyType_Ready(&TokenizerType) < 0)
INIT_ERROR;
#ifdef IS_PY3K
module = PyModule_Create(&module_def);
#else
module = Py_InitModule("_tokenizer", NULL);
#endif
module = CREATE_MODULE;
if (!module)
INIT_ERROR;
Py_INCREF(&TokenizerType);


+ 17
- 0
mwparserfromhell/parser/tokenizer.h 查看文件

@@ -252,6 +252,23 @@ static PyObject* Tokenizer_parse(Tokenizer*, int, int);
static PyObject* Tokenizer_tokenize(Tokenizer*, PyObject*);


/* Macros for Python 2/3 compatibility: */

#ifdef IS_PY3K
#define IMPORT_NAME_FUNC PyUnicode_FromString
#define CREATE_MODULE PyModule_Create(&module_def);
#define ENTITYDEFS_MODULE "html.entities"
#define INIT_FUNC_NAME PyInit__tokenizer
#define INIT_ERROR return NULL
#else
#define IMPORT_NAME_FUNC PyBytes_FromString
#define CREATE_MODULE Py_InitModule("_tokenizer", NULL);
#define ENTITYDEFS_MODULE "htmlentitydefs"
#define INIT_FUNC_NAME init_tokenizer
#define INIT_ERROR return
#endif


/* More structs for creating the Tokenizer type: */

static PyMethodDef Tokenizer_methods[] = {


Loading…
取消
儲存