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 — BUG: raise TypeError for in-place string multiply via arithmetic operator by ngoldbaum · Pull Request #29056 · numpy/numpy

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented May 26, 2025

Copy link
Copy Markdown
Member

Fixes #29011.

I could add bounds checking to the loops themselves too, if people think it's worth adding that defensively.

Sorry, something went wrong.

Uh oh!

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

jorenham reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@ngoldbaum
BUG: raise TypeError for in-place string multiply via arithmetic oper…
8bf9036
…ator
@ngoldbaum ngoldbaum mentioned this pull request May 26, 2025
BUG: in-place fixed-width string multiply doesn't do overflow checking #29011
Closed
@github-actions github-actions Bot added the 00 - Bug label May 26, 2025
ngoldbaum
ngoldbaum commented May 26, 2025
View reviewed changes
Comment thread numpy/typing/tests/data/pass/ma.py
MAR_S: MaskedArray[np.bytes_] = np.ma.MaskedArray([b'foo'], dtype=np.bytes_)
MAR_U: MaskedArray[np.str_] = np.ma.MaskedArray(['foo'], dtype=np.str_)
MAR_T = cast(np.ma.MaskedArray[Any, np.dtypes.StringDType], np.ma.MaskedArray(["a"], "T"))
MAR_T = cast(np.ma.MaskedArray[Any, np.dtypes.StringDType], np.ma.MaskedArray(["a"], dtype="T"))

@ngoldbaum ngoldbaum May 26, 2025

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.

I happened to notice that this was passing "T" as the mask, so the resulting array had dtype='U1'. Perhaps not great that MaskedArray doesn't object if you pass mask=some_string!

Sorry, something went wrong.

Uh oh!

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

jorenham reacted with eyes emoji
All reactions
  • 👀 1 reaction

@jorenham jorenham May 26, 2025

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.

good catch!

Sorry, something went wrong.

Uh oh!

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

All reactions
ngoldbaum
ngoldbaum commented May 26, 2025
View reviewed changes
Comment thread numpy/typing/tests/data/pass/ma.py Resolved

Uh oh!

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

ngoldbaum
ngoldbaum commented May 26, 2025
View reviewed changes
Comment thread numpy/_core/src/umath/string_ufuncs.cpp Resolved

Uh oh!

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

@ngoldbaum ngoldbaum added component: numpy.strings String dtypes and functions 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes labels May 26, 2025
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label May 26, 2025
seberg
seberg approved these changes May 26, 2025
View reviewed changes

@seberg seberg 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.

Ah, hand't thought of this, thought we should just add the check. But this seems safe to me.

No big opinion on the error, but agree your suggested edit is a bit nicer.

Sorry, something went wrong.

Uh oh!

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

All reactions
@seberg

seberg commented May 26, 2025

Copy link
Copy Markdown
Member

Hmmm, sorry hadn't quite thought that through. This works to prevent in-place ops, it will not fix np.multiple(str_arr, 10, out=str_arr.copy()).
Maybe that is good enough for now at least, although I suspect that checking the length inside the ufunc isn't a big deal (but maybe a bit tedious).

All reactions

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum

ngoldbaum commented May 26, 2025

Copy link
Copy Markdown
Member Author

I suspect that checking the length inside the ufunc isn't a big deal (but maybe a bit tedious).

Yeah this is probably the way to go. We can get the value of the right hand side, compare it with the dtype's width, and use that bound to prevent any of the loops from overflowing the buffer.

That way I can also avoid messing with the types or breaking backward compatibility, even if in-place multiplying a string is a weird thing to do...

I'm going to re-issue this with a different approach.

jorenham reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum ngoldbaum closed this May 26, 2025
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 26, 2025
@ngoldbaum ngoldbaum mentioned this pull request May 27, 2025
BUG: add bounds-checking to in-place string multiply #29060
Merged
@charris charris mentioned this pull request May 27, 2025
BUG: add bounds-checking to in-place string multiply #29074
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jorenham jorenham jorenham left review comments
@seberg seberg seberg approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

00 - Bug 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy.strings String dtypes and functions

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: in-place fixed-width string multiply doesn't do overflow checking

Uh oh!

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

4 participants

@ngoldbaum @seberg @jorenham @charris
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 01402775 — 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.