Repository navigation
Add PyBytes_AsString and PyBytes_AsStringAndSize - #8462
Conversation
add bytes_as_string and bytes_as_string_and_size
guilhermeleobas
left a comment
There was a problem hiding this comment.
Hi @ianna, thanks for working on this issue. I only have one small comment regarding the return value of PyBytes_AsStringAndSize
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
|
@guilhermeleobas - Where shall I add a unit test for this PR? Thanks! |
|
Hi @ianna, I don't think Numba has any tests that covers the Python API. Perhaps you can create a file for that. Let me know if you need any help |
PyBytes_AsStringAndSize returns an integer
Thanks! It would be nice to have an example to follow. Could you, please, point me to one? Thanks! |
|
HI @ianna, I wrote a small snippet of code which tests Perhaps there's a better way to test the API (cc @sklam). """
Test Python API
"""
import ctypes
import unittest
from numba.core import types
from numba.core.extending import intrinsic
from numba import jit
@intrinsic
def _pyapi_bytes_as_string(typingctx, csrc, size):
sig = types.voidptr(csrc, size) # cstring == void*
def codegen(context, builder, sig, args):
[csrc, size] = args
api = context.get_python_api(builder)
b = api.bytes_from_string_and_size(csrc, size)
return api.bytes_as_string(b)
return sig, codegen
def PyBytes_AsString(uni):
# test_PyBytes_AsString will call this function with a unicode type.
# We then use the underlying buffer to create a PyBytes object and call the
# PyBytes_AsString function with PyBytes object as argument
return _pyapi_bytes_as_string(uni._data, uni._length)
class TestPythonAPI(unittest.TestCase):
def test_PyBytes_AsString(self):
cfunc = jit(nopython=True)(PyBytes_AsString)
cstr = cfunc('hello') # returns a cstring
fn = ctypes.pythonapi.PyBytes_FromString
fn.argtypes = [ctypes.c_void_p]
fn.restype = ctypes.py_object
obj = fn(cstr)
# Use the cstring created from bytes_as_string to create a python
# bytes object
self.assertEqual(obj, b'hello')
if __name__ == '__main__':
unittest.main()Let me know if you have any questions. |
|
This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks! |
|
Hi @ianna, sorry for the really late review. I just saw you made progress in the PR. Could you merge Numba main branch into your branch? So that CI can run again |
guilhermeleobas
left a comment
There was a problem hiding this comment.
Hi @ianna, could you add a test for PyBytes_AsStringAndSize? The idea for the test is similar to what you wrote:
- Get a pointer to a c string and size by calling PyBytes_AsStringAndSize` inside an intrinsic
- Use ctypes to call
PyBytes_FromStringAndSizewith the return values of the previous function
@intrinsic
def _pyapi_bytes_as_string_and_size(typingctx, csrc, size):
# return a tuple containing the c-string and size
retty = types.Tuple.from_types((csrc, size))
sig = retty(csrc, size)
def codegen(context, builder, sig, args):
[csrc, size] = args
pyapi = context.get_python_api(builder)
b = pyapi.bytes_from_string_and_size(csrc, size)
p_cstr = builder.alloca(pyapi.cstring)
p_size = builder.alloca(pyapi.py_ssize_t)
pyapi.bytes_as_string_and_size(b, p_cstr, p_size)
cstr = builder.load(p_cstr)
size = builder.load(p_size)
tup = context.make_tuple(builder, sig.return_type, (cstr, size))
return tup
return sig, codegen
def PyBytes_AsStringAndSize(uni):
return _pyapi_bytes_as_string_and_size(uni._data, uni._length)
ianna
left a comment
There was a problem hiding this comment.
@guilhermeleobas - I think, I'm done with this PR. Both functions have tests. Please, have a look. Thanks!
guilhermeleobas
left a comment
There was a problem hiding this comment.
Hi @ianna, thanks. Just one last change.
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
guilhermeleobas
left a comment
There was a problem hiding this comment.
LGTM. Thanks @ianna
Resolve issue #8455:
add
bytes_as_stringandbytes_as_string_and_size