[Python-Dev] socket.setsockopt() with optval=NULL

2016-08-21 Thread Christian Heimes
Hi,

the socket.setsockopt(level, optname, value) method has two calling
variants. When it is called with a buffer-like object as value, it calls
the C API function setsockopt() with optval=buffer.buf and
optlen=buffer.len. When value is an integer, setsockopt() packs it as
int32 and sends it with optlen=4.

---
# example.py
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
b'\x00\x00\x00\x00')
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
---

$ strace -e setsockopt ./python example.py
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [0], 4) = 0
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0


For AF_ALG (Linux Kernel crypto) I need a way to call the C API function
setsockopt() with optval=NULL and optlen as any arbitrary number. I have
been playing with multiple ideas. So far I liked the idea of
value=(None, int) most.

setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE, (None, taglen))

What do you think?

Christian
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Anyone know Brendan Scott, author of 'Python for Kids'?

2016-08-21 Thread Terry Reedy
Brendan Scott, author of 'Python for Kids for Dummies' is revising his 
book to produce a Python 3 version.  Great so far.


Unfortunately, he thinks that Python 3 turned range() into a (generator) 
function that produces a generator, one that can be indexed and sliced. 
I left a comment on his blog explaining that range() still produces 
arithmetic sequence objects.  The difference is that in Py 3, the 
compact representation is not expanded to a verbose (and redundant) 
Python list until requested.  It is *not* like map and filter, which 
*were* changed to return iterators.


For whatever reason, my comment had no effect and today on
https://python4kids.brendanscott.com/2016/08/21/python-for-kids-python-3-project-7/
he still says, near the top

>>> # range(10) is no longer a list. Rather, it's a generator
>>> # so the [:] operator slices the generator. You can use list()
>>> # to see what list the generator corresponds to.

I left a second comment, again with interactive code examples, but I 
fear the result (nothing) will be the same.  We cannot stop people from 
publishing such misleading dis-information,  but we can discourage it, 
and I think we should, especially if we can before paper copies are 
printed.  Beginners have enough problem with iterables versus iterators 
without a major beginner book confusing the two.  I imagine this leading 
to tutor-list and Stackoverflow questions like "Why doesn't slicing my 
generator work?  It did for range?"


So, if you agree with me, please either write Brendan personally if you 
know him, or just leave your own comment on the blog.


--
Terry Jan Reedy

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] socket.setsockopt() with optval=NULL

2016-08-21 Thread Guido van Rossum
Wouldn't

setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE, None, taglen)

be more consistent?

--Guido (mobile)

On Aug 21, 2016 5:40 AM, "Christian Heimes"  wrote:

> Hi,
>
> the socket.setsockopt(level, optname, value) method has two calling
> variants. When it is called with a buffer-like object as value, it calls
> the C API function setsockopt() with optval=buffer.buf and
> optlen=buffer.len. When value is an integer, setsockopt() packs it as
> int32 and sends it with optlen=4.
>
> ---
> # example.py
> import socket
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
> b'\x00\x00\x00\x00')
> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> ---
>
> $ strace -e setsockopt ./python example.py
> setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [0], 4) = 0
> setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
>
>
> For AF_ALG (Linux Kernel crypto) I need a way to call the C API function
> setsockopt() with optval=NULL and optlen as any arbitrary number. I have
> been playing with multiple ideas. So far I liked the idea of
> value=(None, int) most.
>
> setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE, (None, taglen))
>
> What do you think?
>
> Christian
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> guido%40python.org
>
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] socket.setsockopt() with optval=NULL

2016-08-21 Thread Martin Panter
On 21 August 2016 at 12:37, Christian Heimes  wrote:
> the socket.setsockopt(level, optname, value) method has two calling
> variants. When it is called with a buffer-like object as value, it calls
> the C API function setsockopt() with optval=buffer.buf and
> optlen=buffer.len. When value is an integer, setsockopt() packs it as
> int32 and sends it with optlen=4.
>
> ---
> # example.py
> import socket
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
> b'\x00\x00\x00\x00')
> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> ---
>
> $ strace -e setsockopt ./python example.py
> setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [0], 4) = 0
> setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
>
>
> For AF_ALG (Linux Kernel crypto) I need a way to call the C API function
> setsockopt() with optval=NULL and optlen as any arbitrary number. I have
> been playing with multiple ideas. So far I liked the idea of
> value=(None, int) most.
>
> setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE, (None, taglen))

Would this new functionality be open-ended? What would happen if you did

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, (None, 4))

On Linux, this seems to fail with errno = EFAULT, which would be fine.
But this does not seem to be specified by Posix, and I imagine other
platforms might crash. On the other hand, these sort of holes are
already present in the socket module. E.g. with MSG_TRUNC
 you can apparently get Python to
copy from unallocated memory.
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com