Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-108963: using random to generate unique string in sys.intern test#109491

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 6 commits intopython:mainfromaisk:fix-gh108963
Oct 2, 2023

Conversation

@aisk
Copy link
Contributor

@aiskaisk commentedSep 16, 2023
edited by bedevere-appbot
Loading

@bedevere-appbedevere-appbot added awaiting review testsTests in the Lib/test dir labelsSep 16, 2023
@aiskaisk changed the titleusing random to generate unique string in sys.intern testgh-108963: using random to generate unique string in sys.intern testSep 16, 2023
@vstinner
Copy link
Member

This change hides the issue that the test actually leaks memory: intern a string which is then only released at Python exit. Can we add a private deintern() function in _testcapi? Or run the test in a subprocess?

Well, i don't know that it's a big deal, this change is ooookay-ish.

INTERN_NUMRUNS+=1
self.assertRaises(TypeError,sys.intern)
s="never interned before"+str(INTERN_NUMRUNS)
s="never interned before"+str(random.random())
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer using random.randint(), for example 10**9 to have 9 random digits. I prefer to avoid float when possible 😁 Same below.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Done

Copy link
Member

@serhiy-storchakaserhiy-storchakaSep 19, 2023
edited
Loading

Choose a reason for hiding this comment

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

But this increases the probability of random failure from 1/253 (about 1/1016) to 1/(9*10**8). If run the test every second, there will be a failure every 30 years in average.

Copy link
ContributorAuthor

@aiskaiskSep 19, 2023
edited
Loading

Choose a reason for hiding this comment

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

On my machine, therandom.random() returns something like0.35934878814573135, which have 18 digits (with the first zero). So can we just increase the max range to10 ** 18 to keep the random resolution almost like as therandom.random()?

deftest_intern(self):
self.assertRaises(TypeError,sys.intern)
s="never interned before"+str(random.int(10**8,10**9))
s="never interned before"+str(random.randint(10**8,10**9))

Choose a reason for hiding this comment

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

If the intention is to generate random 9-digit numbers, userandrange() instead ofrandint().

deftest_intern(self):
self.assertRaises(TypeError,sys.intern)
s="never interned before"+str(random.random())
s="never interned before"+str(random.int(10**8,10**9))
Copy link
Member

Choose a reason for hiding this comment

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

random.int doesn't exist. What's the point of putting a minimum at 10**8? Why not 0?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

There is a more recent commit in the PR fixed the bug. The minimal start is for ensure the random parts have 9 digits, I think it's more consistent.

Copy link
Member

Choose a reason for hiding this comment

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

i don't think that the string length (number of random int digits) matters here, does it?

INTERN_NUMRUNS+=1
self.assertRaises(TypeError,sys.intern)
s="never interned before"+str(INTERN_NUMRUNS)
s="never interned before"+str(random.randrange(0,10**18))
Copy link
Member

Choose a reason for hiding this comment

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

Oh, for me 9 digits was already way enough. 18 digits is just too much 😁 You can just pass the max value:

Suggested change
s="never interned before"+str(random.randrange(0,10**18))
s="never interned before"+str(random.randrange(10**9))

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

It makes me sad to leak memory. But I'm not sure about a private API to de-intern a string. I'm not sure of the consequences.

@vstinnervstinner merged commit44b1e4e intopython:mainOct 2, 2023
@miss-islington
Copy link
Contributor

Thanks@aisk for the PR, and@vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry,@aisk and@vstinner, I could not cleanly backport this to3.12 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 44b1e4ea4842c6cdc1bedba7aaeb93f236b3ec08 3.12

@bedevere-app
Copy link

GH-110216 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelOct 2, 2023
Yhg1s pushed a commit that referenced this pull requestOct 2, 2023
…n test … (#110216)gh-108963: using random to generate unique string in sys.intern test (#109491)(cherry picked from commit44b1e4e)Co-authored-by: AN Long <aisk@users.noreply.github.com>
@aiskaisk deleted the fix-gh108963 branchOctober 3, 2023 07:16
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@vstinnervstinnervstinner approved these changes

Assignees

@vstinnervstinner

Labels

skip newstestsTests in the Lib/test dir

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@aisk@vstinner@miss-islington@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp