Skip to content

BREAKING CHANGE: buffer expose as bytes instead of str#100

Closed
kafka1991 wants to merge 4 commits intomainfrom
update_string_v8
Closed

BREAKING CHANGE: buffer expose as bytes instead of str#100
kafka1991 wants to merge 4 commits intomainfrom
update_string_v8

Conversation

@kafka1991
Copy link
Collaborator

BREAKING CHANGE: buffer expose as bytes instead of str

@kafka1991 kafka1991 requested a review from amunra March 25, 2025 09:10
@kafka1991 kafka1991 self-assigned this Mar 25, 2025

def __str__(self) -> str:
"""Return the constructed buffer as a string. Use for debugging."""
def peek(self) -> bytes:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python it would be idiomatic to use __bytes__ instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 30 to 32
# Ditto, see comment on why not returning a `PyObject` above.
object PyBytes_FromStringAndSize(
const char* u, Py_ssize_t size)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

This is already defined in the cython standard lib correctly.
See: https://github.com/cython/cython/blob/master/Cython/Includes/cpython/bytes.pxd where it defines:

bytes PyBytes_FromStringAndSize(char *v, Py_ssize_t len)

Usage within ingress.pyx would be something like:

from cpython.bytes cimport PyBytes_FromStringAndSize

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

The current number of bytes currently in the buffer.

Equivalent (but cheaper) to ``len(str(sender))``.
Equivalent (but cheaper) to ``len(buffer.peek())``.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update docs to to len(bytes(buffer)).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


def __str__(self) -> str:
"""Return the constructed buffer as a string. Use for debugging."""
def peek(self) -> bytes:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__bytes__

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Number of bytes of unsent data in the internal buffer.

Also see :func:`Sender.__len__`.
Equivalent (but cheaper) to ``len(sender.peek())``.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

"""

def __len__(self) -> int:
def peek(self) -> bytes:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__bytes__

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

The current number of bytes currently in the buffer.

Equivalent (but cheaper) to ``len(str(sender))``.
Equivalent (but cheaper) to ``len(buffer.peek())``.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update doc

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return line_sender_buffer_size(self._impl)

def __str__(self) -> str:
def peek(self) -> bytes:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__bytes__

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return PyUnicode_FromStringAndSize(utf8, <Py_ssize_t>size)
cdef inline object _to_bytes(self):
cdef line_sender_buffer_view view = line_sender_buffer_peek(self._impl)
if view.len:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement is redundant here I think. Impl:

PyObject *
PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
{
    PyBytesObject *op;
    if (size < 0) {
        PyErr_SetString(PyExc_SystemError,
            "Negative size passed to PyBytes_FromStringAndSize");
        return NULL;
    }
    if (size == 1 && str != NULL) {
        op = CHARACTER(*str & 255);
        assert(_Py_IsImmortal(op));
        return (PyObject *)op;
    }
    if (size == 0) {
        return bytes_get_empty();    // <<<<<<<<<<<<<<<<<<<
    }
    ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, PyBytes_FromStringAndSize handles the <= 0 case.

Copy link
Collaborator

@amunra amunra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor tweaks required.

@amunra
Copy link
Collaborator

amunra commented Mar 25, 2025

CI needs a tweak as well:

##[error]No config name or imagelabel provided in request
,##[error]The remote provider was unable to process the request.
Pool: [Azure Pipelines](https://dev.azure.com/questdb/6c9c1a0a-74cf-4f7b-bf65-24ae4f3cd61d/_settings/agentqueues?poolId=&queueId=9)
Image: macOS-12
Started: Today at 09:17
Duration: 1h 53m 59s

I suspect macOS-12 is no longer supported, should be the next available oldest version.

@kafka1991
Copy link
Collaborator Author

I suspect macOS-12 is no longer supported, should be the next available oldest version.

Yeah, macOS-12 has been moved

@kafka1991
Copy link
Collaborator Author

Included In MR #105

@kafka1991 kafka1991 closed this May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants