Skip to main content

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

Project description

ntstatus

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.1.tar.gz (37.8 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.1-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ntstatus-1.1.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for ntstatus-1.1.tar.gz
Algorithm Hash digest
SHA256 542069b3d0d654a37b9e068caaf4d29c8e3f9c1a2a4991ed0ec8c7a858022ee2
MD5 7dfd48dfb0643740809e7fcdf50c8102
BLAKE2b-256 b59941f6934b2dc1b02c6366b9b5d5211f3dc8eb08c9f9d8813e58fe4f09f9b4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ntstatus-1.1-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for ntstatus-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 303239b3286fc0529fc8fd2daad6465ab7f5b2e0bc6abfdf785d48fbab1f2a54
MD5 2eefef0ffcbf5fd3aac5a3201188d3cd
BLAKE2b-256 a5ccc9493a2a64dd9cfa8edf554028571f29398cb6fc693e6f4919f762994768

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