File: func_call.py

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (77 lines) | stat: -rw-r--r-- 2,502 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from ptrace.func_arg import FunctionArgument


class FunctionCallOptions(object):
    """
    Options to format a function call and its arguments.
    """

    def __init__(self,
                 write_types=False, write_argname=False,
                 replace_socketcall=True, string_max_length=300,
                 write_address=False, max_array_count=20):
        self.write_address = write_address
        self.write_types = write_types
        self.write_argname = write_argname
        self.replace_socketcall = replace_socketcall
        self.string_max_length = string_max_length
        self.max_array_count = max_array_count
        self.instr_pointer = False


class FunctionCall(object):
    """
    A function call. Attributes:
     - name (str): function name
     - arguments: list of FunctionArgument objects
     - restype (str, optional): result type
     - resvalue (optional): result value
     - argument_class: class used to build the new arguments

    Methods:
     - format(): create a string representation of the call
     - addArgument(): add a new argument
     - clearArguments(): remove all arguments
    """

    def __init__(self, name, options, argument_class=FunctionArgument):
        self.name = name
        self.options = options
        self.arguments = []
        self.restype = None
        self.resvalue = None
        self.argument_class = argument_class

    def addArgument(self, value=None, name=None, type=None):
        arg = self.argument_class(
            self, len(self.arguments), self.options, value, type, name)
        self.arguments.append(arg)

    def clearArguments(self):
        self.arguments = []

    def __getitem__(self, key):
        if isinstance(key, str):
            for arg in self.arguments:
                if arg.name == key:
                    return arg
            raise KeyError("%r has no argument called %r" % (self, key))
        else:
            # Integer key
            return self.arguments[key]

    def format(self):
        arguments = [arg.format() for arg in self.arguments]

        # Remove empty optionnal arguments
        while arguments and not arguments[-1]:
            arguments.pop(-1)

        arguments = ", ".join(arguments)
        if self.restype and self.options.write_types:
            return "%s %s(%s)" % (self.restype, self.name, arguments)
        else:
            return "%s(%s)" % (self.name, arguments)

    def __repr__(self):
        return "<FunctionCall name=%r>" % self.name