Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python and python3 wrappers for logging, sys.excepthook and WSGI #20

Merged
merged 1 commit into from
Feb 16, 2017

Conversation

pyhedgehog
Copy link
Collaborator

This is only preliminary changes.
Python hook can now be like this:

import microcule
def app(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    return "hello world\n"
if __name__ == '__main__':
    microcule.HookIOHandler(Hook).run(app)

Old format supported too.
Python module microcule injected alongside lua json module.

Limitations:

  1. I've tested it only on windows right now.
  2. Exceptions passed to stderr, but not processed.
  3. Not yet implemented sane way to change log message format.
  4. Hook['isHookio']==True is supported, but not tested.
  5. Streaming not yet tested or specially supported.
  6. Identifiers should be clarified.

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

Reviewing...

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

@pyhedgehog -

I don't understand, why is it required that we import microcule in user services?

It seems to me that we already rely on global variable environ, can we also put import microcule in __init__.py or micro-python binary?

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

@pyhedgehog -

What will happen to legacy Python services? Will they still work with this new code?

@pyhedgehog
Copy link
Collaborator Author

I don't require import microcule in user services. I propose to use it in newer form of service - WSGI-compatible. Old (non-wsgi) services will continue work as before.

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

The very first thing you show me is this:

import microcule
def app(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    return "hello world\n"
if __name__ == '__main__':
    microcule.HookIOHandler(Hook).run(app)

That is a user service which requires import microcule as the very first line...so you do require it...

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

User services shouldn't have to require hook.io.

JavaScript services don't work that way...neither do any of the other languages.

@pyhedgehog
Copy link
Collaborator Author

pyhedgehog commented Jan 13, 2017

You are able to do this. You are not required to do this.

@Marak
Copy link
Collaborator

Marak commented Jan 13, 2017

Understood! Thank you. Will have time to review this weekend.

@Marak
Copy link
Collaborator

Marak commented Jan 16, 2017

@pyhedgehog -

After adjusting a few lines, I was just able to get this to work! Very cool. Thank you for your contributions.

I need to do more review and more testing, but I will attempt to get this work merged into master and deployed soon.

Would be good if we could get a few other comments on this thread for other python developers.

@Marak
Copy link
Collaborator

Marak commented Feb 15, 2017

@pyhedgehog -

Looking good. I'm getting closer to getting this merged.

Can you provide any examples of doing a binary data request with python? Like using gm library for image resize?

@Marak
Copy link
Collaborator

Marak commented Feb 16, 2017

@pyhedgehog -

I'm getting an error for the echo.py3 service:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/a/dev/stackvana/microcule/bin/binaries/micro-python3", line 21, in <module>
    exec(code)
  File "<string>", line 15, in <module>
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 141, in run
    self.handle_error()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 368, in handle_error
    self.finish_response()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 279, in write
    self._write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 453, in _write
    self.stdout.write(data)
TypeError: must be str, not bytes
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/a/dev/stackvana/microcule/bin/binaries/micro-python3", line 21, in <module>
    exec(code)
  File "<string>", line 15, in <module>
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 141, in run
    self.handle_error()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 368, in handle_error
    self.finish_response()
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 279, in write
    self._write(data)
  File "/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 453, in _write
    self.stdout.write(data)
TypeError: must be str, not bytes

Any ideas? Is wsgi fully supported for python3?

@Marak
Copy link
Collaborator

Marak commented Feb 16, 2017

Good news everyone! Merged with db31d6c

Should be included in the upcoming microcule release.

@pyhedgehog
Copy link
Collaborator Author

I'll check WSGI specs for py3. Maybe it required to return bytes (string encoded to 8bit encoding - usually utf8). Then I should change example.

@Marak Marak merged commit f38d0a2 into stackvana:master Feb 16, 2017
@Marak
Copy link
Collaborator

Marak commented Feb 17, 2017

@pyhedgehog -

I have merged this all into master ( minus py3 wsgi examples ).

Thank you for your contributions. This is very good!

@pyhedgehog pyhedgehog deleted the python-beauty-step1 branch February 17, 2017 20:46
@pyhedgehog
Copy link
Collaborator Author

@Marak py3 and other unicode issues fixed by #33.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants