rspec/rules/S5828/python/rule.adoc

57 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The ``++open++`` builtin can open files in different modes, which are provided as a combination of characters. Using an invalid sequence of characters will at best make ``++open++`` fail, or worse, it will have an undefined behavior (ex: it might ignore some characters).
A valid mode:
* should contain one and only one of the following characters: "r" (read), "w" (write), "a" (append), "x" (create).
* should contain zero or one of the following characters: "t" (text), "b" (binary).
* should contain zero or one "+" character (open for updating)
* cannot contain "a", "w", "+", or "x" if "U" (universal newlines) is used. Note that "U" has no effect in python 3, it is deprecated and shouldn't be used anymore.
This rule raises an issue when an invalid "mode" is provided to the ``++open++`` builtin.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
# In python 3 the following fails
# In python 2.7.16 on MacOs, "open" will just ignore the "w" flag
with open("test.txt", "aw") as f: # Noncompliant
pass
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
with open("test.txt", "a") as f:
pass
----
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.python.org/3/library/functions.html#open[Python documentation - the ``++open++`` builtin]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]