Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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

@@ -345,11 +345,11 @@ class Buffer:
"""
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


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

@@ -825,18 +825,20 @@ cdef class Buffer:
"""
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

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