Skip to content
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

PEP 461 format of bytes string returns basestring (python 3.5.1+) #2153

Closed
djvernon68 opened this issue Mar 15, 2018 · 1 comment
Closed

Comments

@djvernon68
Copy link

djvernon68 commented Mar 15, 2018

Issue:
PEP 461 style bytes formatting (b'%b' % variable) returns basestring. So the following code results in a cythonize error:

cpdef bytes cy_foo():
    cdef:
        bytes bytes_1, bytes_2

    bytes_1 = b'bytes 1'
    bytes_2 = b'bytes 2'

    return b'%(bytes_1)b %(bytes_2)b' % {
                    b'bytes_1': bytes_1,
                    b'bytes_2': bytes_2} 

Result:
Cannot convert 'basestring' object to bytes implicitly. This is not portable.

This same basic code in python 3.5.1+ returns a bytes object.

@scoder
Copy link
Contributor

scoder commented Mar 17, 2018

The formatting works correctly, but Cython is not smart enough here to detect that the formatting is guaranteed to return a bytes object in this case because it only works in Py3 (and raises an exception in Py2 due to an unknown format conversion type 'b').

As a work-around, you can use a cast, i.e.
return <bytes> (b'...' % {...})

@scoder scoder added this to the 0.28.1 milestone Mar 17, 2018
@scoder scoder closed this as completed in e09468b Mar 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants