Skip to content

Commit

Permalink
FIX: Use random in IronPython
Browse files Browse the repository at this point in the history
Note: package secrets is not available with IronPython
  • Loading branch information
SMoraisAnsys committed May 6, 2024
1 parent 7eca143 commit e2cb727
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pyaedt/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import math
import os
import re
import secrets
import string
import sys
import tempfile
Expand Down Expand Up @@ -676,6 +675,7 @@ def get_filename_without_extension(path):
return os.path.splitext(os.path.split(path)[1])[0]


# FIXME: Remove usage of random module once IronPython compatibility is removed
@pyaedt_function_handler()
def generate_unique_name(rootname, suffix="", n=6):
"""Generate a new name given a root name and optional suffix.
Expand All @@ -696,7 +696,15 @@ def generate_unique_name(rootname, suffix="", n=6):
"""
alphabet = string.ascii_uppercase + string.digits
uName = "".join(secrets.choice(alphabet) for _ in range(n))
if is_ironpython:
import random

uName = "".join(random.choice(alphabet) for _ in range(n)) # nosec B311
else:
import secrets

uName = "".join(secrets.choice(alphabet) for _ in range(n))

unique_name = rootname + "_" + uName
if suffix:
unique_name += "_" + suffix
Expand Down Expand Up @@ -1887,6 +1895,7 @@ def _arg2dict(arg, dict_out):
raise ValueError("Incorrect data argument format")


# FIXME: Remove usage of random module once IronPython compatibility is removed
def _uname(name=None):
"""Append a 6-digit hash code to a specified name.
Expand All @@ -1901,8 +1910,15 @@ def _uname(name=None):
"""
alphabet = string.ascii_uppercase + string.digits
generator = secrets.SystemRandom()
unique_name = "".join(secrets.SystemRandom.sample(generator, alphabet, 6))
if is_ironpython:
import random

unique_name = "".join(random.sample(alphabet, 6)) # nosec B311
else:
import secrets

generator = secrets.SystemRandom()
unique_name = "".join(secrets.SystemRandom.sample(generator, alphabet, 6))
if name:
return name + unique_name
else:
Expand Down

0 comments on commit e2cb727

Please sign in to comment.