-
Notifications
You must be signed in to change notification settings - Fork 306
Complete O_flags used for file open #1853
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
|
|
||
| CP16623_LOCK = _thread.allocate_lock() | ||
|
|
||
| from iptest import IronPythonTestCase, is_cli, is_cpython, is_netcoreapp, is_posix, run_test, skipUnlessIronPython | ||
| from iptest import IronPythonTestCase, is_cli, is_cpython, is_netcoreapp, is_posix, is_linux, is_osx, is_windows, run_test, skipUnlessIronPython | ||
|
|
||
| class FileTest(IronPythonTestCase): | ||
|
|
||
|
|
@@ -505,12 +505,12 @@ def test_file_manager_leak(self): | |
| # the number of iterations should be larger than Microsoft.Scripting.Utils.HybridMapping.SIZE (currently 4K) | ||
| N = 5000 | ||
| for i in range(N): | ||
| fd = os.open(self.temp_file, os.O_WRONLY | os.O_CREAT) | ||
| fd = os.open(self.temp_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) | ||
| f = os.fdopen(fd, 'w', closefd=True) | ||
| f.close() | ||
|
|
||
| for i in range(N): | ||
| fd = os.open(self.temp_file, os.O_WRONLY | os.O_CREAT) | ||
| fd = os.open(self.temp_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) | ||
| f = os.fdopen(fd, 'w', closefd=False) | ||
| g = os.fdopen(f.fileno(), 'w', closefd=True) | ||
| g.close() | ||
|
|
@@ -624,7 +624,7 @@ def test_modes(self): | |
| self.assertRaisesMessage(ValueError, "invalid mode: 'p'", open, 'abc', 'p') | ||
|
|
||
| # allow anything w/ U but r and w | ||
| err_msg = "mode U cannot be combined with 'x', 'w', 'a', or '+'" if is_cli or sys.version_info >= (3,7) else "mode U cannot be combined with x', 'w', 'a', or '+'" if sys.version_info >= (3,6) else "can't use U and writing mode at once" | ||
| err_msg = "mode U cannot be combined with 'x', 'w', 'a', or '+'" if is_cli or sys.version_info >= (3,7,4) else "mode U cannot be combined with x', 'w', 'a', or '+'" if sys.version_info >= (3,6) else "can't use U and writing mode at once" | ||
| self.assertRaisesMessage(ValueError, err_msg, open, 'abc', 'Uw') | ||
| self.assertRaisesMessage(ValueError, err_msg, open, 'abc', 'Ua') | ||
| self.assertRaisesMessage(ValueError, err_msg, open, 'abc', 'Uw+') | ||
|
|
@@ -701,7 +701,7 @@ def test_errors(self): | |
|
|
||
| with self.assertRaises(OSError) as cm: | ||
| open('path_too_long' * 100) | ||
| self.assertEqual(cm.exception.errno, (36 if is_posix else 22) if is_netcoreapp and not is_posix or sys.version_info >= (3,6) else 2) | ||
| self.assertEqual(cm.exception.errno, (63 if is_osx else 36 if is_linux else 22) if is_netcoreapp and not is_posix or sys.version_info >= (3,6) else 2) | ||
|
||
|
|
||
| def test_write_bytes(self): | ||
| fname = self.temp_file | ||
|
|
@@ -756,6 +756,39 @@ def test_open_with_BOM(self): | |
| with open(fileName, "rb") as f: | ||
| self.assertEqual(f.read(), b"\xef\xbb\xbf\x42\xc3\x93\x4d\x0d\x0a") | ||
|
|
||
|
|
||
| def test_open_flags(self): | ||
| test_data = { | ||
| 'rb': os.O_RDONLY, | ||
| 'wb': os.O_WRONLY | os.O_CREAT | os.O_TRUNC, | ||
| 'ab': os.O_WRONLY | os.O_CREAT | os.O_APPEND, | ||
| 'xb': os.O_WRONLY | os.O_CREAT | os.O_EXCL, | ||
| 'rb+': os.O_RDWR, | ||
| 'wb+': os.O_RDWR | os.O_CREAT | os.O_TRUNC, | ||
| 'ab+': os.O_RDWR | os.O_CREAT | os.O_APPEND, | ||
| 'xb+': os.O_RDWR | os.O_CREAT | os.O_EXCL, | ||
| } | ||
| extra_flags = 0 | ||
| if is_posix: | ||
| extra_flags |= os.O_CLOEXEC | ||
| elif is_windows: | ||
| extra_flags |= os.O_NOINHERIT | os.O_BINARY | ||
| test_data = {k: v | extra_flags for k, v in test_data.items()} | ||
|
|
||
| flags_received = None | ||
| def test_open(name, flags): | ||
| nonlocal flags_received | ||
| flags_received = flags | ||
| if mode[0] == 'x': | ||
| os.unlink(name) | ||
| return os.open(name, flags) | ||
|
|
||
| for mode in sorted(test_data): | ||
| with self.subTest(mode=mode): | ||
| with open(self.temp_file, mode, opener=test_open): pass | ||
| self.assertEqual(flags_received, test_data[mode]) | ||
|
|
||
|
|
||
| def test_opener(self): | ||
| data = "test message\n" | ||
| with open(self.temp_file, "w", opener=os.open) as f: | ||
|
|
||
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.
Having
PythonHiddenon aprivateseems kind of odd but I guess it doesn't hurt...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.
Yes, it doesn't hurt, but the less verbosity the better. I assume the same applies to
internal, though I see there are a fewPythonHiddenthat areinternalalready in the codebase.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.
Weird, I wonder why we'd have them on
internalmembers. Maybe something that waspublicat some point?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.
Look for example at
ByteArray.UnsafeByteList. It was established 5 years ago asinternal, but 4 years ago you addedPythonHiddenin #1000. Do you remember why?Uh oh!
There was an error while loading. Please reload this page.
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.
Can't say I remember, but looking at #693 I changed
Bytes.GetUnsafeByteArray(which was public) to apublicproperty and then in a follow up commit made itinternal. I guess I forgot to remove thePythonHiddenwhen I did this.ByteArray.UnsafeByteListis probably a copy/paste...