Skip to main content

Python enum for NTSTATUS from WinAPI, supporting comparision to both signed and unsigned integers

Project description

py-ntstatus

py-ntstatus is a library that defines a Python enum to represent WinAPI NTSTATUS values.

ntstatus.NtStatus

NtStatus is enumeration representing WinAPI NTSTATUS values.

NtStatus values can be equality-compared to integers; the comparision will return True for both the signed and unsigned interpretation of the underlying 32 bits.

Properties:

  • signed_value: The numeric value of the NtStatus, interpreted as a 32-bit signed integer.
  • unsigned_value: The numeric value of the NtStatus, interpreted as a 32-bit unsigned integer.
  • exit_code: The numeric value of the NtStatus, interpreted in a way that it can be passed to sys.exit() without information loss. (Alias to signed_value.)

Static methods:

  • make(code): Factory method for creating an NtStatus from an integer. Raises ValueError if argument is not a known NtStatus value.
  • decode(code): Factory method for creating an NtStatus from an integer. Returns None if argument is not a known NtStatus value.
  • decode_name(code, default=''): Returns the name of the NtStatus value associated with the first argument if there is such a value. Otherwise, returns the second argument.

Usage examples

Import the enum:

>>> from ntstatus import NtStatus

The 32-bit NTSTATUS value can be interpreted both as signed and unsigned integer:

>>> NtStatus.STATUS_DLL_NOT_FOUND
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.STATUS_DLL_NOT_FOUND.unsigned_value
3221225781

>>> NtStatus.STATUS_DLL_NOT_FOUND.signed_value
-1073741515

NtStatus can be compared to both the signed and unsigned interpretation of the underlying 32 bits:

>>> NtStatus.STATUS_DLL_NOT_FOUND == 0xC0000135
True

>>> NtStatus.STATUS_DLL_NOT_FOUND == 3221225781
True

>>> NtStatus.STATUS_DLL_NOT_FOUND == -1073741515
True

NtStatus can be created from both the signed and unsigned interpretation of the underlying 32 bits:

>>> NtStatus.decode(0xC0000135)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.decode(3221225781)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.decode(-1073741515)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.make(0xC0000135)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.make(3221225781)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

>>> NtStatus.make(-1073741515)
<NtStatus.STATUS_DLL_NOT_FOUND: 0xC0000135>

NtStatus.make() raises ValueError if the value is not representable in 32 bits, or if it is not a known NtStatus:

>>> NtStatus.make(0xCC0300000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:\projects\ntstatus\ntstatus\__init__.py", line 172, in make
    return NtStatus(ThirtyTwoBits(code))
  File "d:\projects\ntstatus\ntstatus\__init__.py", line 26, in __init__
    raise ValueError(f'Value must be in [{min_signed}, {max_unsigned}] to be representable in 32 bits: {value}')
ValueError: Value must be in [-2147483648, 4294967295] to be representable in 32 bits: 54763978752

>>> NtStatus.make(0xC0300000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:\projects\ntstatus\ntstatus\__init__.py", line 172, in make
    return NtStatus(ThirtyTwoBits(code))
  File "C:\Python39\lib\enum.py", line 360, in __call__
    return cls.__new__(cls, value)
  File "C:\Python39\lib\enum.py", line 677, in __new__
    raise ve_exc
ValueError: ThirtyTwoBits(underlying_unsigned_value=3224371200) is not a valid NtStatus

NtStatus.decode() returns None in both cases:

>>> NtStatus.decode(0xC0300000) == None
True

>>> NtStatus.decode(0xCC0300000) == None
True

Name for numeric values can be safely queried:

>>> NtStatus.decode_name(-1073741515)
'STATUS_DLL_NOT_FOUND'

>>> NtStatus.decode_name(0xC0000135)
'STATUS_DLL_NOT_FOUND'

>>> NtStatus.decode_name(0xC0300000)
''

>>> NtStatus.decode_name(-1073741515, 'No NTSTATUS definition found')
'STATUS_DLL_NOT_FOUND'

>>> NtStatus.decode_name(0xC0000135, 'No NTSTATUS definition found')
'STATUS_DLL_NOT_FOUND'

>>> NtStatus.decode_name(0xC0300000, 'No NTSTATUS definition found')
'No NTSTATUS definition found'

exit_code can be used in sys.exit() without information loss:

>>> import sys
>>> sys.exit(NtStatus.STATUS_DLL_NOT_FOUND.exit_code)

(.venv) d:\projects\ntstatus>echo %ERRORLEVEL%
-1073741515

Using the unsigned interpretation would lead to information loss (see explanation):

>>> import sys
>>> sys.exit(NtStatus.STATUS_DLL_NOT_FOUND.unsigned_value)

(.venv) d:\projects\ntstatus>echo %ERRORLEVEL%
-1

List of values

The complete list of NTSTATUS values can be found here.

NtStatus encodes most of them, with the exception of:

  • STATUS_WAIT_n, where n is 0, 1, 2, 3, and 63
  • STATUS_ABANDONED_WAIT_n, where n is 0 and 63
  • STATUS_FLT_DISALLOW_FSFILTER_IO

Licensing

This library is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ntstatus-1.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ntstatus-1.0-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file ntstatus-1.0.tar.gz.

File metadata

  • Download URL: ntstatus-1.0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.2

File hashes

Hashes for ntstatus-1.0.tar.gz
Algorithm Hash digest
SHA256 02213e4f0a94a7db2576f564a9db6c568424d45df91ff905ba72991be1090069
MD5 b5472cc2e914b76a0b70df55dc32611a
BLAKE2b-256 aedacb1da91d387a7fd6a41cd7d93e43e37dace1893a0b95f903957d14bcfab1

See more details on using hashes here.

File details

Details for the file ntstatus-1.0-py3-none-any.whl.

File metadata

  • Download URL: ntstatus-1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.2

File hashes

Hashes for ntstatus-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf64988a31880379061d6e9a7c85b938b8c8e50d392852017a4d68d1923bc72a
MD5 ccc27c4906b5a232b0c76458bca56aa0
BLAKE2b-256 c1d7a7c7059d08115fa0b1a10640e3cf976b571af8a40893d8c19487ac66203b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page