mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	Possibly the end of SF [#460020] bug or feature: unicode() and subclasses.
Changed unicode(i) to return a true Unicode object when i is an instance of a unicode subclass. Added PyUnicode_CheckExact macro.
This commit is contained in:
		
							parent
							
								
									0ebeb584a4
								
							
						
					
					
						commit
						78e0fc74bc
					
				
					 3 changed files with 16 additions and 6 deletions
				
			
		|  | @ -61,6 +61,7 @@ Copyright (c) Corporation for National Research Initiatives. | ||||||
| #ifndef Py_USING_UNICODE | #ifndef Py_USING_UNICODE | ||||||
| 
 | 
 | ||||||
| #define PyUnicode_Check(op)                 0 | #define PyUnicode_Check(op)                 0 | ||||||
|  | #define PyUnicode_CheckExact(op)            0 | ||||||
| 
 | 
 | ||||||
| #else | #else | ||||||
| 
 | 
 | ||||||
|  | @ -373,6 +374,7 @@ typedef struct { | ||||||
| extern DL_IMPORT(PyTypeObject) PyUnicode_Type; | extern DL_IMPORT(PyTypeObject) PyUnicode_Type; | ||||||
| 
 | 
 | ||||||
| #define PyUnicode_Check(op) PyObject_TypeCheck(op, &PyUnicode_Type) | #define PyUnicode_Check(op) PyObject_TypeCheck(op, &PyUnicode_Type) | ||||||
|  | #define PyUnicode_CheckExact(op) ((op)->ob_type == &PyUnicode_Type) | ||||||
| 
 | 
 | ||||||
| /* Fast access macros */ | /* Fast access macros */ | ||||||
| #define PyUnicode_GET_SIZE(op) \ | #define PyUnicode_GET_SIZE(op) \ | ||||||
|  |  | ||||||
|  | @ -1443,7 +1443,7 @@ def rev(self): | ||||||
|     verify(str(s).__class__ is str) |     verify(str(s).__class__ is str) | ||||||
| 
 | 
 | ||||||
|     s = madstring("\x00" * 5) |     s = madstring("\x00" * 5) | ||||||
|     #XXX verify(str(s) == "\x00" ( 5) |     #XXX verify(str(s) == "\x00" * 5) | ||||||
|     verify(str(s).__class__ is str) |     verify(str(s).__class__ is str) | ||||||
| 
 | 
 | ||||||
|     class madunicode(unicode): |     class madunicode(unicode): | ||||||
|  | @ -1460,7 +1460,7 @@ def rev(self): | ||||||
|     verify(u.rev().rev() == madunicode(u"ABCDEF")) |     verify(u.rev().rev() == madunicode(u"ABCDEF")) | ||||||
|     u = madunicode(u"12345") |     u = madunicode(u"12345") | ||||||
|     verify(unicode(u) == u"12345") |     verify(unicode(u) == u"12345") | ||||||
|     #XXX verify(unicode(u).__class__ is unicode) |     verify(unicode(u).__class__ is unicode) | ||||||
| 
 | 
 | ||||||
| def all(): | def all(): | ||||||
|     lists() |     lists() | ||||||
|  |  | ||||||
|  | @ -425,12 +425,20 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, | ||||||
|     } |     } | ||||||
|     if (PyUnicode_Check(obj)) { |     if (PyUnicode_Check(obj)) { | ||||||
| 	if (encoding) { | 	if (encoding) { | ||||||
| 	    PyErr_SetString(PyExc_TypeError, |             PyErr_SetString(PyExc_TypeError, | ||||||
| 			    "decoding Unicode is not supported"); | 			    "decoding Unicode is not supported"); | ||||||
| 	    return NULL; |             return NULL; | ||||||
| 	} | 	} | ||||||
| 	Py_INCREF(obj); |         if (PyUnicode_CheckExact(obj)) { | ||||||
| 	v = obj; | 	    Py_INCREF(obj); | ||||||
|  |             v = obj; | ||||||
|  | 	} | ||||||
|  |         else { | ||||||
|  |             /* For a subclass of unicode, return a true unicode object
 | ||||||
|  |                with the same string value. */ | ||||||
|  |             v = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | ||||||
|  |                                       PyUnicode_GET_SIZE(obj)); | ||||||
|  |         } | ||||||
| 	goto done; | 	goto done; | ||||||
|     } |     } | ||||||
|     else if (PyString_Check(obj)) { |     else if (PyString_Check(obj)) { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tim Peters
						Tim Peters