Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56f6fad

Browse files
authored
Merge pull request#4064 from youknowone/unittests
Unittests
2 parents8577a3f +bf53bb0 commit56f6fad

39 files changed

+2162
-1157
lines changed

‎Lib/runpy.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
importsys
1414
importimportlib.machinery# importlib first so we can test #15386 via -m
1515
importimportlib.util
16+
importio
1617
importtypes
17-
frompkgutilimportread_code,get_importer
18+
importos
1819

1920
__all__= [
2021
"run_module","run_path",
@@ -131,6 +132,9 @@ def _get_module_details(mod_name, error=ImportError):
131132
# importlib, where the latter raises other errors for cases where
132133
# pkgutil previously raised ImportError
133134
msg="Error while finding module specification for {!r} ({}: {})"
135+
ifmod_name.endswith(".py"):
136+
msg+= (f". Try using '{mod_name[:-3]}' instead of "
137+
f"'{mod_name}' as the module name.")
134138
raiseerror(msg.format(mod_name,type(ex).__name__,ex))fromex
135139
ifspecisNone:
136140
raiseerror("No module named %s"%mod_name)
@@ -194,9 +198,24 @@ def _run_module_as_main(mod_name, alter_argv=True):
194198

195199
defrun_module(mod_name,init_globals=None,
196200
run_name=None,alter_sys=False):
197-
"""Execute a module's code without importing it
201+
"""Execute a module's code without importing it.
198202
199-
Returns the resulting top level namespace dictionary
203+
mod_name -- an absolute module name or package name.
204+
205+
Optional arguments:
206+
init_globals -- dictionary used to pre-populate the module’s
207+
globals dictionary before the code is executed.
208+
209+
run_name -- if not None, this will be used for setting __name__;
210+
otherwise, __name__ will be set to mod_name + '__main__' if the
211+
named module is a package and to just mod_name otherwise.
212+
213+
alter_sys -- if True, sys.argv[0] is updated with the value of
214+
__file__ and sys.modules[__name__] is updated with a temporary
215+
module object for the module being executed. Both are
216+
restored to their original values before the function returns.
217+
218+
Returns the resulting module globals dictionary.
200219
"""
201220
mod_name,mod_spec,code=_get_module_details(mod_name)
202221
ifrun_nameisNone:
@@ -228,27 +247,35 @@ def _get_main_module_details(error=ImportError):
228247

229248
def_get_code_from_file(run_name,fname):
230249
# Check for a compiled file first
231-
withopen(fname,"rb")asf:
250+
frompkgutilimportread_code
251+
decoded_path=os.path.abspath(os.fsdecode(fname))
252+
withio.open_code(decoded_path)asf:
232253
code=read_code(f)
233254
ifcodeisNone:
234255
# That didn't work, so try it as normal source code
235-
withopen(fname,"rb")asf:
256+
withio.open_code(decoded_path)asf:
236257
code=compile(f.read(),fname,'exec')
237258
returncode,fname
238259

239260
defrun_path(path_name,init_globals=None,run_name=None):
240-
"""Execute code located at the specified filesystem location
261+
"""Execute code located at the specified filesystem location.
262+
263+
path_name -- filesystem location of a Python script, zipfile,
264+
or directory containing a top level __main__.py script.
265+
266+
Optional arguments:
267+
init_globals -- dictionary used to pre-populate the module’s
268+
globals dictionary before the code is executed.
241269
242-
Returns the resulting top level namespace dictionary
270+
run_name -- if not None, this will be used to set __name__;
271+
otherwise, '<run_path>' will be used for __name__.
243272
244-
The file path may refer directly to a Python script (i.e.
245-
one that could be directly executed with execfile) or else
246-
it may refer to a zipfile or directory containing a top
247-
level __main__.py script.
273+
Returns the resulting module globals dictionary.
248274
"""
249275
ifrun_nameisNone:
250276
run_name="<run_path>"
251277
pkg_name=run_name.rpartition(".")[0]
278+
frompkgutilimportget_importer
252279
importer=get_importer(path_name)
253280
# Trying to avoid importing imp so as to not consume the deprecation warning.
254281
is_NullImporter=False

‎Lib/sched.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@
2626
importtime
2727
importheapq
2828
fromcollectionsimportnamedtuple
29+
fromitertoolsimportcount
2930
importthreading
3031
fromtimeimportmonotonicas_time
3132

3233
__all__= ["scheduler"]
3334

34-
classEvent(namedtuple('Event','time, priority, action, argument, kwargs')):
35-
__slots__= []
36-
def__eq__(s,o):return (s.time,s.priority)== (o.time,o.priority)
37-
def__lt__(s,o):return (s.time,s.priority)< (o.time,o.priority)
38-
def__le__(s,o):return (s.time,s.priority)<= (o.time,o.priority)
39-
def__gt__(s,o):return (s.time,s.priority)> (o.time,o.priority)
40-
def__ge__(s,o):return (s.time,s.priority)>= (o.time,o.priority)
41-
35+
Event=namedtuple('Event','time, priority, sequence, action, argument, kwargs')
4236
Event.time.__doc__= ('''Numeric type compatible with the return value of the
4337
timefunc function passed to the constructor.''')
4438
Event.priority.__doc__= ('''Events scheduled for the same time will be executed
4539
in the order of their priority.''')
40+
Event.sequence.__doc__= ('''A continually increasing sequence number that
41+
separates events if time and priority are equal.''')
4642
Event.action.__doc__= ('''Executing the event means executing
4743
action(*argument, **kwargs)''')
4844
Event.argument.__doc__= ('''argument is a sequence holding the positional
@@ -61,6 +57,7 @@ def __init__(self, timefunc=_time, delayfunc=time.sleep):
6157
self._lock=threading.RLock()
6258
self.timefunc=timefunc
6359
self.delayfunc=delayfunc
60+
self._sequence_generator=count()
6461

6562
defenterabs(self,time,priority,action,argument=(),kwargs=_sentinel):
6663
"""Enter a new event in the queue at an absolute time.
@@ -71,8 +68,10 @@ def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
7168
"""
7269
ifkwargsis_sentinel:
7370
kwargs= {}
74-
event=Event(time,priority,action,argument,kwargs)
71+
7572
withself._lock:
73+
event=Event(time,priority,next(self._sequence_generator),
74+
action,argument,kwargs)
7675
heapq.heappush(self._queue,event)
7776
returnevent# The ID
7877

@@ -136,7 +135,8 @@ def run(self, blocking=True):
136135
withlock:
137136
ifnotq:
138137
break
139-
time,priority,action,argument,kwargs=q[0]
138+
(time,priority,sequence,action,
139+
argument,kwargs)=q[0]
140140
now=timefunc()
141141
iftime>now:
142142
delay=True

‎Lib/secrets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
importbase64
1616
importbinascii
17-
importos
1817

1918
fromhmacimportcompare_digest
2019
fromrandomimportSystemRandom
@@ -44,7 +43,7 @@ def token_bytes(nbytes=None):
4443
"""
4544
ifnbytesisNone:
4645
nbytes=DEFAULT_ENTROPY
47-
returnos.urandom(nbytes)
46+
return_sysrand.randbytes(nbytes)
4847

4948
deftoken_hex(nbytes=None):
5049
"""Return a random text string, in hexadecimal.

‎Lib/socket.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
socket() -- create a new socket object
1313
socketpair() -- create a pair of new socket objects [*]
1414
fromfd() -- create a socket object from an open file descriptor [*]
15+
send_fds() -- Send file descriptor to the socket.
16+
recv_fds() -- Recieve file descriptors from the socket.
1517
fromshare() -- create a socket object from data received from socket.share() [*]
1618
gethostname() -- return the current hostname
1719
gethostbyname() -- map a hostname to its IP number
@@ -104,7 +106,6 @@ def _intenum_converter(value, enum_klass):
104106
exceptValueError:
105107
returnvalue
106108

107-
_realsocket=socket
108109

109110
# WSA error codes
110111
ifsys.platform.lower().startswith("win"):
@@ -336,6 +337,7 @@ def makefile(self, mode="r", buffering=None, *,
336337
buffer=io.BufferedWriter(raw,buffering)
337338
ifbinary:
338339
returnbuffer
340+
encoding=io.text_encoding(encoding)
339341
text=io.TextIOWrapper(buffer,encoding,errors,newline)
340342
text.mode=mode
341343
returntext
@@ -376,7 +378,7 @@ def _sendfile_use_sendfile(self, file, offset=0, count=None):
376378
try:
377379
whileTrue:
378380
iftimeoutandnotselector_select(timeout):
379-
raise_socket.timeout('timed out')
381+
raiseTimeoutError('timed out')
380382
ifcount:
381383
blocksize=count-total_sent
382384
ifblocksize<=0:
@@ -543,6 +545,40 @@ def fromfd(fd, family, type, proto=0):
543545
nfd=dup(fd)
544546
returnsocket(family,type,proto,nfd)
545547

548+
ifhasattr(_socket.socket,"sendmsg"):
549+
importarray
550+
551+
defsend_fds(sock,buffers,fds,flags=0,address=None):
552+
""" send_fds(sock, buffers, fds[, flags[, address]]) -> integer
553+
554+
Send the list of file descriptors fds over an AF_UNIX socket.
555+
"""
556+
returnsock.sendmsg(buffers, [(_socket.SOL_SOCKET,
557+
_socket.SCM_RIGHTS,array.array("i",fds))])
558+
__all__.append("send_fds")
559+
560+
ifhasattr(_socket.socket,"recvmsg"):
561+
importarray
562+
563+
defrecv_fds(sock,bufsize,maxfds,flags=0):
564+
""" recv_fds(sock, bufsize, maxfds[, flags]) -> (data, list of file
565+
descriptors, msg_flags, address)
566+
567+
Receive up to maxfds file descriptors returning the message
568+
data and a list containing the descriptors.
569+
"""
570+
# Array of ints
571+
fds=array.array("i")
572+
msg,ancdata,flags,addr=sock.recvmsg(bufsize,
573+
_socket.CMSG_LEN(maxfds*fds.itemsize))
574+
forcmsg_level,cmsg_type,cmsg_datainancdata:
575+
if (cmsg_level==_socket.SOL_SOCKETandcmsg_type==_socket.SCM_RIGHTS):
576+
fds.frombytes(cmsg_data[:
577+
len(cmsg_data)- (len(cmsg_data)%fds.itemsize)])
578+
579+
returnmsg,list(fds),flags,addr
580+
__all__.append("recv_fds")
581+
546582
ifhasattr(_socket.socket,"share"):
547583
deffromshare(info):
548584
""" fromshare(info) -> socket object
@@ -671,7 +707,7 @@ def readinto(self, b):
671707
self._timeout_occurred=True
672708
raise
673709
excepterrorase:
674-
ife.args[0]in_blocking_errnos:
710+
ife.errnoin_blocking_errnos:
675711
returnNone
676712
raise
677713

@@ -687,7 +723,7 @@ def write(self, b):
687723
returnself._sock.send(b)
688724
excepterrorase:
689725
# XXX what about EINTR?
690-
ife.args[0]in_blocking_errnos:
726+
ife.errnoin_blocking_errnos:
691727
returnNone
692728
raise
693729

@@ -746,8 +782,9 @@ def getfqdn(name=''):
746782
An empty argument is interpreted as meaning the local host.
747783
748784
First the hostname returned by gethostbyaddr() is checked, then
749-
possibly existing aliases. In case no FQDN is available, hostname
750-
from gethostname() is returned.
785+
possibly existing aliases. In case no FQDN is available and `name`
786+
was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
787+
hostname from gethostname() is returned.
751788
"""
752789
name=name.strip()
753790
ifnotnameorname=='0.0.0.0':

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp