Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Modules/unicodedata.c

    r2 r391  
    11/* ------------------------------------------------------------------------
    22
    3    unicodedata -- Provides access to the Unicode 5.1 data base.
    4 
    5    Data was extracted from the Unicode 5.1 UnicodeData.txt file.
     3   unicodedata -- Provides access to the Unicode 5.2 data base.
     4
     5   Data was extracted from the Unicode 5.2 UnicodeData.txt file.
    66
    77   Written by Marc-Andre Lemburg (mal@lemburg.com).
     
    2020
    2121typedef struct {
    22     const unsigned char category;       /* index into
    23                                            _PyUnicode_CategoryNames */
    24     const unsigned char combining;      /* combining class value 0 - 255 */
    25     const unsigned char bidirectional;  /* index into
    26                                            _PyUnicode_BidirectionalNames */
    27     const unsigned char mirrored;       /* true if mirrored in bidir mode */
    28     const unsigned char east_asian_width;       /* index into
    29                                                    _PyUnicode_EastAsianWidth */
     22    const unsigned char category;       /* index into
     23                                           _PyUnicode_CategoryNames */
     24    const unsigned char combining;      /* combining class value 0 - 255 */
     25    const unsigned char bidirectional;  /* index into
     26                                           _PyUnicode_BidirectionalNames */
     27    const unsigned char mirrored;       /* true if mirrored in bidir mode */
     28    const unsigned char east_asian_width;       /* index into
     29                                                   _PyUnicode_EastAsianWidth */
     30    const unsigned char normalization_quick_check; /* see is_normalized() */
    3031} _PyUnicode_DatabaseRecord;
    3132
     
    3637    const unsigned char decimal_changed;
    3738    const unsigned char mirrored_changed;
    38     const int numeric_changed;
     39    const double numeric_changed;
    3940} change_record;
    4041
     
    6768
    6869static PyMemberDef DB_members[] = {
    69         {"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY},
     70        {"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY},
    7071        {NULL}
    7172};
     
    7879                     Py_UCS4 (*normalization)(Py_UCS4))
    7980{
    80         PreviousDBVersion *self;
    81         self = PyObject_New(PreviousDBVersion, &UCD_Type);
    82         if (self == NULL)
    83                 return NULL;
    84         self->name = name;
    85         self->getrecord = getrecord;
     81        PreviousDBVersion *self;
     82        self = PyObject_New(PreviousDBVersion, &UCD_Type);
     83        if (self == NULL)
     84                return NULL;
     85        self->name = name;
     86        self->getrecord = getrecord;
    8687        self->normalization = normalization;
    87         return (PyObject*)self;
     88        return (PyObject*)self;
    8889}
    8990
     
    9495
    9596    if (PyUnicode_GET_SIZE(obj) == 1)
    96         return *v;
     97        return *v;
    9798#ifndef Py_UNICODE_WIDE
    9899    else if ((PyUnicode_GET_SIZE(obj) == 2) &&
    99100             (0xD800 <= v[0] && v[0] <= 0xDBFF) &&
    100101             (0xDC00 <= v[1] && v[1] <= 0xDFFF))
    101         return (((v[0] & 0x3FF)<<10) | (v[1] & 0x3FF)) + 0x10000;
     102        return (((v[0] & 0x3FF)<<10) | (v[1] & 0x3FF)) + 0x10000;
    102103#endif
    103104    PyErr_SetString(PyExc_TypeError,
     
    136137            have_old = 1;
    137138            rc = -1;
    138         } 
     139        }
    139140        else if (old->decimal_changed != 0xFF) {
    140141            have_old = 1;
     
    146147        rc = Py_UNICODE_TODECIMAL(c);
    147148    if (rc < 0) {
    148         if (defobj == NULL) {
    149             PyErr_SetString(PyExc_ValueError,
    150                             "not a decimal");
     149        if (defobj == NULL) {
     150            PyErr_SetString(PyExc_ValueError,
     151                            "not a decimal");
    151152            return NULL;
    152         }
    153         else {
    154             Py_INCREF(defobj);
    155             return defobj;
    156         }
     153        }
     154        else {
     155            Py_INCREF(defobj);
     156            return defobj;
     157        }
    157158    }
    158159    return PyInt_FromLong(rc);
     
    181182    rc = Py_UNICODE_TODIGIT(c);
    182183    if (rc < 0) {
    183         if (defobj == NULL) {
    184             PyErr_SetString(PyExc_ValueError, "not a digit");
     184        if (defobj == NULL) {
     185            PyErr_SetString(PyExc_ValueError, "not a digit");
    185186            return NULL;
    186         }
    187         else {
    188             Py_INCREF(defobj);
    189             return defobj;
    190         }
     187        }
     188        else {
     189            Py_INCREF(defobj);
     190            return defobj;
     191        }
    191192    }
    192193    return PyInt_FromLong(rc);
     
    221222            have_old = 1;
    222223            rc = -1.0;
    223         } 
     224        }
    224225        else if (old->decimal_changed != 0xFF) {
    225226            have_old = 1;
     
    231232        rc = Py_UNICODE_TONUMERIC(c);
    232233    if (rc == -1.0) {
    233         if (defobj == NULL) {
    234             PyErr_SetString(PyExc_ValueError, "not a numeric character");
    235             return NULL;
    236         }
    237         else {
    238             Py_INCREF(defobj);
    239             return defobj;
    240         }
     234        if (defobj == NULL) {
     235            PyErr_SetString(PyExc_ValueError, "not a numeric character");
     236            return NULL;
     237        }
     238        else {
     239            Py_INCREF(defobj);
     240            return defobj;
     241        }
    241242    }
    242243    return PyFloat_FromDouble(rc);
     
    257258
    258259    if (!PyArg_ParseTuple(args, "O!:category",
    259                           &PyUnicode_Type, &v))
    260         return NULL;
     260                          &PyUnicode_Type, &v))
     261        return NULL;
    261262    c = getuchar(v);
    262263    if (c == (Py_UCS4)-1)
     
    274275"bidirectional(unichr)\n\
    275276\n\
    276 Returns the bidirectional category assigned to the Unicode character\n\
     277Returns the bidirectional class assigned to the Unicode character\n\
    277278unichr as string. If no such value is defined, an empty string is\n\
    278279returned.");
     
    286287
    287288    if (!PyArg_ParseTuple(args, "O!:bidirectional",
    288                           &PyUnicode_Type, &v))
    289         return NULL;
     289                          &PyUnicode_Type, &v))
     290        return NULL;
    290291    c = getuchar(v);
    291292    if (c == (Py_UCS4)-1)
     
    317318
    318319    if (!PyArg_ParseTuple(args, "O!:combining",
    319                           &PyUnicode_Type, &v))
    320         return NULL;
     320                          &PyUnicode_Type, &v))
     321        return NULL;
    321322    c = getuchar(v);
    322323    if (c == (Py_UCS4)-1)
     
    346347
    347348    if (!PyArg_ParseTuple(args, "O!:mirrored",
    348                           &PyUnicode_Type, &v))
    349         return NULL;
     349                          &PyUnicode_Type, &v))
     350        return NULL;
    350351    c = getuchar(v);
    351352    if (c == (Py_UCS4)-1)
     
    376377
    377378    if (!PyArg_ParseTuple(args, "O!:east_asian_width",
    378                           &PyUnicode_Type, &v))
    379         return NULL;
     379                          &PyUnicode_Type, &v))
     380        return NULL;
    380381    c = getuchar(v);
    381382    if (c == (Py_UCS4)-1)
     
    407408
    408409    if (!PyArg_ParseTuple(args, "O!:decomposition",
    409                           &PyUnicode_Type, &v))
    410         return NULL;
     410                          &PyUnicode_Type, &v))
     411        return NULL;
    411412    c = getuchar(v);
    412413    if (c == (Py_UCS4)-1)
     
    454455        i += strlen(decomp + i);
    455456    }
    456    
     457
    457458    decomp[i] = '\0';
    458459
     
    474475                               (code&((1<<DECOMP_SHIFT)-1))];
    475476    }
    476        
     477
    477478    /* high byte is number of hex bytes (usually one or two), low byte
    478479       is prefix code (from*/
     
    499500    Py_UNICODE *i, *end, *o;
    500501    /* Longest decomposition in Unicode 3.2: U+FDFA */
    501     Py_UNICODE stack[20]; 
     502    Py_UNICODE stack[20];
    502503    Py_ssize_t space, isize;
    503504    int index, prefix, count, stackptr;
    504505    unsigned char prev, cur;
    505        
     506
    506507    stackptr = 0;
    507508    isize = PyUnicode_GET_SIZE(input);
    508     /* Overallocate atmost 10 characters. */
     509    /* Overallocate at most 10 characters. */
    509510    space = (isize > 10 ? 10 : isize) + isize;
    510511    result = PyUnicode_FromUnicode(NULL, space);
     
    520521            Py_UNICODE code = stack[--stackptr];
    521522            /* Hangul Decomposition adds three characters in
    522                a single step, so we need atleast that much room. */
     523               a single step, so we need at least that much room. */
    523524            if (space < 3) {
    524525                Py_ssize_t newsize = PyString_GET_SIZE(result) + 10;
     
    640641    end = i + PyUnicode_GET_SIZE(result);
    641642    o = PyUnicode_AS_UNICODE(result);
    642        
     643
    643644  again:
    644645    while (i < end) {
    645646      for (index = 0; index < cskipped; index++) {
    646647          if (skipped[index] == i) {
    647               /* *i character is skipped. 
     648              /* *i character is skipped.
    648649                 Remove from list. */
    649650              skipped[index] = skipped[cskipped-1];
     
    656657         pairs, since we always have decomposed data. */
    657658      if (LBase <= *i && *i < (LBase+LCount) &&
    658           i + 1 < end && 
     659          i + 1 < end &&
    659660          VBase <= i[1] && i[1] <= (VBase+VCount)) {
    660661          int LIndex, VIndex;
     
    682683      while (i1 < end) {
    683684          int comb1 = _getrecord_ex(*i1)->combining;
    684           if (comb1 && comb == comb1) {
    685               /* Character is blocked. */
    686               i1++;
    687               continue;
     685          if (comb) {
     686              if (comb1 == 0)
     687                  break;
     688              if (comb >= comb1) {
     689                  /* Character is blocked. */
     690                  i1++;
     691                  continue;
     692              }
    688693          }
    689694          l = find_nfc_index(self, nfc_last, *i1);
     
    705710          if (code == 0)
    706711              goto not_combinable;
    707                        
     712
    708713          /* Replace the original character. */
    709714          *i = code;
    710715          /* Mark the second character unused. */
     716          assert(cskipped < 20);
    711717          skipped[cskipped++] = i1;
    712718          i1++;
     
    721727    return result;
    722728}
    723                
     729
     730/* Return 1 if the input is certainly normalized, 0 if it might not be. */
     731static int
     732is_normalized(PyObject *self, PyObject *input, int nfc, int k)
     733{
     734    Py_UNICODE *i, *end;
     735    unsigned char prev_combining = 0, quickcheck_mask;
     736
     737    /* An older version of the database is requested, quickchecks must be
     738       disabled. */
     739    if (self != NULL)
     740        return 0;
     741
     742    /* The two quickcheck bits at this shift mean 0=Yes, 1=Maybe, 2=No,
     743       as described in http://unicode.org/reports/tr15/#Annex8. */
     744    quickcheck_mask = 3 << ((nfc ? 4 : 0) + (k ? 2 : 0));
     745
     746    i = PyUnicode_AS_UNICODE(input);
     747    end = i + PyUnicode_GET_SIZE(input);
     748    while (i < end) {
     749        const _PyUnicode_DatabaseRecord *record = _getrecord_ex(*i++);
     750        unsigned char combining = record->combining;
     751        unsigned char quickcheck = record->normalization_quick_check;
     752
     753        if (quickcheck & quickcheck_mask)
     754            return 0; /* this string might need normalization */
     755        if (combining && prev_combining > combining)
     756            return 0; /* non-canonical sort order, not normalized */
     757        prev_combining = combining;
     758    }
     759    return 1; /* certainly normalized */
     760}
     761
    724762PyDoc_STRVAR(unicodedata_normalize__doc__,
    725763"normalize(form, unistr)\n\
     
    745783    }
    746784
    747     if (strcmp(form, "NFC") == 0)
     785    if (strcmp(form, "NFC") == 0) {
     786        if (is_normalized(self, input, 1, 0)) {
     787            Py_INCREF(input);
     788            return input;
     789        }
    748790        return nfc_nfkc(self, input, 0);
    749     if (strcmp(form, "NFKC") == 0)
     791    }
     792    if (strcmp(form, "NFKC") == 0) {
     793        if (is_normalized(self, input, 1, 1)) {
     794            Py_INCREF(input);
     795            return input;
     796        }
    750797        return nfc_nfkc(self, input, 1);
    751     if (strcmp(form, "NFD") == 0)
     798    }
     799    if (strcmp(form, "NFD") == 0) {
     800        if (is_normalized(self, input, 0, 0)) {
     801            Py_INCREF(input);
     802            return input;
     803        }
    752804        return nfd_nfkd(self, input, 0);
    753     if (strcmp(form, "NFKD") == 0)
     805    }
     806    if (strcmp(form, "NFKD") == 0) {
     807        if (is_normalized(self, input, 0, 1)) {
     808            Py_INCREF(input);
     809            return input;
     810        }
    754811        return nfd_nfkd(self, input, 1);
     812    }
    755813    PyErr_SetString(PyExc_ValueError, "invalid normalization form");
    756814    return NULL;
     
    773831    unsigned long ix;
    774832    for (i = 0; i < len; i++) {
    775         h = (h * scale) + (unsigned char) toupper(Py_CHARMASK(s[i]));
     833        h = (h * scale) + (unsigned char) Py_TOUPPER(Py_CHARMASK(s[i]));
    776834        ix = h & 0xff000000;
    777835        if (ix)
     
    817875    return (
    818876        (0x3400 <= code && code <= 0x4DB5) || /* CJK Ideograph Extension A */
    819         (0x4E00 <= code && code <= 0x9FBB) || /* CJK Ideograph */
    820         (0x20000 <= code && code <= 0x2A6D6));/* CJK Ideograph Extension B */
     877        (0x4E00 <= code && code <= 0x9FCB) || /* CJK Ideograph, Unicode 5.2 */
     878        (0x20000 <= code && code <= 0x2A6D6) || /* CJK Ideograph Extension B */
     879        (0x2A700 <= code && code <= 0x2B734));  /* CJK Ideograph Extension C */
    821880}
    822881
     
    837896            /* unassigned */
    838897            return 0;
    839         } 
     898        }
    840899    }
    841900
    842901    if (SBase <= code && code < SBase+SCount) {
    843         /* Hangul syllable. */
    844         int SIndex = code - SBase;
    845         int L = SIndex / NCount;
    846         int V = (SIndex % NCount) / TCount;
    847         int T = SIndex % TCount;
    848 
    849         if (buflen < 27)
    850             /* Worst case: HANGUL SYLLABLE <10chars>. */
    851             return 0;
    852         strcpy(buffer, "HANGUL SYLLABLE ");
    853         buffer += 16;
    854         strcpy(buffer, hangul_syllables[L][0]);
    855         buffer += strlen(hangul_syllables[L][0]);
    856         strcpy(buffer, hangul_syllables[V][1]);
    857         buffer += strlen(hangul_syllables[V][1]);
    858         strcpy(buffer, hangul_syllables[T][2]);
    859         buffer += strlen(hangul_syllables[T][2]);
    860         *buffer = '\0';
    861         return 1;
     902        /* Hangul syllable. */
     903        int SIndex = code - SBase;
     904        int L = SIndex / NCount;
     905        int V = (SIndex % NCount) / TCount;
     906        int T = SIndex % TCount;
     907
     908        if (buflen < 27)
     909            /* Worst case: HANGUL SYLLABLE <10chars>. */
     910            return 0;
     911        strcpy(buffer, "HANGUL SYLLABLE ");
     912        buffer += 16;
     913        strcpy(buffer, hangul_syllables[L][0]);
     914        buffer += strlen(hangul_syllables[L][0]);
     915        strcpy(buffer, hangul_syllables[V][1]);
     916        buffer += strlen(hangul_syllables[V][1]);
     917        strcpy(buffer, hangul_syllables[T][2]);
     918        buffer += strlen(hangul_syllables[T][2]);
     919        *buffer = '\0';
     920        return 1;
    862921    }
    863922
     
    920979        return 0;
    921980    for (i = 0; i < namelen; i++) {
    922         if (toupper(Py_CHARMASK(name[i])) != buffer[i])
     981        if (Py_TOUPPER(Py_CHARMASK(name[i])) != buffer[i])
    923982            return 0;
    924983    }
     
    926985}
    927986
    928 static void 
     987static void
    929988find_syllable(const char *str, int *len, int *pos, int count, int column)
    930989{
     
    932991    *len = -1;
    933992    for (i = 0; i < count; i++) {
    934         char *s = hangul_syllables[i][column];
    935         len1 = strlen(s);
    936         if (len1 <= *len)
    937             continue;
    938         if (strncmp(str, s, len1) == 0) {
    939             *len = len1;
    940             *pos = i;
    941         }
     993        char *s = hangul_syllables[i][column];
     994        len1 = strlen(s);
     995        if (len1 <= *len)
     996            continue;
     997        if (strncmp(str, s, len1) == 0) {
     998            *len = len1;
     999            *pos = i;
     1000        }
    9421001    }
    9431002    if (*len == -1) {
    944         *len = 0;
     1003        *len = 0;
    9451004    }
    9461005}
     
    9551014    /* Check for hangul syllables. */
    9561015    if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) {
    957         int len, L = -1, V = -1, T = -1;
    958         const char *pos = name + 16;
    959         find_syllable(pos, &len, &L, LCount, 0);
    960         pos += len;
    961         find_syllable(pos, &len, &V, VCount, 1);
    962         pos += len;
    963         find_syllable(pos, &len, &T, TCount, 2);
    964         pos += len;
    965         if (L != -1 && V != -1 && T != -1 && pos-name == namelen) {
    966             *code = SBase + (L*VCount+V)*TCount + T;
    967             return 1;
    968         }
     1016        int len, L = -1, V = -1, T = -1;
     1017        const char *pos = name + 16;
     1018        find_syllable(pos, &len, &L, LCount, 0);
     1019        pos += len;
     1020        find_syllable(pos, &len, &V, VCount, 1);
     1021        pos += len;
     1022        find_syllable(pos, &len, &T, TCount, 2);
     1023        pos += len;
     1024        if (L != -1 && V != -1 && T != -1 && pos-name == namelen) {
     1025            *code = SBase + (L*VCount+V)*TCount + T;
     1026            return 1;
     1027        }
    9691028        /* Otherwise, it's an illegal syllable name. */
    9701029        return 0;
     
    10261085}
    10271086
    1028 static const _PyUnicode_Name_CAPI hashAPI = 
     1087static const _PyUnicode_Name_CAPI hashAPI =
    10291088{
    10301089    sizeof(_PyUnicode_Name_CAPI),
     
    10581117
    10591118    if (!_getucname(self, c, name, sizeof(name))) {
    1060         if (defobj == NULL) {
    1061             PyErr_SetString(PyExc_ValueError, "no such name");
     1119        if (defobj == NULL) {
     1120            PyErr_SetString(PyExc_ValueError, "no such name");
    10621121            return NULL;
    1063         }
    1064         else {
    1065             Py_INCREF(defobj);
    1066             return defobj;
    1067         }
     1122        }
     1123        else {
     1124            Py_INCREF(defobj);
     1125            return defobj;
     1126        }
    10681127    }
    10691128
     
    11031162#endif
    11041163    str[0] = (Py_UNICODE) code;
    1105     return PyUnicode_FromUnicode(str, 1);   
     1164    return PyUnicode_FromUnicode(str, 1);
    11061165}
    11071166
     
    11281187    {"normalize", unicodedata_normalize, METH_VARARGS,
    11291188                  unicodedata_normalize__doc__},
    1130     {NULL, NULL}                /* sentinel */
     1189    {NULL, NULL}                /* sentinel */
    11311190};
    11321191
    11331192static PyTypeObject UCD_Type = {
    1134         /* The ob_type field must be initialized in the module init function
    1135         * to be portable to Windows without using C++. */
    1136         PyVarObject_HEAD_INIT(NULL, 0)
    1137         "unicodedata.UCD",              /*tp_name*/
    1138         sizeof(PreviousDBVersion),      /*tp_basicsize*/
    1139         0,                      /*tp_itemsize*/
    1140         /* methods */
    1141         (destructor)PyObject_Del, /*tp_dealloc*/
    1142         0,                      /*tp_print*/
    1143         0,                      /*tp_getattr*/
    1144         0,                      /*tp_setattr*/
    1145         0,                      /*tp_compare*/
    1146         0,                      /*tp_repr*/
    1147         0,                      /*tp_as_number*/
    1148         0,                      /*tp_as_sequence*/
    1149         0,                      /*tp_as_mapping*/
    1150         0,                      /*tp_hash*/
     1193        /* The ob_type field must be initialized in the module init function
     1194        * to be portable to Windows without using C++. */
     1195        PyVarObject_HEAD_INIT(NULL, 0)
     1196        "unicodedata.UCD",              /*tp_name*/
     1197        sizeof(PreviousDBVersion),      /*tp_basicsize*/
     1198        0,                      /*tp_itemsize*/
     1199        /* methods */
     1200        (destructor)PyObject_Del, /*tp_dealloc*/
     1201        0,                      /*tp_print*/
     1202        0,                      /*tp_getattr*/
     1203        0,                      /*tp_setattr*/
     1204        0,                      /*tp_compare*/
     1205        0,                      /*tp_repr*/
     1206        0,                      /*tp_as_number*/
     1207        0,                      /*tp_as_sequence*/
     1208        0,                      /*tp_as_mapping*/
     1209        0,                      /*tp_hash*/
    11511210        0,                      /*tp_call*/
    11521211        0,                      /*tp_str*/
     
    11811240defines character properties for all Unicode characters. The data in\n\
    11821241this database is based on the UnicodeData.txt file version\n\
    1183 5.1.0 which is publically available from ftp://ftp.unicode.org/.\n\
     12425.2.0 which is publically available from ftp://ftp.unicode.org/.\n\
    11841243\n\
    11851244The module uses the same names and symbols as defined by the\n\
    1186 UnicodeData File Format 5.1.0 (see\n\
    1187 http://www.unicode.org/Public/5.1.0/ucd/UCD.html).");
     1245UnicodeData File Format 5.2.0 (see\n\
     1246http://www.unicode.org/reports/tr44/tr44-4.html).");
    11881247
    11891248PyMODINIT_FUNC
     
    12091268
    12101269    /* Export C API */
    1211     v = PyCObject_FromVoidPtr((void *) &hashAPI, NULL);
     1270    v = PyCapsule_New((void *)&hashAPI, PyUnicodeData_CAPSULE_NAME, NULL);
    12121271    if (v != NULL)
    12131272        PyModule_AddObject(m, "ucnhash_CAPI", v);
    12141273}
    12151274
    1216 /* 
     1275/*
    12171276Local variables:
    12181277c-basic-offset: 4
Note: See TracChangeset for help on using the changeset viewer.