CN105760161B - The attribute analysis method and device of Python - Google Patents

The attribute analysis method and device of Python Download PDF

Info

Publication number
CN105760161B
CN105760161B CN201610080212.4A CN201610080212A CN105760161B CN 105760161 B CN105760161 B CN 105760161B CN 201610080212 A CN201610080212 A CN 201610080212A CN 105760161 B CN105760161 B CN 105760161B
Authority
CN
China
Prior art keywords
attribute
name
variable
type
python
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201610080212.4A
Other languages
Chinese (zh)
Other versions
CN105760161A (en
Inventor
邓慧
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Netease Hangzhou Network Co Ltd
Original Assignee
Netease Hangzhou Network Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Netease Hangzhou Network Co Ltd filed Critical Netease Hangzhou Network Co Ltd
Priority to CN201610080212.4A priority Critical patent/CN105760161B/en
Publication of CN105760161A publication Critical patent/CN105760161A/en
Application granted granted Critical
Publication of CN105760161B publication Critical patent/CN105760161B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms

Abstract

The invention discloses the attribute analysis method and devices of Python a kind of.Wherein, this method comprises: step 1, read the variable name of user's input, and from parsed in the variable name root attribute-name and its under attribute-names at different levels;Step 2 is searched described attribute-name in predeterminated position, and is returned to dependent variable;Step 3 according to the type search next stage attribute-name of the variable of return, and is returned to dependent variable;The step 3 is repeated, until finding the variable of leaf grade attribute-name return.Through the invention, the technical effect that is successfully parsed of various types of variable names inputted to user in Python kernel is realized, the technical issues of Python debugging plan for solving in the related technology is unable to satisfy debugging demand.

Description

The attribute analysis method and device of Python
Technical field
The present invention relates to computer fields, in particular to the attribute analysis method and device of Python a kind of.
Background technique
Python programming language is an efficient general programming language, and the technology that can be applicable in the overwhelming majority realizes needs. In game engineering, also often use python language as the realization language of game logic, its advantages are can be quickly To Requirements Modeling, since the java standard library of its perfect programming semanteme and abundant high quality provided is with third party library, so that with Family can expand complete game logic on the basis of rapid modeling, and because of the dynamic of its height, be easy to repair Change, is suitably applied in the very frequent game engineering development process of iteration.
The slow disadvantage of the speed of service in order to evade python, usually also will use the mode of c language extension to python Kernel is extended, and some crucial and frequent logic of calling is made to realize in this way, substantially increases its speed of service. But due to the excessively high dynamic of python language, if started a leak in program c language extension (bug), use is general The debugging of debugger (gdb) will become extremely difficult.
For the debud mode of Python, there are mainly two types of at present:
Mode of the first debugging python kernel is directly debugged using general debugger (gdb), but this debugging Mode must disassemble the python variable occurred in program according to the experience of tuner oneself, eventually pass through several steps Suddenly real data value is found;
Second of debud mode is the gdb expansion plugin subsidiary using Python.Python kernel is general when publication Also it can be attached to a python-gdb.py file, this is the expansion plugin of a gdb, can be solved on limited extent above-mentioned The problem of variate-value is found, mainly realizes to issue orders for debugging:
1.py-bt: the call stack of python can be printed.
2.py-list: the current source code of python can be printed (if Debugging message has).
3.py-up, py-down: the python stack frame that switching is currently debugged.
4.py-print: the variate-value of python is printed.
5.py-locals: the python local variable value for the stack frame that printing is currently debugged.
However, in two kinds of above-mentioned debugging plans, the shortcomings that all existing to a certain degree, for example the first debugging plan Tuner is greatly relied on for the degree of understanding of python kernel, if it is not known that how to acquire manually in internal storage data The data type of debugging value, it will so that debugging becomes impossible, and waste time very much to carry out manually to disassemble data type Process.Above-mentioned second of debugging plan has had great progress, but still remains some fatal problems, causes to debug It is not smooth enough.
Aiming at the problem that Python debugging plan in the related technology is unable to satisfy debugging demand, not yet propose at present effective Solution.
Summary of the invention
The embodiment of the invention provides the attribute analysis method and devices of Python a kind of, at least to solve in the related technology Python debugging plan the technical issues of being unable to satisfy debugging demand.
According to an aspect of an embodiment of the present invention, the attribute analytic method of Python a kind of is provided, comprising:
Step 1, read user input variable name, and from parsed in the variable name root attribute-name and its under it is at different levels Attribute-name;
Step 2 is searched described attribute-name in predeterminated position, and is returned to dependent variable;
Step 3 according to the type search next stage attribute-name of the variable of return, and is returned to dependent variable;
The step 3 is repeated, until finding the variable of leaf grade attribute-name return.
Optionally, described attribute-name is searched in predeterminated position, and returning includes at least one of to dependent variable:
This earth's surface for reading current Python stack frame searches whether that there are described attribute-names, if there is described Attribute-name is then returned to dependent variable;
The local variable reference for reading current C stack frame, searches whether that there are described attribute-names, if there is described Attribute-name is then returned to dependent variable;
The global variable C_ of C must then be belonged to if there is described attribute-name by searching current visible symbol Global is returned to dependent variable;
The system module sys.modules table for searching Python is then returned if there is the corresponding module of described attribute-name Return corresponding module variable.
Optionally, according to the type search next stage attribute-name of the variable of return, and return to dependent variable include with down toward It is one of few:
If a is the variable returned in previous step, b is the next stage attribute-name, then
When the type of a is Python universal class, if the type contains _ _ dict__ attribute, it is drawn off, then The next stage attribute-name is searched wherein, if it is found, returning to dependent variable, wherein _ _ dict__ is current variable type Attribute dictionary;
When the type of a is Python universal class, if having member in the C-structure statement of Python universal class Address offset of the members in the Python universal class, then be added, and return to phase by members statement with the address a Address after adding;
When the type of a be Python.list when, if Python universal class can not find the next stage attribute-name and The type of the next stage attribute-name is numeric type, then by the address ob_item in a plus the corresponding number of b and conduct One address returns;
When the type of a is Python.dict, if Python universal class can not find b, utilize The method that python.dict itself is provided calculates the Hash hash value of b, is provided using the hash value with python.dict Hash lookup algorithm searches whether that b exists wherein, if it does, returning to corresponding value;
It is corresponding in a if Python universal class can not find b when the type of a is the C expansion type of Python C expansion type statement in search whether that there are b, if it does, the address offset of b is added with the address a, and return to addition Address afterwards;
When the type of a is general C-structure, the statement of C-structure is found, searches and wherein whether there is b, if it does, The address offset of b is added with the address a, and returns to address after being added;
When the type of a is C array, if b is number, b*size (a)+a is returned, wherein size (a) indicates a's Length;
When the type of a is std::vector, if b is number, by b*size (the template template value class of a Type) the element initial address that is added with the address a returns;
When the type of a is std::map, using the lookup method of red black tree, belonged to using the parent of a, left, right Property search whether there are b, if it does, return to dependent variable;
When the type of a is std::tr1::unordered_map, the hash value of b is calculated, it is corresponding to find the hash value Bucket, search whether in the corresponding chained list of the bucket there are b, if it does, return to dependent variable.
Optionally, the variable name of user's input is being read, and is parsing from the variable name root attribute-name and its lower each Before grade attribute-name, further includes:
Issue the corresponding plug-in unit of attribute analytic method of the Python.
Optionally, the corresponding plug-in unit of attribute analytic method for issuing the Python includes:
The corresponding plug-in unit of attribute analytic method of the Python is copied under the root of gdb plug-in unit;
Initialize the corresponding plug-in unit of attribute analytic method of the Python.
Optionally, after the variable for finding the return of leaf grade attribute-name, further includes:
According to the type for the variable that leaf grade attribute-name returns, the variable that the leaf grade attribute-name returns is printed to aobvious In display screen.
Optionally, the type of the variable returned according to leaf grade attribute-name, the variable that the leaf grade attribute-name is returned Printing includes at least one of into display screen:
When the type for the variable that the leaf grade attribute-name returns is basic type, directly by the leaf grade attribute-name The variable of return is converted to readable character string type and prints to display screen;
When the type for the variable that the leaf grade attribute-name returns is the encapsulation to type of foundation, packed base is taken out Then the variable of type of foundation is converted to readable character string type and printed to display screen by plinth type;
It, will be all in the Container Type when the type for the variable that the leaf grade attribute-name returns is Container Type Element iteration is taken out, and stringification be stitched together after printing to display screen;
When the leaf grade attribute-name return variable type be general python.object when, by its _ _ Printing is to display screen after dict__ attribute is parsed into character string according to python.dict mode, wherein _ _ dict__ is current becomes Measure the attribute dictionary of type;
When the type for the variable that the leaf grade attribute-name returns is general c structure, by attribute all in C-structure It prints after stringification to display screen;
When the type for the variable that the leaf grade attribute-name returns is the type that can not be parsed, corresponding address is printed extremely Display screen.
According to another aspect of an embodiment of the present invention, the attribute resolver of Python a kind of is additionally provided, comprising:
Parsing module, for reading the variable name of user's input, and parse from the variable name root attribute-name and its Under attribute-names at different levels;
First searching module for searching described attribute-name in predeterminated position, and is returned to dependent variable;
Second searching module for the type search next stage attribute-name according to the variable of return, and is returned to dependent variable; It repeats until finding the variable of leaf grade attribute-name return.
Optionally, described device further include:
Release module, for issuing the attribute resolver of the Python.
Optionally, described device further include:
Print module, the type of the variable for being returned according to leaf grade attribute-name return to the leaf grade attribute-name Variable print into display screen.
In embodiments of the present invention, using step 1, the variable name of user's input is read, and is parsed from the variable name Out root attribute-name and its under attribute-names at different levels;Step 2 is searched described attribute-name in predeterminated position, and is returned to dependent variable; Step 3 according to the type search next stage attribute-name of the variable of return, and is returned to dependent variable;Repeat the step Three, until finding the mode of the variable of leaf grade attribute-name return, parsed, is parsed by the variable name for inputting user Root attribute-name and attribute-names at different levels under it out first search root attribute-name in predeterminated position, then step by step according to the variable of return Attribute-name is searched, until finding the variable of leaf grade attribute-name return, user in Python kernel is inputted to realize The technical effect that is successfully parsed of various types of variable names, and then solve Python debugging plan in the related technology The technical issues of being unable to satisfy debugging demand.
Detailed description of the invention
The drawings described herein are used to provide a further understanding of the present invention, constitutes part of this application, this hair Bright illustrative embodiments and their description are used to explain the present invention, and are not constituted improper limitations of the present invention.In the accompanying drawings:
Fig. 1 is the flow chart of the attribute analytic method of Python according to an embodiment of the present invention;
Fig. 2 is the structural block diagram of the attribute resolver of Python according to an embodiment of the present invention;
Fig. 3 is the preferred structure block diagram of the attribute resolver of Python according to an embodiment of the present invention;
Fig. 4 is the integrated stand composition of mixed attributes parsing scheme according to the preferred embodiment of the invention;
Fig. 5 a is the exemplary diagram that Py-print according to the preferred embodiment of the invention supports attribute to search;
Fig. 5 b is the exemplary diagram that Py-print support module according to the preferred embodiment of the invention is searched;
Fig. 5 c is that Py-print according to the preferred embodiment of the invention supports the part of C stack frame to show with what global variable was searched Example diagram;
Fig. 5 d is that Py-print according to the preferred embodiment of the invention supports Python, the exemplary diagram that the mixing of C attribute is searched;
Fig. 6 a is the exemplary diagram of the compound python type printing of Py-print according to the preferred embodiment of the invention;
Fig. 6 b is the exemplary diagram of the compound c type printing of Py-print according to the preferred embodiment of the invention.
Specific embodiment
In order to enable those skilled in the art to better understand the solution of the present invention, below in conjunction in the embodiment of the present invention Attached drawing, technical scheme in the embodiment of the invention is clearly and completely described, it is clear that described embodiment is only The embodiment of a part of the invention, instead of all the embodiments.Based on the embodiments of the present invention, ordinary skill people The model that the present invention protects all should belong in member's every other embodiment obtained without making creative work It encloses.
It should be noted that description and claims of this specification and term " first " in above-mentioned attached drawing, " Two " etc. be to be used to distinguish similar objects, without being used to describe a particular order or precedence order.It should be understood that using in this way Data be interchangeable under appropriate circumstances, so as to the embodiment of the present invention described herein can in addition to illustrating herein or Sequence other than those of description is implemented.In addition, term " includes " and " having " and their any deformation, it is intended that cover Cover it is non-exclusive include, for example, the process, method, system, product or equipment for containing a series of steps or units are not necessarily limited to Step or unit those of is clearly listed, but may include be not clearly listed or for these process, methods, product Or other step or units that equipment is intrinsic.
Inventor has found above-mentioned second of debugging plan, and there are some fatal problems, cause debugging not smooth enough:
1. load is inconvenient, it can not accomplish to load once opening in gdb, usually want to input a pile order after program operation It could load.
2.py-print can all properties under automatic Recursion process variable, lead to print value usually because it is too long and can not It reads.
The seeking scope of 3.py-print only supports the locals under current thread current stack frame with globals, if adjusted Examination person wants to print some variable under certain module, does not accomplish.
4.py-print can not support the attribute of a.b type to parse printing.
5.py-print, if the value to be printed is a python variable for C extension, py-print can not be supported c Pure c variable therein is extended to print.Also the c variable on stack frame can not be supported to print, this, which allows for tuner, to track The variable's attribute chain for having C to extend is debugged.
6.py-print, to locals with globals variable search be using linear iteraction method, if wherein belonged to There are many property number, and will lead to lookup becomes very slow.
7.py-print can not handle getter, the attribute under the access of setter form.
Therefore, the present embodiment solves the problems, such as it, so that using gdb by being extended to above-mentioned second of debugging plan Debugging python kernel becomes possible.This method is to write plug-in unit with python language to be extended gdb, is enabled it to automatic Python types of variables is parsed, so as to correctly print the content of python variable, and the pure c/c of energy automatic identification context ++ variable makes it possible to mix debugging python variable with c/c++ variable, and real using the hash algorithm of the dict of python It is existing, accelerate these search procedures.
According to embodiments of the present invention, a kind of embodiment of the method for the attribute analytic method of Python is provided, needs to illustrate , step shown in the flowchart of the accompanying drawings can hold in a computer system such as a set of computer executable instructions Row, although also, logical order is shown in flow charts, and it in some cases, can be to be different from sequence herein Execute shown or described step.
Fig. 1 is the flow chart of the attribute analytic method of Python according to an embodiment of the present invention, as shown in Figure 1, this method Include the following steps:
Step S102, reads the variable name of user's input, and parses from the variable name root attribute-name and its lower each Grade attribute-name;
Step S104 searches described attribute-name in predeterminated position, and returns to dependent variable;
Step S106 according to the type search next stage attribute-name of the variable of return, and is returned to dependent variable;
The step S106 is repeated, until finding the variable of leaf grade attribute-name return.
Through the above steps, the variable name that user inputs is parsed, parses root attribute-name and categories at different levels under it Property name (is hereinafter mainly described so that the variable name for inputting user resolves to five levels a, b, c, d, e as an example, wherein a For root attribute-name, b is the next stage attribute-name of a, and c is the next stage attribute-name of b, and d is the next stage attribute-name of c, and e is under d Level-one attribute-name, and due to level-one attribute-name in the absence of e, then e is leaf grade attribute-name, kind layering that but not limited to this Grade mode), root attribute-name first is searched in predeterminated position, attribute-name is then searched according to the variable of return step by step, until finding The variable that leaf grade attribute-name returns.In this way, the various types inputted to user in Python kernel may be implemented Variable name successfully parsed, and then solve the skill that Python debugging plan in the related technology is unable to satisfy debugging demand Art problem.
Optionally, search described attribute-name in predeterminated position, and return may include to the particular content of dependent variable but It is not limited at least one of:
This earth's surface for reading current Python stack frame searches whether that there are described attribute-names, if there is described Attribute-name is then returned to dependent variable;
The local variable reference for reading current C stack frame, searches whether that there are described attribute-names, if there is described Attribute-name is then returned to dependent variable;
Global variable (the C_ of C must then be belonged to if there is described attribute-name by searching current visible symbol ), global it returns to dependent variable;
System module (sys.modules) table of Python is searched, if there is the corresponding module of described attribute-name, then Return to corresponding module variable.
Optionally, according to the type search next stage attribute-name of the variable of return, and the concrete mode to dependent variable is returned It can include but is not limited at least one of:
If a is the variable returned in previous step, b is the next stage attribute-name, then
When the type of the variable a of return is Python universal class, if the type contains _ _ dict__ attribute, by it It takes out, then searches the next stage attribute-name wherein, if it is found, returning to dependent variable, wherein _ _ dict__ is current The attribute dictionary of types of variables is an example of python.dict type;
When the type of a is Python universal class, if having member in the C-structure statement of Python universal class (members) it states, is then added address offset of the members in the Python universal class with the address a, and return Address after being added;
When the type of a be Python.list when, if Python universal class can not find the next stage attribute-name and The type of the next stage attribute-name is numeric type, then by the address ob_item in a plus the corresponding number of b and conduct One address returns;
When the type of a is Python.dict, if Python universal class can not find b, utilize The method that python.dict itself is provided calculates Hash (hash) value of b, is provided using the hash value with python.dict Hash lookup algorithm, search whether that b exists wherein, if it does, returning to corresponding value;
It is corresponding in a if Python universal class can not find b when the type of a is the C expansion type of Python C expansion type statement in search whether that there are b, if it does, by b offset+a (i.e. the address offset of b is added with the address a) Address return;
When the type of the variable a of return is general C-structure, the statement of C-structure is found, searches and wherein whether there is b, If it does, the address of b offset+a is returned;
When the type of a is C array, if b is number, b*size (a)+a is returned, wherein size (a) indicates a's Length;
When the type of a is std::vector, if b is number, by b*size (template (template) value class of a Type)+a element initial address return;
When the type of a is std::map, using the lookup method of red black tree, the father node (parent) of a, Zuo Jie are utilized Point (left), right node (right) attribute search whether there are b, if it does, return to dependent variable, wherein red black tree is looked into Looking for method is a kind of general lookup method, has the realization of java standard library in C++, therefore repeat no more herein;
When the type of a is std::tr1::unordered_map, the hash value of b is calculated, it is corresponding to find the hash value Bucket (bucket), search whether in the corresponding chained list of the bucket there are b, if it does, return to dependent variable.Its Middle bucket be hash search in a concept can be placed in a bucket if hash value is identical, that is, The identical variable of many hash values is housed in one bucket.
Optionally, the variable name of user's input is being read, and is parsing from the variable name root attribute-name and its lower each It further include the corresponding plug-in unit of attribute analytic method for issuing the Python before grade attribute-name, i.e. publication the present embodiment provides Gdb plug-in unit.
Wherein, the process of specific publication plug-in unit can be such that the attribute analytic method of the Python is corresponding slotting Part is copied under the root of gdb plug-in unit;Initialize the corresponding plug-in unit of attribute analytic method of the Python.
Optionally, it after the variable for finding the return of leaf grade attribute-name, can also be returned according to leaf grade attribute-name Variable type, by the leaf grade attribute-name return variable print into display screen.
In view of being typically only capable to check the address of printing computer program variable in gdb in python kernel, and Its value cannot be actually printed, it is very unreadable to the mankind, a kind of processing of method is given in the present embodiment in this case Printing python variable can correctly be mixed with the method for c/c++ variable, be conducive to the debugging of python language kernel significantly.This method At least one can specifically include but be not limited to following manner:
When the type for the variable that the leaf grade attribute-name returns is basic type, directly by the leaf grade attribute-name The variable of return is converted to readable character string type and prints to display screen;
When the type for the variable that the leaf grade attribute-name returns is the encapsulation to type of foundation, packed base is taken out Then the variable of type of foundation is converted to readable character string type and printed to display screen by plinth type;
It, will be all in the Container Type when the type for the variable that the leaf grade attribute-name returns is Container Type Element iteration is taken out, and stringification be stitched together after printing to display screen;
When the leaf grade attribute-name return variable type be general python.object (referred to as When pyobject), by its _ _ dict__ according to python.dict (abbreviation pydict) mode is parsed into after character string printing to aobvious Display screen, wherein _ _ dict__ is the attribute dictionary of current variable type, it is an example of python.dict type;
When the type for the variable that the leaf grade attribute-name returns is general c structure, by attribute all in C-structure It prints after stringification to display screen;
When the type for the variable that the leaf grade attribute-name returns is the type that can not be parsed, corresponding address is printed extremely Display screen.
According to embodiments of the present invention, a kind of Installation practice of the attribute analytic method of Python is additionally provided.Fig. 2 is root According to the structural block diagram of the attribute resolver of the Python of the embodiment of the present invention, as shown in Fig. 2, the device includes:
Parsing module 22, for reading the variable name of user's input, and parse from the variable name root attribute-name and Attribute-names at different levels under it;
First searching module 24, is connected with parsing module 22, for searching described attribute-name in predeterminated position, and returns To dependent variable;
Second searching module 26 is connected with the first searching module 24, under the type search according to the variable of return one Grade attribute-name, and return to dependent variable;It repeats until finding the variable of leaf grade attribute-name return.
By above-mentioned module, the variable name that user inputs is parsed, root attribute-name and categories at different levels under it are parsed Property name (is hereinafter mainly described so that the variable name for inputting user resolves to five levels a, b, c, d, e as an example, wherein a For root attribute-name, b is the next stage attribute-name of a, and c is the next stage attribute-name of b, and d is the next stage attribute-name of c, and e is under d Level-one attribute-name, and due to level-one attribute-name in the absence of e, then e is leaf grade attribute-name, kind layering that but not limited to this Grade mode), root attribute-name first is searched in predeterminated position, attribute-name is then searched according to the variable of return step by step, until finding The variable that leaf grade attribute-name returns.In this way, the various types inputted to user in Python kernel may be implemented Variable name successfully parsed, and then solve the skill that Python debugging plan in the related technology is unable to satisfy debugging demand Art problem.
Fig. 3 is the preferred structure block diagram of the attribute resolver of Python according to an embodiment of the present invention, as shown in figure 3, Optionally, described device can also include: release module 32, be connected with parsing module 22, for issuing the category of the Python Property resolver.
Refering to what is shown in Fig. 3, optionally, described device can also include: print module 34, with 26 phase of the second searching module Even, the type of the variable for being returned according to leaf grade attribute-name prints the variable that the leaf grade attribute-name returns to aobvious In display screen.
It is illustrated below with reference to preferred embodiment, preferred embodiment below combines above-described embodiment and its preferred implementation Mode.
In preferred embodiment below, it is extended, mentions for the function of current Python subsidiary gdb expansion plugin The gdb expansion plugin of new Python a kind of has been supplied, and the mixed attributes of Python a kind of are provided based on the gdb expansion plugin Parsing scheme.Fig. 4 is the integrated stand composition of mixed attributes parsing scheme according to the preferred embodiment of the invention, as shown in figure 4, should It include the module of plug-in unit publication, the module of mixed attributes lookup, the module and category of attribute type parsing in the framework of scheme Property printing module, wherein after three kinds of modules can be located in the gdb expansion plugin for the Python that this preferred embodiment provides.Under Function in face of modules is described in detail.
One, plug-in unit release module:
The process that the module is issued for realizing plug-in unit, wherein the process of plug-in unit publication is made of following steps:
1. plug-in unit python_gdb.py is copied under the root of gdb plug-in unit: namely $ HOME/.gdb/python_ gdb.py。
2. opening $ HOME/.gdbinit file, if there is no then creating this document, and it is set to 0x744 category Property.It is inserted into following one section of plug-in unit under this document and starts code automatically:
python
import sys
sys.path.insert(0,“$HOME/.gdb/”)
import python_gdb
end
Two, mixed attributes searching module and attribute type parsing module:
Above-mentioned module is respectively used to realize mixed attributes lookup and attribute type analytic method.Mixed attributes are searched and are belonged to The implementation process of property type analytic method is as follows:
1. reading the variable name of the a.b.c.d.e form of user's input, root name a is taken out, and execute following sub-step:
A) the locals table for reading current python stack frame searches whether, if there is a, to return to strain there are a Amount.
B) the local variable reference for reading current C stack frame searches whether, if there is a, to return to dependent variable there are a.
C) current visible symbol is searched, if there is a, then must belong to c_global, return to dependent variable.
D) the sys.modules table for searching python returns to a if there is a module.
2. if returning step 1 does not find a and searching failure, and terminate program, otherwise carry out step 3.
3. parsing the type of a, and lookup method appropriate is distributed for it, carries out the lookup of its properties b, distribution is looked into Method is looked for be divided into according to a type following several:
A) it Python universal class: if the type contains _ _ dict__ attribute, is drawn off, then searches b wherein Attribute, if it is found, the value is returned, wherein _ _ dict__ is a python.dict type, it will behind such lookup It can describe.If having members statement in the C-structure statement of the type, address of the members in the type is found Offset, which is added with the address a, returns to the address.
B) Python.list: if Python universal class can not find b, and b is a numeric type, then will be in a The address ob_item take out, by it plus one address of conduct return of number b.
C) it Python.dict: if Python universal class can not find b, is provided using python.dict itself Method calculates the hash value h of b, and the hash lookup algorithm provided using h with python.dict searches whether that b exists wherein, such as Fruit exists, and returns to the value.
D) the c expansion type of Python: if Python universal class can not find b, and it is understood that the corresponding c of a Expansion type statement is attempted to look in its c statement with the presence or absence of b, if so, the address of its offset+a is returned.
E) general C-structure: finding the statement of C-structure, and whether wherein have b, if so, by the address of its offset+a if searching It returns.
F) C array: if b is a number, b*size (a)+a is returned.
G) std::vector: if b is a number, by the element starting point of b*size (the template Value Types of a)+a Location returns to (being a._M_impl._M_start in libstdc++).
H) std::map: using the lookup method of red black tree, using the parent of a, (libstdc++ works as by left, right In be _ M_parent, _ M_left, _ M_right) attribute searches whether that there are b attributes, if so, then returning.
I) std::tr1::unordered_map: the hash value h of b is calculated, the corresponding bucket of h is found, in the bucket B has been searched whether in corresponding chained list, if so, returning to the value.
J) it other types: can support to extend, it is only necessary to realize member (findmember) method of lookup, wherein Findmember refers to the method that expansion type needs to realize, for searching some member variable under current variable.
It is searched by above-mentioned type attribute, if not finding b, returns to lookup failure, terminate program, otherwise continue C is searched in b, and so on, until finding leaf attribute e, as the input of next step.
Three, attribute print module
The module is for realizing the printing for the above-mentioned attribute found, i.e., using the attribute e finally found as people The readable mode of class prints in computer screen, different according to the type of e, is divided into following several method:
The type of foundation such as 1.int, char*, float, double: readable word directly is converted into using snprintf Accord with string type.
The encapsulation of 2.pyint, pystring, pyfloat, std::string to type of foundation: taking out the type of encapsulation, Execute the process in 1.
3.pylist, pydict, std::vector, std::map, std::tr1::unordered_map: these are to hold Device type takes out its all element iteration, and stringification splicing returns together.
4. general pyobject: by its _ _ dict__ according to pydict mode be parsed into character string return, if there is Members statement, is also returned to.
5. general c struct: its all propertystringization is returned.
6. other types: parsing can be added in the type that can not be parsed at any time, or only prints corresponding address.
The use example of the plug-in unit provided using this preferred embodiment is provided below by the screenshotss in practical debugging process:
Fig. 5 a is the exemplary diagram that Py-print according to the preferred embodiment of the invention supports attribute to search.Fig. 5 b is according to this The exemplary diagram that the Py-print support module of invention preferred embodiment is searched.Fig. 5 c is Py- according to the preferred embodiment of the invention The exemplary diagram that print supports the part of C stack frame to search with global variable.Fig. 5 d is Py- according to the preferred embodiment of the invention Print supports Python, the exemplary diagram that the mixing of C attribute is searched.Fig. 6 a is Py-print according to the preferred embodiment of the invention multiple Close the exemplary diagram of python type printing.Fig. 6 b is the compound c type printing of Py-print according to the preferred embodiment of the invention Exemplary diagram.Pass through the above-mentioned exemplary diagram provided, it is seen that the gdb expansion plugin of the new Python provided in this preferred embodiment, It can be realized the parsing of Python attribute and mixing is searched and printing function, and then solve Python debugging in the related technology Scheme is unable to satisfy the technical issues of debugging demand.
The serial number of the above embodiments of the invention is only for description, does not represent the advantages or disadvantages of the embodiments.
In the above embodiment of the invention, it all emphasizes particularly on different fields to the description of each embodiment, does not have in some embodiment The part of detailed description, reference can be made to the related descriptions of other embodiments.
In several embodiments provided herein, it should be understood that disclosed technology contents can pass through others Mode is realized.Wherein, the apparatus embodiments described above are merely exemplary, such as the division of the unit, Ke Yiwei A kind of logical function partition, there may be another division manner in actual implementation, for example, multiple units or components can combine or Person is desirably integrated into another system, or some features can be ignored or not executed.Another point, shown or discussed is mutual Between coupling, direct-coupling or communication connection can be through some interfaces, the INDIRECT COUPLING or communication link of unit or module It connects, can be electrical or other forms.
The unit as illustrated by the separation member may or may not be physically separated, aobvious as unit The component shown may or may not be physical unit, it can and it is in one place, or may be distributed over multiple On unit.It can some or all of the units may be selected to achieve the purpose of the solution of this embodiment according to the actual needs.
It, can also be in addition, the functional units in various embodiments of the present invention may be integrated into one processing unit It is that each unit physically exists alone, can also be integrated in one unit with two or more units.Above-mentioned integrated list Member both can take the form of hardware realization, can also realize in the form of software functional units.
If the integrated unit is realized in the form of SFU software functional unit and sells or use as independent product When, it can store in a computer readable storage medium.Based on this understanding, technical solution of the present invention is substantially The all or part of the part that contributes to existing technology or the technical solution can be in the form of software products in other words It embodies, which is stored in a storage medium, including some instructions are used so that a computer Equipment (can for personal computer, server or network equipment etc.) execute each embodiment the method for the present invention whole or Part steps.And storage medium above-mentioned includes: that USB flash disk, read-only memory (ROM, Read-Only Memory), arbitrary access are deposited Reservoir (RAM, Random Access Memory), mobile hard disk, magnetic or disk etc. be various to can store program code Medium.
The above is only a preferred embodiment of the present invention, it is noted that for the ordinary skill people of the art For member, various improvements and modifications may be made without departing from the principle of the present invention, these improvements and modifications are also answered It is considered as protection scope of the present invention.

Claims (10)

1. the attribute analytic method of Python a kind of characterized by comprising
Step 1, read user input variable name, and from parsed in the variable name root attribute-name and its under attributes at different levels Name;
Step 2 is searched described attribute-name in predeterminated position, and is returned to dependent variable;
Step 3 according to the type search next stage attribute-name of the variable of return, and is returned to dependent variable;
The step 3 is repeated, until the variable of leaf grade attribute-name return is found,
Wherein, described attribute-name is searched in predeterminated position, and returning includes at least one of to dependent variable: read current This earth's surface of Python stack frame is searched whether there are described attribute-name, if there is described attribute-name, is then returned to strain Amount;The local variable reference for reading current C stack frame, searches whether there are described attribute-name, if there is described attribute Name is then returned to dependent variable.
2. the method according to claim 1, wherein searching described attribute-name, and return pair in predeterminated position Dependent variable further includes at least one of:
The global variable C_global of C must then be belonged to, return if there is described attribute-name by searching current visible symbol It returns to dependent variable;
The system module sys.modules table for searching Python, if there is the corresponding module of described attribute-name, then return pair The module variable answered.
3. the method according to claim 1, wherein according to the type search next stage attribute of the variable of return Name, and returning includes at least one of to dependent variable:
If a is the variable returned in previous step, b is the next stage attribute-name, then
When the type of a is Python universal class, if the type contains _ _ dict__ attribute, it is drawn off, then at it It is middle to search the next stage attribute-name, if it is found, returning to dependent variable, wherein _ _ dict__ is the attribute of current variable type Dictionary;
When the type of a is Python universal class, if having member in the C-structure statement of Python universal class Address offset of the members in the Python universal class, then be added, and return to phase by members statement with the address a Address after adding;
When the type of a is Python.list, if Python universal class can not find the next stage attribute-name and described The type of next stage attribute-name is numeric type, then by the address ob_item in a plus the corresponding number of b and as one Address returns;
When the type of a is Python.dict, if Python universal class can not find b, python.dict sheet is utilized The Hash hash value for the method calculating b that body provides, the hash lookup algorithm provided using the hash value with python.dict, Search whether that b exists wherein, if it does, returning to corresponding value;
When the type of a is the C expansion type of Python, if Python universal class can not find b, expand in the corresponding C of a It searches whether that there are b in exhibition type declarations, if it does, the address offset of b is added with the address a, and returns after being added Address;
When the type of a is general C-structure, the statement of C-structure is found, searches and wherein whether there is b, if it does, by b's Address offset is added with the address a, and returns to address after being added;
When the type of a is C array, if b is number, b*size (a)+a is returned, wherein size (a) indicates the length of a Degree;
When the type of a is std::vector, if b is number, by b*size (the template template Value Types of a) and a The element initial address that address is added returns;
When the type of a is std::map, using the lookup method of red black tree, using the parent of a, left, right attribute is looked into It looks for the presence or absence of b, if it does, returning to dependent variable;
When the type of a is std::tr1::unordered_map, the hash value of b is calculated, it is corresponding to find the hash value Bucket is searched whether in the corresponding chained list of the bucket there are b, if it does, returning to dependent variable.
4. the method according to claim 1, wherein in the variable name for reading user's input, and from the variable Parsed in name root attribute-name and its under before attribute-name at different levels, further includes:
Issue the corresponding plug-in unit of attribute analytic method of the Python.
5. according to the method described in claim 4, it is characterized in that, the attribute analytic method for issuing the Python is corresponding slotting Part includes:
The corresponding plug-in unit of attribute analytic method of the Python is copied under the root of gdb plug-in unit;
Initialize the corresponding plug-in unit of attribute analytic method of the Python.
6. the method according to claim 1, wherein find leaf grade attribute-name return variable after, Further include:
According to the type for the variable that leaf grade attribute-name returns, the variable that the leaf grade attribute-name returns is printed to display screen In.
7. according to the method described in claim 6, it is characterized in that, the type of the variable returned according to leaf grade attribute-name, is incited somebody to action It includes at least one of that the variable that the leaf grade attribute-name returns, which is printed into display screen:
When the type for the variable that the leaf grade attribute-name returns is basic type, directly the leaf grade attribute-name is returned Variable be converted to readable character string type and print to display screen;
When the type for the variable that the leaf grade attribute-name returns is the encapsulation to type of foundation, packed foundation class is taken out Then the variable of type of foundation is converted to readable character string type and printed to display screen by type;
When the type for the variable that the leaf grade attribute-name returns is Container Type, by element all in the Container Type Iteration is taken out, and stringification be stitched together after printing to display screen;
When the leaf grade attribute-name return variable type be general python.object when, by its _ _ dict__ belong to Property be parsed into after character string that printing is to display screen according to python.dict mode, wherein _ _ dict__ is current variable type Attribute dictionary;
When the type for the variable that the leaf grade attribute-name returns is general c structure, by attribute character all in C-structure It prints after stringization to display screen;
When the type for the variable that the leaf grade attribute-name returns is the type that can not be parsed, corresponding address is printed to display Screen.
8. the attribute resolver of Python a kind of characterized by comprising
Parsing module for reading the variable name of user's input, and parses from the variable name root attribute-name and its lower each Grade attribute-name;
First searching module for searching described attribute-name in predeterminated position, and is returned to dependent variable;
Second searching module for the type search next stage attribute-name according to the variable of return, and is returned to dependent variable;It repeats The variable up to finding the return of leaf grade attribute-name is executed,
Wherein, described attribute-name is searched in predeterminated position, and returning includes at least one of to dependent variable: read current This earth's surface of Python stack frame is searched whether there are described attribute-name, if there is described attribute-name, is then returned to strain Amount;The local variable reference for reading current C stack frame, searches whether there are described attribute-name, if there is described attribute Name is then returned to dependent variable.
9. device according to claim 8, which is characterized in that described device further include:
Release module, for issuing the attribute resolver of the Python.
10. device according to claim 8, which is characterized in that described device further include:
Print module, the type of the variable for being returned according to leaf grade attribute-name, the change that the leaf grade attribute-name is returned Amount printing is into display screen.
CN201610080212.4A 2016-02-04 2016-02-04 The attribute analysis method and device of Python Active CN105760161B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610080212.4A CN105760161B (en) 2016-02-04 2016-02-04 The attribute analysis method and device of Python

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610080212.4A CN105760161B (en) 2016-02-04 2016-02-04 The attribute analysis method and device of Python

Publications (2)

Publication Number Publication Date
CN105760161A CN105760161A (en) 2016-07-13
CN105760161B true CN105760161B (en) 2019-01-08

Family

ID=56330633

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610080212.4A Active CN105760161B (en) 2016-02-04 2016-02-04 The attribute analysis method and device of Python

Country Status (1)

Country Link
CN (1) CN105760161B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107678974A (en) * 2017-11-02 2018-02-09 郑州云海信息技术有限公司 A kind of method and device of debugging Python scripts
CN107861725B (en) * 2017-11-22 2020-12-22 北京酷我科技有限公司 iOS data reverse automatic analysis strategy
CN110737412A (en) * 2019-09-30 2020-01-31 北京奇艺世纪科技有限公司 information output method, device, electronic equipment and storage medium

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102662829A (en) * 2012-03-16 2012-09-12 北京邮电大学 Processing method and apparatus for complex data structure in code static state testing
US8839193B1 (en) * 2011-06-06 2014-09-16 The Mathworks, Inc. Control surfaces for a technical computing environment

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8839193B1 (en) * 2011-06-06 2014-09-16 The Mathworks, Inc. Control surfaces for a technical computing environment
CN102662829A (en) * 2012-03-16 2012-09-12 北京邮电大学 Processing method and apparatus for complex data structure in code static state testing

Also Published As

Publication number Publication date
CN105760161A (en) 2016-07-13

Similar Documents

Publication Publication Date Title
Olsen Systems engineering using SDL-92
CN105913316B (en) Rule configuration method and device
CN101482817B (en) Large-particle Java component assembly method based on black box
CN106371825A (en) Dynamic form generation method and apparatus for mobile end application interface
CN104615462B (en) Cross-platform Mobile solution generation service end and system
CN102855178A (en) Method and device for generating Mock base during unit test
CN103605518A (en) Object deserialization method and device
CN105760161B (en) The attribute analysis method and device of Python
CN109145235B (en) Method and device for analyzing webpage and electronic equipment
CN102982010A (en) Method and device for abstracting document structure
WO2008099393A2 (en) Service provisioning system
CN104050085A (en) Forced code standard inspection method and system
CN102981882B (en) Analytic method and device
CN102622444A (en) XML (extensible markup language) message processing method and XML message processing device
CN105320681A (en) Database content combination method and device
CN107301347A (en) A kind of method that Hbuilder App calling figures are obtained based on static analysis
CN109739704A (en) A kind of interface test method, server-side and computer readable storage medium
CN111159040A (en) Test data generation method, device, equipment and storage medium
CN110851123A (en) WebGIS power grid visualization framework construction method, system and device based on SpringMVC
CN106126299A (en) The processing method and processing device of business plug-in unit
CN101908174A (en) Method, device and EAP (Enterprise Application Platform) system for processing part object information in drawing file
CN103678687B (en) Establishment of item method and device based on configuration system
Berkeley et al. Software development project management: process and support
CN104050264A (en) Method and device for generating SQL statement
CN101272222A (en) Restriction calibration method and device

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant