-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Fix old index bug in call_with_inout
#473
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
It is a happy situation for OSS maintainers such as me to the community has contributors who are willing to investigate defects and PR proper level of granularity.
I think this problem is getting to the crux of this project.
The _fix_inout_args
may have never been tested by boundary values.
As I mentioned in #346, the non-skipped existing tests succeeded with or without the _fix_inout_args
. I also did not encounter any problems with the projects I am involved with that use comtypes
.
I think it is necessary another PR adding the tests with three cases.
- Test the behavior of methods that can work without the
_fix_inout_args
- Test the behavior of methods that can work with the current
_fix_inout_args
. - Test the behavior of methods that can work with Fix old index bug in
call_with_inout
#473 changes- Until the production code is changed, this test must be decorated by the
unittest.expectedFailure
.
- Until the production code is changed, this test must be decorated by the
Once the test-only PR is merged, you can simply rebase this branch.
This is my opinion and there may be other better ways to do.
Any opinions also would be appreciated.
Thanks a lot! I'd suggest to backport it to current master branch as well as a separate PR. From point of being explicit, we should raise an exception in case |
I added a unit test here which fails on the unmodified |
OK, adding tests and changing the production code in one PR would be fine. However, it is better to use the actual com library for testing. Please show us the code that reproduces the problem you encountered while using I am guessing that However, just as you were able to create the IEnum test from the DirectShow api, members of the community may be able to come up with a more suitable test from your code. |
I have temporarily added a test here to be removed later. It does not create new files on my device, but I can't guarantee that it never does so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My guess is that you will continue to improve this test code (even if in your local env) and post issues about technical problems that occurred there.
Therefore, please keep in mind that this code will be shared with us again in the future, so please make sure that it is more readable.
comtypes/test/test_portabledevice.py
Outdated
WPD_OBJECT_NAME = PropertyKey( | ||
0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WPD_OBJECT_NAME = PropertyKey( | |
0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C, 4) | |
WPD_OBJECT_NAME = PropertyKey(comtypes.GUID("{EF6B490D-5CD8-437A-AFFC-DA8B60EE4A3C}"), 4) |
It should be applied to other PropertyKey
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I did it this way is because it allows easy copy-paste from the Windows header files, eliminating a possible source of errors. I can of course change that.
Oh, a misunderstanding - I was under the impression that you merely wanted to see some example test code and didn't want to integrate this test into production code (especially because I can't guarantee that it doesn't modify content on portable devices). I will update the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree that add a test using portabledevice to the project, under the following conditions ...
- Assume the target device is a sandbox consisting of a few folders and a few very small files.
- Use
os.path
,glob
,pathlib
, etc. to check if the target device has the same directory structure as the expected sandbox or skip the test if not. - Eliminate the using conditional branches such as inside of
findDir
that are not used to skip or fail tests. - Make assertions that inspect contents of the target device.
- If doing something like
RemoteWrite
, create a temporary folder usingtempfile
or something similar and write to it. - If possible, assert after the test that the sandbox is not different from before the test.
Existing this test is no matter, however, be that as it may, I think it is necessary to test _fix_inout_args
in another environment-independent way, since it is difficult to always require such a sandbox in developer and CI environments.
As for |
Would you reconsider your stance on this matter, since it appears to be hard to catch all edge cases of |
Thanks a lot for your contribution! Indeed, I too felt that it would be difficult to cover all edges in the actual COM library.
I want to see! The following is my opinion. Currently you are trying to cover the edges implicitly with Then, you can use a mock to assert how the function was actually called. import functools as ft
from typing import Any, Callable
import unittest as ut
from unittest.mock import MagicMock
def add_a_eqaul_one_arg(func: Callable[..., Any], *args, **kwargs):
@ft.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs, a=1)
return wrapper
class Test(ut.TestCase):
def test(self):
m = MagicMock()
new_m = add_a_eqaul_one_arg(m)
new_m(4, 3, b=2)
m.assert_called_once_with(4, 3, a=1, b=2) I think that such a test is necessary if it cannot be provided actual stuffs. |
I have rewritten and expanded the unit test, feel free to take a look when you have time. Surprisingly, a different test had some global side effects despite it being skipped, breaking my new test. I made some modifications there as well. Regarding the lines comtypes/comtypes/_memberspec.py Lines 186 to 188 in dac3a12
I was unable to find a practical use case where they would be called, and none of the existing tests cover them either. Any advice would be appreciated. If it turns out that they don't appear to be used at all, we could add a comment explaining the situation but leave the code in for backwards compatibility. |
Thank you for further research.
I think certainly because a real COM library like My question is, can you actually instantiate the I know from previous discussions that it is difficult to test everything with real things. However, if it is possible to do even part of it with real things, I think it is better to do it. |
I have added a real-life test as requested. @junkmd , could you clear the "Changes requested" review? |
to supress warnings
Thank you for all your efforts. I added the I also confirmed that CIs are passed. memohttps://ci.appveyor.com/project/cfarrow/comtypes/builds/46670688 Let's wait for @vasily-v-ryabov's review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general the logic looks OK to me. I can only rely on your testing. So please proceed with the fix.
And sorry for such a long waiting. Hope @junkmd can finish work on release 1.3.0
. Please don't wait for my opinion if I don't reply for more than 1 week. Most probably 2024 will be too busy again for me. 😄 But I never say "never" to open source projects! 😄
comtypes/_memberspec.py
Outdated
# why the original authors did not do that. Instead, they replace the inout variables in 'rescode' | ||
# by those in 'outargs', and call __ctypes_from_outparam__() on them. | ||
# | ||
# - What I don't understand is: Why is the behaviour different for outnum==1? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only reason is that singe rescode
is not a tuple (iterable) so it's not possible to convert it to list by list(rescode)
. It can be easily rewritten by creating list with rescode = [rescode]
, but there is no such need as current implementation should be very little faster. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this point.
It is necessary to add comments explaining why outargs[0]
and rescode
indicate the same thing when outnum == 1
and len(outargs) == 1
.
- It would also be useful to comment on the line where the variable declaration is located.
However, regarding the implementation, I believe it's fine as it is, considering the desire to minimize changes and the slightly faster processing speed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be useful to comment on the line where the variable declaration is located.
What exactly would you like to see commented there, that is not redundant with the information in the big comment block? If you have a specific idea, feel free to change it and then merge. Thank you for your efforts!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my brief understanding the comment lines 252 - 260 are not necessary. Though some comment about better speed is also OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be useful to comment on the line where the variable declaration is located.
What exactly would you like to see commented there, that is not redundant with the information in the big comment block? If you have a specific idea, feel free to change it and then merge. Thank you for your efforts!
I tried moving the comment to the variable declaration section, but it became confusing.
I am currently considering comments that would be helpful to future maintainers.
I will add a commit with corrected comments after this.
I appreciate your waiting. |
Quick with your responses as always, I appreciate it! I'll most likely update the branch today or tomorrow. |
I added a commit. I updated the method and class name quoting in some comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for dealing with this topic over a long period of time.
This was a very old bug, as the PR title suggests, as well as production code that had not been boundary value tested before.
Your contribution is very beneficial.
Thank you for your efforts at maintaining this package! You greatly contributed to the end result here. |
* bugfix in `call_with_inout` * minor cleanup * handling the case of no in and no out * Test case for _fix_inout_args * additional cleanup and error handling * code formatting fixed * fix python 3.7 and 3.8 compatibility * Temporary addition of real-world test * code cleanup * intermediate commit, do not review * Refactor of unit test, removing portdevice test * fix global side-effect of other skipped test * Update comtypes/test/test_outparam.py Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> * work on tests for inout_args and outparam - cleanup for test_outparam.py - improvements to test_inout_args.py - comments on a possible error in _memberspec.py * removing dead code * rename variables and add assertions * pass `MagicMock` instead of `ut.TestCase` * make tests for each argument passing patterns * remove duplicated comments * update test code for readability - remove name from mock - move line breaks to between mock preparations and assertions * split the testcases * add `Test_Error` * minor corrections, remove redundancy, migration - rewrite the permutations test - missing direction and omitted name redundant - migrate autogenerated keywords - TBD: more real life tests * Add tests covering 24 patterns - instead of using `if` statements and `permutations` * update test name * add real world tests, remove old code * formatting issue * Update comtypes/_memberspec.py dict type annotation Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> * Change missing 'in' or 'out' to be treated as 'in' * Add real-world test: param without 'in' or 'out' * add `contextlib.redirect_stdout` to supress warnings * apply review feedback * update comments * add line breaks to lines longer than 88 characters * Update comtypes/test/test_inout_args.py --------- Co-authored-by: jonschz <jonschz@users.noreply.github.com> Co-authored-by: Jonathan <jonschz@noreply.github.com> Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> (cherry picked from commit 876801f)
* update `appveyor.yml` (#398) (cherry picked from commit 30f497a) * just remove trailing whitespaces (#410) (cherry picked from commit 7a14804) * adjust comments (#411) (cherry picked from commit 80ec49b) * adjust comments (#412) (cherry picked from commit 817b6b7) * adjust comments (#413) (cherry picked from commit 5b0dc8d) * adjust comments (#414) (cherry picked from commit a6fc08b) * adjust comments (#415) (cherry picked from commit 21b6ab6) * adjust comments (#416) (cherry picked from commit 086ec13) * adjust comments (#417) (cherry picked from commit 4e53792) * adjust comments and brackets (#420) (cherry picked from commit 14ae44d) * unify styles for `__all__` and `__known_symbols__` (#421) (cherry picked from commit 1398608) * Improve error message on non Win (#423) * Improve COM error message * Drop Py2 stuff * Remove dot * Remove empty line (cherry picked from commit 18cafb0) * adjust styles of `client/__init__.py` (#424) (cherry picked from commit 12d22a2) * add auto-formatter GHA triggered by PR (#427) * add `# fmt: ...` for avoiding redundant format * add GHA setting * apply automatic formatter Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> (cherry picked from commit 22bfc50) * fix GHA settings (#431) (cherry picked from commit 0664400) * fix passing keyword arguments in `client.dynamic` (#432) (cherry picked from commit 32d6621) * remove `__cmp__` methods in `client.dynamic` (#434) (cherry picked from commit b6a4181) * remove `sys.version_info` bridges (#436) (cherry picked from commit 0cadf77) * remove `hints.AnnoField` (#437) (cherry picked from commit 39d9e00) * adjust import part and references (#439) (cherry picked from commit 0fecfea) * add tests for `client.dynamic` (#440) (cherry picked from commit 7d69dce) * fix `client.dynamic._Dispatch.QueryInterface` (#442) (cherry picked from commit 6b551a4) * add type annotations other than `Dispatch` func (#444) (cherry picked from commit 11410ce) * fix importing `typing` under `if TYPE_CHECKING:` (#448) (cherry picked from commit ba1b18f) * remove `sys.version_info` bridges (#449) (cherry picked from commit 9238cc7) * fix `IPersist.GetClassID` to `TYPE_CHECKING` only method from `Callable` annotation (#451) (cherry picked from commit 446f52e) * remove skip marks (#452) (cherry picked from commit 6732c26) * fix type hints to inline annotations (#453) (cherry picked from commit 7321eaf) * change type hints from comment annotations to inline annotations in `comtypes/__init__.py`, `comtypes/automation.py` and `comtypes/typeinfo.py` (#454) * update `comtypes/__init__.py` * update `comtypes/automation.py` * update `comtypes/typeinfo.py` * fix `Array[T]` to quoted literal strings (cherry picked from commit 7b38a7a) * change type hints from comment annotations to inline annotations in `_memberspec` (#455) (cherry picked from commit 5d230aa) * change type hints from comment annotations to inline annotations in `comtypes/client/...` (#456) * change type hints from comment annotations to inline annotations in `comtypes/client/...` * fix type annotations (cherry picked from commit 18dc2cc) * Update `codegenerator` type annotations (#457) * update `codegenerator` type annotations * lcid (cherry picked from commit 9e7b900) * Update `tlbparser` type annotations (#459) (cherry picked from commit 0b4dbca) * Update type annotations in `typedesc` and `typedesc_base` (#461) * update `typedesc` type annotations * update `typedesc_base` type annotations (cherry picked from commit 95962e9) * remove raising `unittest.SkipTest` from `test_getactiveobj` (#462) (cherry picked from commit 00f2317) * add `black` style checking to GHA settings (#465) (cherry picked from commit 04037ae) * modernize `test_client` (#466) * change from udf `silence_stdout` to stdlib `contextlib.contextmanager` * remove `sys.version_info` bridges * remove excessive blank lines (cherry picked from commit d470b62) * fix `__next__()` in IEnum code generator (#467) (cherry picked from commit a76f0b1) * correcting type hint for CreateObject (#470) Co-authored-by: jonschz <jonschz@users.noreply.github.com> (cherry picked from commit 1615af5) * Add unit test for generated IEnum (#468) (#471) * unit test for generated IEnum (#468) * change enum_test to a non-empty IEnum * improved checks and clarity * apply requested changes Co-authored-by: jonschz <jonschz@users.noreply.github.com> (cherry picked from commit e1ee6f0) * make explicit the symbols that imports from the wrapper module into the friendly module (#469) * add `Library` to generated modules' `__all__`. because that symbol is public but not included. * add `typelib_path` to generated modules' `__all__`. because that symbol is public but not included. * make `ModuleGenerator` class that encapsulates `CodeGenerator` instance. * rename to `generate_wrapper_code` from `generate_code` * add `generate_friendly_code` * add type annotations to `generate_wrapper_code` * add docstring * add `get_symbols` methods to `DeclaredNamespaces` and `ImportedNamespaces` * update imporing symbols * add type annotation to return value for `__init__` * change to using f-string in `generate_friendly_code` (cherry picked from commit 532c399) * remove `'Programming Language :: Python :: 2.7'` (#483) from `setup.py` (cherry picked from commit 9724dc0) * change `_MemberSpec`s to `NamedTuple`s (#484) * change `_MemberSpec` to `NamedTuple` * `_ComMemberSpec` is no more `Generic` for Py<3.11 backward compatibility. * apply `black` style (cherry picked from commit 94b81af) * split `DISPPARAMS` instantiation in `IDispatch.Invoke` method with `__make_dp` method (#485) (cherry picked from commit 62ce303) * 477 Move clear_comtypes_cache to be a callable module (#478) * move clear_comtypes_cache to be a callable module This commit modifies the clear_comtypes_cache.py script so that it is inside the main comtypes module (renamed as just clear_cache) so that is can be called more easily as "py -m comtypes.clear_cache". The main function of the script is also exported using the "console_scripts" entry point so that the script also goes into the standard Python "Scripts" folder as before, but now as a .exe instead of a .py script, which makes it easier to run if systems are set to open .py files instead of running them. This version also includes a test case using the 3rd party package pyfakefs. Currently, this is not desired to avoid the requirement of 3rd party packages in comtypes, but is included here for potential use if the position changes. A subsequent commit will modify the tests to use unittest.patch instead, which is an inferior technical solution but avoids a 3rd party package. * modify clear_cache tests to not use pyfakefs This commit updates the test for comtypes.clear_cache to not use any 3rd party packages, instead relying on mocking the shutil.rmtree function which is used to do the actual cache deletion. * change quotes in print string * style changes based on review by @junkmd * Apply suggestions from code review Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> --------- Co-authored-by: Ben Rowland <ben.rowland@hallmarq.net> Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> (cherry picked from commit 8954f61) * delint long lines (#487) (cherry picked from commit 6532d32) * remove `from ctypes import *` (#488) (cherry picked from commit 65e3c10) * remove dead optional `for_stub` arg (#489) from `ImportedNamespaces.getvalue` (cherry picked from commit da27aaf) * importing the wrapper-module into the friendly-module using an abstracted name, `__wrapper_module__` (#493) * add test * add the `__wrapper_module__` to friendly modules By doing so, it is possible to achieve a combination of friendly-modules functionalities and improved access to the wrapper-modules. (cherry picked from commit b7d5f1f) * Fix old index bug in `call_with_inout` (#473) * bugfix in `call_with_inout` * minor cleanup * handling the case of no in and no out * Test case for _fix_inout_args * additional cleanup and error handling * code formatting fixed * fix python 3.7 and 3.8 compatibility * Temporary addition of real-world test * code cleanup * intermediate commit, do not review * Refactor of unit test, removing portdevice test * fix global side-effect of other skipped test * Update comtypes/test/test_outparam.py Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> * work on tests for inout_args and outparam - cleanup for test_outparam.py - improvements to test_inout_args.py - comments on a possible error in _memberspec.py * removing dead code * rename variables and add assertions * pass `MagicMock` instead of `ut.TestCase` * make tests for each argument passing patterns * remove duplicated comments * update test code for readability - remove name from mock - move line breaks to between mock preparations and assertions * split the testcases * add `Test_Error` * minor corrections, remove redundancy, migration - rewrite the permutations test - missing direction and omitted name redundant - migrate autogenerated keywords - TBD: more real life tests * Add tests covering 24 patterns - instead of using `if` statements and `permutations` * update test name * add real world tests, remove old code * formatting issue * Update comtypes/_memberspec.py dict type annotation Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> * Change missing 'in' or 'out' to be treated as 'in' * Add real-world test: param without 'in' or 'out' * add `contextlib.redirect_stdout` to supress warnings * apply review feedback * update comments * add line breaks to lines longer than 88 characters * Update comtypes/test/test_inout_args.py --------- Co-authored-by: jonschz <jonschz@users.noreply.github.com> Co-authored-by: Jonathan <jonschz@noreply.github.com> Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com> (cherry picked from commit 876801f) * Fix rendering emojis in `CONTRIBUTING.md` and remove "Breaking troubles down" from "Table of contents" (#505) (cherry picked from commit af78614) --------- Co-authored-by: Cristi Fati <fati_utcluj@yahoo.com> Co-authored-by: jonschz <17198703+jonschz@users.noreply.github.com> Co-authored-by: Ben Rowland <bennyrowland@mac.com>
Another day, another pull request...
I found this rather ancient bug in
call_with_inout
. It triggers whenever a method takes an['out']
parameter followed by an['in', 'out']
parameter, which seems to be rather uncommon. My specific problem was with IPortableDeviceContent::CreateObjectWithPropertiesAndData.Before this bugfix it was impossible to call this method without getting an error. Providing four arguments would result in
call takes exactly 4 arguments (5 given)
, and providing the correct three arguments would result in'LP_c_wchar_p' object cannot be interpreted as an integer
, since the old code would mismatch the expected and actual arguments.I am fairly certain this change will not break any correct existing code, but there is a chance it may break some code containing a workaround for this bug.
Fun fact:
git blame
shows that this code (including the bug) was pushed 14 years ago in 8776b1b and never modified.I also cleaned up the code a little bit which does not affect the logic at all, but increases readability.
One open question
We should think about how to handle the case when a parameter has neither
'out'
nor'in'
is specified. As far as I am aware, this does not happen in the code generator, but it if did, it would likely cause this code to malfunction. One idea would be to raise an exception, the other idea would be to log a warning and then explicitly set the parameter to be'in'
. I think the old code does the latter implicitly.One could also consider merging this bugfix into
master
since it is a bugfix for existing python2 code.