Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IO instance methods #1692

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sampersand
Copy link
Contributor

@sampersand sampersand commented Dec 20, 2023

Phew! This one took awhile :-P. This PR updates IO's instance methods.

More specifically, this makes the following general changes:

  • Deprecates ::_ReaderPartial (renamed to IO::_Readpartial) and ::_Rewindable (now unused)
  • Deprecates String::_EncodeFallbackAref (moved within to Encoding, as it's used for encoding purposes).
  • Fixes a minor bug in lib/rbs/unit_test/spy.rb where .new() is called instead of .allocate(). This prevents testing instance methods named :initialize.
  • Removed test/stdlib/test_helper.rb:ArefFromStringToString, as it's no longer needed
  • Added in TestHelper::RUBY_EXE, used when spawning shells (IO needs it for IO#pid)
  • Added test/stdlib/util/small-file.txt as a small file that can be read from fro testing
  • Redid all the tests in IO_test.rb to accomodate the with_xxx forms. However, wait_writable and wait_prioroity currently don't have any tests associated with them, as I couldn't figure out a good way to test them. (However, their signatures are pretty simple)

It also makes the following changes to io.rbs:

  • The %a{ruby:since:3.2.0} annotation has been added to methods introduced in ruby 3.2.0
  • Moved all of IO's constant definitions inside of it
  • Removed constant definitions that IO inherits from File::Constants (APPEND, FNM_CASEFOLD, LOCK_SH, etc.)
  • Added IO::EWOULDBLOCKWait{Readable,Writable} constants
  • IO#initialize: mode is now open_mode; updated all the options to be the entire list of accepted options (along with the **untyped that it for some reason accepts)
  • IO#advise: Added IO::advice type alias, offset and len are now int?
  • IO#{lineno,autoclose,sync}=: Now returns their arguments
  • IO#each: Now its own function; each_line aliases it. Also added in the limit-only variant.
  • IO#each_{byte,char,codepoint}: Reordered branches to have Enumerator first
  • IO#external_encoding: Added in. (weird that it didnt exist, since internal_encoding existed before...)
  • IO#{fcntl,ioctl}: Now uses implicits, argument also accepts bool | nil as well.
  • IO#{f{data,}sync,rewind}: Always returns 0
  • IO#pid: Can now return nil
  • IO#pos=: Now takes int
  • IO#p{read,write}: Added in
  • IO#{print,puts}: objects are now _ToS not untyped
  • IO#printf: format string is now string
  • IO#putc: Separated cases out so return values aren't unioned
  • IO#{gets,read}: reordered branches slightly
  • IO#readline{,s}: Added in limit-only variant
  • IO#readpartial: outbuf is now nilable
  • IO#reopen: Changed (IO) -> IO to be (io) -> sef; added in optional keywords to second case
  • IO#seek: amount is now nil; also added in IO::whence type alias.
  • IO#set_encoding: Added in optional encoding arguments
  • IO#sysread: added implicit conversions; outbuf is optional and nilable.
  • IO#sysseek: ADded implicit int for amount; uses IO::whence.
  • IO#timeout=: Now returns self not void (which is weird, since almost every other setter in ruby returns its argument...)
  • IO#to_io: Now marked %a{pure}
  • IO#to_path: Added in
  • IO#ungetbyte: Argument accepts implicit string and is nilable.
  • IO#ungetc: Argument can now be Integer.
  • IO#write_nonblock: Can no longer return nil now

Since Ruby 3.2 moved the wait_* methods into io directly (no need for require 'io/wait'), the methods IO#wait{,_{readable,writable,priority}} have been moved from io/wait.rbs, along with IO::wait_mode and marked as `%a{ruby:since:3.2.0}.

@sampersand sampersand force-pushed the swesterman/23-12-13/update-io branch 2 times, most recently from cbadb6e to ccfc69f Compare December 21, 2023 01:22
@@ -28,6 +28,13 @@
# useful when you use other ASCII-compatible encodings.
#
class Encoding
# The `_EncodeFallbackAref` is used in functions such as `String::encode` to get the fallback
# value when a charcater can't be converted between encodings.
interface _EncodeFallbackAref
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was moved from String to here

@@ -128,7 +128,7 @@ def wrapped_object
end
end.ruby2_keywords
)
end.new()
end.allocate()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed because .new implicitly calls .initialize; if method_name is :initialize, then this will accidentally call it with no arguments. Doing it this way prevents that (and since the parent object is BasicObject, :initialize is a no-op anyways)

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, why not MiniTest::Mock?

@sampersand sampersand force-pushed the swesterman/23-12-13/update-io branch 7 times, most recently from e75df24 to e328d88 Compare December 21, 2023 02:05
@sampersand sampersand changed the title Update IO instance methods and IO.{new,open} Update IO instance methods Dec 21, 2023
@sampersand sampersand marked this pull request as ready for review December 26, 2023 22:07
@sampersand sampersand marked this pull request as draft December 26, 2023 22:52
@sampersand sampersand marked this pull request as ready for review December 26, 2023 23:26
@ParadoxV5
Copy link
Contributor

  • The %a{ruby:since:3.2.0} annotation has been added to methods introduced in ruby 3.2.0

We have Ruby 3.3 now (:

Co-authored-by: ParadoxV5 <paradox.ver5@gmail.com>
@sampersand sampersand marked this pull request as draft February 8, 2024 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants