Skip to content

Commit

Permalink
Change to use sha256 hashing
Browse files Browse the repository at this point in the history
hash() is intended to be used for sets and dictionaries, where the
occasional collisions are expected. Also, in some versions of Python,
hash() is affected by hash randomization, it's not clear whether this
will be desirable.
  • Loading branch information
lieryan committed Nov 5, 2023
1 parent 7c39119 commit 9856c02
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import contextlib
import json
import random
from hashlib import sha256
import secrets
import re
import sqlite3
import sys
Expand Down Expand Up @@ -154,18 +155,21 @@ def create_database_connection(
memory : bool
if true, don't persist to disk
"""
def calculate_project_hash(data: str) -> str:
return sha256(data.encode()).hexdigest()

if not memory and project is None:
raise Exception("if memory=False, project must be provided")
if memory or project is None or project.ropefolder is None:
# Allows the in-memory db to be shared across threads
# See https://www.sqlite.org/inmemorydb.html
project_hash: int
project_hash: str
if project is None:
project_hash = random.randint(100_000_000, 999_999_999)
project_hash = secrets.token_hex()
elif project.ropefolder is None:
project_hash = hash(project.address)
project_hash = calculate_project_hash(project.address)
else:
project_hash = hash(project.ropefolder.real_path)
project_hash = calculate_project_hash(project.ropefolder.real_path)
return sqlite3.connect(
f"file:memdb{project_hash}:?mode=memory&cache=shared", uri=True
)
Expand Down

0 comments on commit 9856c02

Please sign in to comment.