bpo-1635741, unicodedata: add ucd_type parameter to UCD_Check() macro (GH-22328)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Mohamed Koubaa 2020-09-23 05:38:16 -05:00 committed by GitHub
parent 83de110dce
commit ddc0dd001a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 13 deletions

View File

@ -92,7 +92,10 @@ static PyMemberDef DB_members[] = {
/* forward declaration */ /* forward declaration */
static PyTypeObject UCD_Type; static PyTypeObject UCD_Type;
#define UCD_Check(o) Py_IS_TYPE(o, &UCD_Type)
// Check if self is an instance of UCD_Type.
// Return 0 if self is NULL (when the PyCapsule C API is used).
#define UCD_Check(self, ucd_type) (self != NULL && Py_IS_TYPE(self, ucd_type))
static PyObject* static PyObject*
new_previous_version(const char*name, const change_record* (*getrecord)(Py_UCS4), new_previous_version(const char*name, const change_record* (*getrecord)(Py_UCS4),
@ -135,7 +138,7 @@ unicodedata_UCD_decimal_impl(PyObject *self, int chr,
long rc; long rc;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) { if (old->category_changed == 0) {
/* unassigned */ /* unassigned */
@ -223,7 +226,7 @@ unicodedata_UCD_numeric_impl(PyObject *self, int chr,
double rc; double rc;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) { if (old->category_changed == 0) {
/* unassigned */ /* unassigned */
@ -268,7 +271,7 @@ unicodedata_UCD_category_impl(PyObject *self, int chr)
int index; int index;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
index = (int) _getrecord_ex(c)->category; index = (int) _getrecord_ex(c)->category;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed != 0xFF) if (old->category_changed != 0xFF)
index = old->category_changed; index = old->category_changed;
@ -295,7 +298,7 @@ unicodedata_UCD_bidirectional_impl(PyObject *self, int chr)
int index; int index;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
index = (int) _getrecord_ex(c)->bidirectional; index = (int) _getrecord_ex(c)->bidirectional;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) if (old->category_changed == 0)
index = 0; /* unassigned */ index = 0; /* unassigned */
@ -324,7 +327,7 @@ unicodedata_UCD_combining_impl(PyObject *self, int chr)
int index; int index;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
index = (int) _getrecord_ex(c)->combining; index = (int) _getrecord_ex(c)->combining;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) if (old->category_changed == 0)
index = 0; /* unassigned */ index = 0; /* unassigned */
@ -352,7 +355,7 @@ unicodedata_UCD_mirrored_impl(PyObject *self, int chr)
int index; int index;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
index = (int) _getrecord_ex(c)->mirrored; index = (int) _getrecord_ex(c)->mirrored;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) if (old->category_changed == 0)
index = 0; /* unassigned */ index = 0; /* unassigned */
@ -379,7 +382,7 @@ unicodedata_UCD_east_asian_width_impl(PyObject *self, int chr)
int index; int index;
Py_UCS4 c = (Py_UCS4)chr; Py_UCS4 c = (Py_UCS4)chr;
index = (int) _getrecord_ex(c)->east_asian_width; index = (int) _getrecord_ex(c)->east_asian_width;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) if (old->category_changed == 0)
index = 0; /* unassigned */ index = 0; /* unassigned */
@ -413,7 +416,7 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
code = (int)c; code = (int)c;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
const change_record *old = get_old_record(self, c); const change_record *old = get_old_record(self, c);
if (old->category_changed == 0) if (old->category_changed == 0)
return PyUnicode_FromString(""); /* unassigned */ return PyUnicode_FromString(""); /* unassigned */
@ -460,7 +463,7 @@ get_decomp_record(PyObject *self, Py_UCS4 code, int *index, int *prefix, int *co
{ {
if (code >= 0x110000) { if (code >= 0x110000) {
*index = 0; *index = 0;
} else if (self && UCD_Check(self) && } else if (UCD_Check(self, &UCD_Type) &&
get_old_record(self, code)->category_changed==0) { get_old_record(self, code)->category_changed==0) {
/* unassigned in old version */ /* unassigned in old version */
*index = 0; *index = 0;
@ -558,7 +561,7 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
continue; continue;
} }
/* normalization changes */ /* normalization changes */
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
Py_UCS4 value = ((PreviousDBVersion*)self)->normalization(code); Py_UCS4 value = ((PreviousDBVersion*)self)->normalization(code);
if (value != 0) { if (value != 0) {
stack[stackptr++] = value; stack[stackptr++] = value;
@ -799,7 +802,7 @@ is_normalized_quickcheck(PyObject *self, PyObject *input,
{ {
/* An older version of the database is requested, quickchecks must be /* An older version of the database is requested, quickchecks must be
disabled. */ disabled. */
if (self && UCD_Check(self)) if (UCD_Check(self, &UCD_Type))
return NO; return NO;
Py_ssize_t i, len; Py_ssize_t i, len;
@ -1066,7 +1069,7 @@ _getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen,
if (!with_alias_and_seq && (IS_ALIAS(code) || IS_NAMED_SEQ(code))) if (!with_alias_and_seq && (IS_ALIAS(code) || IS_NAMED_SEQ(code)))
return 0; return 0;
if (self && UCD_Check(self)) { if (UCD_Check(self, &UCD_Type)) {
/* in 3.2.0 there are no aliases and named sequences */ /* in 3.2.0 there are no aliases and named sequences */
const change_record *old; const change_record *old;
if (IS_ALIAS(code) || IS_NAMED_SEQ(code)) if (IS_ALIAS(code) || IS_NAMED_SEQ(code))