Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf19d16

Browse files
author
Martin Panter
committed
Issue #23738: Document and test actual keyword parameter names
Also fix signature because os.utime(..., ns=None) is not allowed.
1 parent 5558d4f commit bf19d16

9 files changed

Lines changed: 73 additions & 31 deletions

File tree

Doc/library/binascii.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The :mod:`binascii` module defines the following functions:
5959
should be at most 57 to adhere to the base64 standard.
6060

6161

62-
.. function:: a2b_qp(string, header=False)
62+
.. function:: a2b_qp(data, header=False)
6363

6464
Convert a block of quoted-printable data back to binary and return the binary
6565
data. More than one line may be passed at a time. If the optional argument

Doc/library/os.rst

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,9 @@ as internal buffering of data.
863863
:data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`.
864864

865865

866-
.. function:: open(file, flags, mode=0o777, *, dir_fd=None)
866+
.. function:: open(path, flags, mode=0o777, *, dir_fd=None)
867867

868-
Open the file *file* and set various flags according to *flags* and possibly
868+
Open the file *path* and set various flags according to *flags* and possibly
869869
its mode according to *mode*. When computing *mode*, the current umask value
870870
is first masked out. Return the file descriptor for the newly opened file.
871871
The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
@@ -1071,10 +1071,10 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
10711071
:meth:`~file.read` or :meth:`~file.readline` methods.
10721072

10731073

1074-
.. function:: sendfile(out, in, offset, nbytes)
1075-
sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)
1074+
.. function:: sendfile(out, in, offset, count)
1075+
sendfile(out, in, offset, count, headers=None, trailers=None, flags=0)
10761076

1077-
Copy *nbytes* bytes from file descriptor *in* to file descriptor *out*
1077+
Copy *count* bytes from file descriptor *in* to file descriptor *out*
10781078
starting at *offset*.
10791079
Return the number of bytes sent. When EOF is reached return 0.
10801080

@@ -1088,7 +1088,7 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
10881088
*trailers* are arbitrary sequences of buffers that are written before and
10891089
after the data from *in* is written. It returns the same as the first case.
10901090

1091-
On Mac OS X and FreeBSD, a value of 0 for *nbytes* specifies to send until
1091+
On Mac OS X and FreeBSD, a value of 0 for *count* specifies to send until
10921092
the end of *in* is reached.
10931093

10941094
All platforms support sockets as *out* file descriptor, and some platforms
@@ -1683,10 +1683,10 @@ features:
16831683
The *dir_fd* argument.
16841684

16851685

1686-
.. function:: mknod(filename, mode=0o600, device=0, *, dir_fd=None)
1686+
.. function:: mknod(path, mode=0o600, device=0, *, dir_fd=None)
16871687

16881688
Create a filesystem node (file, device special file or named pipe) named
1689-
*filename*. *mode* specifies both the permissions to use and the type of node
1689+
*path*. *mode* specifies both the permissions to use and the type of node
16901690
to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
16911691
``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
16921692
available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
@@ -2210,9 +2210,9 @@ features:
22102210
.. versionadded:: 3.3
22112211

22122212

2213-
.. function:: symlink(source, link_name, target_is_directory=False, *, dir_fd=None)
2213+
.. function:: symlink(src, dst, target_is_directory=False, *, dir_fd=None)
22142214

2215-
Create a symbolic link pointing to *source* named *link_name*.
2215+
Create a symbolic link pointing to *src* named *dst*.
22162216

22172217
On Windows, a symlink represents either a file or a directory, and does not
22182218
morph to the target dynamically. If the target is present, the type of the
@@ -2282,20 +2282,20 @@ features:
22822282
The *dir_fd* parameter.
22832283

22842284

2285-
.. function:: utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)
2285+
.. function:: utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)
22862286

22872287
Set the access and modified times of the file specified by *path*.
22882288

22892289
:func:`utime` takes two optional parameters, *times* and *ns*.
22902290
These specify the times set on *path* and are used as follows:
22912291

2292-
- If *ns* is not ``None``,
2292+
- If *ns* is specified,
22932293
it must be a 2-tuple of the form ``(atime_ns, mtime_ns)``
22942294
where each member is an int expressing nanoseconds.
22952295
- If *times* is not ``None``,
22962296
it must be a 2-tuple of the form ``(atime, mtime)``
22972297
where each member is an int or float expressing seconds.
2298-
- If *times* and *ns* are both ``None``,
2298+
- If *times* is ``None`` and *ns* is unspecified,
22992299
this is equivalent to specifying ``ns=(atime_ns, mtime_ns)``
23002300
where both times are the current time.
23012301

@@ -2846,9 +2846,10 @@ written in Python, such as a mail server's external command delivery program.
28462846
Availability: Unix.
28472847

28482848

2849-
.. function:: popen(command, mode='r', buffering=-1)
2849+
.. function:: popen(cmd, mode='r', buffering=-1)
28502850

2851-
Open a pipe to or from *command*. The return value is an open file object
2851+
Open a pipe to or from command *cmd*.
2852+
The return value is an open file object
28522853
connected to the pipe, which can be read or written depending on whether *mode*
28532854
is ``'r'`` (default) or ``'w'``. The *buffering* argument has the same meaning as
28542855
the corresponding argument to the built-in :func:`open` function. The

Doc/library/zlib.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The available exception and functions in this module are:
5858
Raises the :exc:`error` exception if any error occurs.
5959

6060

61-
.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
61+
.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
6262

6363
Returns a compression object, to be used for compressing data streams that won't
6464
fit into memory at once.
@@ -75,9 +75,9 @@ The available exception and functions in this module are:
7575
should be an integer from ``8`` to ``15``. Higher values give better
7676
compression, but use more memory.
7777

78-
*memlevel* controls the amount of memory used for internal compression state.
79-
Valid values range from ``1`` to ``9``. Higher values using more memory,
80-
but are faster and produce smaller output.
78+
The *memLevel* argument controls the amount of memory used for the
79+
internal compression state. Valid values range from ``1`` to ``9``.
80+
Higher values use more memory, but are faster and produce smaller output.
8181

8282
*strategy* is used to tune the compression algorithm. Possible values are
8383
``Z_DEFAULT_STRATEGY``, ``Z_FILTERED``, and ``Z_HUFFMAN_ONLY``.

Lib/test/test_binascii.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ def test_hex(self):
179179
self.assertEqual(binascii.unhexlify(self.type2test(t)), u)
180180

181181
def test_qp(self):
182+
binascii.a2b_qp(data=b"", header=False) # Keyword arguments allowed
183+
182184
# A test for SF bug 534347 (segfaults without the proper fix)
183185
try:
184186
binascii.a2b_qp(b"", **{1:1})
185187
except TypeError:
186188
pass
187189
else:
188190
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
191+
189192
self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
190193
self.assertEqual(binascii.a2b_qp(b"=="), b"=")
191194
self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")

Lib/test/test_os.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# Tests creating TESTFN
5959
class FileTests(unittest.TestCase):
6060
def setUp(self):
61-
if os.path.exists(support.TESTFN):
61+
if os.path.lexists(support.TESTFN):
6262
os.unlink(support.TESTFN)
6363
tearDown = setUp
6464

@@ -162,6 +162,19 @@ def test_replace(self):
162162
with open(TESTFN2, 'r') as f:
163163
self.assertEqual(f.read(), "1")
164164

165+
def test_open_keywords(self):
166+
f = os.open(path=__file__, flags=os.O_RDONLY, mode=0o777,
167+
dir_fd=None)
168+
os.close(f)
169+
170+
def test_symlink_keywords(self):
171+
symlink = support.get_attribute(os, "symlink")
172+
try:
173+
symlink(src='target', dst=support.TESTFN,
174+
target_is_directory=False, dir_fd=None)
175+
except (NotImplementedError, OSError):
176+
pass # No OS support or unprivileged user
177+
165178

166179
# Test attributes on return values from os.*stat* family.
167180
class StatAttributeTests(unittest.TestCase):
@@ -2151,6 +2164,14 @@ def test_invalid_offset(self):
21512164
os.sendfile(self.sockno, self.fileno, -1, 4096)
21522165
self.assertEqual(cm.exception.errno, errno.EINVAL)
21532166

2167+
def test_keywords(self):
2168+
# Keyword arguments should be supported
2169+
os.sendfile(out=self.sockno, offset=0, count=4096,
2170+
**{'in': self.fileno})
2171+
if self.SUPPORT_HEADERS_TRAILERS:
2172+
os.sendfile(self.sockno, self.fileno, offset=0, count=4096,
2173+
headers=None, trailers=None, flags=0)
2174+
21542175
# --- headers / trailers tests
21552176

21562177
@requires_headers_trailers

Lib/test/test_popen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def test_iterating(self):
5757
with os.popen("echo hello") as f:
5858
self.assertEqual(list(f), ["hello\n"])
5959

60+
def test_keywords(self):
61+
with os.popen(cmd="exit 0", mode="w", buffering=-1):
62+
pass
63+
6064
def test_main():
6165
support.run_unittest(PopenTest)
6266

Lib/test/test_posix.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ def test_mknod(self):
443443
else:
444444
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
445445

446+
# Keyword arguments are also supported
447+
support.unlink(support.TESTFN)
448+
try:
449+
posix.mknod(path=support.TESTFN, mode=mode, device=0,
450+
dir_fd=None)
451+
except OSError as e:
452+
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
453+
446454
@unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
447455
@unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
448456
def test_makedev(self):

Lib/test/test_zlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,20 @@ def test_compressoptions(self):
222222
level = 2
223223
method = zlib.DEFLATED
224224
wbits = -12
225-
memlevel = 9
225+
memLevel = 9
226226
strategy = zlib.Z_FILTERED
227-
co = zlib.compressobj(level, method, wbits, memlevel, strategy)
227+
co = zlib.compressobj(level, method, wbits, memLevel, strategy)
228228
x1 = co.compress(HAMLET_SCENE)
229229
x2 = co.flush()
230230
dco = zlib.decompressobj(wbits)
231231
y1 = dco.decompress(x1 + x2)
232232
y2 = dco.flush()
233233
self.assertEqual(HAMLET_SCENE, y1 + y2)
234234

235+
# keyword arguments should also be supported
236+
zlib.compressobj(level=level, method=method, wbits=wbits,
237+
memLevel=memLevel, strategy=strategy, zdict=b"")
238+
235239
def test_compressincremental(self):
236240
# compress object in steps, decompress object as one-shot
237241
data = HAMLET_SCENE * 128

Modules/posixmodule.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,7 +4695,7 @@ posix_uname(PyObject *self, PyObject *noargs)
46954695

46964696

46974697
PyDoc_STRVAR(posix_utime__doc__,
4698-
"utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\
4698+
"utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
46994699
Set the access and modified time of path.\n\
47004700
\n\
47014701
path may always be specified as a string.\n\
@@ -4704,10 +4704,10 @@ On some platforms, path may also be specified as an open file descriptor.\n\
47044704
\n\
47054705
If times is not None, it must be a tuple (atime, mtime);\n\
47064706
atime and mtime should be expressed as float seconds since the epoch.\n\
4707-
If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\
4707+
If ns is specified, it must be a tuple (atime_ns, mtime_ns);\n\
47084708
atime_ns and mtime_ns should be expressed as integer nanoseconds\n\
47094709
since the epoch.\n\
4710-
If both times and ns are None, utime uses the current time.\n\
4710+
If times is None and ns is unspecified, utime uses the current time.\n\
47114711
Specifying tuples for both times and ns is an error.\n\
47124712
\n\
47134713
If dir_fd is not None, it should be a file descriptor open to a directory,\n\
@@ -8245,10 +8245,10 @@ posix_write(PyObject *self, PyObject *args)
82458245

82468246
#ifdef HAVE_SENDFILE
82478247
PyDoc_STRVAR(posix_sendfile__doc__,
8248-
"sendfile(out, in, offset, nbytes) -> byteswritten\n\
8249-
sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
8248+
"sendfile(out, in, offset, count) -> byteswritten\n\
8249+
sendfile(out, in, offset, count, headers=None, trailers=None, flags=0)\n\
82508250
-> byteswritten\n\
8251-
Copy nbytes bytes from file descriptor in to file descriptor out.");
8251+
Copy count bytes from file descriptor in to file descriptor out.");
82528252

82538253
static PyObject *
82548254
posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
@@ -8266,6 +8266,7 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
82668266
off_t sbytes;
82678267
struct sf_hdtr sf;
82688268
int flags = 0;
8269+
/* Beware that "in" clashes with Python's own "in" operator keyword */
82698270
static char *keywords[] = {"out", "in",
82708271
"offset", "count",
82718272
"headers", "trailers", "flags", NULL};
@@ -8655,9 +8656,9 @@ posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs)
86558656

86568657
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
86578658
PyDoc_STRVAR(posix_mknod__doc__,
8658-
"mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\
8659+
"mknod(path, mode=0o600, device=0, *, dir_fd=None)\n\n\
86598660
Create a filesystem node (file, device special file or named pipe)\n\
8660-
named filename. mode specifies both the permissions to use and the\n\
8661+
named path. mode specifies both the permissions to use and the\n\
86618662
type of node to be created, being combined (bitwise OR) with one of\n\
86628663
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
86638664
device defines the newly created device special file (probably using\n\

0 commit comments

Comments
 (0)