Skip to content

stdin for sam local doesn't have correct encoding #517

Closed

Description

@koblas

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:

  1. Make sure you're using the python2.7 SAM
  2. Create a test file like : { "city": "Tromsø" } -- the 'o' character is non-ASCII character
  3. 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

Activity

  1. sanathkr commented on Jun 29, 2018

    @sanathkr

    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 not unicode. 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

  2. added
    contributors/good-first-issue
    contributors/welcome
    on Jun 29, 2018
  3. koblas commented on Jun 30, 2018

    @koblas
    ContributorAuthor

    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.

  4. added
    stage/waiting-for-release
    on Aug 7, 2018
  5. jfuss commented on Aug 29, 2018

    @jfuss
    Contributor

    Closing this as it was released in 0.6.0

  6. removed
    stage/waiting-for-release
    on Aug 29, 2018
  7. added a commit that references this issue on Nov 15, 2023
  8. added a commit that references this issue on Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/lambda-invokecontributors/good-first-issuecontributors/welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions