Skip to content

Commit e2b446d

Browse files
author
Shati Patel
committed
Docs: Update Python
1 parent 3337eaf commit e2b446d

File tree

7 files changed

+50
-58
lines changed

7 files changed

+50
-58
lines changed

docs/language/learn-ql/python/control-flow.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Tutorial: Control flow analysis
22
===============================
33

4-
In order to analyze the `Control-flow graph <http://en.wikipedia.org/wiki/Control_flow_graph>`__ of a ``Scope`` we can use the two QL classes ``ControlFlowNode`` and ``BasicBlock``. These classes allow you to ask such questions as "can you reach point A from point B?" or "Is it possible to reach point B *without* going through point A?". To report results we use the class ``AstNode``, which represents a syntactic element and corresponds to the source code - allowing the results of the query to be more easily understood.
4+
To analyze the `Control-flow graph <http://en.wikipedia.org/wiki/Control_flow_graph>`__ of a ``Scope`` we can use the two CodeQL classes ``ControlFlowNode`` and ``BasicBlock``. These classes allow you to ask such questions as "can you reach point A from point B?" or "Is it possible to reach point B *without* going through point A?". To report results we use the class ``AstNode``, which represents a syntactic element and corresponds to the source code - allowing the results of the query to be more easily understood.
55

66
The ``ControlFlowNode`` class
77
-----------------------------
88

9-
The ``ControlFlowNode`` class represents nodes in the control flow graph. There is a one-to-many relation between AST nodes and control flow nodes. Each syntactic element, the ``AstNode,`` maps to zero, one or many ``ControlFlowNode`` classes, but each ControlFlowNode maps to exactly one ``AstNode``.
9+
The ``ControlFlowNode`` class represents nodes in the control flow graph. There is a one-to-many relation between AST nodes and control flow nodes. Each syntactic element, the ``AstNode,`` maps to zero, one, or many ``ControlFlowNode`` classes, but each ``ControlFlowNode`` maps to exactly one ``AstNode``.
1010

1111
To show why this complex relation is required consider the following Python code:
1212

@@ -21,7 +21,7 @@ To show why this complex relation is required consider the following Python code
2121
2222
There are many paths through the above code. There are three different paths through the call to ``close_resource();`` one normal path, one path that breaks out of the loop, and one path where an exception is raised by ``might_raise()``. (An annotated flow graph can be seen :doc:`here <control-flow-graph>`.)
2323

24-
The simplest use of the ``ControlFlowNode`` and ``AstNode`` classes is to find unreachable code. There is one ``ControlFlowNode`` per path through any ``AstNode`` and any ``AstNode`` that is unreachable has no paths flowing through it; therefore any ``AstNode`` without a corresponding ``ControlFlowNode`` is unreachable.
24+
The simplest use of the ``ControlFlowNode`` and ``AstNode`` classes is to find unreachable code. There is one ``ControlFlowNode`` per path through any ``AstNode`` and any ``AstNode`` that is unreachable has no paths flowing through it. Therefore, any ``AstNode`` without a corresponding ``ControlFlowNode`` is unreachable.
2525

2626
**Unreachable AST nodes**
2727

@@ -103,5 +103,5 @@ Combining these conditions we get:
103103
What next?
104104
----------
105105

106-
- Experiment with the worked examples in the QL for Python tutorial topic: :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
106+
- Experiment with the worked examples in the tutorial topic :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
107107
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.

docs/language/learn-ql/python/functions.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Tutorial: Functions
22
===================
33

4-
This example uses the standard QL class ``Function`` (see :doc:`Introducing the Python libraries <introduce-libraries-python>`).
4+
This example uses the standard CodeQL class ``Function`` (see :doc:`Introducing the Python libraries <introduce-libraries-python>`).
55

66
Finding all functions called "get..."
77
-------------------------------------
88

99
In this example we look for all the "getters" in a program. Programmers moving to Python from Java are often tempted to write lots of getter and setter methods, rather than use properties. We might want to find those methods.
1010

11-
Using the member predicate ``Function.getName()``, we can list all of the getter functions in a snapshot:
11+
Using the member predicate ``Function.getName()``, we can list all of the getter functions in a database:
1212

1313
Tip
1414

@@ -77,5 +77,5 @@ In a later tutorial we will see how to use the type-inference library to find ca
7777
What next?
7878
----------
7979

80-
- Experiment with the worked examples in the QL for Python tutorial topics: :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>`.
80+
- Experiment with the worked examples in the following tutorial topics: :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, and :doc:`Points-to analysis and type inference <pointsto-type-infer>`.
8181
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.

docs/language/learn-ql/python/introduce-libraries-python.rst

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
Introducing the QL libraries for Python
2-
=======================================
1+
Introducing the CodeQL libraries for Python
2+
===========================================
33

4-
These libraries have been created to help you analyze Python code, providing an object-oriented layer on top of the raw data in the snapshot database. They are written in standard QL.
5-
6-
The QL libraries all have a ``.qll`` extension, to signify that they contain QL library code but no actual queries. Each file contains a QL class or hierarchy of classes.
7-
8-
You can include all of the standard libraries by beginning each query with this statement:
4+
There is an extensive library for analyzing CodeQL databases extracted from Python projects. The classes in this library present the data from a database in an object-oriented form and provide abstractions and predicates to help you with common analysis tasks. The library is implemented as a set of QL modules, that is, files with the extension ``.qll``. The module ``python.qll`` imports all the core Python library modules, so you can include the complete library by beginning your query with:
95

106
.. code-block:: ql
117
128
import python
139
14-
The rest of this tutorial summarizes the contents of the standard QL libraries. We recommend that you read this and then work through the practical examples in the Python tutorials shown at the end of the page.
10+
The rest of this tutorial summarizes the contents of the standard libraries for Python. We recommend that you read this and then work through the practical examples in the tutorials shown at the end of the page.
1511

1612
Overview of the library
1713
-----------------------
1814

19-
The QL Python library incorporates a large number of classes, each class corresponding either to one kind of entity in Python source code or to an entity that can be derived form the source code using static analysis. These classes can be divided into four categories:
15+
The CodeQL library for Python incorporates a large number of classes. Each class corresponds either to one kind of entity in Python source code or to an entity that can be derived form the source code using static analysis. These classes can be divided into four categories:
2016

2117
- **Syntactic** - classes that represent entities in the Python source code.
2218
- **Control flow** - classes that represent entities from the control flow graphs.
2319
- **Type inference** - classes that represent the inferred values and types of entities in the Python source code.
24-
- **Taint tracking** - classes that represent the source, sinks and kinds of taint used to implement taint-tracking queries.
20+
- **Taint tracking** - classes that represent the source, sinks and kinds of taint used to implement taint-tracking queries.
2521

2622
Syntactic classes
2723
~~~~~~~~~~~~~~~~~
2824

29-
This part of the library represents the Python source code. The ``Module``, ``Class`` and ``Function`` classes correspond to Python modules, classes and functions respectively, collectively these are known as ``Scope`` classes. Each ``Scope`` contains a list of statements each of which is represented by a subclass of the class ``Stmt``. Statements themselves can contain other statements or expressions which are represented by subclasses of ``Expr``. Finally, there are a few additional classes for the parts of more complex expressions such as list comprehensions. Collectively these classes are subclasses of ``AstNode`` and form an `Abstract syntax tree <http://en.wikipedia.org/wiki/Abstract_syntax_tree>`__ (AST). The root of each AST is a ``Module``.
25+
This part of the library represents the Python source code. The ``Module``, ``Class``, and ``Function`` classes correspond to Python modules, classes, and functions respectively, collectively these are known as ``Scope`` classes. Each ``Scope`` contains a list of statements each of which is represented by a subclass of the class ``Stmt``. Statements themselves can contain other statements or expressions which are represented by subclasses of ``Expr``. Finally, there are a few additional classes for the parts of more complex expressions such as list comprehensions. Collectively these classes are subclasses of ``AstNode`` and form an `Abstract syntax tree <http://en.wikipedia.org/wiki/Abstract_syntax_tree>`__ (AST). The root of each AST is a ``Module``.
3026

3127
`Symbolic information <http://en.wikipedia.org/wiki/Symbol_table>`__ is attached to the AST in the form of variables (represented by the class ``Variable``).
3228

3329
Scope
3430
^^^^^
3531

36-
A Python program is a group of modules. Technically a module is just a list of statements, but we often think of it as composed of classes and functions. These top-level entities, the module, class and function are represented by the three classes (`Module <https://help.semmle.com/qldoc/python/semmle/python/Module.qll/type.Module$Module.html>`__, `Class <https://help.semmle.com/qldoc/python/semmle/python/Class.qll/type.Class$Class.html>`__ and `Function <https://help.semmle.com/qldoc/python/semmle/python/Function.qll/type.Function$Function.html>`__ which are all subclasses of ``Scope``.
32+
A Python program is a group of modules. Technically a module is just a list of statements, but we often think of it as composed of classes and functions. These top-level entities, the module, class, and function are represented by the three CodeQL classes (`Module <https://help.semmle.com/qldoc/python/semmle/python/Module.qll/type.Module$Module.html>`__, `Class <https://help.semmle.com/qldoc/python/semmle/python/Class.qll/type.Class$Class.html>`__ and `Function <https://help.semmle.com/qldoc/python/semmle/python/Function.qll/type.Function$Function.html>`__ which are all subclasses of ``Scope``.
3733

3834
- ``Scope``
3935

@@ -65,7 +61,7 @@ A statement is represented by the `Stmt <https://help.semmle.com/qldoc/python/se
6561
else:
6662
return 0
6763
68-
The QL `For <https://help.semmle.com/qldoc/python/semmle/python/Stmts.qll/type.Stmts$For.html>`__ class representing the ``for`` statement has a number of member predicates to access its parts:
64+
The `For <https://help.semmle.com/qldoc/python/semmle/python/Stmts.qll/type.Stmts$For.html>`__ class representing the ``for`` statement has a number of member predicates to access its parts:
6965

7066
- ``getTarget()`` returns the ``Expr`` representing the variable ``var``.
7167
- ``getIter()`` returns the ``Expr`` resenting the variable ``seq``.
@@ -98,7 +94,7 @@ As an example, to find expressions of the form ``a+2`` where the left is a simpl
9894
Variable
9995
^^^^^^^^
10096

101-
Variables are represented by the `Variable <https://help.semmle.com/qldoc/python/semmle/python/Variables.qll/type.Variables$Variable.html>`__ class in the Python QL library. There are two subclasses, ``LocalVariable`` for function-level and class-level variables and ``GlobalVariable`` for module-level variables.
97+
Variables are represented by the `Variable <https://help.semmle.com/qldoc/python/semmle/python/Variables.qll/type.Variables$Variable.html>`__ class in the CodeQL library. There are two subclasses, ``LocalVariable`` for function-level and class-level variables and ``GlobalVariable`` for module-level variables.
10298

10399
Other source code elements
104100
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -108,7 +104,7 @@ Although the meaning of the program is encoded by the syntactic elements, ``Scop
108104
Examples
109105
^^^^^^^^
110106

111-
Each syntactic element in Python source is recorded in the snapshot. These can be queried via the corresponding class. Let us start with a couple of simple examples.
107+
Each syntactic element in Python source is recorded in the CodeQL database. These can be queried via the corresponding class. Let us start with a couple of simple examples.
112108

113109
1. Finding all finally blocks
114110
'''''''''''''''''''''''''''''
@@ -137,13 +133,13 @@ A block that does nothing is one that contains no statements except ``pass`` sta
137133
138134
not exists(Stmt s | s = ex.getAStmt() | not s instanceof Pass)
139135
140-
where ``ex`` is an ``ExceptStmt`` and ``Pass`` is the class representing ``pass`` statements. Instead of using the double negative, **"no**\ *statements that are*\ **not**\ *pass statements"*, this can also be expressed positively, "all statements must be pass statements." The positive form is expressed in QL using the ``forall`` quantifier:
136+
where ``ex`` is an ``ExceptStmt`` and ``Pass`` is the class representing ``pass`` statements. Instead of using the double negative, "**no** \ *statements that are* \ **not** \ *pass statements"*, this can also be expressed positively, *"all statements must be pass statements."* The positive form is expressed using the ``forall`` quantifier:
141137

142138
.. code-block:: ql
143139
144140
forall(Stmt s | s = ex.getAStmt() | s instanceof Pass)
145141
146-
Both forms are equivalent. Using the positive QL expression, the whole query looks like this:
142+
Both forms are equivalent. Using the positive expression, the whole query looks like this:
147143

148144
**Find pass-only ``except`` blocks**
149145

@@ -160,9 +156,9 @@ Both forms are equivalent. Using the positive QL expression, the whole query loo
160156
Summary
161157
^^^^^^^
162158

163-
The most commonly used standard QL library classes in the syntactic part of the library are organized as follows:
159+
The most commonly used standard classes in the syntactic part of the library are organized as follows:
164160

165-
``Module``, ``Class``, ``Function``, ``Stmt`` and ``Expr`` - they are all subclasses of `AstNode <https://help.semmle.com/qldoc/python/semmle/python/AST.qll/type.AST$AstNode.html>`__.
161+
``Module``, ``Class``, ``Function``, ``Stmt``, and ``Expr`` - they are all subclasses of `AstNode <https://help.semmle.com/qldoc/python/semmle/python/AST.qll/type.AST$AstNode.html>`__.
166162

167163
Abstract syntax tree
168164
''''''''''''''''''''
@@ -293,7 +289,7 @@ The classes in the control-flow part of the library are:
293289
Type-inference classes
294290
----------------------
295291

296-
The QL library for Python also supplies some classes for accessing the inferred types of values. The classes ``Value`` and ``ClassValue`` allow you to query the possible classes that an expression may have at runtime. For example, which ``ClassValue``\ s are iterable can be determined using the query:
292+
The CodeQL library for Python also supplies some classes for accessing the inferred types of values. The classes ``Value`` and ``ClassValue`` allow you to query the possible classes that an expression may have at runtime. For example, which ``ClassValue``\ s are iterable can be determined using the query:
297293

298294
**Find iterable "ClassValue"s**
299295

@@ -321,7 +317,7 @@ These classes are explained in more detail in :doc:`Tutorial: Points-to analysis
321317
Taint-tracking classes
322318
----------------------
323319

324-
The QL library for Python also supplies classes to specify taint-tracking analyses. The ``Configuration`` class can be overrridden to specify a taint-tracking analysis, by specifying source, sinks, sanitizers and additional flow steps. For those analyses that require additional types of taint to be tracked the ``TaintKind`` class can be overridden.
320+
The CodeQL library for Python also supplies classes to specify taint-tracking analyses. The ``Configuration`` class can be overridden to specify a taint-tracking analysis, by specifying source, sinks, sanitizers and additional flow steps. For those analyses that require additional types of taint to be tracked the ``TaintKind`` class can be overridden.
325321

326322

327323
Summary
@@ -336,5 +332,5 @@ These classes are explained in more detail in :doc:`Tutorial: Taint tracking and
336332
What next?
337333
----------
338334

339-
- Experiment with the worked examples in the QL for Python tutorial topics: :doc:`Functions <functions>`, :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>` and :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
335+
- Experiment with the worked examples in the following tutorial topics: :doc:`Functions <functions>`, :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>`, and :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
340336
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.

docs/language/learn-ql/python/pointsto-type-infer.rst

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Tutorial: Points-to analysis and type inference
22
===============================================
33

4-
This topic contains worked examples of how to write queries using the standard QL library classes for Python type inference.
4+
This topic contains worked examples of how to write queries using the standard CodeQL library classes for Python type inference.
55

66
The ``Value`` class
77
--------------------
88

9-
The ``Value`` class and its subclasses ``FunctionValue``, ``ClassValue`` and ``ModuleValue`` represent the values an expression may hold at runtime.
9+
The ``Value`` class and its subclasses ``FunctionValue``, ``ClassValue``, and ``ModuleValue`` represent the values an expression may hold at runtime.
1010

1111
Summary
1212
~~~~~~~
@@ -201,7 +201,7 @@ There are two problems with this query:
201201
- It assumes that any call to something named "eval" is a call to the builtin ``eval`` function, which may result in some false positive results.
202202
- It assumes that ``eval`` cannot be referred to by any other name, which may result in some false negative results.
203203

204-
We can get much more accurate results using call-graph analysis. First, we can precisely identify the ``FunctionValue`` for the ``eval`` function, by using the ``Value::named`` QL predicate as follows:
204+
We can get much more accurate results using call-graph analysis. First, we can precisely identify the ``FunctionValue`` for the ``eval`` function, by using the ``Value::named`` predicate as follows:
205205

206206
.. code-block:: ql
207207
@@ -227,9 +227,5 @@ Then we can use ``Value.getACall()`` to identify calls to the ``eval`` function,
227227
What next?
228228
----------
229229

230-
For more information on writing QL, see:
231-
232-
- `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ - an introduction to the concepts of QL.
233-
- :doc:`Learning QL <../../index>` - an overview of the resources for learning how to write your own QL queries.
234-
- `Database generation <https://lgtm.com/help/lgtm/generate-database>`__ - an overview of the process that creates a database from source code.
235-
- :doc:`What's in a CodeQL database? <../database>` - a description of the CodeQL database.
230+
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
231+
- Read a description of the CodeQL database in :doc:`What's in a CodeQL database? <../database>`

0 commit comments

Comments
 (0)