#1 generation cache does not work for restricted users

closed-fixed
None
5
2008-08-15
2008-07-10
No

If working with restricted user rights and having Python installed under the %Program Files% folder the dynamic generation of comtypes wrapper does not work due to restricted access to the %Programs Folder%. Hence the location should be changed to the user specific directory for each installed python version e.g. "C:\Documents and Settings\username\Application Data\Python25\comtypesgen"

For this the function _find_gen_dir in comtypes\client\__init__.py has to be changed as attached.

Stefan

Discussion

  • Stefan Schukat

    Stefan Schukat - 2008-07-10

    comtypes.client.init.py with patched _find_gen_dir

     
  • Thomas Heller

    Thomas Heller - 2008-07-16

    Logged In: YES
    user_id=11105
    Originator: NO

    I feared this would come up.

    I think would prefer to keep the current scheme (comtypes\gen folder), and only use a folder like the one you suggested when comtypes\gen is unwriteable by the user. This could be established by some __path__ trickery. What do you think?

     
  • Stefan Schukat

    Stefan Schukat - 2008-07-17

    Logged In: YES
    user_id=977439
    Originator: YES

    I see your point, but if in the rare case more than one user is using the system, the check would have to take into account the sequence the files are generated. I.e. initial creation by an admin, the usage by a restricted user. Then the gen folder is already there for an import but additional files could be not be created. Hence the check where to generate would be more difficult. So I thought the same approach for all users would be better.

    Stefan

     
  • Stefan Schukat

    Stefan Schukat - 2008-07-17

    Logged In: YES
    user_id=977439
    Originator: YES

    I see your point, but if in the rare case more than one user is using the system, the check would have to take into account the sequence the files are generated. I.e. initial creation by an admin, the usage by a restricted user. Then the gen folder is already there for an import but additional files could be not be created. Hence the check where to generate would be more difficult. So I thought the same approach for all users would be better.

    Stefan

     
  • Thomas Heller

    Thomas Heller - 2008-08-13

    comtypes.client._code_cache module

     
  • Thomas Heller

    Thomas Heller - 2008-08-13

    Logged In: YES
    user_id=11105
    Originator: NO

    Stefan, I combined your ideas with mine and wrote a new _find_gen_dir() function. It is now in a separate helper module comtypes.client._code_cache (attached).

    The strategy is as follows:

    1. If the comtypes.gen module cannot be imported for whatever reasons, it is created in the file system Lib/site-packages/comtypes/gen. If that does not work, a module in memory only is created.

    2. If the comtypes.gen is in the file system directory, it is tested if a temporary file can be created within it. If this fails, or if comtypes.gen is not a directory then an additional directory is created, and that is appended to the __path__ list of comtypes.gen.

    3. The additional directory is %APPDATA%\&lt;username>\Python\Python25 (for Python 2.5) for a normal Python script, or <tempdir>\&lt;imagename>-25 for a py2exe'd image.

    4. The last part on comtypes.gen.__path__ should now ALWAYS be a writeable directory used as a cache for generated modules.

    What do you think?

    3. The last part of comtypes.gen.__path__ is the directory where

    File Added: _code_cache.py

     
  • Thomas Heller

    Thomas Heller - 2008-08-13

    Logged In: YES
    user_id=11105
    Originator: NO

    Please remove the part after 'What do you think?' from the previous message.

    I forgot to mention that with this change py2exe'd programs will now cache code generated at runtime, although I am not sure that the temp directory is the right place for this.

    It should also be mentioned that __path__ munging at runtime (which this patch does when the additional directory is actually used) brings some problems with py2exe.

     
  • Stefan Schukat

    Stefan Schukat - 2008-08-14

    Logged In: YES
    user_id=977439
    Originator: YES

    Hi Thomas,

    your code goes far beyond what I would have thought of. The strategy is good without breaking backwards compability. But I think having <imagename>-25 directories directly in the temporary path should be changed to some sort of aggregation %temp%\genpy\&lt;imagename>-25 so it is easier to clean up the cache. In addition to that I would use tempfile.gettempdir() instead of GetTempPath(MAX_PATH, path) since you test the existence with tempfile.TemporaryFile(). So you don't have to worry if the tempfile module changes the way it find the temorary directory.

     
  • Thomas Heller

    Thomas Heller - 2008-08-15

    Logged In: YES
    user_id=11105
    Originator: NO

    Stefan, thanks for your helpful comments. I have changed the code according to your latter suggestion regarding tempfile and committed it to SVN.

    Thanks again.

     
  • Thomas Heller

    Thomas Heller - 2008-08-15
    • assigned_to: nobody --> theller
    • status: open --> closed-fixed
     

Log in to post a comment.