A static type inference method and system for Python external functions
By analyzing the declaration and implementation of external functions in Python/C multi-language projects, and combining cross-language interface layer information, the static type inference system PyCType is designed, which solves the problem of Python external function type inference and achieves reliable inference of the number and type of parameters. , which enhances the capabilities of static typing tools.
Patent Information
- Application Number
- CN202111105813.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-22
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-09-22
AI Technical Summary
Existing Python static type inference tools cannot effectively handle the type inference of external functions. Especially for the dynamically typed language Python, the type inference accuracy of external functions is poor or requires manual assistance.
By performing language separation on Python/C multi-language projects, identifying external function declarations and analyzing parameter type conversion and return type conversion, combined with the implicit information of the cross-language interface layer for type inference, designing the static type inference system PyCType, checking external function declarations Consistency with implementation.
It achieves reliable inference of the number and type of most external function parameters, enhances the capabilities of Python static type inference tools, avoids errors in parameterless external functions receiving arbitrary type parameters, and improves type inference accuracy and tool effectiveness.
Smart Images

Figure CN113885854B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of programming languages and static analysis, and in particular to a static type inference method and system for Python external functions. Background Art
[0002] Static type inference is one of the important means to maintain the security of dynamically typed language programs. However, external functions implemented in another programming language are often not within the scope of type inference in the host language. Python is a popular dynamically typed language, and a large number of widely used Python packages are multi-language architectures that include C / C++ external extensions.
[0003] Both academia and industry have made a lot of attempts to solve the problem of static type inference in Python. These efforts can be divided into the following three categories:
[0004] 1. Support type annotations through extended syntax, and perform type inference based on type annotations. Representative examples include Mypy from the Python community, Pyright from Microsoft, and Pyre from Facebook. The disadvantage of this method is that it requires modifying the source code and affects the agility of Python development.
[0005] 2. Machine learning-based methods. Representative methods include the method proposed by Xu et al. in 2016 and Typilus proposed by Allamanis et al. in 2020. On the one hand, the data set of this method often comes from the inference results of traditional methods. On the other hand, this method is non-deterministic and can only obtain probabilistic results.
[0006] 3. Traditional program analysis methods based on data flow analysis, abstract interpretation, SMT solving, etc. Representative methods include Pytype from Google, PySonar used by companies such as Google and Sourcegraph, the method proposed by Fritz et al. in 2017, the method proposed by Hassan et al. in 2018, and the method proposed by Monat et al. in 2020. These works often directly ignore external objects (such as treating them as object types) or pre-set type stubs, and the type inference accuracy is poor or requires manual assistance.
[0007] As we can see, existing deterministic Python static type inference tools that are not based on type annotations cannot effectively handle external functions. Therefore, how to statically infer the types of Python external functions has become an urgent problem to be solved. Summary of the invention
[0008] In order to solve the above technical problems, the present invention provides a static type inference method and system for Python external functions.
[0009] The technical solution of the present invention is: a static type inference method for Python external functions, comprising:
[0010] Step S1: performing language separation on the Python / C multi-language project; for the C language file obtained after separation, identifying the external function declaration therein, and analyzing the parameter type conversion and return type conversion of the external function implementation corresponding to each of the external function declarations;
[0011] Step S2: Perform type inference based on the external function declaration, parameter type conversion and return type conversion, and check the consistency of the parameterless function declaration between the external function declaration and the corresponding external function implementation.
[0012] Compared with the prior art, the present invention has the following advantages:
[0013] The present invention proposes a static type inference method for Python external functions, which infers the type signature of external functions by analyzing the implicit information of the cross-language interface layer, and designs and implements a static type inference system PyCType accordingly. The present invention can reliably infer the number and type of parameters of most external functions. At the same time, the present invention can further enhance the existing Python static type inference tool so that it can analyze external function calls in programs. In addition, the present invention can check for vulnerabilities in which external function declarations are inconsistent with their implementations, thereby avoiding errors in which such vulnerabilities cause external functions without parameters to receive parameters of any type. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 This is a flowchart of a static type inference method for Python external functions in an embodiment of the present invention;
[0015] Figure 2A This is an example of an external function call in a C extension module of Python in an embodiment of the present invention;
[0016] Figure 2B This is an example of mapping an external function to C implementation through Python / C API in an embodiment of the present invention;
[0017] Figure 2C This is an example of C implementation of an external function in an embodiment of the present invention;
[0018] Figure 3 A schematic diagram of an external function call from the perspective of a type system in an embodiment of the present invention;
[0019] Figure 4A schematic diagram of the abstract syntax of the Python / C multi-language software system in an embodiment of the present invention;
[0020] Figure 5 is the type of the Python side in the embodiment of the present invention;
[0021] Figure 6 It is the sub-typing rule of the Python side type in the embodiment of the present invention;
[0022] Figure 7 It is the C-side type in the embodiment of the present invention;
[0023] Figure 8 The formatting unit conversion rule in the embodiment of the present invention;
[0024] Fig. 9 This is an example of a complex return type conversion in an embodiment of the present invention;
[0025] Fig.10 is the inference rate of the types of parameters of external functions in CPython, NumPy and Pillow in the embodiments of the present invention;
[0026] Fig.11 The type inference enhancement experimental results in the embodiments of the present invention;
[0027] Fig.12 It is a structural block diagram of a static type inference system for Python external functions in an embodiment of the present invention;
[0028] Fig.13 It is a schematic diagram of the PyCType structure in an embodiment of the present invention. DETAILED DESCRIPTION
[0029] The present invention provides a static type inference method for Python external functions, which can infer the number and type of parameters of most external functions, further enhance the existing Python static type inference tool, and check the vulnerability of inconsistency between external function declaration and its implementation.
[0030] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention is further described in detail below through specific implementations and in conjunction with the accompanying drawings.
[0031] Before performing static type inference of Python external functions, an embodiment of the present invention has a built-in dependency configuration library and configuration debugging tool for the static type inference system PyCType, which configures and stores the dependencies required to make the multi-language project source code compilable. Taking a Python / C multi-language project as an example, input PyCType, and the configuration debugging tool of PyCType will output the problems encountered in parsing the file, such as unknown header files and undefined objects. The dependency configuration library will record the compilation parameters required to successfully parse the corresponding file. For each project, this process only needs to be executed once. For a configured project, its configuration library will be loaded directly. At the same time, the configuration library will have some common compilation options built in, and this part of the configuration will be loaded by default for each project and new project.
[0032] In order to better explain the present invention, the symbolic basis used in it is first explained. The inference rule is formally expressed as It is assumed that the judgment J1…J k The premises of the inference are called the conclusion. In the hypothetical judgment of It indicates syntactic consequence, that is, it can be deduced that J holds true in the context Γ.
[0033] Embodiment 1
[0034] like Figure 1 As shown, a static type inference method for Python external functions provided by an embodiment of the present invention includes the following steps:
[0035] Step S1: language separation of the Python / C multi-language project; for the C language file obtained after separation, identifying the external function declaration therein, and analyzing the parameter type conversion and return type conversion of the external function implementation corresponding to each external function declaration;
[0036] Step S2: Perform type inference based on the external function declaration, parameter type conversion and return type conversion, and check the consistency of the no-parameter function declaration on the external function declaration and the corresponding external function implementation.
[0037] Python's external function interface Python / C API is the middle layer that bridges Python and C / C++. Figure 2A The program host.py shown is an example of a Python C extension module, in which the external function extm.foo is called by Figure 2B The Python / C API PyMethodDef in the program interface.c is mapped to the C implementation _foo, and then associated to the module extm through the Python / C API PyModuleDef. Figure 2CIn the program foreign.c shown in the figure, inside the C implementation of the foreign function, the Python / C API PyArg_ParseTuple is a common parameter parsing method, which indicates the type conversion from Python to C through the format string, and the Python / C API PyLong_FromLong is a common return type conversion, which converts a C integer variable to a Python integer variable and returns it to the Python side.
[0038] For statically typed languages, external functions are declared with explicit types, while dynamically typed languages do not have this information (e.g. Figure 2B ), but the interface layer still needs to provide a call interface description containing information such as cross-language type conversion. The present invention infers the type constraints acting on external functions by modeling and analyzing the implicit information in these call interface descriptions.
[0039] Figure 3 The figure shows the external function call from the perspective of the type system. If we only look at it from a single language perspective (dashed line), the parameter type and return type of the external function are unknown (gray box); but from a multi-language perspective (solid line), combined with the call interface description obtained by the interface layer, the type of the external function can actually be deduced. The embodiment of the present invention divides this implicit information into three parts:
[0040] 1. External function declaration (D) establishes the mapping relationship between the calling name of the external function on the Python side and the external function implementation on the C side.
[0041] 2. Parameter type conversion (P) describes the type conversion of the actual parameters passed into the external function from the Python side to the C variable.
[0042] 3. Return type conversion (R) describes the type conversion when the return value variable on the C side is returned to the Python side.
[0043] Figure 4 It is a formal representation of the abstract syntax of the Python / C multi-language software system, where the superscripts p and c mark the Python and C language sides respectively. Unlike local functions, which are declared and defined on the Python side and applied on the Python side, external functions are applied on the Python side, but their declarations and definitions are on the C side.
[0044] For example, as a dynamically typed language, such as Figure 5The Python-side types shown are the type values that Python variables are bound to at runtime, including built-in types such as str, int, and object. At the same time, the function type pFunc is introduced to represent functions, the product type pProduct is introduced to represent types such as list, tuple, and dict, and the sum type pUnion is introduced to support unions, a common language feature on the C side. Some types such as module and iterator are not included in the type set because they will be treated as object type objects when passed to external functions.
[0045] The calling interface description of the Python / C interface layer can give stricter type / value constraints. For example Figure 2C In the Python / C API function PyArg_ParseTuple, the second parameter value specifies one of the formatting units 'I', which requires that the actual parameter passed into the corresponding external function is a Python integer and non-negative. The embodiment of the present invention uses subtyping to characterize such rules. The subtyping rules of Python-side types are as follows Figure 6 shown.
[0046] like Figure 7 The C-side types shown, in addition to common built-in types, also include some structures corresponding to the Python-side type implementations inside the CPython interpreter. As part of the Python / C API, they are also used to receive and convert objects passed by the Python side at the interface layer.
[0047] Based on the above core ideas and grammar definitions, the present invention defines type inference rules as a formalized representation.
[0048] In one embodiment, the above step S1: performing language separation on the Python / C multi-language project; for the C language file obtained after separation, identifying the external function declaration therein, and analyzing the parameter type conversion and return type conversion of the external function implementation corresponding to each external function declaration, specifically includes:
[0049] Step S11: The type inference rule is formally defined as follows in the form of "rule description rule name":
[0050]
[0051] Among them, the external function declaration D, parameter type conversion P and return type conversion R together constitute the reasoning premise; P Indicates the calling name of the external function on the Python side; pFunc indicates the function type, and its two parameters correspond to the parameter type and return type of the function respectively;
[0052] The embodiment of the present invention utilizes external function declaration D, parameter type conversion P and return type conversion R to jointly constitute reasoning, thereby deriving the type signature of the external function contained in the Python / C interface layer calling interface description, and the parameter type and return type of the function type are determined by the specific combination of D, P, and R.
[0053] Step S12: The external function declaration D is expressed as follows:
[0054]
[0055] Among them, flag gives the calling convention, f P Indicates the calling name of the external function on the Python side, f c Indicates the C-side implementation corresponding to the external function. This expression represents the mapping relationship between the external function's call name on the Python side and its implementation on the C side.
[0056] The external function declaration D describes the mapping between the external function call name on the Python side and the implementation on the C side. For example, in CPython, when the calling convention flag is METH_VARARGS, it means that the external function receives one or more Python objects as parameters, which are packaged into an object and passed to the C side. The interface layer needs to give rules for parsing this packaged object into multiple C variables.
[0057] Step S13: Parameter type conversion P is expressed as follows:
[0058]
[0059] Among them, this expression represents the actual parameter passed from the Python side to the external function in the program property pType represents a Python-side type, cType represents a C-side type, and the type set is shown in Figure 5 and Figure 7 .
[0060] Step S14: The return type conversion R is expressed as follows:
[0061]
[0062] The expression indicates that the return value of the external function on the C side is in the program property When returning to the Python side in the context of C, the conversion from C type to Python type is established; as part of Python, its external functions also support the language feature of multiple returns, which C itself does not support.
[0063] In one embodiment, the above step S13: parameter type conversion specifically includes:
[0064] Step S131: When the calling convention flag is METH_NOARGS, the external function is declared as having no arguments, which is expressed as follows:
[0065]
[0066] Among them, the shape The judgment means that only when the nature of the procedure Threshold semantic predicate When true, judgment J holds, and the threshold semantic predicate Analyze the calling convention flag to determine whether the external function is declared as having no parameters. If it has no parameters, the parameter type conversion of the external function is represented as the corresponding empty type on both the Python side and the C side.
[0067] Step S132: When the C implementation corresponding to the external function does not parse and use the passed-in parameters, the external function is considered to be parameterless, as shown below:
[0068]
[0069] The threshold semantic predicate Perform parameter unused analysis on C implementation.
[0070] The differences in type systems between dynamically typed languages (Python) and statically typed languages (C / C++), as well as the design of Python's external function interface, lead to this redundancy in declarations and leave hidden dangers of inconsistent declarations. That is, only when both of the above threshold semantic predicates are true can it be determined that the external function has no parameters.
[0071] Step S133: When both threshold semantic predicates in S131 and S132 are true, it can be determined that the external function is parameterless, which is expressed as follows:
[0072]
[0073] Step S134: When both threshold semantic predicates in S131 and S132 are false, the external function receives at least one Python object, which is represented as follows:
[0074]
[0075] Among them, l ap is a Python / C API for a family of parameter parsing. iis the i-th formatting unit. For example, formatting unit I corresponds to the conversion of Python non-negative integer to C unsigned int type. The complete formatting unit conversion rules are as follows Figure 8 shown.
[0076] In this step, when both threshold semantic predicates are false, the external function receives at least one Python object, which is packaged as an intermediate parameter of the interface layer, and the parameter is parsed and restored to several C variables. Most commonly, this type conversion is completed by the parameter parsing family Python / C API. These Python / C APIs use a format string to specify the conversion rules. A format string contains zero or more format units, and each format unit (except special meaning characters) corresponds to a conversion from a Python type to a C type.
[0077] In one embodiment, the above step S14: return type conversion specifically includes:
[0078] Step S141: value construction analysis, return type conversion is expressed as follows:
[0079]
[0080] Among them, l vb is a Python / C API for constructing a family of values, u j is its jth formatting unit, and the m C variables are converted to Python objects according to the corresponding formatting units and together constitute a tuple object as a value that supports Python's multiple return feature.
[0081] Step S142: Explicit conversion analysis, the return type conversion is expressed as follows:
[0082]
[0083] Among them, the Python / C API for explicit conversion ec It includes the following three forms:
[0084] a) PyPT_FromCT() converts a C variable of type CT to a Python variable of type PT;
[0085] b) PyPT_New() creates and returns a Python variable of type PT;
[0086] c) Py_PT itself is directly returned to the Python side as an object of type PT.
[0087] C programs support explicit type conversion as a right-associative operator. For a function of the form return(β1)...(β n )w's return expression can be expressed using the following steps.
[0088] Step S143: type conversion analysis, the return type conversion is expressed as follows:
[0089]
[0090] Among them, the C type β1 is the return of the C implementation of the external function to the Python side, and is a structure that corresponds one-to-one to the Python built-in type.
[0091] In addition to the above three return types, there are also Fig. 9 The example of a complex return type conversion is shown in FIG. 1 , where the variable result is declared as type T1 (generally PyObject*), which may be assigned to a more refined type (T2, T3) by calling some of the aforementioned Python / C APIs. To analyze this type of propagation. The return type conversion analysis of the previous three categories will be called internally, and the return type conversion is shown in the following steps.
[0092] Step S144: reachable definition analysis, return type conversion is expressed as follows:
[0093]
[0094] in, The return type conversion of the three types in the internal call S141, S142 and S143 analyzes the return type in the case of variable propagation.
[0095] In one embodiment, the type inference in the above step S2 is performed according to the external function declaration, parameter type conversion and return type conversion, specifically including:
[0096] Step S21: Determine the specific combination of the inference premises in S11 from S1, where D contains one form of S12, P contains two forms of S133 or S134, and R contains four forms of S141 or S142 or S143 or S144;
[0097] For external functions that use parameter resolution analysis for parameter type conversion and value construction analysis for return type conversion, the type inference rules are based on the external function declaration D in S12, the parameter type conversion Pap in S134, and the return type conversion Rvb in S141, and are inferred as follows:
[0098]
[0099] For a parameterless external function with an explicit return, the type inference rule is based on the external function declaration D in S12, the parameter type conversion Pcc in S133, and the return type conversion Rec in S142, and is inferred as follows:
[0100]
[0101] For all possible inference premises: D contains one form of S12, P contains two forms of S133 or S134, and R contains four forms of S141 or S142 or S143 or S144, that is, there are a total of 8 combinations, and their inference rules are similar to the two situations described in the above step S21, which will not be repeated here.
[0102] Step S22: Check the consistency of the external function declaration without parameters and the corresponding external function implementation: When the threshold semantic predicate in S131 is true and the threshold semantic predicate in S132 is false, the declaration is inconsistent, which will lead to the error that the external function without parameters can accept any type of parameters.
[0103] by Figure 2B Taking a simple external function as an example, first create an external function declaration using PyMethodDef (D) Then the function pointer finds the corresponding external function implementation _foo, and the parameter parsing family Python / C API establishes (P) parameter type conversion:
[0104]
[0105]
[0106] (R)Return type conversions are established by the Python / C API using explicit conversions Then the type inference using the rule (TInfer:D-Pap-Rec) is as follows:
[0107]
[0108]
[0109]
[0110]
[0111] That is, the Python external function foo receives two non-negative integer positional parameters as input and returns an integer variable as the return value. When flag is METH_NOARGS, If it is false, further analysis is performed based on the external function. If true, the corresponding Python-side external function has declaration consistency issues.
[0112] After completing the above steps, PyCType will record all files containing external function declarations. For each external function, it will record the location of its declaration and implementation, as well as the parameter types (including positional parameters and optional parameters) and the result of return type inference. For each vulnerability found, PyCType will record the location of the corresponding external function.
[0113] After obtaining the type inference result of the external function, it is encoded into a pyi file according to the Python standard type annotation format, and then can be used as the type stub of the Python single-language type inference tool Pytype, so that it can analyze external function calls in Python programs.
[0114] The reliability of the static type inference method for Python external functions provided by the present invention is one of the main purposes of building the above strict inference system, that is, the result of type inference is error-free (but may not be accurate enough, such as inferring int as object). The embodiment of the present invention verifies the reliability of the static type inference system through manual inspection. At the same time, there are no false positives in vulnerability discovery, and corresponding trigger codes can be constructed for all errors.
[0115] On the basis of reliability, the completeness of the static type inference method for Python external functions provided by the present invention is an important indicator for measuring the effectiveness of the type inference system, namely the inference rate. On the one hand, the number of parameters is often more than the return value. On the other hand, as part of the Python program, the return value of the external function call may be the parameter of other (external) functions. Therefore, the effectiveness of the entire system needs to be evaluated in combination with the existing single-language type inference tools. Fig.10 The following table shows the inference rates of the types of parameters of external functions in CPython, NumPy, and Pillow. As can be seen, for parameter type conversion, rules (Pcc) and (Pap) can cover most cases. At the same time, it is not difficult to describe more qualified Python / C APIs in the rules.
[0116] Since reliability does not mean accuracy, the embodiment of the present invention adopts Fig.10We use Pillow, which has the highest inference rate, to build a type enhancement experiment to further measure effectiveness. Pytype from Google is a state-of-the-art static type inference tool for Python. It does not support automatic inference of external functions, but presets type signatures of some external functions, such as common standard library functions, through type stubs. This embodiment of the present invention encodes the result of external function type inference in Pillow into a type stub of Pytype, and then compares the inference rate before and after this type enhancement. We selected Python repositories in GitHub that use Pillow and have more than 30,000 stars as experimental targets. The results of the type inference enhancement experiment are as follows: Fig.11 As shown, we can see that PyCType improves Pytype by 7% to 80% (27.5% on average).
[0117] The present invention proposes a static type inference method for Python external functions, which infers the external function type signature by analyzing the implicit information of the cross-language interface layer, and designs and implements a static type inference system PyCType accordingly. The present invention can reliably infer the number and type of parameters of most external functions. At the same time, the present invention can further enhance the existing Python static type inference tool so that it can analyze external function calls in the program. In addition, the present invention can check for vulnerabilities in which the external function declaration is inconsistent with its implementation, thereby avoiding the error that such vulnerabilities cause the parameterless external function to receive parameters of any type.
[0118] Embodiment 2
[0119] like Fig.12 As shown, an embodiment of the present invention provides a static type inference system for Python external functions, including the following modules:
[0120] The recognition and analysis module 21 is used to perform language separation on the Python / C multi-language project; for the C language file obtained after separation, the external function declaration therein is recognized, and the parameter type conversion and return type conversion of the external function implementation corresponding to each external function declaration are analyzed;
[0121] The type inference module 22 is used to perform type inference based on the external function declaration, parameter type conversion and return type conversion, and to check the consistency of the parameterless function declaration between the external function declaration and the corresponding external function implementation.
[0122] like Fig.13As shown in the figure, a schematic diagram of the PyCType structure is shown, in which the language separator separates Python and C code. The preprocessor configurator configures the dependencies required for parsing files. The AST (Abstract Syntax Tree) generates the C99 parser pycparser based on Python. After obtaining the AST of the interface code, most analyses are implemented as AST access. When an analysis requires other intermediate representations such as CFG, the AST conversion transforms the corresponding AST fragment. The other main processing modules (rounded rectangles) correspond to the previous text.
[0123] The above embodiments are provided only for the purpose of describing the present invention, and are not intended to limit the scope of the present invention. The scope of the present invention is defined by the appended claims. Various equivalent substitutions and modifications made without departing from the spirit and principles of the present invention should all be included within the scope of the present invention.
Claims
1. A static type inference method for Python external functions, characterized in that: include: Step S1: Perform language separation on the Python / C multi-language project; for the C language files obtained after separation, identify the external function declarations therein, and analyze the parameter type conversion and return type conversion of the external function corresponding to each external function declaration, specifically including: Step S11: Type inference rules are formally defined as follows: , Among them, the external function declaration D, parameter type conversion P and return type conversion R together constitute the reasoning premise; P Indicates the calling name of the external function on the Python side; pFunc indicates the function type, and its two parameters correspond to the parameter type and return type of the function respectively; Step S12: The external function declaration D is expressed as follows: ; Among them, flag gives the calling convention, f P Indicates the calling name of the external function on the Python side, f C Indicates the C-side implementation corresponding to the external function. This expression represents the mapping relationship between the external function's call name on the Python side and its implementation on the C side. Step S13: The parameter type conversion P is expressed as follows: ; Among them, this expression indicates that the actual parameters passed from the Python side to the external function are in the program properties The parameter type conversion to C variable in the established context; the parameter type conversion specifically includes: Step S131: When the calling convention flag is METH_NOARGS, the external function is declared as having no arguments, which is represented as follows: ; Among them, the shape The judgment indicates that only when the nature of the procedure Threshold semantic predicates When it is true, judgment J is established; Step S132: When the C implementation corresponding to the external function does not parse and use the passed-in parameters, the external function is considered to be parameterless, as shown below: ; Step S133: When both the threshold semantic predicates in S131 and S132 are true, it can be determined that the external function is parameterless, which is expressed as follows: ; Step S134: When both the threshold semantic predicates in S131 and S132 are false, the external function receives at least one Python object, which is represented as follows: ; in, is a Python / C API for a family of parameter parsing, is its i-th formatting unit; Step S14: The return type conversion R is expressed as follows: ; Among them, this expression indicates that the return value of the external function on the C side is in the program nature The conversion from C type to Python type when returning to Python side under the established context; the return type conversion specifically includes: Step S141: Value construction analysis, the return type conversion is expressed as follows: ; in, is a Python / C API for constructing a family of values, is its jth formatting unit, m C variables are converted to Python objects according to the corresponding formatting units and together form a tuple object as a value that supports Python's multiple return feature; Step S142: Explicit conversion analysis, the return type conversion is expressed as follows: ; Among them, the Python / C API with explicit conversion It includes the following three forms: a) PyPT_FromCT() converts a C variable of type CT to a Python variable of type PT; b) PyPT_New() creates and returns a Python variable of type PT; c) Py_PT itself is directly returned to the Python side as an object of type PT; Step S143: Type conversion analysis, the return type conversion is expressed as follows: ; Among them, C type The return value from the C implementation of the external function to the Python side is a structure that corresponds one-to-one with the Python built-in type; Step S144: reachable definition analysis, the return type conversion is expressed as follows: ; in, The return type conversion of the three types in internal calls S141, S142 and S143 analyzes the return type in the case of variable propagation; Step S2: performing type inference based on the external function declaration, parameter type conversion, and return type conversion, and simultaneously checking the consistency of the external function declaration and the corresponding external function implementation for a no-parameter function declaration; wherein performing type inference based on the external function declaration, parameter type conversion, and return type conversion specifically includes: Step S21: Determine the specific combination of the inference premises in S11 from S1, where D contains one form of S12, P contains two forms of S133 or S134, and R contains four forms of S141 or S142 or S143 or S144; For external functions that use parameter resolution analysis for parameter type conversion and value construction analysis for return type conversion, the type inference rule is based on the external function declaration D described in S12, the parameter type conversion Pap described in S134, and the return type conversion Rvb described in S141, and is inferred as follows: ; For a parameterless external function with an explicit return, the type inference rule is based on the external function declaration D in S12, the parameter type conversion Pcc in S133, and the return type conversion Rec in S142, and is inferred as follows: ; Step S22: Check the consistency of the external function declaration without parameters with the corresponding external function implementation: when the threshold semantic predicate in S131 is true and the threshold semantic predicate in S132 is false, the declaration is inconsistent, which will lead to the error that the external function without parameters can accept parameters of any type.
2. A static type inference system for Python external functions, characterized by: Includes the following modules: The recognition and analysis module is used to perform language separation on Python / C multi-language projects. For the C language files obtained after separation, the module identifies the external function declarations therein and analyzes the parameter type conversion and return type conversion of the external function corresponding to each external function declaration. Specifically, the module includes: Step S11: Type inference rules are formally defined as follows: , Among them, the external function declaration D, parameter type conversion P and return type conversion R together constitute the reasoning premise; P Indicates the calling name of the external function on the Python side; pFunc indicates the function type, and its two parameters correspond to the parameter type and return type of the function respectively; Step S12: The external function declaration D is expressed as follows: ; Among them, flag gives the calling convention, f P Indicates the calling name of the external function on the Python side, f C Indicates the C-side implementation corresponding to the external function. This expression represents the mapping relationship between the external function's call name on the Python side and its implementation on the C side. Step S13: The parameter type conversion P is expressed as follows: ; Among them, this expression indicates that the actual parameters passed from the Python side to the external function are in the program properties Type conversion to C variables in the established context; The parameter type conversion specifically includes: Step S131: When the calling convention flag is METH_NOARGS, the external function is declared as having no arguments, which is represented as follows: ; Among them, the shape The judgment indicates that only when the nature of the procedure Threshold semantic predicates When it is true, judgment J is established; Step S132: When the C implementation corresponding to the external function does not parse and use the passed-in parameters, the external function is considered to be parameterless, as shown below: ; Step S133: When both the threshold semantic predicates in S131 and S132 are true, it can be determined that the external function is parameterless, which is expressed as follows: ; Step S134: When both the threshold semantic predicates in S131 and S132 are false, the external function receives at least one Python object, which is represented as follows: ; in, is a Python / C API for a family of parameter parsing, is its i-th formatting unit; Step S14: The return type conversion R is expressed as follows: ; Among them, this expression indicates that the return value of the external function on the C side is in the program nature The conversion from C type to Python type when returning to Python side in the established context; The return type conversion specifically includes: Step S141: Value construction analysis, the return type conversion is expressed as follows: ; in, is a Python / C API for constructing a family of values, is its jth formatting unit, m C variables are converted to Python objects according to the corresponding formatting units and together form a tuple object as a value that supports Python's multiple return feature; Step S142: Explicit conversion analysis, the return type conversion is expressed as follows: ; Among them, the Python / C API with explicit conversion It includes the following three forms: a) PyPT_FromCT() converts a C variable of type CT to a Python variable of type PT; b) PyPT_New() creates and returns a Python variable of type PT; c) Py_PT itself is directly returned to the Python side as an object of type PT; Step S143: Type conversion analysis, the return type conversion is expressed as follows: ; Among them, C type The return value from the C implementation of the external function to the Python side is a structure that corresponds one-to-one with the Python built-in type; Step S144: reachable definition analysis, the return type conversion is expressed as follows: ; in, The return type conversion of the three types in internal calls S141, S142 and S143 analyzes the return type in the case of variable propagation; A type inference module is configured to perform type inference based on the external function declaration, parameter type conversion, and return type conversion, and to check the consistency of the external function declaration and the corresponding external function implementation for a no-parameter function declaration; wherein performing type inference based on the external function declaration, parameter type conversion, and return type conversion specifically includes: Step S21: Determine the specific combination of the inference premises in S11 from S1, where D contains one form of S12, P contains two forms of S133 or S134, and R contains four forms of S141 or S142 or S143 or S144; For external functions that use parameter resolution analysis for parameter type conversion and value construction analysis for return type conversion, the type inference rule is based on the external function declaration D described in S12, the parameter type conversion Pap described in S134, and the return type conversion Rvb described in S141, and is inferred as follows: ; For a parameterless external function with an explicit return, the type inference rule is based on the external function declaration D in S12, the parameter type conversion Pcc in S133, and the return type conversion Rec in S142, and is inferred as follows: ; Step S22: Check the consistency of the external function declaration without parameters with the corresponding external function implementation: when the threshold semantic predicate in S131 is true and the threshold semantic predicate in S132 is false, the declaration is inconsistent, which will lead to the error that the external function without parameters can accept parameters of any type.
