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

get_excel_hwnds throws an error in case no Excel application is running #269

Closed
sdementen opened this Issue Oct 20, 2015 · 12 comments

Comments

Projects
None yet
2 participants
@sdementen
Contributor

sdementen commented Oct 20, 2015

While running nosetests, I get errors if no Excel is open when starting the tests.

Some debugging led me to an issue in the get_excel_hwnds function. The function win32gui.FindWindowEx is used in a if construct but in fact the function throws a pywintypes.error when it fails. I think the function should instead be

def get_excel_hwnds():
    hwnds = []
    win32gui.EnumWindows(lambda hwnd, result_list: result_list.append(hwnd), hwnds)

    excel_hwnds = []
    for hwnd in hwnds:
        try:
            win32gui.FindWindowEx(hwnd, 0, 'XLDESK', None)
        except pywintypes.error:
            pass
        else:
            excel_hwnds.append(hwnd)

    return excel_hwnds
@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

I can't duplicate that. Can you check in the Task Manager if you have some Zombie Excel processes running when you get this?

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

Also, what's the exact error message you're seeing?

@fzumstein fzumstein added the bug label Oct 20, 2015

@sdementen

This comment has been minimized.

Contributor

sdementen commented Oct 20, 2015

When I have an excel process (normal but also just zombie), I do not have
the error. It is once I have no more excel process (nor normal nor zombie)
that I get the error

On Tuesday, October 20, 2015, Felix Zumstein notifications@github.com
wrote:

I can't duplicate that. Can you check if you check in the Task Manager if
have some Zombie Excel processes running when you get this?


Reply to this email directly or view it on GitHub
#269 (comment)
.

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

What versions of Excel/Python/Windows are you using?

@sdementen

This comment has been minimized.

Contributor

sdementen commented Oct 20, 2015

Excel 2007/2.7/xp

On Tuesday, October 20, 2015, Felix Zumstein notifications@github.com
wrote:

What versions of Excel/Python/Windows are you using?


Reply to this email directly or view it on GitHub
#269 (comment)
.

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

ooh xp is a tough one ,-)

@sdementen

This comment has been minimized.

Contributor

sdementen commented Oct 20, 2015

Error code = 2 "The system cannot find the file specified."

On Tuesday, October 20, 2015, Felix Zumstein notifications@github.com
wrote:

Also, what's the exact error message you're seeing?


Reply to this email directly or view it on GitHub
#269 (comment)
.

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

When you try to reproduce it manually, does it happen when you run Workbook() or only when you run Workbook(r'some\path\file.xlsx')?

@sdementen

This comment has been minimized.

Contributor

sdementen commented Oct 20, 2015

I did not try anything else than running nosetests...
in the tests it tries to open the test_workbook_1.xlsx file in the test
folder and it fails when it goes through the workbook init
-> is_file_open -> get_xl_apps -> get_excel_hwns -> error

On Tuesday, October 20, 2015, Felix Zumstein notifications@github.com
wrote:

When you try to reproduce it manually, does it happen when you run
Workbook() or only when you run Workbook(r'some\path\file.xlsx')?


Reply to this email directly or view it on GitHub
#269 (comment)
.

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 20, 2015

sure but if the tests are responsible for making it fail then I'd rather fix the tests than the library itself.

@sdementen

This comment has been minimized.

Contributor

sdementen commented Oct 21, 2015

except if the test uses xlwings in a non "approved/orthodox" way, I would rather think the test has found a bug that need to be fixed ;-)

The traceback is the following

======================================================================
ERROR: xlwings.tests.test_xlwings.TestWorkbook.test_workbook_name
----------------------------------------------------------------------
Traceback (most recent call last):
  File "...\lib\site-packages\nose\case.py", line 381, in setUp
    try_run(self.inst, ('setup', 'setUp'))
  File "...\lib\site-packages\nose\util.py", line 471, in try_run
    return func()
  File "...\xlwings\tests\test_xlwings.py", line 152, in setUp
    self.wb = Workbook(xl_file1, app_visible=False, app_target=APP_TARGET)
  File "...\xlwings\main.py", line 153, in __init__
    if not os.path.isfile(fullname) or xlplatform.is_file_open(self.fullname):
  File "...\xlwings\_xlwindows.py", line 118, in is_file_open
    for xl_app in get_xl_apps():
  File "...\xlwings\_xlwindows.py", line 98, in get_xl_apps
    hwnds = get_excel_hwnds()
  File "...\xlwings\_xlwindows.py", line 90, in get_excel_hwnds
    if  win32gui.FindWindowEx(hwnd, 0, 'XLDESK', None):
error: (2, 'FindWindowEx', 'The system cannot find the file specified.')

And I have printed the hwnd and the GetWindowText(hwnd)

-------------------- >> begin captured stdout << ---------------------
65992 TF_FloatingLangBar_WndTitle

--------------------- >> end captured stdout << ----------------------

When running just the script

import win32gui
win32gui.FindWindowEx(65992, 0, 'XLDESK', None)

I get the same error

Traceback (most recent call last):
  File ".../xlwings/tests/test.py", line 3, in <module>
    win32gui.FindWindowEx(65992, 0, 'XLDESK', None)
pywintypes.error: (2, 'FindWindowEx', 'The system cannot find the file specified.')

So I wonder if the proper way to use FindWindowEx is both to check the output and to catch exceptions ... but I must say I can't find a lot of doc on this on google.

@fzumstein

This comment has been minimized.

Member

fzumstein commented Oct 25, 2015

fixed by d2d3f95

@fzumstein fzumstein closed this Oct 25, 2015

@fzumstein fzumstein added this to the v0.4.2 milestone Oct 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment