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

Conflict with Python's built-in open function, triggering AttributeError: __enter__ #142

Open
zZan54 opened this issue Jul 6, 2024 · 1 comment
Labels
pymem 2.0 pymem 2.0 release

Comments

@zZan54
Copy link

zZan54 commented Jul 6, 2024

Describe the bug
I encountered an issue with the package where the open function defined in your code conflicts with Python's built-in open function. Here is the relevant part of your code:

def open(process_id, debug=True, process_access=None):
    """Open a process given its process_id.
    By default, the process is opened with full access and in debug mode.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms684320%28v=vs.85%29.aspx
    https://msdn.microsoft.com/en-us/library/windows/desktop/aa379588%28v=vs.85%29.aspx

    Parameters
    ----------
    process_id: int
        The identifier of the process to be opened
    debug: bool
        If the process should be opened in debug mode
    process_access: pymem.ressources.structure.PROCESS
        Desired access level, defaulting to all access

    Returns
    -------
    int
        A handle to the opened process
    """
    if not process_access:
        process_access = pymem.ressources.structure.PROCESS.PROCESS_ALL_ACCESS.value
    if debug:
        set_debug_privilege('SeDebugPrivilege', True)
    process_handle = pymem.ressources.kernel32.OpenProcess(process_access, False, process_id)
    return process_handle

When I try to use Python's built-in open function within my code, it triggers an AttributeError: enter because Python uses the open function defined in your package instead of its own. This is causing compatibility issues with file operations.

Your Environment

  1. Python 3.10.9
  2. 64-bit
  3. pymem==1.13.1

Expected behavior

Traceback
Here's an example of the code that will trigger this error:

import pymem
from pymem.process import *

with open('example.txt', 'r') as file:
    content = file.read()

Error:

AttributeError: __enter__

Stackoverflow explanation: https://stackoverflow.com/questions/53564755/python-error-attributeerror-enter

Additional context
This happens because the open function in pymem overrides the built-in open function. Renaming the open function in your package to something more specific (e.g., open_process) could resolve this conflict.

@StarrFox
Copy link
Collaborator

StarrFox commented Jul 6, 2024

you can also avoid this by not doing a * import which is generally a good idea

@StarrFox StarrFox added the pymem 2.0 pymem 2.0 release label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pymem 2.0 pymem 2.0 release
Projects
None yet
Development

No branches or pull requests

2 participants