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
Extended the examples of string.rst and put them in the examples directory for testing. #2361
Conversation
…ctory for testing.
# A type cast to `object` or `bytes` will do the same thing: | ||
py_string = <bytes> c_string | ||
|
||
free(c_string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely needs a try-finally, because the object creation might fail with MemoryError
.
And since that adds even more complexity to an originally very simple piece of example code, I'd rather leave this specific 2-line snippet untouched.
# get pointer and length from a C function | ||
get_a_c_string(&c_string, &length) | ||
|
||
py_bytes_string = c_string[:length] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a try-finally.
6f3a711
to
c6be828
Compare
try: | ||
py_bytes_string = c_string[:length] # Performs a copy of the data | ||
finally: | ||
PyMem_Free(c_string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, no, please use plain malloc()
and free()
here. The intention is to show how to interact with something C-ish (e.g. an external C library), not with something that understands the CPython C-API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I though it would be a good example. I'll change it.
…ippet back to the .rst file.
c6be828
to
5656dea
Compare
I added examples of manipulation of strings in Cython (the
c_func.pyx
andc_func.pxd
) because I think it's a good thing to show more cython examples (even if they are trivial).If you don't want them in the docs, we can keep them in the examples directory (for the tests) and not display them in the documentation.