Changeset 4165 for neercs


Ignore:
Timestamp:
Dec 22, 2009, 9:29:51 AM (10 years ago)
Author:
Sam Hocevar
Message:

Start porting the Python interpreter to Python 2.4/2.5.

Location:
neercs/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/configure.ac

    r4079 r4165  
    6767
    6868CACA="no"
    69 PKG_CHECK_MODULES(CACA, caca >= 0.99.beta17,
     69PKG_CHECK_MODULES(CACA, caca >= 0.99.beta16,
    7070 [CACA="yes"],
    7171 [AC_MSG_RESULT(no)
     
    108108AC_DEFINE(_GNU_SOURCE, 1, [Use GNU extentions])
    109109
    110 PYTHON3="yes"
     110PYTHON="no"
    111111if test "${enable_python}" != "no"; then
    112112PKG_CHECK_MODULES(PYTHON3, python3 >= 3.1,
    113  [PYTHON3="yes"  AC_DEFINE(USE_PYTHON, 1, [Define to 1 to use Python interpreter])],
    114  [AC_MSG_RESULT(you need Python 3.1 or later to compile the interpreter)
    115 ])
     113 [PYTHON="yes"
     114  PYTHON_CFLAGS="${PYTHON3_CFLAGS}"
     115  PYTHON_LIBS="${PYTHON3_LIBS}"
     116  AC_DEFINE(HAVE_PYTHON3, 1, [Define to 1 if Python 3 is present])],
     117 [AC_PATH_PROG(PYTHON_CONFIG, python2.5-config, no)
     118  if test "${PYTHON_CONFIG}" = "no"; then
     119    AC_PATH_PROG(PYTHON_CONFIG, python2.4-config, no)
     120  fi
     121  if test "${PYTHON_CONFIG}" != "no"; then
     122    PYTHON="yes"
     123    PYTHON_CFLAGS="`${PYTHON_CONFIG} --cflags`"
     124    PYTHON_LIBS="`${PYTHON_CONFIG} --libs`"
     125    AC_DEFINE(HAVE_PYTHON2, 1, [Define to 1 if Python 2 is present])
     126  fi])
     127  if test "${PYTHON}" = "yes"; then
     128    AC_DEFINE(USE_PYTHON, 1, [Define to 1 to use Python interpreter])
     129  else
     130    AC_MSG_RESULT(you need Python 2.5 or later to compile the interpreter)
     131  fi
    116132fi
     133
     134AC_SUBST(PYTHON_CFLAGS)
     135AC_SUBST(PYTHON_LIBS)
    117136
    118137# Optimizations / Profiling
  • neercs/trunk/src/Makefile.am

    r4074 r4165  
    77                 client.c screen_list.c help.c python/interpreter.c python/py_module.c \
    88                 widgets.c ansi.c       
    9 neercs_CFLAGS = @CACA_CFLAGS@ @PYTHON3_CFLAGS@
    10 neercs_LDADD = @CACA_LIBS@ @UTIL_LIBS@ @PAM_LIBS@ @PYTHON3_LIBS@
     9neercs_CFLAGS = @CACA_CFLAGS@ @PYTHON_CFLAGS@
     10neercs_LDADD = @CACA_LIBS@ @UTIL_LIBS@ @PAM_LIBS@ @PYTHON_LIBS@
  • neercs/trunk/src/python/interpreter.c

    r4071 r4165  
    4242static char *getPythonError(void);
    4343
    44 
     44#if defined HAVE_PYTHON2
     45static PyObject *PyUnicode_FromString(char const *str)
     46{
     47    PyObject *tmp, *ret;
     48    tmp = PyString_FromString(str);
     49    if (!tmp)
     50        return NULL;
     51    ret = PyString_AsDecodedObject(tmp, "utf-8", "replace");
     52    Py_DECREF(tmp);
     53    return ret;
     54}
     55#endif
    4556
    4657int python_command_handle_key(struct screen_list *screen_list, unsigned int c)
     
    7788int python_init(struct screen_list *sl)
    7889{
    79     Py_Initialize();
    8090    initNeercsModule(sl);
    8191
     
    103113
    104114
    105     PyObject *pModule, *pName, *pFunc;
     115    PyObject *pModule, *pName;
    106116
    107117    /* Module from which to call the function */
     
    139149            err = 1;
    140150        }
    141         goto end;
    142151
     152        Py_DECREF(pModule);
    143153    }
    144154    else
     
    146156        widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
    147157        err = 1;
    148         goto end;
    149158    }
    150159
    151160  end:
    152 
    153     Py_XDECREF(pFunc);
    154     Py_DECREF(pModule);
    155161
    156162    if (!err)
     
    164170    return 0;
    165171}
    166 
    167 
    168172
    169173static char *getPythonError(void)
     
    183187{
    184188    PyObject *str = PyObject_Repr(p);
    185     char *err = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8",
    186                                                             "Error ~"));
    187 
    188     char *ret = strdup(err);
    189     return ret;
     189    char *tmp;
     190#if defined HAVE_PYTHON3
     191    tmp = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8", "Error ~"));
     192#elif defined HAVE_PYTHON2
     193    tmp = PyString_AsString(str);
     194#endif
     195    return strdup(tmp);
    190196}
    191197
  • neercs/trunk/src/python/py_module.c

    r4047 r4165  
    4040static struct screen_list *screen_list;
    4141
    42 PyObject *PyInit_neercs(void);
    43 void removeTrailingStuff(char *b);
     42#if defined HAVE_PYTHON3
     43static PyObject *PyInit_neercs(void);
     44#else
     45static void PyInit_neercs(void);
     46#endif
     47static void removeTrailingStuff(char *b);
    4448
    4549
     
    5660    debug("py get '%s' to '%s'\n", varname,
    5761          get_config(configname)->get(screen_list));
    58 
    5962}
    6063
    61 void removeTrailingStuff(char *b)
     64static void removeTrailingStuff(char *b)
    6265{
    63         if(!b) return;
     66    if(!b)
     67        return;
    6468    if(b[0]=='\'')
    6569    {
    66         memmove(b, &b[1], strlen(b)-1);   
     70        memmove(b, &b[1], strlen(b)-1);   
    6771        b[strlen(b)-2] = 0;
    6872    }
     
    8993            /* Make sure it's a string */
    9094            char *err =
     95#if defined HAVE_PYTHON3
    9196                PyBytes_AS_STRING(PyUnicode_AsEncodedString
    9297                                  (str, "utf-8", "Error ~"));
    93                         /* FIXME leak leak leak */
     98#elif defined HAVE_PYTHON2
     99                PyString_AsString(str);
     100#endif
     101            /* FIXME leak leak leak */
    94102            char *s = strdup(err);
    95103
     
    116124    }
    117125}
    118 
    119126
    120127static PyObject *neercs_get(PyObject * self, PyObject * args)
     
    147154}
    148155
    149 static PyMethodDef NeercsMethods[] = {
    150     {"version", neercs_version, METH_NOARGS, "Return the neercs version."},
    151     {"get", neercs_get, METH_VARARGS,
    152      "Return the specified variable's value."},
    153     {NULL, NULL, 0, NULL}
     156static PyMethodDef NeercsMethods[] =
     157{
     158    { "version", neercs_version, METH_NOARGS, "Return the neercs version." },
     159    { "get", neercs_get, METH_VARARGS,
     160      "Return the specified variable's value." },
     161    { NULL, NULL, 0, NULL }
    154162};
    155163
    156 static PyModuleDef NeercsModule = {
    157     PyModuleDef_HEAD_INIT, "neercs", NULL, -1, NeercsMethods,
    158     NULL, NULL, NULL, NULL
    159 };
     164#if defined HAVE_PYTHON3
     165static PyObject *PyInit_neercs(void)
     166{
     167    static PyModuleDef NeercsModule =
     168    {
     169        PyModuleDef_HEAD_INIT, "neercs", NULL, -1, NeercsMethods,
     170        NULL, NULL, NULL, NULL
     171    };
    160172
    161 PyObject *PyInit_neercs(void)
     173    return PyModule_Create(&NeercsModule);
     174}
     175
     176#elif defined HAVE_PYTHON2
     177static void PyInit_neercs(void)
    162178{
    163     PyObject *o = PyModule_Create(&NeercsModule);
    164     PyImport_AppendInittab("neercs", &PyInit_neercs);
    165     return o;
     179    PyMethodDef *m = NeercsMethods;
     180    PyObject *mod = PyModule_New("neercs");
     181    PyModule_AddStringConstant(mod, "__file__", "<synthetic>");
     182
     183    for (m = NeercsMethods; m->ml_name; m++)
     184        PyModule_AddObject(mod, m->ml_name, PyCFunction_New(m, NULL));
    166185}
     186#endif
    167187
    168188void initNeercsModule(struct screen_list *sl)
    169189{
    170190    screen_list = sl;
    171     PyInit_neercs();
     191    PyImport_AppendInittab("neercs", &PyInit_neercs);
     192    Py_Initialize();
    172193}
    173194
    174 
    175 
    176195#endif
Note: See TracChangeset for help on using the changeset viewer.