Skip to content

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

@zZan54

Description

@zZan54

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions