Pkg.PYC
— API for parsing Python compiled bytecode files¶
Disassembling PYC Bytecode File¶
The following code example demonstrates how to disassemble a Python bytecode file:
from Pro.Core import *
from Pkg.PYC import *
def disassemblePYC(fname):
c = createContainerFromFile(fname)
c.isNull()
if c.isNull():
return
obj = PYCObject()
if not obj.Load(c) or not obj.Parse():
return
out = NTTextBuffer()
obj.Disassemble(out)
print(out.buffer)
Module API¶
Pkg.PYC module API.
Attributes:
Reference flag for object types.
ASCII string object type.
Interned ASCII string object type.
Binary complex object type.
Binary float object type.
Code object type.
Code object type.
Complex object type.
Dictionary object type.
Ellipsis object type.
False object type.
Float object type.
FrozenSet object type.
Integer object type.
64-bit integer object type.
Interned object type.
List object type.
Long object type.
None object type.
Null object type.
Type reference object type.
Set object type.
Short ASCII string object type.
Interned short ASCII string object type.
Small tuple object type.
StopIter object type.
String object type.
String reference object type.
True object type.
Tuple object type.
Unicode string object type.
Unknown object type.
Classes:
This class represents a Python compiled bytecode file.
This class represents a Python Binary Complex object (
OBJECT_TYPE_BINARY_COMPLEX
).
PyCFloat
()This class represents a Python Binary Float object (
OBJECT_TYPE_BINARY_FLOAT
).
PyCode
()This class represents a Python Code object (e.g.,
OBJECT_TYPE_CODE
).This class represents a Python Complex object (
OBJECT_TYPE_COMPLEX
).
PyDict
()This class represents a Python Dictionary object (
OBJECT_TYPE_DICT
).This class represents a Python Ellipsis object (
OBJECT_TYPE_ELLIPSIS
).
PyFalse
()This class represents a Python False object (
OBJECT_TYPE_FALSE
).
PyFloat
()This class represents a Python Float object (
OBJECT_TYPE_FLOAT
).
PyInt
()This class represents a Python Integer object (e.g.,
OBJECT_TYPE_INT
).
PyList
()This class represents a Python List object (
OBJECT_TYPE_LIST
).
PyLong
()This class represents a Python Long object (e.g.,
OBJECT_TYPE_LONG
).
PyNone
()This class represents a Python None object (
OBJECT_TYPE_NONE
).
PyNull
()This class represents a Python Null object (
OBJECT_TYPE_NULL
).
PyObject
()This class represents the base class for every Python object.
PySet
()This class represents a Python Set object (
OBJECT_TYPE_SET
).This class represents a Python StopIter object (
OBJECT_TYPE_STOPITER
).
PyString
()This class represents a Python String object (e.g.,
OBJECT_TYPE_STRING
).
PyTrue
()This class represents a Python True object (
OBJECT_TYPE_TRUE
).
PyTuple
()This class represents a Python Tuple object (e.g.,
OBJECT_TYPE_TUPLE
).
- OBJECT_FLAG_REF¶
Reference flag for object types.
- OBJECT_TYPE_ASCII¶
ASCII string object type.
- OBJECT_TYPE_ASCII_INTERNED¶
Interned ASCII string object type.
- OBJECT_TYPE_BINARY_COMPLEX¶
Binary complex object type.
- OBJECT_TYPE_BINARY_FLOAT¶
Binary float object type.
- OBJECT_TYPE_CODE¶
Code object type.
- OBJECT_TYPE_CODE_10_12¶
Code object type.
- OBJECT_TYPE_COMPLEX¶
Complex object type.
- OBJECT_TYPE_DICT¶
Dictionary object type.
- OBJECT_TYPE_ELLIPSIS¶
Ellipsis object type.
- OBJECT_TYPE_FALSE¶
False object type.
- OBJECT_TYPE_FLOAT¶
Float object type.
- OBJECT_TYPE_FROZENSET¶
FrozenSet object type.
- OBJECT_TYPE_INT¶
Integer object type.
- OBJECT_TYPE_INT64¶
64-bit integer object type.
- OBJECT_TYPE_INTERNED¶
Interned object type.
- OBJECT_TYPE_LIST¶
List object type.
- OBJECT_TYPE_LONG¶
Long object type.
- OBJECT_TYPE_NONE¶
None object type.
- OBJECT_TYPE_NULL¶
Null object type.
- OBJECT_TYPE_REF¶
Type reference object type.
- OBJECT_TYPE_SET¶
Set object type.
- OBJECT_TYPE_SHORT_ASCII¶
Short ASCII string object type.
- OBJECT_TYPE_SHORT_ASCII_INTERNED¶
Interned short ASCII string object type.
- OBJECT_TYPE_SMALL_TUPLE¶
Small tuple object type.
- OBJECT_TYPE_STOPITER¶
StopIter object type.
- OBJECT_TYPE_STRING¶
String object type.
- OBJECT_TYPE_STRINGREF¶
String reference object type.
- OBJECT_TYPE_TRUE¶
True object type.
- OBJECT_TYPE_TUPLE¶
Tuple object type.
- OBJECT_TYPE_UNICODE¶
Unicode string object type.
- OBJECT_TYPE_UNKNOWN¶
Unknown object type.
- class PYCObject¶
Bases:
Pro.Core.CFFObject
This class represents a Python compiled bytecode file.
Methods:
CreateModuleHeaderFromMagic
(magic)Computes the module header by specifying the Python’s magic signature.
CreateModuleHeaderFromVersion
(major_version, …)Computes the module header by specifying the major and minor version of Python.
Disassemble
(out)Disassembles the entire file.
DisassembleCode
(out, code_obj)Disassembles a specific code object.
Returns the computed end offset of the PYC file.
Returns the module file name if available; otherwise returns
None
.Returns the object tree as a map.
Returns the root object of the tree.
Returns the Python major and minor version of the PYC file as a tuple.
Parse
([wo, verbose])Parses the format.
- static CreateModuleHeaderFromMagic(magic: int)¶
Computes the module header by specifying the Python’s magic signature.
- Parameters
magic (int) – The magic signature.
- Returns
Returns the computed header if successful; otherwise returns
None
.- Return type
Optional[bytes]
See also
CreateModuleHeaderFromVersion()
.
- static CreateModuleHeaderFromVersion(major_version: int, minor_version: int) → Optional[bytes]¶
Computes the module header by specifying the major and minor version of Python.
- Parameters
major_version (int) – The major version of Python.
minor_version (int) – The minor version of Python.
- Returns
Returns the computed header if successful; otherwise returns
None
.- Return type
Optional[bytes]
See also
CreateModuleHeaderFromMagic()
.
- Disassemble(out: Pro.Core.NTTextStream) → None¶
Disassembles the entire file.
- Parameters
out (NTTextStream) – The output text stream.
See also
DisassembleCode()
.
- DisassembleCode(out: Pro.Core.NTTextStream, code_obj: Pkg.PYC.PyCode) → None¶
Disassembles a specific code object.
- Parameters
out (NTTextStream) – The output text stream.
code_obj (PyCode) – The code object.
See also
Disassemble()
.
- GetEndOffset() → int¶
- Returns
Returns the computed end offset of the PYC file.
- Return type
int
- GetModuleFileName() → Optional[str]¶
- Returns
Returns the module file name if available; otherwise returns
None
.- Return type
Optional[str]
- GetObjectMap() → Dict[int, Pkg.PYC.PyObject]¶
- Returns
Returns the object tree as a map.
- Return type
Dict[int, PyObject]
See also
GetObjectTree()
.
- GetObjectTree() → Pkg.PYC.PyObject¶
- Returns
Returns the root object of the tree.
- Return type
See also
GetObjectMap()
.
- GetVersion() → Tuple[int, int]¶
- Returns
Returns the Python major and minor version of the PYC file as a tuple.
- Return type
Tuple[int, int]
See also
CreateModuleHeaderFromVersion()
.
- Parse(wo: Optional[Pro.Core.NTIWait] = None, verbose: bool = False) → bool¶
Parses the format.
- Parameters
wo (NTIWait) – An optional wait object for the parsing operation.
verbose (bool) – If
True
, provides verbose output.- Returns
Returns
True
if successful; otherwise returnsFalse
.- Return type
bool
- class PyCComplex¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Binary Complex object (
OBJECT_TYPE_BINARY_COMPLEX
).
- class PyCFloat¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Binary Float object (
OBJECT_TYPE_BINARY_FLOAT
).
- class PyCode¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Code object (e.g.,
OBJECT_TYPE_CODE
).
- class PyComplex¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Complex object (
OBJECT_TYPE_COMPLEX
).
- class PyDict¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Dictionary object (
OBJECT_TYPE_DICT
).
- class PyEllipsis¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Ellipsis object (
OBJECT_TYPE_ELLIPSIS
).
- class PyFalse¶
Bases:
Pkg.PYC.PyObject
This class represents a Python False object (
OBJECT_TYPE_FALSE
).
- class PyFloat¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Float object (
OBJECT_TYPE_FLOAT
).
- class PyInt¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Integer object (e.g.,
OBJECT_TYPE_INT
).Methods:
ToInt
()Returns the integer value of the object.
- ToInt() → int¶
- Returns
Returns the integer value of the object.
- Return type
int
- class PyList¶
Bases:
Pkg.PYC.PyObject
This class represents a Python List object (
OBJECT_TYPE_LIST
).
- class PyLong¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Long object (e.g.,
OBJECT_TYPE_LONG
).Methods:
ToInt
()Returns the integer value of the object.
- ToInt() → int¶
- Returns
Returns the integer value of the object.
- Return type
int
- class PyNone¶
Bases:
Pkg.PYC.PyObject
This class represents a Python None object (
OBJECT_TYPE_NONE
).
- class PyNull¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Null object (
OBJECT_TYPE_NULL
).
- class PyObject¶
This class represents the base class for every Python object.
Methods:
Dump
(out[, inline])Dumps the contents of the object to a text stream.
Equals
(other)Compares this Python object to another one.
GetChild
(i)Retrieves a child object.
Returns the number of child objects.
Returns
True
if the object has children; otherwise returnsFalse
.Attributes:
The object id (assigned by
PYCObject
).Variable that determines whether the object is a reference.
The object offset.
The object size.
The parent object id.
The parent position.
The object type (e.g.,
OBJECT_TYPE_NONE
).
- Dump(out: Pro.Core.NTTextStream, inline: bool = False) → None¶
Dumps the contents of the object to a text stream.
- Parameters
out (NTTextStream) – The text stream.
inline (bool) – If
True
, dumps the contents on a single line.
- Equals(other: Pkg.PYC.PyObject) → bool¶
Compares this Python object to another one.
- Parameters
other (PyObject) – The other Python object.
- Returns
Returns
True
if the objects are the same; otherwise returnsFalse
.- Return type
bool
- GetChild(i: int) → Optional[Pkg.PYC.PyObject]¶
Retrieves a child object.
- Parameters
i (int) – The index of the child object.
- Returns
Returns the child object if successful; otherwise returns
None
.- Return type
Optional[PyObject]
See also
GetChildCount()
.
- GetChildCount() → int¶
- Returns
Returns the number of child objects.
- Return type
bool
See also
GetChild()
.
- HasChildren() → bool¶
- Returns
Returns
True
if the object has children; otherwise returnsFalse
.- Return type
bool
See also
GetChildCount()
.
- is_ref¶
Variable that determines whether the object is a reference.
- object_offset¶
The object offset.
- object_size¶
The object size.
- parent_id¶
The parent object id.
- parent_pos¶
The parent position.
- type¶
The object type (e.g.,
OBJECT_TYPE_NONE
).
- class PySet¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Set object (
OBJECT_TYPE_SET
).
- class PyStopIter¶
Bases:
Pkg.PYC.PyObject
This class represents a Python StopIter object (
OBJECT_TYPE_STOPITER
).
- class PyString¶
Bases:
Pkg.PYC.PyObject
This class represents a Python String object (e.g.,
OBJECT_TYPE_STRING
).Methods:
ToString
()Returns the object as a string.
- ToString() → str¶
- Returns
Returns the object as a string.
- Return type
str
- class PyTrue¶
Bases:
Pkg.PYC.PyObject
This class represents a Python True object (
OBJECT_TYPE_TRUE
).
- class PyTuple¶
Bases:
Pkg.PYC.PyObject
This class represents a Python Tuple object (e.g.,
OBJECT_TYPE_TUPLE
).