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 8233881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:16:54+00:00 2026-06-07T18:16:54+00:00

According to this: http://code.activestate.com/lists/python-list/413540/ , tokenize.generate_tokens should be used and not tokenize.tokenize . This

  • 0

According to this: http://code.activestate.com/lists/python-list/413540/, tokenize.generate_tokens should be used and not tokenize.tokenize.

This works perfectly fine in Python 2.6. But it does not work anymore in Python 3:

>>> a = list(tokenize.generate_tokens(io.BytesIO("1\n".encode()).readline))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/tokenize.py", line 439, in _tokenize
    if line[pos] in '#\r\n':           # skip comments or blank lines

However, also in Python 3, this works (and returns also the desired output):

a = list(tokenize.tokenize(io.BytesIO("1\n".encode()).readline))

According to the documentation, it seems like tokenize.tokenize is the new way to use this module: http://docs.python.org/py3k/library/tokenize.html. tokenize.generate_tokens isn’t even documented anymore.

But, why is there still a generate_tokens function in this module, if it’s not documented? I haven’t found any PEP regarding this.

I’m trying to maintain a code base for Python 2.5-3.2, should I call generate_tokens for Python 2 and tokenize for Python 3? Aren’t there any better ways?

  • 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-07T18:16:56+00:00Added an answer on June 7, 2026 at 6:16 pm

    generate_tokens seems to be really a strange thing in Python 3. It doesn’t work like in Python 2. However, tokenize.tokenize behaves like the old Python 2 tokenize.generate_tokens. Therefore I wrote a little workaround:

    import tokenize                             
    if sys.hexversion >= 0x03000000d:                               
        tokenize_func = tokenize.tokenize       
    else:                                       
        tokenize_func = tokenize.generate_tokens
    

    Now I just use tokenize_func, which works without problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

according to this site http://www.cplusplus.com/reference/std/functional/unary_function/ this code should work #include <iostream> #include <functional> using
According to this: http://code.google.com/appengine/docs/whatisgoogleappengine.html it seems that GAE only uses Datastore to store data,
According to this reference http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html , tooltip.trigger = 'none' turns off tooltips on a
I am trying to execute the QR Code tutorial according to this website http://www.onbarcode.com/products/android_barcode/barcodes/qrcode.html
According to this documentation http://www.cplusplus.com/reference/clibrary/ctime/time/ for time(NULL) If the function could not retrieve the
I understand that according to this issue ticket on google code http://code.google.com/p/fullcalendar/issues/detail?id=143 that there
According to this article, http://msdn.microsoft.com/en-us/library/ms177197.aspx on MSDN, we should release the unmanaged resources in
I've got a PHP-fpm setup on nginx setup according to this article: http://interfacelab.com/nginx-php-fpm-apc-awesome/ PHP
http://www.frostjedi.com/terra/scripts/demo/jquery02.html According to this link elements can be moved around by doing $('#container1').append($('#container2')). Unfortunately,
According to the Grails literature http://grails.org/doc/2.0.x/ref/Domain%20Classes/getAll.html I should be able to do this def

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.