diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
index 3e15f01..ebcd510 100644
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -324,10 +324,9 @@ def check_cpaste(code, should_fail=False):
     _ip.user_ns['code_ran'] = False
 
     src = StringIO()
-    try:
-        src.encoding = None  # IPython expects stdin to have an encoding attribute
-    except Exception:
-        pass                 # ...but it's a read-only attribute in Python 3
+    if not hasattr(src, 'encoding'):
+        # IPython expects stdin to have an encoding attribute
+        src.encoding = None
     src.write('\n')
     src.write(code)
     src.write('\n--\n')