2023-06-30 09:16:01 +02:00
This rule raises an issue when an invalid "mode" is provided to the `open` builtin function.
2023-05-03 11:06:20 +02:00
2023-06-30 09:16:01 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
2023-06-30 09:16:01 +02:00
The `open` builtin function can open files in different modes. These modes are provided as a combination of characters. Using an invalid sequence of characters will make `open` fail with a `ValueError`.
2021-04-28 16:49:39 +02:00
A valid mode:
2023-06-30 09:16:01 +02:00
* should contain 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)
For example: `a`, `rt`, `r+` and `w+b` are valid modes.
If no `t` or `b` character is provided the mode will default to `t` (text), so specifying `r` is equivalent to `rt`.
*Note*: __In Python 2, providing an incorrect mode may have an undefined behavior (ex: it might ignore some characters)__
== How to fix it
2021-04-28 16:49:39 +02:00
2023-06-30 09:16:01 +02:00
Make sure to provide a valid mode to the `open` builtin function.
2021-04-28 16:49:39 +02:00
2023-06-30 09:16:01 +02:00
=== Code examples
2021-04-28 18:08:03 +02:00
2023-06-30 09:16:01 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-06-30 09:16:01 +02:00
In Python 3 the following program will throw a `ValueError`.
In Python 2.7.16 on MacOs, `open` will just ignore the `w` flag.
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-06-30 09:16:01 +02:00
with open("test.txt", "aw") as f: # Noncompliant: ValueError
2021-04-28 16:49:39 +02:00
pass
----
2023-06-30 09:16:01 +02:00
==== Compliant solution
2021-04-28 18:08:03 +02:00
2023-06-30 09:16:01 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
with open("test.txt", "a") as f:
pass
----
2023-06-30 09:16:01 +02:00
=== Pitfalls
In Python 2, the character `U` (universal newlines) is available as a mode character but it cannot be combined with `a`, `w`, `x` or `+`.
*Note*: __``++U++`` has no effect in Python 3, it is deprecated and is replaced by the parameter `newline` of the `open` function. The default value of `newline` is `None`, meaning universal newlines mode is enabled.__
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-06-30 09:16:01 +02:00
=== Documentation
* https://docs.python.org/3/library/functions.html#open[The `open` builtin function]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Fix this invalid mode string.
=== Highlighting
The mode parameter
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is related to: S5488
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]