[pypy-commit] pypy default: Adapt the files for PyPy usage.

arigo noreply at buildbot.pypy.org
Tue May 10 22:58:24 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r44052:89ccab9548f1
Date: 2011-05-10 11:42 +0200
http://bitbucket.org/pypy/pypy/changeset/89ccab9548f1/

Log:	Adapt the files for PyPy usage.

diff --git a/pypy/module/_multibytecodec/cjkcodecs/README b/pypy/module/_multibytecodec/cjkcodecs/README
--- a/pypy/module/_multibytecodec/cjkcodecs/README
+++ b/pypy/module/_multibytecodec/cjkcodecs/README
@@ -1,3 +1,10 @@
+Source
+------
+The .c and .h files come directly from CPython, with the exception of
+cjkcodecs.h and multibytecodec.h, which have been ripped of their
+CPython dependencies.
+
+
 To generate or modify mapping headers
 -------------------------------------
 Mapping headers are imported from CJKCodecs as pre-generated form.
diff --git a/pypy/module/_multibytecodec/cjkcodecs/_codecs_hk.c b/pypy/module/_multibytecodec/cjkcodecs/_codecs_hk.c
--- a/pypy/module/_multibytecodec/cjkcodecs/_codecs_hk.c
+++ b/pypy/module/_multibytecodec/cjkcodecs/_codecs_hk.c
@@ -13,17 +13,14 @@
  * BIG5HKSCS codec
  */
 
+USING_IMPORTED_MAP(big5);
 static const encode_map *big5_encmap = NULL;
 static const decode_map *big5_decmap = NULL;
 
 CODEC_INIT(big5hkscs)
 {
-    static int initialized = 0;
-
-    if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
-        return -1;
-    initialized = 1;
-    return 0;
+  IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap);
+  return 0;
 }
 
 /*
diff --git a/pypy/module/_multibytecodec/cjkcodecs/_codecs_iso2022.c b/pypy/module/_multibytecodec/cjkcodecs/_codecs_iso2022.c
--- a/pypy/module/_multibytecodec/cjkcodecs/_codecs_iso2022.c
+++ b/pypy/module/_multibytecodec/cjkcodecs/_codecs_iso2022.c
@@ -538,6 +538,21 @@
 
 /*-*- mapping table holders -*-*/
 
+USING_IMPORTED_MAP(cp949)
+USING_IMPORTED_MAP(ksx1001)
+USING_IMPORTED_MAP(jisxcommon)
+USING_IMPORTED_MAP(jisx0208)
+USING_IMPORTED_MAP(jisx0212)
+USING_IMPORTED_MAP(jisx0213_bmp)
+USING_IMPORTED_MAP(jisx0213_1_bmp)
+USING_IMPORTED_MAP(jisx0213_2_bmp)
+USING_IMPORTED_MAP(jisx0213_emp)
+USING_IMPORTED_MAP(jisx0213_1_emp)
+USING_IMPORTED_MAP(jisx0213_2_emp)
+USING_IMPORTED_MAP(jisx0213_pair)
+USING_IMPORTED_MAP(gbcommon)
+USING_IMPORTED_MAP(gb2312)
+
 #define ENCMAP(enc) static const encode_map *enc##_encmap = NULL;
 #define DECMAP(enc) static const decode_map *enc##_decmap = NULL;
 
@@ -567,14 +582,9 @@
 static int
 ksx1001_init(void)
 {
-    static int initialized = 0;
-
-    if (!initialized && (
-                    IMPORT_MAP(kr, cp949, &cp949_encmap, NULL) ||
-                    IMPORT_MAP(kr, ksx1001, NULL, &ksx1001_decmap)))
-        return -1;
-    initialized = 1;
-    return 0;
+  IMPORT_MAP(kr, cp949, &cp949_encmap, NULL);
+  IMPORT_MAP(kr, ksx1001, NULL, &ksx1001_decmap);
+  return 0;
 }
 
 static ucs4_t
@@ -603,14 +613,9 @@
 static int
 jisx0208_init(void)
 {
-    static int initialized = 0;
-
-    if (!initialized && (
-                    IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
-                    IMPORT_MAP(jp, jisx0208, NULL, &jisx0208_decmap)))
-        return -1;
-    initialized = 1;
-    return 0;
+  IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL);
+  IMPORT_MAP(jp, jisx0208, NULL, &jisx0208_decmap);
+  return 0;
 }
 
 static ucs4_t
@@ -644,14 +649,9 @@
 static int
 jisx0212_init(void)
 {
-    static int initialized = 0;
-
-    if (!initialized && (
-                    IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
-                    IMPORT_MAP(jp, jisx0212, NULL, &jisx0212_decmap)))
-        return -1;
-    initialized = 1;
-    return 0;
+  IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL);
+  IMPORT_MAP(jp, jisx0212, NULL, &jisx0212_decmap);
+  return 0;
 }
 
 static ucs4_t
@@ -681,27 +681,15 @@
 static int
 jisx0213_init(void)
 {
-    static int initialized = 0;
-
-    if (!initialized && (
-                    jisx0208_init() ||
-                    IMPORT_MAP(jp, jisx0213_bmp,
-                               &jisx0213_bmp_encmap, NULL) ||
-                    IMPORT_MAP(jp, jisx0213_1_bmp,
-                               NULL, &jisx0213_1_bmp_decmap) ||
-                    IMPORT_MAP(jp, jisx0213_2_bmp,
-                               NULL, &jisx0213_2_bmp_decmap) ||
-                    IMPORT_MAP(jp, jisx0213_emp,
-                               &jisx0213_emp_encmap, NULL) ||
-                    IMPORT_MAP(jp, jisx0213_1_emp,
-                               NULL, &jisx0213_1_emp_decmap) ||
-                    IMPORT_MAP(jp, jisx0213_2_emp,
-                               NULL, &jisx0213_2_emp_decmap) ||
-                    IMPORT_MAP(jp, jisx0213_pair, &jisx0213_pair_encmap,
-                               &jisx0213_pair_decmap)))
-        return -1;
-    initialized = 1;
-    return 0;
+  jisx0208_init();
+  IMPORT_MAP(jp, jisx0213_bmp, &jisx0213_bmp_encmap, NULL);
+  IMPORT_MAP(jp, jisx0213_1_bmp, NULL, &jisx0213_1_bmp_decmap);
+  IMPORT_MAP(jp, jisx0213_2_bmp, NULL, &jisx0213_2_bmp_decmap);
+  IMPORT_MAP(jp, jisx0213_emp, &jisx0213_emp_encmap, NULL);
+  IMPORT_MAP(jp, jisx0213_1_emp, NULL, &jisx0213_1_emp_decmap);
+  IMPORT_MAP(jp, jisx0213_2_emp, NULL, &jisx0213_2_emp_decmap);
+  IMPORT_MAP(jp, jisx0213_pair, &jisx0213_pair_encmap, &jisx0213_pair_decmap);
+  return 0;
 }
 
 #define config ((void *)2000)
@@ -951,14 +939,9 @@
 static int
 gb2312_init(void)
 {
-    static int initialized = 0;
-
-    if (!initialized && (
-                    IMPORT_MAP(cn, gbcommon, &gbcommon_encmap, NULL) ||
-                    IMPORT_MAP(cn, gb2312, NULL, &gb2312_decmap)))
-        return -1;
-    initialized = 1;
-    return 0;
+  IMPORT_MAP(cn, gbcommon, &gbcommon_encmap, NULL);
+  IMPORT_MAP(cn, gb2312, NULL, &gb2312_decmap);
+  return 0;
 }
 
 static ucs4_t
diff --git a/pypy/module/_multibytecodec/cjkcodecs/cjkcodecs.h b/pypy/module/_multibytecodec/cjkcodecs/cjkcodecs.h
--- a/pypy/module/_multibytecodec/cjkcodecs/cjkcodecs.h
+++ b/pypy/module/_multibytecodec/cjkcodecs/cjkcodecs.h
@@ -1,14 +1,14 @@
 /*
- * cjkcodecs.h: common header for cjkcodecs
+ * cjkcodecs.h is inspired by the file of the same name from CPython,
+ * but was heavily modified to suit PyPy.
  *
- * Written by Hye-Shik Chang <perky at FreeBSD.org>
+ * Original author: Hye-Shik Chang <perky at FreeBSD.org>
+ * Modified by: Armin Rigo <arigo at tunes.org>
  */
 
 #ifndef _CJKCODECS_H_
 #define _CJKCODECS_H_
 
-#define PY_SSIZE_T_CLEAN
-#include "Python.h"
 #include "multibytecodec.h"
 
 
@@ -60,9 +60,6 @@
     DBCHAR code;
 };
 
-static const MultibyteCodec *codec_list;
-static const struct dbcs_map *mapping_list;
-
 #define CODEC_INIT(encoding)                                            \
     static int encoding##_codec_init(const void *config)
 
@@ -201,14 +198,15 @@
 #define GET_INSIZE(c)   1
 #endif
 
-#define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
-#define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
-#define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
-#define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
-#define END_MAPPINGS_LIST                               \
-    {"", NULL, NULL} };                                 \
-    static const struct dbcs_map *mapping_list =        \
-        (const struct dbcs_map *)_mapping_list;
+#define BEGIN_MAPPINGS_LIST /* empty */
+#define MAPPING_ENCONLY(enc)                                            \
+  const struct dbcs_map _pypy_cjkmap_##enc = {#enc, (void*)enc##_encmap, NULL};
+#define MAPPING_DECONLY(enc)                                            \
+  const struct dbcs_map _pypy_cjkmap_##enc = {#enc, NULL, (void*)enc##_decmap};
+#define MAPPING_ENCDEC(enc)                                             \
+  const struct dbcs_map _pypy_cjkmap_##enc = {#enc, (void*)enc##_encmap, \
+                                              (void*)enc##_decmap};
+#define END_MAPPINGS_LIST /* empty */
 
 #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
 #define _STATEFUL_METHODS(enc)          \
@@ -235,84 +233,8 @@
     _STATELESS_METHODS(enc)             \
 },
 #define END_CODECS_LIST                                 \
-    {"", NULL,} };                                      \
-    static const MultibyteCodec *codec_list =           \
-        (const MultibyteCodec *)_codec_list;
+    {"", NULL,} };
 
-static PyObject *
-getmultibytecodec(void)
-{
-    static PyObject *cofunc = NULL;
-
-    if (cofunc == NULL) {
-        PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
-        if (mod == NULL)
-            return NULL;
-        cofunc = PyObject_GetAttrString(mod, "__create_codec");
-        Py_DECREF(mod);
-    }
-    return cofunc;
-}
-
-static PyObject *
-getcodec(PyObject *self, PyObject *encoding)
-{
-    PyObject *codecobj, *r, *cofunc;
-    const MultibyteCodec *codec;
-    const char *enc;
-
-    if (!PyString_Check(encoding)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "encoding name must be a string.");
-        return NULL;
-    }
-
-    cofunc = getmultibytecodec();
-    if (cofunc == NULL)
-        return NULL;
-
-    enc = PyString_AS_STRING(encoding);
-    for (codec = codec_list; codec->encoding[0]; codec++)
-        if (strcmp(codec->encoding, enc) == 0)
-            break;
-
-    if (codec->encoding[0] == '\0') {
-        PyErr_SetString(PyExc_LookupError,
-                        "no such codec is supported.");
-        return NULL;
-    }
-
-    codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL);
-    if (codecobj == NULL)
-        return NULL;
-
-    r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
-    Py_DECREF(codecobj);
-
-    return r;
-}
-
-static struct PyMethodDef __methods[] = {
-    {"getcodec", (PyCFunction)getcodec, METH_O, ""},
-    {NULL, NULL},
-};
-
-static int
-register_maps(PyObject *module)
-{
-    const struct dbcs_map *h;
-
-    for (h = mapping_list; h->charset[0] != '\0'; h++) {
-        char mhname[256] = "__map_";
-        int r;
-        strcpy(mhname + sizeof("__map_") - 1, h->charset);
-        r = PyModule_AddObject(module, mhname,
-                        PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL));
-        if (r == -1)
-            return -1;
-    }
-    return 0;
-}
 
 #ifdef USING_BINARY_PAIR_SEARCH
 static DBCHAR
@@ -344,55 +266,25 @@
 }
 #endif
 
+
 #ifdef USING_IMPORTED_MAPS
-#define IMPORT_MAP(locale, charset, encmap, decmap) \
-    importmap("_codecs_" #locale, "__map_" #charset, \
-              (const void**)encmap, (const void**)decmap)
+#define USING_IMPORTED_MAP(charset) \
+  extern const struct dbcs_map _pypy_cjkmap_##charset;
 
-static int
-importmap(const char *modname, const char *symbol,
-          const void **encmap, const void **decmap)
+#define IMPORT_MAP(locale, charset, encmap, decmap)                     \
+  importmap(&_pypy_cjkmap_##charset, encmap, decmap)
+
+static void importmap(const struct dbcs_map *src, void *encmp,
+                      void *decmp)
 {
-    PyObject *o, *mod;
-
-    mod = PyImport_ImportModule((char *)modname);
-    if (mod == NULL)
-        return -1;
-
-    o = PyObject_GetAttrString(mod, (char*)symbol);
-    if (o == NULL)
-        goto errorexit;
-    else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
-        PyErr_SetString(PyExc_ValueError,
-                        "map data must be a Capsule.");
-        goto errorexit;
-    }
-    else {
-        struct dbcs_map *map;
-        map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME);
-        if (encmap != NULL)
-            *encmap = map->encmap;
-        if (decmap != NULL)
-            *decmap = map->decmap;
-        Py_DECREF(o);
-    }
-
-    Py_DECREF(mod);
-    return 0;
-
-errorexit:
-    Py_DECREF(mod);
-    return -1;
+  if (encmp) *(const encode_map **)encmp = src->encmap;
+  if (decmp) *(const decode_map **)decmp = src->decmap;
 }
 #endif
 
-#define I_AM_A_MODULE_FOR(loc)                                          \
-    void                                                                \
-    init_codecs_##loc(void)                                             \
-    {                                                                   \
-        PyObject *m = Py_InitModule("_codecs_" #loc, __methods);\
-        if (m != NULL)                                                  \
-            (void)register_maps(m);                                     \
-    }
+
+#define I_AM_A_MODULE_FOR(loc)                                  \
+    const MultibyteCodec *pypy_codec_list_##loc = _codec_list;
+
 
 #endif
diff --git a/pypy/module/_multibytecodec/cjkcodecs/multibytecodec.h b/pypy/module/_multibytecodec/cjkcodecs/multibytecodec.h
--- a/pypy/module/_multibytecodec/cjkcodecs/multibytecodec.h
+++ b/pypy/module/_multibytecodec/cjkcodecs/multibytecodec.h
@@ -1,26 +1,18 @@
-/*
- * multibytecodec.h: Common Multibyte Codec Implementation
- *
- * Written by Hye-Shik Chang <perky at FreeBSD.org>
- */
 
-#ifndef _PYTHON_MULTIBYTECODEC_H_
-#define _PYTHON_MULTIBYTECODEC_H_
-#ifdef __cplusplus
-extern "C" {
-#endif
+#ifndef _PYPY_MULTIBYTECODEC_H_
+#define _PYPY_MULTIBYTECODEC_H_
 
-#ifdef uint32_t
-typedef uint32_t ucs4_t;
-#else
-typedef unsigned int ucs4_t;
-#endif
 
-#ifdef uint16_t
+#include <stddef.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <assert.h>
+
+#define Py_UNICODE_SIZE  4
+typedef uint32_t ucs4_t, Py_UNICODE;
 typedef uint16_t ucs2_t, DBCHAR;
-#else
-typedef unsigned short ucs2_t, DBCHAR;
-#endif
+typedef ssize_t Py_ssize_t;
+
 
 typedef union {
     void *p;
@@ -62,80 +54,14 @@
     mbdecodereset_func decreset;
 } MultibyteCodec;
 
-typedef struct {
-    PyObject_HEAD
-    MultibyteCodec *codec;
-} MultibyteCodecObject;
-
-#define MultibyteCodec_Check(op) ((op)->ob_type == &MultibyteCodec_Type)
-
-#define _MultibyteStatefulCodec_HEAD            \
-    PyObject_HEAD                               \
-    MultibyteCodec *codec;                      \
-    MultibyteCodec_State state;                 \
-    PyObject *errors;
-typedef struct {
-    _MultibyteStatefulCodec_HEAD
-} MultibyteStatefulCodecContext;
-
-#define MAXENCPENDING   2
-#define _MultibyteStatefulEncoder_HEAD          \
-    _MultibyteStatefulCodec_HEAD                \
-    Py_UNICODE pending[MAXENCPENDING];          \
-    Py_ssize_t pendingsize;
-typedef struct {
-    _MultibyteStatefulEncoder_HEAD
-} MultibyteStatefulEncoderContext;
-
-#define MAXDECPENDING   8
-#define _MultibyteStatefulDecoder_HEAD          \
-    _MultibyteStatefulCodec_HEAD                \
-    unsigned char pending[MAXDECPENDING];       \
-    Py_ssize_t pendingsize;
-typedef struct {
-    _MultibyteStatefulDecoder_HEAD
-} MultibyteStatefulDecoderContext;
-
-typedef struct {
-    _MultibyteStatefulEncoder_HEAD
-} MultibyteIncrementalEncoderObject;
-
-typedef struct {
-    _MultibyteStatefulDecoder_HEAD
-} MultibyteIncrementalDecoderObject;
-
-typedef struct {
-    _MultibyteStatefulDecoder_HEAD
-    PyObject *stream;
-} MultibyteStreamReaderObject;
-
-typedef struct {
-    _MultibyteStatefulEncoder_HEAD
-    PyObject *stream;
-} MultibyteStreamWriterObject;
 
 /* positive values for illegal sequences */
 #define MBERR_TOOSMALL          (-1) /* insufficient output buffer space */
 #define MBERR_TOOFEW            (-2) /* incomplete input buffer */
 #define MBERR_INTERNAL          (-3) /* internal runtime error */
 
-#define ERROR_STRICT            (PyObject *)(1)
-#define ERROR_IGNORE            (PyObject *)(2)
-#define ERROR_REPLACE           (PyObject *)(3)
-#define ERROR_ISCUSTOM(p)       ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
-#define ERROR_DECREF(p) do {                    \
-    if (p != NULL && ERROR_ISCUSTOM(p)) {       \
-        Py_DECREF(p);                           \
-    }                                           \
-} while (0);
-
 #define MBENC_FLUSH             0x0001 /* encode all characters encodable */
 #define MBENC_MAX               MBENC_FLUSH
 
-#define PyMultibyteCodec_CAPSULE_NAME "multibytecodec.__map_*"
 
-
-#ifdef __cplusplus
-}
 #endif
-#endif


More information about the pypy-commit mailing list