[Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread Martin v. Löwis
#1653736 reports that slot_nb_inplace_power has the wrong
type: it should be a ternary function, but only is a binary.
The proposed change is to make it ternary, and to invoke __ipow__
with three arguments.

In researching this, I came to wonder why nb_inplace_power is
ternary in the first place. It is the implementation of

foo **= bar

(and that's its only use), so it ought to be binary. Historically,
the reason probably is that it was copied from nb_power, which is
ternary because it also implements pow()).

So here is my proposed change:

1. For 2.5.1, rewrite slot_nb_inplace_power to raise an exception
if the third argument is not None, and then invoke __ipow__
with only one argument.

2. For 2.6, change the API to make nb_inplace_power binary.

Optionally, in 2.5, the exception could be added into other places
as well, e.g. PyNumber_InPlacePower and instance_ipow (rather than
invoking __ipow__ with a third argument if its not None).

Comments?

Regards,
Martin
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread python
[MvL]
>>> So here is my proposed change:
>>> 
>>> 1. For 2.5.1, rewrite slot_nb_inplace_power to raise an exception
>>> if the third argument is not None, and then invoke __ipow__ with only one 
>>> argument.

Why would you change Py2.5?  There is no bug here.


Raymond
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread Martin v. Löwis
[EMAIL PROTECTED] schrieb:
 1. For 2.5.1, rewrite slot_nb_inplace_power to raise an exception
 if the third argument is not None, and then invoke __ipow__ with only one 
 argument.
> 
> Why would you change Py2.5?  There is no bug here.

There is: slot_nb_inplace has the signature

static PyObject * slot_nb_inplace_power(PyObject *self, PyObject * arg1)

yet it is stored in as_number.nb_inplace_power, which is defined as

typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
ternaryfunc nb_inplace_power;

This has undefined behavior. Even if it had, slot_nb_inplace_power
would silently discard its third argument.

Regards,
Martin
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread Brett Cannon
On 2/8/07, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
> #1653736 reports that slot_nb_inplace_power has the wrong
> type: it should be a ternary function, but only is a binary.
> The proposed change is to make it ternary, and to invoke __ipow__
> with three arguments.
>
> In researching this, I came to wonder why nb_inplace_power is
> ternary in the first place. It is the implementation of
>
> foo **= bar
>
> (and that's its only use), so it ought to be binary. Historically,
> the reason probably is that it was copied from nb_power, which is
> ternary because it also implements pow()).
>
> So here is my proposed change:
>
> 1. For 2.5.1, rewrite slot_nb_inplace_power to raise an exception
> if the third argument is not None, and then invoke __ipow__
> with only one argument.
>
> 2. For 2.6, change the API to make nb_inplace_power binary.
>
> Optionally, in 2.5, the exception could be added into other places
> as well, e.g. PyNumber_InPlacePower and instance_ipow (rather than
> invoking __ipow__ with a third argument if its not None).
>
> Comments?
>

Seems reasonable to me.  Is the argument of None passed in
automatically somewhere?  I think the 2.6 change is definitely good,
just don't know if the exception is really needed.  I realize the
signature is off, but it doesn't hurt anyone at this point if it
stayed wrong since obviously the semantics would be the same as they
were.

-Brett
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread Greg Ewing
Martin v. Löwis wrote:
> It is the implementation of
> 
> foo **= bar
> 
> (and that's its only use), so it ought to be binary.

Maybe it's so that a type can plug the same implementation
into both nb_pow and nb_inplace_pow. Although the same
effect could be achieved by just leaving nb_inplace_pow
null, so I suppose that's not necessary.

Might we want to add an in-place version of the 3-arg
pow() function one day? If so, leaving the third argument
there could be useful.

--
Greg
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Adding timeout option to httplib...connect()

2007-02-08 Thread Guido van Rossum
I recently needed to access an HTTP URL with a timeout. I ended up
monkey-patching httplib.HTTPConnection so that the connect() method
has an optional second paramer, timeout, defaulting to None; if not
None, a call to settimeout() is added right after successful creation
of the socket.

Does anybody else think this is a good idea? (Personally I think this
should've been done years ago. :-) Shall I check it into the head?

Index: httplib.py
===
--- httplib.py  (revision 53456)
+++ httplib.py  (working copy)
@@ -656,7 +656,7 @@
 def set_debuglevel(self, level):
 self.debuglevel = level

-def connect(self):
+def connect(self, timeout=None):
 """Connect to the host and port specified in __init__."""
 msg = "getaddrinfo returns an empty list"
 for res in socket.getaddrinfo(self.host, self.port, 0,
@@ -664,6 +664,8 @@
 af, socktype, proto, canonname, sa = res
 try:
 self.sock = socket.socket(af, socktype, proto)
+if timeout is not None:
+self.sock.settimeout(timeout)
 if self.debuglevel > 0:
 print "connect: (%s, %s)" % (self.host, self.port)
 self.sock.connect(sa)


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adding timeout option to httplib...connect()

2007-02-08 Thread Raymond Hettinger
[GvR]
>I recently needed to access an HTTP URL with a timeout. I ended up
> monkey-patching httplib.HTTPConnection so that the connect() method
> has an optional second paramer, timeout, defaulting to None; if not
> None, a call to settimeout() is added right after successful creation
> of the socket.
> 
> Does anybody else think this is a good idea? (Personally I think this
> should've been done years ago. :-) Shall I check it into the head?

Yes.  This is has been requested more than once, but no one ever
got around to adding the timeout..


Raymond
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adding timeout option to httplib...connect()

2007-02-08 Thread Josiah Carlson

"Guido van Rossum" <[EMAIL PROTECTED]> wrote:
> 
> I recently needed to access an HTTP URL with a timeout. I ended up
> monkey-patching httplib.HTTPConnection so that the connect() method
> has an optional second paramer, timeout, defaulting to None; if not
> None, a call to settimeout() is added right after successful creation
> of the socket.
> 
> Does anybody else think this is a good idea? (Personally I think this
> should've been done years ago. :-) Shall I check it into the head?

I've done similar things to urllib in the past (also handling timeouts
and insufficient data transfer rates).

Seems like a reasonable change to me.  It also makes me think that I
should switch to httplib (since all the urls I ever fetch are http).

 - Josiah

___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Why is nb_inplace_power ternary?

2007-02-08 Thread Raymond Hettinger
[MvL]
> 1. For 2.5.1, rewrite slot_nb_inplace_power to raise an exception
> if the third argument is not None, and then invoke __ipow__ with only one 
> argument.

[Raymond]
>> Why would you change Py2.5?  There is no bug here.

[MvL]
> There is: slot_nb_inplace has the signature
>
> static PyObject * slot_nb_inplace_power(PyObject *self, PyObject * arg1)
>
> yet it is stored in as_number.nb_inplace_power, which is defined as
>
> typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
> ternaryfunc nb_inplace_power;
>
> This has undefined behavior. Even if it had, slot_nb_inplace_power
> would silently discard its third argument.

That made sense, but my question was whether there would be benefit
to making the change in the middle of a major release.  At worst, code that is
currently working due to undefined behavior will stop working.   I don't
see any offsetting benefit.  ISTM that Py2.5 should be left as-is and that
the full signature change be made in Py2.6.


Raymond

___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com