Skip to content
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

python scripts: use raw strings (or double-escapes) for regular expression patterns #7741

Closed
2 tasks done
SwooshyCueb opened this issue May 13, 2024 · 1 comment
Closed
2 tasks done
Assignees
Labels
Milestone

Comments

@SwooshyCueb
Copy link
Member

SwooshyCueb commented May 13, 2024

  • main
  • 4-3-stable

We have a few regular expresisons in database_connect.py that we use to attempt to find odbc libraries:

def get_odbc_driver_paths(db_type, oracle_home=None):
if db_type == 'postgres' or db_type == 'cockroachdb':
return sorted(unique_list(itertools.chain(
lib.find_shared_object('psqlodbcw.so'),
lib.find_shared_object('libodbcpsql.so'),
lib.find_shared_object('psqlodbc.*\.so', regex=True))),
key = lambda p: 0 if is_64_bit_ELF(p) else 1)
elif db_type == 'mysql':
return sorted(unique_list(itertools.chain(
lib.find_shared_object('libmaodbc.so'),
lib.find_shared_object('libmyodbc.so'),
lib.find_shared_object('libmyodbc5.so'),
lib.find_shared_object('libmyodbc3.so'),
lib.find_shared_object('libmyodbc.*\.so', regex=True))),
key = lambda p: 0 if is_64_bit_ELF(p) else 1)
elif db_type == 'oracle':
return sorted(unique_list(itertools.chain(
lib.find_shared_object('libsqora\.so.*', regex=True, additional_directories=[d for d in [oracle_home] if d]))),
key = lambda p: 0 if is_64_bit_ELF(p) else 1)
else:
raise IrodsError('No default ODBC driver paths for %s' % (db_type))

However, the escapes are not done properly.
From the Python 3.12 documetnation:

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal. Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a SyntaxWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

@SwooshyCueb SwooshyCueb added this to the 4.3.3 milestone May 13, 2024
@SwooshyCueb SwooshyCueb self-assigned this May 13, 2024
@SwooshyCueb SwooshyCueb changed the title database_connect.py: use raw strings (or double-escapes) for regular expression patterns python scripts: use raw strings (or double-escapes) for regular expression patterns May 14, 2024
@SwooshyCueb
Copy link
Member Author

I'm expanding this issue to cover regular expressions in all of our python scripts and tests.

SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
SwooshyCueb added a commit to SwooshyCueb/irods that referenced this issue Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant