Skip to content

Conversation

@BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Aug 16, 2025

This PR adds primitives for:

  • bytes.rjust(width)
  • bytes.ljust(width, fillbyte)
  • bytes.rjust(width)
  • bytes.ljust(width, fillbyte)

This PR is ready for review.

@BobTheBuidler BobTheBuidler marked this pull request as draft August 16, 2025 15:45
@BobTheBuidler BobTheBuidler marked this pull request as ready for review August 16, 2025 21:35
@BobTheBuidler BobTheBuidler force-pushed the just branch 2 times, most recently from 14b314e to f766dff Compare August 26, 2025 19:37


PyObject *CPyBytes_RjustCustomFill(PyObject *self, CPyTagged width, PyObject *fillbyte) {
if (!PyBytes_Check(self)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we get rid of these type checks?

Copy link
Collaborator

Choose a reason for hiding this comment

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

A variable annotated with bytes can also be a bytearray, and all primitives need to support both. Also we need to consider subclasses overriding the method -- we only assume a small number of key methods are not overridden, and we try to keep this set small. These methods are not common enough to be worth adding as special cases, when more commonly used methods support overriding. See CPyBytes_Join for an example of how to do this -- special case for exact bytes values (i.e. no subclass instances) and provide a fallback implementation using generic operations.

PyErr_SetString(PyExc_TypeError, "self must be bytes");
return NULL;
}
if (!PyBytes_Check(fillbyte) || PyBytes_Size(fillbyte) != 1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we get rid of these type checks?

Copy link
Collaborator

Choose a reason for hiding this comment

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

See above -- we need type checks, and they must use an exact bytes check, and there must also be a fallback implementation.



PyObject *CPyBytes_RjustCustomFill(PyObject *self, CPyTagged width, PyObject *fillbyte) {
if (!PyBytes_Check(self)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A variable annotated with bytes can also be a bytearray, and all primitives need to support both. Also we need to consider subclasses overriding the method -- we only assume a small number of key methods are not overridden, and we try to keep this set small. These methods are not common enough to be worth adding as special cases, when more commonly used methods support overriding. See CPyBytes_Join for an example of how to do this -- special case for exact bytes values (i.e. no subclass instances) and provide a fallback implementation using generic operations.

PyErr_SetString(PyExc_TypeError, "self must be bytes");
return NULL;
}
if (!PyBytes_Check(fillbyte) || PyBytes_Size(fillbyte) != 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above -- we need type checks, and they must use an exact bytes check, and there must also be a fallback implementation.

char *res_buf = PyBytes_AsString(result);
memset(res_buf, ' ', pad);
memcpy(res_buf + pad, PyBytes_AsString(self), len);
return result;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please share most of the implementations using a helper function, since the functions are very similar (across all four functions), and these methods probably aren't super performance critical to make the code duplication worth it (the likely gains would be very small, since most of the CPU is spent in the common shared code). E.g. add a function that takes the bytes value, width, a bool flag for ljust/rjust and the fill character (char) as arguments.

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