urllib.parse photo

Incorrectly escaping characters is one of the more common errors I see developers run into. If you’re parsing, manipulating, or serializing URLs in code, you should try to use standard libraries that cover a lot of these nuances for you.

Python 3 has urllib.parse. That page covers really all you need to know about it, but sometimes one might think they can find an answer without reading the docs. For me, I had parsed a query string using parse_qs and was looking for the inverse function to turn such an object back into a string. Here’s the answer…

Inverting parse_qs

Parse a query string with parse_qs. This function expects no "?":

from urllib.parse import parse_qs

parse_qs('foo=bar&foo=baz&bing=bong')

# {'foo': ['bar', 'baz'], 'bing': ['bong']}

Wrong way: convert it back using urlencode (it serializes each array of values as a single value):

from urllib.parse import urlencode

urlencode({'foo': ['bar', 'baz'], 'bing': ['bong']})

# 'foo=%5B%27bar%27%2C+%27baz%27%5D&bing=%5B%27bong%27%5D'

Correct way: convert it back using urlencode with doseq=True:

from urllib.parse import urlencode

urlencode({'foo': ['bar', 'baz'], 'bing': ['bong']}, doseq=True)

# 'foo=bar&foo=baz&bing=bong'

Via help(urlencode):

urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=<function quote_plus>)

Encode a dict or sequence of two-element tuples into a URL query string.

If any values in the query arg are sequences and doseq is true, each sequence element is converted to a separate parameter.

Further reading

  1. urllib.parse.parse_qsl - this alternative to parse_qs makes a list of tuples instead of a dict, which means duplicate keys can safely exist at the top level. As a result, using urlencode with this format works as you’d expect regardless of whether you set doseq or not, since none of the values in the tuples are sequences.

  2. urllib.parse.urlparse - this is a nice way to parse an entire URL. The ParseResult object is immutable, but you can use _replace and geturl to create a new URL: parsed._replace(query=new_query).geturl()

  3. URLSearchParams - if you’re working with query strings in the browser, then this JavaScript interface is your friend and it has good browser support too.

  4. mrcoles.com/urlparse - an old blog post where you can paste in any URL and it will pretty-print the URL, query string, and hash separately.

In the recent update to Chrome 99 a number of users have encountered a bug where the in-browser database stops working. We received reports of this from GoFullPage users. Here are some steps to mitigate the issue.

The Chrome team has been helpful in tracking this issue and the following notes are mostly copied from Simeon’s response on this bug—thanks, Simeon et al.!

You can check for the problem by opening up the JavaScript console (right-click window > inspect > console) and seeing if there’s one of the following errors:

  • InvalidStateError: An operation that depends on state cached in an interface object was made but the state had changed since it was read from disk.
  • DOMException: Internal error retrieving bucket data directory
  • UnknownError: Internal error opening backing store for indexedDB.open

See the workarounds below. If you’re up for it (especially if you know your way around a terminal), then I recommend doing option 3, but the quickest way to get something going in a time crunch is option 1.

Workaround 1: Create a new profile

The issue is profile-specific. So if you create a new one or switch to another you can likely get around this issue. It’s the quickest way to get around this issue.

See instructions to add or switch a profile on support.google.com.

  1. On your computer, open Chrome.
  2. At the top right, click the circular Profile image.
  3. Click Add or select another one.
  4. Try the extension here (install it first if necessary since extensions are installed per-profile)

Workaround 2: Reset your current profile

This allows you to continue using Chrome as you normally do, but many features will go back to default settings. See these instructions on resetting your profile for info on what gets reset and what remains.

  1. On your computer, open Chrome.
  2. At the top right, click the 3 dots for “More” and then Settings and then Advanced.
  3. Follow the appropriate instructions for your operating system
    • On Chromebook, Linux, and Mac: Click Reset settings and then Restore settings to their original defaults and then Reset settings.
    • On Windows: Click Reset and cleanup and then Reset settings to their original defaults and then Reset settings.

Workaround 3: Manually delete your QuotaManager database

This approach will surgically address the underlying database corruption without affecting your profile settings. The QuotaManager database will be rebuilt next time Chrome starts.

Before proceeding, copying these instructions to a text file as you will have to close Chrome.

  1. Enter the URL chrome://version in the URL bar of Chrome
  2. Search for the key "Profile Path" and copy the value, which should look something like the following (note: each profile has a unique path):
    • Windows:
      C:\Users\MyUserName\AppData\Local\Google\Chrome\User Data\Default
    • Mac:
      /Users/MyUserName/Library/Application Support/Google/Chrome/Default
    • Linux:
      /home/MyUserName/.config/google-chrome/Default
  3. Navigate to this path in your operating system's file browser (likely easier from the terminal if possible)
  4. Completely exit Chrome.
  5. Delete the "QuotaManager" and "QuotaManager-journal" entries inside this directory. Using the Mac example from above it would look like this:
rm "/Users/MyUserName/Library/Application Support/Google/Chrome/Default/QuotaManager"
rm "/Users/MyUserName/Library/Application Support/Google/Chrome/Default/QuotaManager-journal"

6. Reopen Chrome.

If you’re encountering this issue and tried one of the workarounds, let me know below. If you want to contact support for the extension, then just follow the prompts in the extension and someone will help you out!

27 February 2022

Browser tab hygiene

My approach to maintaining proper browser tab hygiene and not getting overwhelmed by too many tabs. read full post…
A story of remembering a death scene from the video game Shadowgate. read full post…
A Wordle starting word randomizer. If the game feels too easy, then this can bring a little bit of fun chaos. read full post…

Peter Coles

Peter Coles

is a software engineer living in NYC who is building Superset 💪 and also created GoFullPage 📸
more »

github · soundcloud · @lethys · rss