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

  • SEARCH
  • Home
  • 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 9221189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:26+00:00 2026-06-18T03:31:26+00:00

Is there a way to either: Always have -t enabled by default (warn about

  • 0

Is there a way to either:

  • Always have -t enabled by default (warn about inconsistent tab usage)
  • Be able to enable it programmatically on startup (eg. in a sitecustomize.py module)

It would need to work for embedded Python too (so aliasing python or similar solutions will be of no use). Use of sitecustomize.py allows us to hook into embedded Python instances, so this seems like a good place for it.

I thought the warnings module would provide a way to turn this warning on but I don’t see anything.

For reference:

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
...
-t     : issue warnings about inconsistent tab usage (-tt: issue errors)
...

Any suggestions on how this might be done?

Thanks.

  • 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-06-18T03:31:27+00:00Added an answer on June 18, 2026 at 3:31 am

    The only solution I was able to come to involves import hooks. While this is somewhat beyond what I’d hoped to have to do, I felt it was a good excuse for me to learn how they work.

    This solution doesn’t check for “inconsistent whitespace” it just checks for tabs, but it would be easy to extend.

    Here is the result:

    import sys
    import imp
    import warnings
    
    
    class TabCheckImporter(object):
    
        """Finder and loader class for checking for the presence of tabs
    
        """
    
        def find_module(self, fullname, path=None):
            """Module finding method
    
            """
            # Save the path so we know where to look in load_module
            self.path = path
            return self
    
        def load_module(self, name):
            """Module loading method.
    
            """
            # Check if it was already imported
            module = sys.modules.get(name)
            if module is not None:
                return module
    
            # Find the module and check for tabs
            file_, pathname, description = imp.find_module(name, self.path)
            try:
                content = file_.read()
                tab = content.find("\t")
                if tab > -1:
                    lineno = content[:tab].count("\n") + 1
                    warnings.warn_explicit(
                            "module '{0}' contains a tab character".format(name),
                            ImportWarning,
                            pathname,
                            lineno)
            except Exception as e:
                warnings.warn("Module '{0}' could not be checked".format(name),
                              ImportWarning)
    
            # Import the module
            try:
                module = imp.load_module(name, file_, pathname, description)
            finally:
                if file_:
                    file_.close()
    
            sys.modules[name] = module
            return module
    
    
    # Register the hook
    sys.meta_path = (sys.meta_path or []) + [TabCheckImporter()]
    
    # Enable ImportWarnings
    warnings.simplefilter("always", ImportWarning)
    

    Importing this file (replacing ->| with a literal tab):

    # File: test_tabbed.py
    if True:
    ->| print "This line starts with a tab"
    

    Yields this output:

    $ python -c 'import hook; import test_normal; import test_tabbed;'
    test_tabbed.py:3: ImportWarning: module 'test_tabbed' contains a tab character
      print "This line starts with a tab"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get the style: display attribute which would have either
Is there a way to either suppress the generation of destructors for global objects
I'm very new to Oracle and was wondering if there's a way either through
Is there a way to zip a string (either with a gem or standard
Is there a way that I can add non-static data to dataannotation attributes (either
Is there any way to create breakpoints in clojurescript? Either in the repl or
Maybe there is no difference, but is either way better than the other (or
Is there any built in way in mysql in which it either inserts all
I'm using TFS 2010 and am wondering whether there is an easy way, either
Is there a way with dataTables to have it not show the sort icons

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.