Skip to content

Commit 19974f0

Browse files
authored
Merge pull request #16245 from github/tausbn/python-rename-StrConst-to-StringLiteral
Python: Rename `StrConst` to `StringLiteral`
2 parents 35d1a92 + 81246cd commit 19974f0

File tree

164 files changed

+936
-884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+936
-884
lines changed

python/ql/examples/snippets/raw_string.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
import python
1010

11-
from StrConst s
11+
from StringLiteral s
1212
where s.getPrefix().matches("%r%")
1313
select s

python/ql/examples/snippets/singlequotestring.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
import python
1111

12-
from StrConst s
12+
from StringLiteral s
1313
where s.getPrefix().charAt(_) = "'"
1414
select s

python/ql/lib/analysis/DefinitionTracking.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private predicate sets_attribute(ArgumentRefinement def, string name) {
410410
call = def.getDefiningNode() and
411411
call.getFunction().refersTo(Object::builtin("setattr")) and
412412
def.getInput().getAUse() = call.getArg(0) and
413-
call.getArg(1).getNode().(StrConst).getText() = name
413+
call.getArg(1).getNode().(StringLiteral).getText() = name
414414
)
415415
}
416416

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: deprecated
3+
---
4+
5+
- Renamed the `StrConst` class to `StringLiteral`, for greater consistency with other languages. The `StrConst` and `Str` classes are now deprecated and will be removed in a future release.

python/ql/lib/experimental/cryptography/modules/stdlib/HashlibModule.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module Hashes {
2626
}
2727

2828
override string getName() {
29-
result = super.normalizeName(this.asExpr().(StrConst).getText())
29+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
3030
or
3131
// if not a known/static string, assume from an outside source and the algorithm is UNKNOWN
32-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
32+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
3333
}
3434
}
3535

@@ -49,10 +49,10 @@ module Hashes {
4949
}
5050

5151
override string getName() {
52-
result = super.normalizeName(this.asExpr().(StrConst).getText())
52+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
5353
or
5454
// if not a known/static string, assume from an outside source and the algorithm is UNKNOWN
55-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
55+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
5656
}
5757
}
5858

@@ -88,9 +88,9 @@ module Hashes {
8888
// Name is a string constant or consider the name unknown
8989
// NOTE: we are excluding hmac.new and hmac.HMAC constructor calls so we are expecting
9090
// a string or an outside configuration only
91-
result = super.normalizeName(this.asExpr().(StrConst).getText())
91+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
9292
or
93-
not this.asExpr() instanceof StrConst and
93+
not this.asExpr() instanceof StringLiteral and
9494
result = unknownAlgorithm()
9595
}
9696
}

python/ql/lib/experimental/cryptography/modules/stdlib/HmacModule.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ module Hashes {
6262
then result = super.normalizeName("MD5")
6363
else (
6464
// Else get the string name, if its a string constant, or UNKNOWN if otherwise
65-
result = super.normalizeName(this.asExpr().(StrConst).getText())
65+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
6666
or
67-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
67+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
6868
)
6969
}
7070
}

python/ql/lib/semmle/python/ApiGraphs.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ module API {
257257
*/
258258
Node getSubscript(string key) {
259259
exists(API::Node index | result = this.getSubscriptAt(index) |
260-
key = index.getAValueReachingSink().asExpr().(PY::StrConst).getText()
260+
key = index.getAValueReachingSink().asExpr().(PY::StringLiteral).getText()
261261
)
262262
}
263263

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ module Http {
855855

856856
/** Gets the URL pattern for this route, if it can be statically determined. */
857857
string getUrlPattern() {
858-
exists(StrConst str |
858+
exists(StringLiteral str |
859859
this.getUrlPatternArg().getALocalSource() = DataFlow::exprNode(str) and
860860
result = str.getText()
861861
)
@@ -983,7 +983,7 @@ module Http {
983983

984984
/** Gets the mimetype of this HTTP response, if it can be statically determined. */
985985
string getMimetype() {
986-
exists(StrConst str |
986+
exists(StringLiteral str |
987987
this.getMimetypeOrContentTypeArg().getALocalSource() = DataFlow::exprNode(str) and
988988
result = str.getText().splitAt(";", 0)
989989
)

python/ql/lib/semmle/python/Exprs.qll

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class Call extends Call_ {
236236
string getANamedArgumentName() {
237237
result = this.getAKeyword().getArg()
238238
or
239-
result = this.getKwargs().(Dict).getAKey().(StrConst).getText()
239+
result = this.getKwargs().(Dict).getAKey().(StringLiteral).getText()
240240
}
241241

242242
/** Gets the positional argument count of this call, provided there is no more than one tuple (*) argument. */
@@ -299,7 +299,7 @@ class Repr extends Repr_ {
299299
* A bytes constant, such as `b'ascii'`. Note that unadorned string constants such as
300300
* `"hello"` are treated as Bytes for Python2, but Unicode for Python3.
301301
*/
302-
class Bytes extends StrConst {
302+
class Bytes extends StringLiteral {
303303
/* syntax: b"hello" */
304304
Bytes() { not this.isUnicode() }
305305

@@ -446,7 +446,7 @@ class NegativeIntegerLiteral extends ImmutableLiteral, UnaryExpr {
446446
* A unicode string expression, such as `u"\u20ac"`. Note that unadorned string constants such as
447447
* "hello" are treated as Bytes for Python2, but Unicode for Python3.
448448
*/
449-
class Unicode extends StrConst {
449+
class Unicode extends StringLiteral {
450450
/* syntax: "hello" */
451451
Unicode() { this.isUnicode() }
452452

@@ -599,7 +599,7 @@ class Slice extends Slice_ {
599599
/**
600600
* Returns all string prefixes in the database that are explicitly marked as Unicode strings.
601601
*
602-
* Helper predicate for `StrConst::isUnicode`.
602+
* Helper predicate for `StringLiteral::isUnicode`.
603603
*/
604604
pragma[nomagic]
605605
private string unicode_prefix() {
@@ -610,20 +610,27 @@ private string unicode_prefix() {
610610
/**
611611
* Returns all string prefixes in the database that are _not_ explicitly marked as bytestrings.
612612
*
613-
* Helper predicate for `StrConst::isUnicode`.
613+
* Helper predicate for `StringLiteral::isUnicode`.
614614
*/
615615
pragma[nomagic]
616616
private string non_byte_prefix() {
617617
result = any(Str_ s).getPrefix() and
618618
not result.charAt(_) in ["b", "B"]
619619
}
620620

621-
/** A string constant. This is a placeholder class -- use `StrConst` instead. */
622-
class Str = StrConst;
621+
/** DEPRECATED. Use `StringLiteral` instead. */
622+
deprecated class Str = StringLiteral;
623+
624+
/** DEPRECATED. Use `StringLiteral` instead. */
625+
deprecated class StrConst = StringLiteral;
623626

624627
/** A string constant. */
625-
class StrConst extends Str_, ImmutableLiteral {
628+
class StringLiteral extends Str_, ImmutableLiteral {
626629
/* syntax: "hello" */
630+
/**
631+
* Holds if this string is a unicode string, either by default (e.g. if Python 3), or with an
632+
* explicit prefix.
633+
*/
627634
predicate isUnicode() {
628635
this.getPrefix() = unicode_prefix()
629636
or
@@ -652,6 +659,8 @@ class StrConst extends Str_, ImmutableLiteral {
652659
}
653660

654661
override Object getLiteralObject() { none() }
662+
663+
override string toString() { result = "StringLiteral" }
655664
}
656665

657666
private predicate name_consts(Name_ n, string id) {

python/ql/lib/semmle/python/Files.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class File extends Container, Impl::File {
9393
exists(Stmt s | s.getLocation().getFile() = this)
9494
or
9595
// The file contains the usual `if __name__ == '__main__':` construction
96-
exists(If i, Name name, StrConst main, Cmpop op |
96+
exists(If i, Name name, StringLiteral main, Cmpop op |
9797
i.getScope().(Module).getFile() = this and
9898
op instanceof Eq and
9999
i.getTest().(Compare).compares(name, op, main) and
@@ -123,7 +123,7 @@ private predicate occupied_line(File f, int n) {
123123
exists(Location l | l.getFile() = f |
124124
l.getStartLine() = n
125125
or
126-
exists(StrConst s | s.getLocation() = l | n in [l.getStartLine() .. l.getEndLine()])
126+
exists(StringLiteral s | s.getLocation() = l | n in [l.getStartLine() .. l.getEndLine()])
127127
)
128128
}
129129

0 commit comments

Comments
 (0)