Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:17:17+00:00 2026-05-26T04:17:17+00:00

The python codecs module provides StreamWriter classes for transparently encoding output streams. For instance:

  • 0

The python codecs module provides StreamWriter classes for transparently encoding output streams. For instance:

outstream = codecs.getwriter('utf8')(sys.__stdout__)
outstream.write(u'\u2713')
outstream.write(' A-OK!\n') # I want this to fail!
outstream.close()

However the problem I have with the default StreamWriter is that it will except str objects as well as unicode objects. If my program is writing a str to this stream, it is a bug and I want it to fail! Is there a way to make this happen without writing my own StreamWriter that enforces the type of objects written?

Also, I don’t want my solution to be sensitive to sys.stdout.encoding, sys.stdout.isatty(), locale.getpreferredencoding(), sys.getfilesystemencoding(), os.environ["PYTHONIOENCODING"] or whatever other ways python has of trying to be clever.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:17:18+00:00Added an answer on May 26, 2026 at 4:17 am

    If possible, do what you’re trying to do in Python 3, which has a much stronger distinction between unicode and bytes. Failing that, you’ll need to subclass StreamWriter, for example:

    import codecs
    
    class StrictUTF8Writer(codecs.StreamWriter):
        '''A StreamWriter for utf8 that requires written objects be unicode'''
        encode = codecs.utf_8_encode
    
        def write(self, object):
            if not isinstance(object, unicode):
                raise ValueError('write() requires unicode object')
            return codecs.StreamWriter.write(self, object)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Importing the standard logging module pollutes sys.modules with a bunch of dummy entries: Python
I'm reading in a file with Python's csv module, and have Yet Another Encoding
python's time module seems a little haphazard. For example, here is a list of
I'm calling pipe.communicate from Python's subprocess module from Python 2.6. I get the following
I have a script that uses the cmd Python module. The cmd module uses
I am stuck with a fairly complex Python module that does not return useful
Python is supposed to be strongly typed. For instance: 'abc'['1'] won't work, because you're
Here's a little program: #!/usr/bin/env python # -*- encoding: utf-8 -*- print('abcd kΩ ☠
im new to python gasp . im wondering do i need extra add-on module
Does anybody know how to get the encoding of a file in Python. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.