Skip to content

protocol/display: fix Python 3 buffer abstraction#62

Merged
vasily-v-ryabov merged 1 commit into
python-xlib:masterfrom
benoit-pierre:fix_python3_buffer_abstraction
Oct 25, 2016
Merged

protocol/display: fix Python 3 buffer abstraction#62
vasily-v-ryabov merged 1 commit into
python-xlib:masterfrom
benoit-pierre:fix_python3_buffer_abstraction

Conversation

@benoit-pierre

Copy link
Copy Markdown
Member

With Python 2, slicing a buffer yield new data, but with Python 3, slicing a memoryview return another memoryview:

> python2 -c 'print buffer(b"12345")[1:3].decode()'
23

> python3 -c 'print(memoryview(b"12345")[1:3].decode())'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'memoryview' object has no attribute 'decode'

@codecov-io

codecov-io commented Oct 22, 2016

Copy link
Copy Markdown

Current coverage is 81.24% (diff: 100%)

Merging #62 into master will increase coverage by 0.09%

@@             master        #62   diff @@
==========================================
  Files            39         39          
  Lines          4399       4415    +16   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           3570       3587    +17   
+ Misses          829        828     -1   
  Partials          0          0          

Powered by Codecov. Last update 9ffdb29...95ba513

@vasily-v-ryabov vasily-v-ryabov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good observation. Just let me criticize some minor things.

Comment thread Xlib/protocol/display.py Outdated

class bytesview(object):

def __init__(self, data, offset=None, size=None):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not just offset=0 by default? It saves 2 lines below.

Comment thread Xlib/protocol/display.py Outdated
if isinstance(data, bytes):
view = memoryview(data)
elif isinstance(data, bytesview):
view = data._view

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All static analyzers will produce a warning for this line. Accessing pseudo-private member of another object can be eliminated with a read-only property view for the _view field (as an option). Or you may make it public for simplicity.

Comment thread Xlib/protocol/display.py Outdated
elif isinstance(data, bytesview):
view = data._view
else:
raise ValueError('unsupported type: %s' % type(data))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe raise TypeError('unsupported type: {}'.format(type(data))) is better and should work on py2.6+.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, but we don't support 2.6 anymore. In fact, there's a extraneous view = memoryview(data) in the Python 2 code that made me realize that memoryview is also supported in Python 2, starting with 2.7, so we could further simplify the code by just using one BytesView class for all supported Python versions. What do you think?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I remember, we don't support py2.6 (just mentioned it because .format() is new in 2.6). I like the idea of BytesView class.

Comment thread Xlib/protocol/display.py Outdated

def bytesview(data, offset=None, size=None):
if not isinstance(data, (bytes, buffer)):
raise ValueError('unsupported type: %s' % type(data))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The same as above.

@benoit-pierre

Copy link
Copy Markdown
Member Author

Thanks for the review, I'll amend.

With Python 2, slicing a buffer yield new data, but with Python 3,
slicing a memoryview return another memoryview:

> python2 -c 'print buffer(b"12345")[1:3].decode()'
23

> python3 -c 'print(memoryview(b"12345")[1:3].decode())'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'memoryview' object has no attribute 'decode'
@benoit-pierre benoit-pierre force-pushed the fix_python3_buffer_abstraction branch from 69768a3 to 95ba513 Compare October 22, 2016 20:20
@benoit-pierre

Copy link
Copy Markdown
Member Author

Unfortunately, memoryview behaves differently in Python 2 and 3...

> python2 -c 'print bytes(memoryview(b"12345"))'
<memory at 0x2ae8f7a2f9d0>

> python3 -c 'print(bytes(memoryview(b"12345")))'
b'12345'

So I kept the 2 different code paths for Python 2 and 3, and addressed your comments.

@vasily-v-ryabov vasily-v-ryabov merged commit 3ab64bf into python-xlib:master Oct 25, 2016
@benoit-pierre benoit-pierre deleted the fix_python3_buffer_abstraction branch October 27, 2016 21:11
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.

3 participants