Skip to content

Supported FileHandle API for Swift #1889

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

Merged
merged 2 commits into from
Feb 12, 2019
Merged

Supported FileHandle API for Swift #1889

merged 2 commits into from
Feb 12, 2019

Conversation

millenomi
Copy link
Contributor

@millenomi millenomi commented Feb 7, 2019

Adds API to FileHandle that allows clients to catch and handle errors, rather than panic with fatalError on I/O issues. This API will be supported going forward and is not experimental.

@millenomi
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@millenomi
Copy link
Contributor Author

@swift-ci please test

@@ -405,6 +280,213 @@ open class FileHandle : NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool {
return true
}

private var _isPlatformHandleValid: Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a good idea to move this up into the Windows/Non-Windows FD & handle blocks at the top to keep all the fd and fd checks in one place.

@spevans
Copy link
Contributor

spevans commented Feb 8, 2019

I think the _nulldeviceFileHandle might need its own specialised versions of the new Swift 5 API methods and deprecations as it is using an invalid handle.

@spevans
Copy link
Contributor

spevans commented Feb 8, 2019

Should also have at least one test for each of the new methods (one and the same for nulDevice as well ) just to check coverage.

@millenomi
Copy link
Contributor Author

Oh, right. We need to probably do a subclass check in the implementation, because the new entry points are (on purpose) public and not open.

@pvieito
Copy link
Contributor

pvieito commented Feb 8, 2019

@millenomi Is this new API going to be added to the Foundation SDK overlay? Also, why some API (like FileHandle.readToEnd()) return Data? instead of Data, I think returning an empty Data value would be more correct.

}

@available(swift 5.0)
public func close() throws {
Copy link
Member

Choose a reason for hiding this comment

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

Might be nicer to write this as:

guard _isPlatformHandleValid else { return }
#if os(Windows)
guard CloseHandle(_handle) != FALSE else {
  throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
_handle = INVALID_HANDLE_VALUE
#else
gurad _close(_fd) >= 0 else {
  throw _NSErrorWithErrno(errno, reading: true)
}
_fd = -1
#endif

@millenomi
Copy link
Contributor Author

@pvieito I cannot comment on future Mac SDKs.

The API that return nil do so at, essentially, EOF.

@@ -35,20 +35,19 @@ class TestPipe: XCTestCase {
pipes = []
}

func test_Pipe() {
func test_Pipe() throws {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this disqualify the test from XCTest (acknowledging that we do not automatically discover them at this time).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No; this is a pattern we use elsewhere that XCTest supports.

@compnerd
Copy link
Member

@millenomi - I don't think that @pvieito was asking about future macOS SDKs, I think he was asking about the Swift repository side.

@millenomi
Copy link
Contributor Author

Tests will be forthcoming in a separate patch.

@millenomi
Copy link
Contributor Author

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Feb 12, 2019

LGTM

@millenomi
Copy link
Contributor Author

@swift-ci please test

@millenomi
Copy link
Contributor Author

there it goes.

@millenomi millenomi merged commit fcc46b0 into swiftlang:master Feb 12, 2019
@spevans
Copy link
Contributor

spevans commented Feb 13, 2019

@millenomi Should we be porting these changes to swift-5.0-branch?

@parkera
Copy link
Contributor

parkera commented Feb 14, 2019

Our intent is to line this up with the next release of Swift.

Sign up for free to 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.

5 participants