Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a81bf72

Browse files
committed
Python: Modernise the py/not-named-self query.
1 parent c6d9eb9 commit a81bf72

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

python/ql/src/Functions/NonSelf.ql

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,37 @@
1616
import python
1717
import semmle.python.libraries.Zope
1818

19-
predicate first_arg_self(Function f) {
20-
f.getArgName(0) = "self"
19+
predicate is_type_method(FunctionValue fv) {
20+
exists(ClassValue c | c.declaredAttribute(_) = fv and c.getASuperType() = ClassValue::type())
2121
}
2222

23-
predicate is_type_method(FunctionObject f) {
24-
exists(ClassObject c | c.lookupAttribute(_) = f and c.getASuperType() = theTypeType())
25-
}
26-
27-
predicate used_in_defining_scope(FunctionObject f) {
28-
exists(Call c |
29-
c.getScope() = f.getFunction().getScope() and
30-
c.getFunc().refersTo(f)
23+
predicate used_in_defining_scope(FunctionValue fv) {
24+
exists(Call c |
25+
c.getScope() = fv.getScope().getScope() and c.getFunc().pointsTo(fv)
3126
)
3227
}
3328

34-
from Function f, PyFunctionObject func, string message
29+
from Function f, FunctionValue fv, string message
3530
where
36-
exists(ClassObject cls, string name |
37-
cls.declaredAttribute(name) = func and cls.isNewStyle() and
31+
exists(ClassValue cls, string name |
32+
cls.declaredAttribute(name) = fv and cls.isNewStyle() and
3833
not name = "__new__" and
3934
not name = "__metaclass__" and
4035
/* declared in scope */
41-
f.getScope() = cls.getPyClass()
36+
f.getScope() = cls.getScope()
4237
) and
43-
not first_arg_self(f) and not is_type_method(func) and
44-
func.getFunction() = f and not f.getName() = "lambda" and
45-
not used_in_defining_scope(func) and
38+
not f.getArgName(0) = "self" and
39+
not is_type_method(fv) and
40+
fv.getScope() = f and
41+
not f.getName() = "lambda" and
42+
not used_in_defining_scope(fv) and
4643
(
4744
if exists(f.getArgName(0)) then
4845
message = "Normal methods should have 'self', rather than '" + f.getArgName(0) + "', as their first parameter."
4946
else
50-
message = "Normal methods should have at least one parameter (the first of which should be 'self')." and not f.hasVarArg()
47+
message = "Normal methods should have at least one parameter (the first of which should be 'self')." and
48+
not f.hasVarArg()
5149
) and
52-
not func instanceof ZopeInterfaceMethod
50+
not fv instanceof ZopeInterfaceMethodValue
5351

5452
select f, message

python/ql/src/semmle/python/libraries/Zope.qll

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import python
44

5+
private import semmle.python.pointsto.PointsTo
6+
57
/** A method that to a sub-class of `zope.interface.Interface` */
6-
class ZopeInterfaceMethod extends PyFunctionObject {
8+
deprecated class ZopeInterfaceMethod extends PyFunctionObject {
79

810
/** Holds if this method belongs to a class that sub-classes `zope.interface.Interface` */
911
ZopeInterfaceMethod() {
@@ -26,3 +28,20 @@ class ZopeInterfaceMethod extends PyFunctionObject {
2628
}
2729

2830
}
31+
32+
/** A method that belongs to a sub-class of `zope.interface.Interface` */
33+
class ZopeInterfaceMethodValue extends PythonFunctionValue {
34+
35+
/** Holds if this method belongs to a class that sub-classes `zope.interface.Interface` */
36+
ZopeInterfaceMethodValue() {
37+
exists(Value interface, ClassValue owner |
38+
interface = Module::named("zope.interface").attr("Interface") and
39+
owner.declaredAttribute(_) = this and
40+
// `zope.interface.Interface` will be recognized as a Value by the pointsTo analysis,
41+
// because it is the result of instantiating a "meta" class. getASuperType only returns
42+
// ClassValues, so we do this little trick to make things work
43+
Types::getBase(owner.getASuperType(), _) = interface
44+
)
45+
}
46+
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| Function yes |
1+
| Function Z.yes |

python/ql/test/library-tests/types/functions/Zope.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
import python
33
import semmle.python.libraries.Zope
44

5-
from ZopeInterfaceMethod f
5+
from ZopeInterfaceMethodValue f
66
select f.toString()

0 commit comments

Comments
 (0)