Skip to content

Change solution for issue#164 #245

@nedbat

Description

@nedbat

Originally reported by Johan Olsen (Bitbucket: johanolsen, GitHub: Unknown)


Hello,

I had the issue #164, and tried the fix for it. On some of my code however, this gave some errors at the end of the coverage run. It turns out that this is to my code defining its own cmp on classes derived from threading.Thread. I use this to be able to sort threads on my own. I use this test code:

#!python

import threading

class MyThread (threading.Thread):
    
    def __init__(self, num):
        self.num = num
        threading.Thread.__init__(self)
    
    def run(self):
        print "Hello %s, %s" % (self.num, self.ident)
    
    def __cmp__(self, other):
        return cmp(self.num, other.num)

thr1 = MyThread(1)
thr2 = MyThread(2)
thr1.start()
thr2.start()
thr2.join()
thr1.join()

and get this printouts / traceback:

coverage run cover_test.py
Hello 1, 7028
 Hello 2, 6292
Traceback (most recent call last):
  File "C:\Python27\Scripts\coverage-script.py", line 8, in <module>  
    load_entry_point('coverage==3.6', 'console_scripts', 'coverage')()  
  File "c:\Python27\lib\site-packages\coverage\cmdline.py", line 710, in main  
    status = CoverageScript().command_line(argv)
  File "c:\Python27\lib\site-packages\coverage\cmdline.py", line 429, in command_line
    self.do_execute(options, args)
  File "c:\Python27\lib\site-packages\coverage\cmdline.py", line 565, in do_execute
    self.coverage.stop()
  File "c:\Python27\lib\site-packages\coverage\control.py", line 374, in stop
    self.collector.stop()
  File "c:\Python27\lib\site-packages\coverage\collector.py", line 287, in stop
    self.pause()
  File "c:\Python27\lib\site-packages\coverage\collector.py", line 299, in pause
    tracer.stop()
  File "c:\Python27\lib\site-packages\coverage\collector.py", line 127, in stop
    if self.thread != threading.currentThread():
  File "cover_test.py", line 13, in __cmp__
    if self.num > other.num:
AttributeError: '_MainThread' object has no attribute 'num'

I suggest to replace
if self.thread != threading.currentThread():
with
if self.thread.ident != threading.currentThread().ident:
in collector.py, which I think will solve this problem.


Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions