Skip to content

Allow read-only methods to work against a frozen StringIO - #122

Closed
headius wants to merge 4 commits into
ruby:masterfrom
headius:frozen_readonly_methods
Closed

Allow read-only methods to work against a frozen StringIO#122
headius wants to merge 4 commits into
ruby:masterfrom
headius:frozen_readonly_methods

Conversation

@headius

Copy link
Copy Markdown
Contributor

Add tests and fix behavior to allow several read-only StringIO methods to function when the StringIO is frozen.

  • string (returns underlying String but does not mutate anything)
  • lineno
  • pos
  • closed?/closed_read?/closed_write?
  • eof/eof?
  • sync
  • pid (a dummy method but it writes nothing)
  • fileno (dummy)
  • pread (by definition does not modify state)
  • isatty/tty?
  • size/length
  • external_encoding
  • internal_encoding
  • initialize_copy (allow original to be frozen)

See #120 for details.

@headius

Copy link
Copy Markdown
ContributorAuthor

This is based on #121 and those commits will drop off when that PR is merged.

I would like input from @nobu@kou@hsbt@byroot since you all have related changes here. 🙇

This fixes the JRuby extension to allow read-only methods to work
against a frozen StringIO, as described in ruby#119.
@headius

Copy link
Copy Markdown
ContributorAuthor

I can make the same change in the C extension (mostly switching a few StringIO() macro calls to check_strio() function calls, but @nobu@kou please advise me.

@byroot

Copy link
Copy Markdown
Member

I would like input

It would make sense to me that StringIO.new(str.freeze) would try to behave like a read only file. But I'm not StringIO maintainer.

@kou

kou commented Aug 9, 2025

Copy link
Copy Markdown
Member

@headius

Copy link
Copy Markdown
ContributorAuthor

StringIO.new(str.freeze) would try to behave like a read only file

This isn't really the use case here. What I'm seeking is a way to freeze a StringIO so it can no longer be read from or written to, but still be able to access its current state like lineno and pos.

There's no reason I can think of that accessing current StringIO state should require that the StringIO be mutable, since we're just reading that state.

f.freeze
assert_nothing_raised do
f2 = f.dup
assert_true(f2.frozen?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Usually dupped objects are not frozen.

@nobu

nobu commented Mar 3, 2026

Copy link
Copy Markdown
Member

@headius Can this be closed?

@headius

Copy link
Copy Markdown
ContributorAuthor

@nobu Yes I believe your PR covers my case. Thank you!

@headiusheadius closed this Mar 3, 2026
@headius
headius deleted the frozen_readonly_methods branch March 3, 2026 06:22
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@headius@byroot@kou@nobu