Skip to content

Fix set_auto_conf with single quotes #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 4, 2024
Merged

Conversation

demonolock
Copy link
Contributor

@demonolock demonolock commented Dec 3, 2024

This patch improves an PostgresNode::set_auto_conf method

New implementation does the following things:

  • escaping '\n', '\r', '\t', '\b' and '\' symbols
  • translating bool-values into 'on|off'
  • saving a presentation of values those were not changed

This patch closes a problem with storing a command line with quotes in postgres' configuration files.

A test is provided.


It is a temporary solution to exist problems. In the future we should work with configuration files a more cleverly:

  • do not reorder declarations
  • do not remove comments

@dmitry-lipetsk
Copy link
Collaborator

dmitry-lipetsk commented Dec 3, 2024

Postgres' functions for escaping and unescaping configuration property values.

/*
* Strip the quotes surrounding the given string, and collapse any embedded
* '' sequences and backslash escapes.
*/
char * DeescapeQuotedString(const char *s)

https://github.com/postgres/postgres/blob/e3fa2b037c6f0f435838e99200050dc54c306085/src/backend/utils/misc/guc-file.l#L653


/*
 * Escape (by doubling) any single quotes or backslashes in given string
 *
*/
char* escape_single_quotes_ascii(const char *src)

https://github.com/postgres/postgres/blob/1acf10549e64c6a52ced570d712fcba1a2f5d1ec/src/port/quotes.c#L33

- we do not touch existing values
- escaping of '\n', '\r', '\t', '\b' and '\\' is added
- translation of bool into 'on|off' is added

test_set_auto_conf is updated.
Let's control a correct usage of this function.

Plus one assert was added in _escape_config_value.
@demonolock
Copy link
Contributor Author

Ошибки flake8
./testgres/node.py:1637:20: E721 do not compare types, for exact checks use is / is not, for instance checks use isinstance()
./testgres/node.py:1702:16: E721 do not compare types, for exact checks use is / is not, for instance checks use isinstance()

@dmitry-lipetsk
Copy link
Collaborator

dmitry-lipetsk commented Dec 3, 2024

flake8 error:

./build/lib/testgres/node.py:1637:20: E721 do not compare types, for exact checks use is / is not, for instance checks use isinstance()

It is a really interesting problem.

Flake does not like:

assert type(option) == str

but doesn't mind it:

if valueType == str:

For me - it need to configure flake and disable E721.

https://stackoverflow.com/questions/52395064/e721-do-not-compare-types-use-isinstance-error

you should just ignore the linter because it's not suggesting anything useful to you.

In my experience Python linters are blunt tools and implemented fairly naively.

What I do not use isinstance:

Never use isinstance() to validate types, one example is that True and False are considered instances of int

@dmitry-lipetsk
Copy link
Collaborator

dmitry-lipetsk commented Dec 3, 2024

https://www.flake8rules.com/rules/E721.html

A object should be be compared to a type by using isinstance. This is because isinstance can handle subclasses as well.

They are really thinking that know what I want :)

Copy link
Collaborator

@dmitry-lipetsk dmitry-lipetsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch was tested with pg_probackup2 test system. All is OK.

@dmitry-lipetsk dmitry-lipetsk merged commit 5e9ecbc into master Dec 4, 2024
2 checks passed
@demonolock demonolock deleted the fix-set-auto-conf branch December 4, 2024 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants