[Python-Dev] Re: NamedTemporaryFile and context managers
08.04.21 23:31, Ethan Furman пише: > In issue14243 [1] there are two issues being tracked: > > - the difference in opening shared files between posix and Windows > - the behavior of closing the underlying file in the middle of > NamedTemporaryFile's context management > > I'd like to address and get feedback on the context management issue. > > ```python > from tempfile import NamedTemporaryFile > > with NamedTemporaryFile() as fp: > fp.write(b'some data') > fp = open(fp.name()) > data = fp.read() > > assert data == 'some_data' > ``` These issues are usually solved by using TemporaryDirectory: with TemporaryDirectory() as td: filename = os.path.join(td, 'tempfile') with open(filename, 'wb') as fp: fp.write(b'some data') with open(filename, 'rb') as fp: data = fp.read() What if make NamedTemporaryFile a wrapper around TemporaryDirectory and always create a new temporary directory for file? @contextmanager def NamedTemporaryFile(): with TemporaryDirectory() as td: filename = os.path.join(td, 'tempfile') with open(filename, 'wb') as fp: yield fp ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/2H2PIMPZ3VE2JDBUA7WVX25ULIC3C4SM/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Summary of Python tracker Issues
ACTIVITY SUMMARY (2021-04-02 - 2021-04-09) Python tracker at https://bugs.python.org/ To view or respond to any of the issues listed below, click on the issue. Do NOT respond to this message. Issues counts and deltas: open7524 (+19) closed 48008 (+70) total 55532 (+89) Open issues with patches: 2999 Issues opened (60) == #24160: Pdb sometimes raises exception when trying to remove a breakpo https://bugs.python.org/issue24160 reopened by pablogsal #39899: `pathlib.Path.expanduser()` does not call `os.path.expanduser( https://bugs.python.org/issue39899 reopened by steve.dower #43708: Tkinter theme settings object schema is missing https://bugs.python.org/issue43708 opened by WHK102 #43712: PEP 597: fileinput uses locale encoding https://bugs.python.org/issue43712 opened by methane #43714: re.split(), re.sub(): '\Z' must consume end of string if it ma https://bugs.python.org/issue43714 opened by alegrigoriev #43715: curses inch() and scrbkgd() documentation ommissions https://bugs.python.org/issue43715 opened by pjfarleyiii #43717: Confusing OSError on HTTP CONNECT failure https://bugs.python.org/issue43717 opened by michael-o #43718: HTTP CONNECT response not subject to debug level https://bugs.python.org/issue43718 opened by michael-o #43721: Documentation of property.{getter,setter,deleter} fails to men https://bugs.python.org/issue43721 opened by Antony.Lee #43722: PEP 597: FileCookieJar uses locale encoding https://bugs.python.org/issue43722 opened by methane #43723: Deprecate camelCase aliases from threading.py https://bugs.python.org/issue43723 opened by Jelle Zijlstra #43725: Create a release branch ABI stability regression test https://bugs.python.org/issue43725 opened by gregory.p.smith #43727: futures cancelled by ThreadPoolExecutor.shutdown() not yielded https://bugs.python.org/issue43727 opened by bigbenhur #43731: PEP 597: logging.fileConfig() uses locale encoding. https://bugs.python.org/issue43731 opened by methane #43732: PEP 597: mailcap uses locale encoding https://bugs.python.org/issue43732 opened by methane #43733: PEP 597: netrc uses locale encoding. https://bugs.python.org/issue43733 opened by methane #43735: PEP 597: os.popen() and pipes uses locale encoding https://bugs.python.org/issue43735 opened by methane #43737: Documentation of modulo operator should document behaviour cle https://bugs.python.org/issue43737 opened by anthony-flury #43738: Clarify public name of curses.window https://bugs.python.org/issue43738 opened by rmccampbell7 #43739: Fixing the example code in Doc/extending/extending.rst to decl https://bugs.python.org/issue43739 opened by shreyanavigyan #43740: Long paths in imp.load_dynamic() lead to segfault https://bugs.python.org/issue43740 opened by xxm #43741: http.client leaks from self.fp.read() https://bugs.python.org/issue43741 opened by HynekPetrak #43742: tcp_echo_client in asyncio streams example does not work. Hang https://bugs.python.org/issue43742 opened by jcolo #43743: BlockingIOError: [Errno 11] Resource temporarily unavailable: https://bugs.python.org/issue43743 opened by p.conesa.mingo #43744: enum: Adding a member named _classname__ raises IndexError https://bugs.python.org/issue43744 opened by smurfix #43749: venv module does not copy the correct python exe https://bugs.python.org/issue43749 opened by Ian Norton #43750: Undefined constant PACKET_MULTIHOST referred to in package soc https://bugs.python.org/issue43750 opened by Tom Cook #43751: await anext() returns None when default is given https://bugs.python.org/issue43751 opened by pewscorner #43752: [sqlite3] Fetching an empty value from date column raises Valu https://bugs.python.org/issue43752 opened by felixxm #43753: [C API] Add Py_Is(x, y) and Py_IsNone(x) functions https://bugs.python.org/issue43753 opened by vstinner #43754: Eliminate bindings for partial pattern matches https://bugs.python.org/issue43754 opened by brandtbucher #43756: About updating audit events when function gains new arguments https://bugs.python.org/issue43756 opened by gousaiyang #43757: pathlib: move 'resolve()' logic out of path flavour https://bugs.python.org/issue43757 opened by barneygale #43758: dict.config TimedRotatingFileHandler filename .suffix does not https://bugs.python.org/issue43758 opened by tea940314 #43760: The DISPATCH() macro is not as efficient as it could be. https://bugs.python.org/issue43760 opened by Mark.Shannon #43761: Documenting dataclass and namedtuple changes for structural pa https://bugs.python.org/issue43761 opened by freundTech #43762: Add audit events for loading of sqlite3 extensions https://bugs.python.org/issue43762 opened by erlendaasland #43763: [sqlite3] Use SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION iso. sqlit https://bugs.python.org/issue43763 opened by erlendaasland #43766: Implement PEP 647 (User-Defined Type Guards) in typing.py https://bugs.python.org/issue43766
[Python-Dev] Re: Python Core Mentorship program
Thanks ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/IZNMWR6KO5CQUXA6H6HU4HHTP3WVIRH4/ Code of Conduct: http://python.org/psf/codeofconduct/