* Return a pointer to the underlying C string in 'value'. The pointer is * guaranteed by 'tvalue', decrease its refcount when done with the string. */
| 85 | * guaranteed by 'tvalue', decrease its refcount when done with the string. |
| 86 | */ |
| 87 | const char* |
| 88 | pgit_borrow_encoding(PyObject *value, const char *encoding, const char *errors, PyObject **tvalue) |
| 89 | { |
| 90 | PyObject *py_value = NULL; |
| 91 | PyObject *py_str = NULL; |
| 92 | |
| 93 | py_value = PyOS_FSPath(value); |
| 94 | if (py_value == NULL) { |
| 95 | Error_type_error("unexpected %.200s", value); |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | // Get new PyBytes reference from value |
| 100 | if (PyUnicode_Check(py_value)) { // Text string |
| 101 | py_str = PyUnicode_AsEncodedString( |
| 102 | py_value, |
| 103 | encoding ? encoding : "utf-8", |
| 104 | errors ? errors : "strict" |
| 105 | ); |
| 106 | |
| 107 | Py_DECREF(py_value); |
| 108 | if (py_str == NULL) |
| 109 | return NULL; |
| 110 | } else if (PyBytes_Check(py_value)) { // Byte string |
| 111 | py_str = py_value; |
| 112 | } else { // Type error |
| 113 | Error_type_error("unexpected %.200s", value); |
| 114 | Py_DECREF(py_value); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | // Borrow c string from the new PyBytes reference |
| 119 | char *c_str = PyBytes_AsString(py_str); |
| 120 | if (c_str == NULL) { |
| 121 | Py_DECREF(py_str); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | // Return the borrowed c string and the new PyBytes reference |
| 126 | *tvalue = py_str; |
| 127 | return c_str; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
no test coverage detected
searching dependent graphs…