* Converts an input object into datetime metadata. The input * may be either a string or a tuple. * * Returns 0 on success, -1 on failure. */
| 1933 | * Returns 0 on success, -1 on failure. |
| 1934 | */ |
| 1935 | NPY_NO_EXPORT int |
| 1936 | convert_pyobject_to_datetime_metadata(PyObject *obj, |
| 1937 | PyArray_DatetimeMetaData *out_meta) |
| 1938 | { |
| 1939 | if (PyTuple_Check(obj)) { |
| 1940 | return convert_datetime_metadata_tuple_to_datetime_metadata( |
| 1941 | obj, out_meta, NPY_FALSE); |
| 1942 | } |
| 1943 | |
| 1944 | /* Get a UTF8 string */ |
| 1945 | PyObject *utf8 = NULL; |
| 1946 | if (PyBytes_Check(obj)) { |
| 1947 | /* Allow bytes format strings: convert to unicode */ |
| 1948 | utf8 = PyUnicode_FromEncodedObject(obj, NULL, NULL); |
| 1949 | if (utf8 == NULL) { |
| 1950 | return -1; |
| 1951 | } |
| 1952 | } |
| 1953 | else if (PyUnicode_Check(obj)) { |
| 1954 | utf8 = obj; |
| 1955 | Py_INCREF(utf8); |
| 1956 | } |
| 1957 | else { |
| 1958 | PyErr_SetString(PyExc_TypeError, |
| 1959 | "Invalid object for specifying NumPy datetime metadata"); |
| 1960 | return -1; |
| 1961 | } |
| 1962 | |
| 1963 | Py_ssize_t len = 0; |
| 1964 | char const *str = PyUnicode_AsUTF8AndSize(utf8, &len); |
| 1965 | if (str == NULL) { |
| 1966 | Py_DECREF(utf8); |
| 1967 | return -1; |
| 1968 | } |
| 1969 | |
| 1970 | if (len > 0 && str[0] == '[') { |
| 1971 | int r = parse_datetime_metadata_from_metastr(str, len, out_meta); |
| 1972 | Py_DECREF(utf8); |
| 1973 | return r; |
| 1974 | } |
| 1975 | else { |
| 1976 | if (parse_datetime_extended_unit_from_string(str, len, |
| 1977 | NULL, out_meta) < 0) { |
| 1978 | Py_DECREF(utf8); |
| 1979 | return -1; |
| 1980 | } |
| 1981 | |
| 1982 | Py_DECREF(utf8); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Return the datetime metadata as a Unicode object. |
nothing calls this directly
no test coverage detected