-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
threading.Timer should be a class so that it can be derived #55177
Comments
Hi, Due to Timer's object creation behavior, it is impossible to subclass it. This kind of declaration will result to an exception raising: class A(Timer): pass |
Works for me: >>> from timeit import Timer
>>> class A(Timer): pass
... (Tested with Python 3.1 and 3.2 on OSX.) |
I've tested it with Python 3.1.2 under Windows 7 32 bits. It raises the following TypeError exception "function() argument 1 must be code, not str" |
I can't reproduce the error. Do you have a script that shows the issue? |
Ah, got it. It's about threading.Timer, which looks like a class, but is actually a function. |
It seems to be by design: from the documentation: What are you trying to do here? overriding run() is probably wrong; and overriding __init__ is better done by passing the correct parameters to threading.Timer(). |
Yes that's it. I Should precise it. I've taken a screenshot from my python's interpreter to spot it. |
AFAIK this is by design. Not that I agree with this decision anyway. |
Adding Guido, who checked the module in, to nosy. Guido: Could you tell us whether the fake classes in threading.py should stay as is or can be fixed? The whole _Verbose business and Thing/_Thing indirection seem a bit outdated and unneeded. |
IIRC: The design started out this way because it predates new-style classes. When this was put in one couldn't subclass extension types, and there were plans/hopes to replace some of the lock types with platform-specific built-in versions on some platforms. Since it is now possible to write subclassable extension types the Thing/_Thing design is no longer needed. I'm not sure about the _Verbose hacks, if it is deemed not useful I'm fine with letting it go. |
See also bpo-5831. That should probably be closed as a dup of this since this has an explanation. |
One concern expressed on a duplicate report by Martijn van Oosterhout:
Stable versions (2.7, 3.1, soon 3.2) won’t get this change. They may get a doc patch to warn people about the fake class/factory thing. In the py3k documentation for threading, some of the fake classes are documented as factories (Event) and other ones as classes (Timer); do you people think there would be harm in cleaning up all those outdated shenanigans for 3.3, with due versionchanged notes in the doc? |
Attached patch removes the indirection functions; the _Verbose shenanigans are left alone. The test suite passes; I haven’t edited the doc yet. |
I’ve committed the cleanup to my 3.3 clone and will push this week. Here’s a doc patch. Before my patch, the various classes were documented in two parts: one entry with the factory function (e.g. Thread), without index reference, and one section (e.g. “Thread Objects”), which used a class directive (and thus index target) for most but not all classes. After my patch, all classes are documented with a class directive, in their section (i.e. “Thread Objects”), with a versionchanged note informing that the name used to be that of a factory function. The only remaining glitch is that the “X Objects” sections start with a description of the class’ use, which references methods with constructs like :meth:`run`, which cannot be turned into links as Sphinx lacks context: the class directive only happens after. I could move the class directives right after the heading (“X Objects”), so that the meth roles get turned into links. |
You ought to be able to use either a context directive (I forget its name and syntax), or the full reference syntax (:meth:`~threading.Thread.run`) to make those links work without moving things around. |
Hm, do you mean something similar to currentmodule?
Yes, Thread.run would work, but I find that inelegant. |
New changeset 7f606223578a by Éric Araujo in branch 'default': |
@eric: The doc has to be updated: http://docs.python.org/dev/library/threading.html#threading.activeCount "threading.Condition() See Condition Objects." -- See also (maybe) issue bpo-12960. |
Victor: Yes, I know the doc needs an update, that’s why I posted a patch six weeks ago and asked for a review. |
Oh ok, cool, I missed the patches. |
Ping. |
New changeset 98499371ca53 by R David Murray in branch '3.3': New changeset ad435964fc9c by R David Murray in branch 'default': |
I have now committed (a revised version of) the doc changes. Like I said in the commit message, it is unfortunate that the underscore names were not kept as aliases and that RLock wasn't also converted to a class, but it is too late to fix that in 3.3. If someone wants to do RLock in 3.4 they should open a new issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: