runtime/typemapping.py
author GP Orcullo <kinsamanka@gmail.com>
Fri, 28 Oct 2022 14:56:07 +0800
branchpython3
changeset 3756 8db203310f68
parent 3750 f62625418bff
child 3771 67a0df6478b3
permissions -rw-r--r--
fix ctypes encoding
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     3
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
     4
# See COPYING.Runtime file for copyrights details.
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     5
#
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
     6
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
     7
import ctypes
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
     8
from ctypes import *
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
     9
from datetime import timedelta as td
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
    10
3756
8db203310f68 fix ctypes encoding
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    11
ctypes.pythonapi.PyUnicode_AsUTF8.argtypes = (ctypes.c_void_p,)
8db203310f68 fix ctypes encoding
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    12
ctypes.pythonapi.PyUnicode_AsUTF8.restype = ctypes.POINTER(ctypes.c_char)
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    13
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    14
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    15
class IEC_STRING(Structure):
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    16
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    17
    Must be changed according to changes in iec_types.h
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    18
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    19
    _fields_ = [("len", c_uint8),
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    20
                ("body", c_char * 126)]
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    21
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    22
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    23
class IEC_TIME(Structure):
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    24
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    25
    Must be changed according to changes in iec_types.h
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    26
    """
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    27
    _fields_ = [("s", c_long),   # tv_sec
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    28
                ("ns", c_long)]  # tv_nsec
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    29
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    30
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    31
def _t(t, u=lambda x: x.value, p=lambda t, x: t(x)):
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    32
    return (t, u, p)
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    33
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    34
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    35
def _ttime():
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    36
    return (IEC_TIME,
1963
cded8d8a0485 fix rounding milliseconds from nanoseconds
Surkov Sergey <surkovsv93@gmail.com>
parents: 1881
diff changeset
    37
            lambda x: td(0, x.s, x.ns/1000.0),
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    38
            lambda t, x: t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000))
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    39
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1748
diff changeset
    40
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    41
SameEndianessTypeTranslator = {
1774
ac0fe8aabb5e clean-up: fix PEP8 E272 multiple spaces before keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    42
    "BOOL":       _t(c_uint8, lambda x: x.value != 0),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    43
    "STEP":       _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    44
    "TRANSITION": _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    45
    "ACTION":     _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    46
    "SINT":       _t(c_int8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    47
    "USINT":      _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    48
    "BYTE":       _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    49
    "STRING":     (IEC_STRING,
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    50
                   lambda x: x.body[:x.len],
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    51
                   lambda t, x: t(len(x), x)),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    52
    "INT":        _t(c_int16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    53
    "UINT":       _t(c_uint16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    54
    "WORD":       _t(c_uint16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    55
    "DINT":       _t(c_int32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    56
    "UDINT":      _t(c_uint32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    57
    "DWORD":      _t(c_uint32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    58
    "LINT":       _t(c_int64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    59
    "ULINT":      _t(c_uint64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    60
    "LWORD":      _t(c_uint64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    61
    "REAL":       _t(c_float),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    62
    "LREAL":      _t(c_double),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    63
    "TIME":       _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    64
    "TOD":        _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    65
    "DATE":       _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    66
    "DT":         _ttime(),
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    67
    }
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    68
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    69
SwapedEndianessTypeTranslator = {
1733
dea107dce0c4 clean-up: fix some PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    70
    # TODO
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    71
    }
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    72
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1741
diff changeset
    73
TypeTranslator = SameEndianessTypeTranslator
1075
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
    74
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    75
# Construct debugger natively supported types
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3395
diff changeset
    76
DebugTypesSize = dict([(key, sizeof(t)) for key, (t, p, u) in SameEndianessTypeTranslator.items() if t is not None])
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    77
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    78
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    79
def UnpackDebugBuffer(buff, indexes):
1758
845ca626db09 clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    80
    res = []
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    81
    buffoffset = 0
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    82
    buffsize = len(buff)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    83
    buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)), c_void_p).value
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    84
    for iectype in indexes:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1835
diff changeset
    85
        c_type, unpack_func, _pack_func = \
1767
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
    86
            TypeTranslator.get(iectype, (None, None, None))
3395
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    87
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    88
        cursor = c_void_p(buffptr + buffoffset)
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    89
        if iectype == "STRING":
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    90
            # strlen is stored in c_uint8 and sizeof(c_uint8) is 1
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    91
            # first check we can read size
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    92
            if (buffoffset + 1) <= buffsize:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    93
                size = 1 + cast(cursor,POINTER(c_type)).contents.len
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    94
            else:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    95
                return None
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    96
        else:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    97
            size = sizeof(c_type)
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    98
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    99
        if c_type is not None and (buffoffset + size) <= buffsize:
1747
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
   100
            value = unpack_func(cast(cursor,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   101
                                     POINTER(c_type)).contents)
3395
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
   102
            buffoffset += size
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
   103
            res.append(value)
1075
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
   104
        else:
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents: 1973
diff changeset
   105
            return None
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
   106
    if buffoffset and buffoffset == buffsize:
1075
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
   107
        return res
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
   108
    return None