diff --git a/IPython/utils/io.py b/IPython/utils/io.py
index df1e39e60..3d236eb4d 100644
--- a/IPython/utils/io.py
+++ b/IPython/utils/io.py
@@ -267,17 +267,18 @@ def atomic_writing(path, text=True, encoding='utf-8', **kwargs):
         path = os.path.join(os.path.dirname(path), os.readlink(path))
 
     dirname, basename = os.path.split(path)
-    handle, tmp_path = tempfile.mkstemp(prefix=basename, dir=dirname)
+    tmp_dir = tempfile.mkdtemp(prefix=basename, dir=dirname)
+    tmp_path = os.path.join(tmp_dir, basename)
     if text:
-        fileobj = io.open(handle, 'w', encoding=encoding, **kwargs)
+        fileobj = io.open(tmp_path, 'w', encoding=encoding, **kwargs)
     else:
-        fileobj = io.open(handle, 'wb', **kwargs)
+        fileobj = io.open(tmp_path, 'wb', **kwargs)
 
     try:
         yield fileobj
     except:
         fileobj.close()
-        os.remove(tmp_path)
+        shutil.rmtree(tmp_dir)
         raise
 
     # Flush to disk
@@ -299,6 +300,7 @@ def atomic_writing(path, text=True, encoding='utf-8', **kwargs):
         os.remove(path)
 
     os.rename(tmp_path, path)
+    shutil.rmtree(tmp_dir)
 
 
 def raw_print(*args, **kw):

atomic write umask
Reported on gitter.im/ipython/ipython by @bigzachattack 

Sadly no one was on the chat at that time, too busy with a big bearded men 
dressed in red trying to smuggle things in our houses through the cheminee. 

```
I noticed today working in master, that any new notebook I create or copy has permissions
0o600 and ignores my umask. In IPython.utils.io.atomic_writing() uses mkstemp() to create
a temporary file for the atomic write. According to the python docs, mkstemp creates
files as 0o600.

After the write succeeds to the tmp file, _copy_metadata is called to copy the metadata
from the original file to destination file. It will throw an exception if there is no
source file. Thus when the notebook is copied into the notebook dir, it has
permissions 0o600.

Is this desired behavior, temporary, or a bug? I work in an environment where are default
permissions are 0o660 to allow for users to easily share information, so defaulting new
notebooks to 0o600 seriously inhibits this ability.
```
diff --git a/IPython/utils/tests/test_io.py b/IPython/utils/tests/test_io.py
index 023c9641b..aa00a882b 100644
--- a/IPython/utils/tests/test_io.py
+++ b/IPython/utils/tests/test_io.py
@@ -1,16 +1,9 @@
 # encoding: utf-8
 """Tests for io.py"""
 
-#-----------------------------------------------------------------------------
-#  Copyright (C) 2008-2011  The IPython Development Team
-#
-#  Distributed under the terms of the BSD License.  The full license is in
-#  the file COPYING, distributed as part of this software.
-#-----------------------------------------------------------------------------
-
-#-----------------------------------------------------------------------------
-# Imports
-#-----------------------------------------------------------------------------
+# Copyright (c) IPython Development Team.
+# Distributed under the terms of the Modified BSD License.
+
 from __future__ import print_function
 from __future__ import absolute_import
 
@@ -24,7 +17,7 @@
 
 import nose.tools as nt
 
-from IPython.testing.decorators import skipif
+from IPython.testing.decorators import skipif, skip_win32
 from IPython.utils.io import (Tee, capture_output, unicode_std_stream,
                               atomic_writing,
                               )
@@ -36,10 +29,6 @@
 else:
     from StringIO import StringIO
 
-#-----------------------------------------------------------------------------
-# Tests
-#-----------------------------------------------------------------------------
-
 
 def test_tee_simple():
     "Very simple check with stdout only"
@@ -177,6 +166,33 @@ class CustomExc(Exception): pass
             with stdlib_io.open(f1, 'r') as f:
                 nt.assert_equal(f.read(), u'written from symlink')
 
+def _save_umask():
+    global umask
+    umask = os.umask(0)
+    os.umask(umask)
+
+def _restore_umask():
+    os.umask(umask)
+
+@skip_win32
+@nt.with_setup(_save_umask, _restore_umask)
+def test_atomic_writing_umask():
+    with TemporaryDirectory() as td:
+        os.umask(0o022)
+        f1 = os.path.join(td, '1')
+        with atomic_writing(f1) as f:
+            f.write(u'1')
+        mode = stat.S_IMODE(os.stat(f1).st_mode)
+        nt.assert_equal(mode, 0o644, '{:o} != 644'.format(mode))
+
+        os.umask(0o057)
+        f2 = os.path.join(td, '2')
+        with atomic_writing(f2) as f:
+            f.write(u'2')
+        mode = stat.S_IMODE(os.stat(f2).st_mode)
+        nt.assert_equal(mode, 0o620, '{:o} != 620'.format(mode))
+
+
 def test_atomic_writing_newlines():
     with TemporaryDirectory() as td:
         path = os.path.join(td, 'testfile')

pip install -e .[all]
parse_log_pytest
pytest
mock sphinx pandoc pytest
apt-get update apt-get install -y gcc
3.7
docs/source/install/install.rst
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.13
attrs @ file:///croot/attrs_1668696182826/work
Babel==2.14.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
charset-normalizer==3.4.1
docutils==0.19
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
idna==3.10
imagesize==1.4.1
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1648562407465/work
importlib-resources==5.12.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
-e git+https://github.com/ipython/ipython.git@296f56bf70643d1d19ae0c1ab2a9d86b326d5559#egg=ipython
Jinja2==3.1.6
jsonschema==4.17.3
MarkupSafe==2.1.5
mistune==3.0.2
mock==5.2.0
nose==1.3.7
numpydoc==1.5.0
packaging @ file:///croot/packaging_1671697413597/work
pandoc==2.4
pkgutil_resolve_name==1.3.10
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
plumbum==1.8.3
ply==3.11
py @ file:///opt/conda/conda-bld/py_1644396412707/work
Pygments==2.17.2
pyrsistent==0.19.3
pytest==7.1.2
pytz==2025.2
pyzmq==26.2.1
requests==2.31.0
snowballstemmer==2.2.0
Sphinx==5.3.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
tornado==6.2
typing_extensions @ file:///croot/typing_extensions_1669924550328/work
urllib3==2.0.7
zipp @ file:///croot/zipp_1672387121353/work

name: ipython
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib-metadata=4.11.3=py37h06a4308_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - typing_extensions=4.4.0=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.13
      - babel==2.14.0
      - charset-normalizer==3.4.1
      - docutils==0.19
      - idna==3.10
      - imagesize==1.4.1
      - importlib-resources==5.12.0
      - jinja2==3.1.6
      - jsonschema==4.17.3
      - markupsafe==2.1.5
      - mistune==3.0.2
      - mock==5.2.0
      - nose==1.3.7
      - numpydoc==1.5.0
      - pandoc==2.4
      - pkgutil-resolve-name==1.3.10
      - plumbum==1.8.3
      - ply==3.11
      - pygments==2.17.2
      - pyrsistent==0.19.3
      - pytz==2025.2
      - pyzmq==26.2.1
      - requests==2.31.0
      - snowballstemmer==2.2.0
      - sphinx==5.3.0
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tornado==6.2
      - urllib3==2.0.7
prefix: /opt/conda/envs/ipython

IPython/utils/tests/test_io.py::test_atomic_writing_umask
IPython/utils/tests/test_io.py::test_tee_simple IPython/utils/tests/test_io.py::TeeTestCase::test IPython/utils/tests/test_io.py::test_io_init IPython/utils/tests/test_io.py::test_capture_output IPython/utils/tests/test_io.py::test_UnicodeStdStream IPython/utils/tests/test_io.py::test_UnicodeStdStream_nowrap IPython/utils/tests/test_io.py::test_atomic_writing IPython/utils/tests/test_io.py::test_atomic_writing_newlines
IPython/utils/io.py
IPython/utils/io.py