Skip to content

Conversation

StanFromIreland
Copy link
Member

@StanFromIreland StanFromIreland commented Apr 23, 2025

There was little support for raising errors so a docs note is probably the best thing to do -- atleast for now


📚 Documentation preview 📚: https://cpython-previews--132858.org.readthedocs.build/

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

This looks fine. I'll tag @encukou as the current ctypes expert, but be aware he's currently on leave.

@encukou
Copy link
Member

encukou commented Apr 28, 2025

I'd write it like this, to also mention what happens if size is too large:

.. function:: create_string_buffer(init, size=None)
              create_string_buffer(size)

   Create a mutable character buffer. The returned object is a
   ctypes array of :class:`c_char`.

   If *size* is given (and not None), it must be an :cls:`int.
   It specifies the size of the returned array.

   If the *init* argument is given, it must be :cls:`bytes`. It is used
   to initialize the array items. Bytes not initialized this way are
   set to zero (NUL).

   If *size* is not given (or if it is None), the buffer is made one byte
   larger than *init*, effectively adding a NUL termination byte.

   If both arguments are given, *size* must not be less than ``len(init)``.

   .. warning::

      If *size* is equal to ``len(init)``, a NUL terminator is
      not added. Do not treat such a buffer as a C string.

   For example::

      >>> bytes(create_string_buffer(2))
      b'\x00\x00'

      >>> bytes(create_string_buffer(b'ab'))
      b'ab\x00'

      >>> bytes(create_string_buffer(b'ab', 2))
      b'ab'

      >>> bytes(create_string_buffer(b'ab', 4))
      b'ab\x00\x00'

      >>> bytes(create_string_buffer(b'abcdef', 2))
      Traceback (most recent call last):
         ...
      ValueError: byte string too long

# Conflicts:
#	Doc/library/ctypes.rst
@StanFromIreland
Copy link
Member Author

@encukou I have applied your suggested format.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@StanFromIreland
Copy link
Member Author

@encukou Friendly ping :-)

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Oops, I left my comments in the “pending” state. Sorry for the delay!

@StanFromIreland StanFromIreland requested a review from encukou May 18, 2025 07:45
@encukou
Copy link
Member

encukou commented May 29, 2025

Looks great now. Thank you!

@encukou encukou merged commit b783e17 into python:main May 29, 2025
24 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs May 29, 2025
@miss-islington-app
Copy link

Thanks @StanFromIreland for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 29, 2025
…pythonGH-132858)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
(cherry picked from commit b783e17)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 29, 2025
…pythonGH-132858)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
(cherry picked from commit b783e17)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented May 29, 2025

GH-134881 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label May 29, 2025
@bedevere-app
Copy link

bedevere-app bot commented May 29, 2025

GH-134882 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 29, 2025
@StanFromIreland StanFromIreland deleted the ctypes-note branch May 29, 2025 13:17
encukou pushed a commit that referenced this pull request May 29, 2025
GH-132858) (GH-134882)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
(cherry picked from commit b783e17)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
encukou pushed a commit that referenced this pull request May 29, 2025
…H-132858) (GH-134881)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
(cherry picked from commit b783e17)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…pythonGH-132858)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…pythonGH-132858)

This adds a warning about the possibly-missing NUL terminator, but in a way
that doesn't make it sound like a bug/wart.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news topic-ctypes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants