return -1;
}
+ /* Should be caused by first init or the resize to 0. */
+ assert(self->ob_bytes_object == Py_GetConstantBorrowed(Py_CONSTANT_EMPTY_BYTES));
+ assert(self->ob_exports == 0);
+
/* Make a quick exit if no first argument */
if (arg == NULL) {
if (encoding != NULL || errors != NULL) {
return -1;
}
encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
- if (encoded == NULL)
+ if (encoded == NULL) {
return -1;
+ }
assert(PyBytes_Check(encoded));
+
+ /* Most encodes return a new unique bytes, just use it as buffer. */
+ if (_PyObject_IsUniquelyReferenced(encoded)
+ && PyBytes_CheckExact(encoded))
+ {
+ Py_ssize_t size = Py_SIZE(encoded);
+ self->ob_bytes_object = encoded;
+ bytearray_reinit_from_bytes(self, size, size);
+ return 0;
+ }
new = bytearray_iconcat((PyObject*)self, encoded);
Py_DECREF(encoded);
if (new == NULL)