Repository navigation
Closed
Labels
area/lambda-invokecontributors/good-first-issueGood first issue for a contributorcontributors/welcomeContributors are welcome to work on this
Description
stdin for sam local doesn't have correct encoding
When piping data to sam local invoke you will get UnicodeEncodeError when presented with non-ASCII Unicode streams.
Steps to reproduce the issue:
- Make sure you're using the python2.7 SAM
- Create a test file like :
{ "city": "Tromsø" }-- the 'o' character is non-ASCII character cat test.json | sam local invoke
Note: this only reproduces when it's piped in (via stdin) rather than provided as a '-e file'
Observed result:
Traceback (most recent call last):
File "/Users/koblas/.virtualenvs/aws-sam/bin/sam", line 11, in <module>
sys.exit(cli())
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/commands/local/invoke/cli.py", line 44, in cli
docker_network, log_file, skip_pull_image, profile) # pragma: no cover
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/commands/local/invoke/cli.py", line 76, in do_cli
stderr=context.stderr)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/commands/local/lib/local_lambda.py", line 80, in invoke
stdout=stdout, stderr=stderr)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/local/lambdafn/runtime.py", line 68, in invoke
env_vars = environ.resolve()
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/local/lambdafn/env_vars.py", line 100, in resolve
result[name] = self._stringify_value(value)
File "/Users/koblas/.virtualenvs/aws-sam/lib/python2.7/site-packages/samcli/local/lambdafn/env_vars.py", line 195, in _stringify_value
result = str(value)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 16: ordinal not in range(128)
Expected result:
Lambda function should run in the sam way as if invoked as sam local invoke -e test.jsonAdditional environment details (Ex: Windows, Mac, Amazon Linux etc)
Output of sam --version:
SAM CLI, version 0.4.0
Optional Debug logs:
Add --debug flag to command you are running
Reacted by Vladimir Setka
Metadata
Metadata
Assignees
Labels
area/lambda-invokecontributors/good-first-issueGood first issue for a contributorcontributors/welcomeContributors are welcome to work on this
Activity
sanathkr commentedon Jun 29, 2018
Actually, it is not the stdin. It is stringifying the environment variables that are passed.
It is easy to fix: At this line, update the condition to do
str(value)only if string is notunicode. You also need to do this only for Python2.Will you be interested in sending a Pull Request? I can help you out with it if you are stuck
koblas commentedon Jun 30, 2018
Truthfully, dealing with these encoding issues in python is not something that I enjoy. As you probably know, dealing with the Python 2 vs 3 and the file encoding vs the locale encoding always leaves edge cases around.
Since what happens if the file is in UTF-8 and the locale is in ISO-8859-5 so you have environment variables in a different encoding than the file input. Maybe you do have an easy fix that I'm over thinking.
jfuss commentedon Aug 29, 2018
Closing this as it was released in 0.6.0
Add python3.12. (aws#517)
Add python3.12. (#517) (#6273)