@@ -2764,15 +2764,15 @@ arraydescr_struct_list_str(PyArray_Descr *dtype)
2764
2764
}
2765
2765
/* Special case subarray handling here */
2766
2766
if (PyDataType_HASSUBARRAY (fld_dtype )) {
2767
- tmp = arraydescr_short_construction_repr (
2768
- fld_dtype -> subarray -> base , 0 );
2767
+ tmp = arraydescr_construction_repr (
2768
+ fld_dtype -> subarray -> base , 0 , 1 );
2769
2769
PyUString_ConcatAndDel (& ret , tmp );
2770
2770
PyUString_ConcatAndDel (& ret , PyUString_FromString (", " ));
2771
2771
PyUString_ConcatAndDel (& ret ,
2772
2772
PyObject_Str (fld_dtype -> subarray -> shape ));
2773
2773
}
2774
2774
else {
2775
- tmp = arraydescr_short_construction_repr (fld_dtype , 0 );
2775
+ tmp = arraydescr_construction_repr (fld_dtype , 0 , 1 );
2776
2776
PyUString_ConcatAndDel (& ret , tmp );
2777
2777
}
2778
2778
PyUString_ConcatAndDel (& ret , PyUString_FromString (")" ));
@@ -2830,7 +2830,7 @@ arraydescr_struct_dict_str(PyArray_Descr *dtype, int includealignedflag)
2830
2830
if (title != NULL && title != Py_None ) {
2831
2831
has_titles = 1 ;
2832
2832
}
2833
- tmp = arraydescr_short_construction_repr (fld_dtype , 0 );
2833
+ tmp = arraydescr_construction_repr (fld_dtype , 0 , 1 );
2834
2834
PyUString_ConcatAndDel (& ret , tmp );
2835
2835
if (i != names_size - 1 ) {
2836
2836
PyUString_ConcatAndDel (& ret , PyUString_FromString ("," ));
@@ -2914,7 +2914,7 @@ arraydescr_subarray_str(PyArray_Descr *dtype)
2914
2914
PyObject * p , * ret ;
2915
2915
2916
2916
ret = PyUString_FromString ("(" );
2917
- p = arraydescr_short_construction_repr (dtype -> subarray -> base , 0 );
2917
+ p = arraydescr_construction_repr (dtype -> subarray -> base , 0 , 1 );
2918
2918
PyUString_ConcatAndDel (& ret , p );
2919
2919
PyUString_ConcatAndDel (& ret , PyUString_FromString (", " ));
2920
2920
PyUString_ConcatAndDel (& ret , PyObject_Str (dtype -> subarray -> shape ));
@@ -2968,26 +2968,10 @@ arraydescr_struct_repr(PyArray_Descr *dtype)
2968
2968
return s ;
2969
2969
}
2970
2970
2971
- /*
2972
- * This creates a shorter repr using 'kind' and 'itemsize',
2973
- * instead of the longer type name. This is the object passed
2974
- * as the first parameter to the dtype constructor, and if no
2975
- * additional constructor parameters are given, will reproduce
2976
- * the exact memory layout.
2977
- *
2978
- * If 'includealignflag' is true, this includes the 'align=True'
10000
parameter
2979
- * inside the struct dtype construction dict when needed. Use this flag
2980
- * if you want a proper repr string without the 'dtype()' part around it.
2981
- *
2982
- * If 'includealignflag' is false, this does not preserve the
2983
- * 'align=True' parameter or sticky NPY_ALIGNED_STRUCT flag for
2984
- * struct arrays like the regular repr does, because the 'align'
2985
- * flag is not part of first dtype constructor parameter. This
2986
- * mode is intended for a full 'repr', where the 'align=True' is
2987
- * provided as the second parameter.
2988
- */
2971
+ /* See descriptor.h for documentation */
2989
2972
NPY_NO_EXPORT PyObject *
2990
- arraydescr_short_construction_repr (PyArray_Descr * dtype , int includealignflag )
2973
+ arraydescr_construction_repr (PyArray_Descr * dtype , int includealignflag ,
2974
+ int shortrepr )
2991
2975
{
2992
2976
PyObject * ret ;
2993
2977
PyArray_DatetimeMetaData * meta ;
@@ -3019,11 +3003,44 @@ arraydescr_short_construction_repr(PyArray_Descr *dtype, int includealignflag)
3019
3003
3020
3004
/* Handle booleans, numbers, and custom dtypes */
3021
3005
if (dtype -> type_num == NPY_BOOL ) {
3022
- return PyUString_FromString ("'?'" );
3006
+ if (shortrepr ) {
3007
+ return PyUString_FromString ("'?'" );
3008
+ }
3009
+ else {
3010
+ return PyUString_FromString ("'bool'" );
3011
+ }
3023
3012
}
3024
3013
else if (PyTypeNum_ISNUMBER (dtype -> type_num )) {
3025
- return PyUString_FromFormat ("'%s%c%d'" , byteorder , (int )dtype -> kind ,
3026
- dtype -> elsize );
3014
+ /* Short repr with endianness, like '<f8' */
3015
+ if (shortrepr || (dtype -> byteorder != NPY_NATIVE &&
3016
+ dtype -> byteorder != NPY_IGNORE )) {
3017
+ return PyUString_FromFormat ("'%s%c%d'" , byteorder ,
3018
+ (int )dtype -> kind , dtype -> elsize );
3019
+ }
3020
+ /* Longer repr, like 'float64' */
3021
+ else {
3022
+ char * kindstr ;
3023
+ switch (dtype -> kind ) {
3024
+ case 'u' :
3025
+ kindstr = "uint" ;
3026
+ break ;
3027
+ case 'i' :
3028
+ kindstr = "int" ;
3029
+ break ;
3030
+ case 'f' :
3031
+ kindstr = "float" ;
3032
+ break ;
3033
+ case 'c' :
3034
+ kindstr = "complex" ;
3035
+ break ;
3036
+ default :
3037
+ PyErr_Format (PyExc_RuntimeError ,
3038
+ "internal dtype repr error, unknown kind '%c'" ,
3039
+ (int )dtype -> kind );
3040
+ return NULL ;
3041
+ }
3042
+ return PyUString_FromFormat ("'%s%d'" , kindstr , 8 * dtype -> elsize );
3043
+ }
3027
3044
}
3028
3045
else if (PyTypeNum_ISUSERDEF (dtype -> type_num )) {
3029
3046
char * s = strrchr (dtype -> typeobj -> tp_name , '.' );
@@ -3102,27 +3119,17 @@ arraydescr_short_construction_repr(PyArray_Descr *dtype, int includealignflag)
3102
3119
static PyObject *
3103
3120
arraydescr_repr (PyArray_Descr * dtype )
3104
3121
{
3105
- PyObject * sub , * s ;
3122
+ PyObject * ret ;
3106
3123
3107
3124
if (PyDataType_HASFIELDS (dtype )) {
3108
3125
return arraydescr_struct_repr (dtype );
3109
3126
}
3110
3127
else {
3111
- s = PyUString_FromString ("dtype(" );
3112
- sub = arraydescr_str (dtype );
3113
- if (sub == NULL ) {
3114
- return NULL ;
3115
- }
3116
- if (!PyDataType_HASSUBARRAY (dtype )) {
3117
- PyObject * t = PyUString_FromString ("'" );
3118
- PyUString_Concat (& sub , t );
3119
- PyUString_ConcatAndDel (& t , sub );
3120
- sub = t ;
3121
- }
3122
- PyUString_ConcatAndDel (& s , sub );
3123
- sub = PyUString_FromString (")" );
3124
- PyUString_ConcatAndDel (& s , sub );
3125
- return s ;
3128
+ ret = PyUString_FromString ("dtype(" );
3129
+ PyUString_ConcatAndDel (& ret ,
3130
+ arraydescr_construction_repr (dtype , 1 , 0 ));
3131
+ PyUString_ConcatAndDel (& ret , PyUString_FromString (")" ));
3132
+ return ret ;
3126
3133
}
3127
3134
}
3128
3135
0 commit comments