Errorsο
PyMemoryEditor defines a small exception hierarchy. All custom exceptions
inherit from PyMemoryEditorError, so you can catch the whole library:
from PyMemoryEditor import PyMemoryEditorError
try:
with OpenProcess(name="game.exe") as process:
...
except PyMemoryEditorError as exc:
print("Library error:", exc)
The hierarchyο
Exception
βββ PyMemoryEditorError
βββ BitnessDetectionError
βββ ClosedProcess
βββ ProcessIDNotExistsError
βββ ProcessNotFoundError
βββ AmbiguousProcessNameError
The library also raises standard built-in exceptions when they are the
more idiomatic match (PermissionError, OSError, TypeError, ValueError,
NotImplementedError).
Custom exceptionsο
PyMemoryEditorErrorο
- exception PyMemoryEditorErrorο
Base class for every PyMemoryEditor-specific exception. Catch it to handle any library error.
ClosedProcessο
- exception ClosedProcessο
Raised when you call any method on a process whose handle has already been
closed (manually or by leaving the with block).
with OpenProcess(pid=1234) as process:
pass
process.read_process_memory(0x1000, int) # raises ClosedProcess
ProcessIDNotExistsErrorο
Raised when the given pid= doesnβt correspond to a running process.
ProcessNotFoundErrorο
Raised when no running process matches the given name=.
AmbiguousProcessNameErrorο
Raised when more than one running process matches the given
name= (typical when using exact_match=False).
- exception AmbiguousProcessNameErrorο
Example:
from PyMemoryEditor import OpenProcess, AmbiguousProcessNameError
try:
OpenProcess(name="chrome", exact_match=False)
except AmbiguousProcessNameError as exc:
print("Multiple matches:", exc.pids)
process = OpenProcess(pid=exc.pids[0])
BitnessDetectionErrorο
Raised when strict_bitness=True and the targetβs 32-/64-bit width could
not be read from its own headers (the ELF class on Linux, the Mach-O magic
on macOS, IsWow64Process on Windows).
Without strict mode the library falls back to the host word size β a guess that may silently produce wrong pointer-width defaults.
Example:
from PyMemoryEditor import OpenProcess, BitnessDetectionError
try:
with OpenProcess(pid=1234, strict_bitness=True) as process:
print(process.is_64bit)
except BitnessDetectionError as exc:
print(f"Could not detect bitness of PID {exc.pid}")
Standard exceptionsο
| Exception | When it's raised |
|---|---|
TypeError | Neither name nor pid provided to OpenProcess; or a scan pattern that is not str, bytes or a compiled re.Pattern. |
ValueError | Invalid pytype, missing bufflength for str/bytes, invalid ptr_size, malformed pattern, byte_length omitted for a regex pattern, etc. |
PermissionError | OS denied access to the target process or a specific region. |
OSError | Low-level read/write failure (e.g. page was freed between scan and read). |
NotImplementedError | allocate_memory / free_memory on Linux. |
UserWarning | permission= passed on a non-Windows platform (ignored, with this warning emitted). |
ResourceWarning | macOS: mach_vm_protect failed to restore a page's original protection after a write. |
Catching robustlyο
A pattern that handles every realistic failure mode:
from PyMemoryEditor import (
OpenProcess,
PyMemoryEditorError,
ProcessNotFoundError,
AmbiguousProcessNameError,
)
try:
with OpenProcess(name="game.exe") as process:
for address in process.search_by_value(int, value=100):
try:
value = process.read_process_memory(address, int)
except OSError:
continue # the page disappeared mid-scan β keep going
print(hex(address), value)
except ProcessNotFoundError:
print("The game isn't running.")
except AmbiguousProcessNameError as exc:
print("Pick one PID:", exc.pids)
except PermissionError:
print("OS denied access β see Platform Notes.")
except PyMemoryEditorError as exc:
print("Library error:", exc)
See also