question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Can't use a string pattern on a bytes-like object

See original GitHub issue

When I try to export a file with the flags --gfm --export, I get the following error:

grip --gfm --export Kalender.md
Exporting to Kalender.html
Traceback (most recent call last):
  File "/usr/local/bin/grip", line 9, in <module>
    load_entry_point('grip==3.2.0', 'console_scripts', 'grip')()
  File "/usr/local/lib/python3.4/dist-packages/grip/command.py", line 78, in main
    True, args['<address>'])
  File "/usr/local/lib/python3.4/dist-packages/grip/exporter.py", line 36, in export
    render_offline, render_wide, render_inline)
  File "/usr/local/lib/python3.4/dist-packages/grip/exporter.py", line 18, in render_page
    return render_app(app)
  File "/usr/local/lib/python3.4/dist-packages/grip/renderer.py", line 8, in render_app
    response = c.get('/')
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/test.py", line 774, in get
    return self.open(*args, **kw)
  File "/usr/local/lib/python3.4/dist-packages/flask/testing.py", line 108, in open
    follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/test.py", line 742, in open
    response = self.run_wsgi_app(environ, buffered=buffered)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/test.py", line 659, in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/test.py", line 867, in run_wsgi_app
    app_iter = app(environ, start_response)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1470, in full_dispatch_request
    self.try_trigger_before_first_request_functions()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1497, in try_trigger_before_first_request_functions
    func()
  File "/usr/local/lib/python3.4/dist-packages/grip/server.py", line 111, in retrieve_styles
    app.config['STYLE_ASSET_URLS_INLINE']))
  File "/usr/local/lib/python3.4/dist-packages/grip/server.py", line 304, in _get_styles
    content = re.sub(asset_pattern, match_asset, _download(app, style_url))
  File "/usr/lib/python3.4/re.py", line 175, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: can't use a string pattern on a bytes-like object

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ssokolowcommented, Mar 4, 2015

I ran into that error while working to get one of my scripts working on Python 3.x. Thankfully, I was working on a unit test suite because, given the kind of code it was, it needed to accept varied input and that was a pain to get the semantics correct for.

Judging by that backtrace, asset_pattern is str() while _download(app, style_url) is returning bytes().

In Python 2.x, str = bytes and it’ll automatically coerce str to unicode as necessary (causing a UnicodeDecodeError if the string contains characters outside the 7-bit ASCII range… which isn’t a problem with hard-coded regex patterns).

In Python 3.x, str = unicode and it treats str<->bytes conversion as strictly as it treats str<->int conversion.

The solution is to either make _download return a decoded Unicode string or make the regex pattern a bytes. (I prefer doing the former by using the Requests library’s Response.text property to retrieve the content.)

0reactions
joeyespocommented, Jun 1, 2015

Addressed this in b995503fc8e73000ca8fc541fa325f3999154c9b and 92d2f2c85f9768f129e03dc86c60a33f5cebea22. I tested it with Python 3.4.2. Try installing from master and seeing if it works for you too. Feel free to re-open if it’s still broken.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: can't use a string pattern on a bytes-like object in re ...
This solved the error TypeError: cannot use a string pattern on a bytes-like object but then I got errors like UnicodeDecodeError: 'utf-8' codec ......
Read more >
Cannot use a string pattern on a bytes-like object in Python
The Python "TypeError: cannot use a string pattern on a bytes-like object" occurs when we try to use a string pattern to match...
Read more >
TypeError: cannot use a string pattern on a bytes-like
Bytes objects contain a sequence of single bytes. They are immutable, like strings, which means they cannot be changed. A bytes object is ......
Read more >
TypeError: can't use a string pattern on a bytes-like object in re ...
PYTHON : TypeError : can't use a string pattern on a bytes-like object in re.findall() [ Gift : Animated Search Engine ...
Read more >
TypeError: can't use a string pattern on a bytes-like object
Coding example for the question TypeError: can't use a string pattern on a bytes-like object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found