GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-108963: using random to generate unique string in sys.intern test by aisk · Pull Request #109491 · python/cpython

Conversation

@aisk

@aisk aisk commented Sep 16, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: test_sys.test_intern() fails if run more than once in the same process #108963

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aisk
using random to generate unique string in sys.intern test
bdcae08
@bedevere-app bedevere-app Bot added awaiting review tests Tests in the Lib/test dir labels Sep 16, 2023
@aisk aisk changed the title using random to generate unique string in sys.intern test gh-108963: using random to generate unique string in sys.intern test Sep 16, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Sep 16, 2023
test_sys.test_intern() fails if run more than once in the same process #108963
Closed
@serhiy-storchaka
serhiy-storchaka requested a review from vstinner September 17, 2023 08:54
serhiy-storchaka
serhiy-storchaka approved these changes Sep 17, 2023
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Sep 17, 2023
@vstinner

vstinner commented Sep 18, 2023

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Sep 18, 2023
View reviewed changes
Comment thread Lib/test/test_sys.py Outdated
INTERN_NUMRUNS += 1
self.assertRaises(TypeError, sys.intern)
s = "never interned before" + str(INTERN_NUMRUNS)
s = "never interned before" + str(random.random())

@vstinner vstinner Sep 18, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aisk aisk Sep 19, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Sep 19, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aisk aisk Sep 19, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
aisk added 2 commits September 19, 2023 19:34
@aisk
remove float in intern test
1653d05
@aisk
fix test cases
a586b39
serhiy-storchaka
serhiy-storchaka reviewed Sep 19, 2023
View reviewed changes
Comment thread Lib/test/test_sys.py Outdated
def test_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))

@serhiy-storchaka serhiy-storchaka Sep 19, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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, use randrange() instead of randint().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner
vstinner reviewed Sep 19, 2023
View reviewed changes
Comment thread Lib/test/test_sys.py Outdated
def test_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))

@vstinner vstinner Sep 19, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aisk aisk Sep 19, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 19, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aisk
using random.randrange instead of random.randint and increase range
4978df9
vstinner
vstinner reviewed Sep 21, 2023
View reviewed changes
Comment thread Lib/test/test_sys.py Outdated
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))

@vstinner vstinner Sep 21, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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))

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aisk
using 9 digits random range in intern test
785393d
vstinner
vstinner approved these changes Sep 21, 2023
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
Merge branch 'main' into fix-gh108963
14008d9
@vstinner vstinner added skip news needs backport to 3.12 only security fixes labels Oct 2, 2023
@vstinner
vstinner merged commit 44b1e4e into python:main Oct 2, 2023
@miss-islington

miss-islington commented Oct 2, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Oct 2, 2023
@miss-islington

miss-islington commented Oct 2, 2023

Copy link
Copy Markdown
Contributor

Sorry, @aisk and @vstinner, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 44b1e4ea4842c6cdc1bedba7aaeb93f236b3ec08 3.12
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington miss-islington assigned vstinner Oct 2, 2023
vstinner pushed a commit to vstinner/cpython that referenced this pull request Oct 2, 2023
@aisk @vstinner
pythongh-108963: using random to generate unique string in sys.intern…
52c001d
… test (python#109491)

(cherry picked from commit 44b1e4e)
@vstinner vstinner mentioned this pull request Oct 2, 2023
[3.12] gh-108963: using random to generate unique string in sys.intern test … #110216
Merged
@bedevere-app

bedevere-app Bot commented Oct 2, 2023

Copy link
Copy Markdown

GH-110216 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Oct 2, 2023
Yhg1s pushed a commit that referenced this pull request Oct 2, 2023
@vstinner @aisk
[3.12] gh-108963: using random to generate unique string in sys.inter…
f911d7e
…n test … (#110216)

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

(cherry picked from commit 44b1e4e)

Co-authored-by: AN Long <[email protected]>
@aisk
aisk deleted the fix-gh108963 branch October 3, 2023 07:16
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@aisk @Glyphack
pythongh-108963: using random to generate unique string in sys.intern…
34f3941
… test (python#109491)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@vstinner vstinner vstinner approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@vstinner vstinner

Labels

skip news tests Tests in the Lib/test dir

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@aisk @vstinner @miss-islington @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.