Skip to content
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

Partial wrapper files generated in gen folder #114

Open
vijairaj opened this issue Feb 28, 2017 · 5 comments
Open

Partial wrapper files generated in gen folder #114

vijairaj opened this issue Feb 28, 2017 · 5 comments
Labels
bug Something isn't working

Comments

@vijairaj
Copy link
Contributor

With Comtypes 1.1.2 and Python 2.7.10

I have noticed that some times the generated wrappers have only the first two lines as below:

# -*- coding: mbcs -*-
typelib_path = u'C:\\XXXXXXXXXXXXXXXXXXXXX.tlb'

This manifests as issues such as:
AttributeError: 'module' object has no attribute 'XXXXX'

I had to delete the generated file and regenerate it to fix the issue whenever I see them. However I wasn't able to isolate the root cause and neither is the issue very frequent. After going through the github issues and searching over the internet, I don't see many complaining about such an issue. Only two instance that were discussed elsewhere seem relevant:

so I guess this is not specific to my setup but a generic issue.

@vijairaj
Copy link
Contributor Author

vijairaj commented Mar 6, 2017

After some more analysis of the information collected during this issue, I believe that the issue could have been due to the user closing the application while the cache files are being generated. Looking at the code generator, I see that it creates a cache file and writes the first two lines before it proceeds with resolving the dependencies. The rest of the code is buffered in to StringIO before making it to the file and if there's an interruption in between, it would not make it to the file and result in an incomplete file. I guess we can make this a bit more robust by:

  1. Not creating the file upfront and instead Buffering the header to StringIO as well. Then, after all the dependencies are resolved and the code is generated in the buffer, create the file and dump all the buffers to the file thereby reducing the chances of incomplete file.
  2. Add a file valid check to the end of the file (a variable like _comtypes_cache_valid = True) to the end of the file which the GetModule logic can validate to detect if the file is valid, In case of invalid files the file can be automatically regenerated.

vijairaj added a commit to vijairaj/comtypes that referenced this issue Mar 13, 2017
vijairaj added a commit to vijairaj/comtypes that referenced this issue Mar 13, 2017
@vijairaj vijairaj mentioned this issue Mar 13, 2017
junkmd added a commit to junkmd/comtypes that referenced this issue Jun 11, 2022
junkmd added a commit to junkmd/comtypes that referenced this issue Jun 15, 2022
vasily-v-ryabov pushed a commit that referenced this issue Jun 18, 2022
* remove dependency with test_support

* divide testcase into Test_GetModule and Test_CreateObject

* add test methods

* update import order

* update import

* to pep8-compliant line breaks

* update docstring

* align non-public function case like any other

* make _invalidate_import_caches function

* remove ugly KeyError catching

* split creating friendly module from GetModule function

* split loading ITypeLib from GetModule function

* Rename variables as they really are

* fix the timing of when module files are written, see #114

* update to use context manager

* update to use print function

* add dirname to _resolve_filename args

* update assertion
@junkmd
Copy link
Collaborator

junkmd commented Jun 19, 2022

I PRed #300 to fix this problem.

junkmd pushed a commit to junkmd/comtypes that referenced this issue Jul 30, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Jul 30, 2022
junkmd added a commit to junkmd/comtypes that referenced this issue Jul 30, 2022
…s in tools.codegenerator and client._generate. (related to enthought#114 and enthought#116)
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
junkmd pushed a commit to junkmd/comtypes that referenced this issue Aug 1, 2022
@junkmd
Copy link
Collaborator

junkmd commented Dec 9, 2022

Although #116 is closed without merging, the "Partial wrapper files generated in gen folder" can still occur.
This issue still needs to be considered about proper implementation and testing.

@vasily-v-ryabov vasily-v-ryabov added the bug Something isn't working label Dec 12, 2022
@vasily-v-ryabov
Copy link
Collaborator

Maybe wrapper generation should become a transaction to guarantee full file generation or fail.

@vijairaj
Copy link
Contributor Author

Maybe wrapper generation should become a transaction to guarantee full file generation or fail.

I agree. The problem with the original implementation was that the first two lines of the generated code was immediately written to the file but the remaining lines were streamed to StringIO and later written to file. The partial file problem could occur when the execution is interrupted b/w these two steps.

In my original PRs - I employed two strategies to solve this problem

  1. Reduce the chance of partial file generation by first using StringIO to cache all the generated content and use a single call to transfer the content to a file
  2. Auto regenerate files during import if they are incomplete - this was done by using a marker statement to the generated file and checking for the valid marker during import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants