【问题标题】:python3.3 TypeError: embedded NUL characterpython3.3 TypeError:嵌入的NUL字符
【发布时间】:2013-02-06 23:13:17
【问题描述】:

我搜索了我们的问题,但找不到任何解决方案。

我已经在虚拟环境中安装了 mysqlconnector。我还将连接字符串从使用 sqllite 连接的金字塔中的 sqlalchemy 脚手架更改为使用 mysqlconnector 的连接字符串:

sqlalchemy.url = mysql+mysqlconnector://admin:admin@127.0.0.1:3306/tetten

我们使用这段代码来初始化我们的数据库。

class Page(Base):
    """ The SQLAlchemy declarative model class for a Page object. """
    __tablename__ = 'pages'
    id = Column(Integer, primary_key=True)
    name = Column(String(40, convert_unicode=True), unique=True)
    data = Column(String(40, convert_unicode=True))

    def __init__(self, name, data):
        self.name = name
        self.data = data


class RootFactory(object):
    __acl__ = [ (Allow, Everyone, 'view'),
                (Allow, 'group:editors', 'edit') ]
    def __init__(self, request):
        pass

表“pages”在数据库中创建,并填充了正确的数据。 当我转到我的浏览器时,我收到以下错误:

**builtins.TypeError**
TypeError: embedded NUL character

这个输出:

Traceback (most recent call last):
  File "c:\env\pyramid_debugtoolbar-1.0.4-py3.3.egg\pyramid_debugtoolbar\panels\performance.py", line 69, in noresource_timer_handler
    result = handler(request)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "C:\env\lib\site-packages\pyramid_tm-0.7-py3.3.egg\pyramid_tm\__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "C:\env\lib\site-packages\pyramid_tm-0.7-py3.3.egg\pyramid_tm\compat.py", line 13, in reraise
    raise value
  File "C:\env\lib\site-packages\pyramid_tm-0.7-py3.3.egg\pyramid_tm\__init__.py", line 63, in tm_tween
    response = handler(request)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\config\views.py", line 367, in rendered_view
    context)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\renderers.py", line 531, in render_view
    return self.render_to_response(response, system, request=request)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\renderers.py", line 561, in render_to_response
    result = self.render(value, system_values, request=request)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\renderers.py", line 557, in render
    result = renderer(value, system_values)
  File "C:\env\lib\site-packages\pyramid-1.4-py3.3.egg\pyramid\chameleon_zpt.py", line 42, in __call__
    result = self.template(**system)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 125, in __call__
    return self.render(**kwargs)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\zpt\template.py", line 257, in render
    return super(PageTemplate, self).render(**vars)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 169, in render
    self.cook_check()
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 293, in cook_check
    self.cook(body)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 147, in cook
    program = self._cook(body, digest, names)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 218, in _cook
    source = self._make(body, builtins)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\template.py", line 249, in _make
    program = self.parse(body)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\zpt\template.py", line 202, in parse
    trim_attribute_space=self.trim_attribute_space,
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\zpt\program.py", line 145, in __init__
    super(MacroProgram, self).__init__(*args, **kwargs)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\program.py", line 32, in __init__
    node = self.visit(kind, args)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\program.py", line 38, in visit
    return visitor(*args)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\zpt\program.py", line 270, in visit_element
    STATIC_ATTRIBUTES = self._create_static_attributes(prepared)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\zpt\program.py", line 790, in _create_static_attributes
    return Static(parse(repr(static_attrs)).body)
  File "C:\env\lib\site-packages\chameleon-2.11-py3.3.egg\chameleon\astutil.py", line 48, in parse
    return compile(source, '', mode, ast.PyCF_ONLY_AST)
TypeError: embedded NUL character

anny1 有解决我的问题的方法吗?

编辑:

我在 python 的 bug 部分找到了这个链接。虽然我不认为这是一个错误,但这可能与我的问题有关。似乎无法弄清楚它是如何工作的。 http://bugs.python.org/issue13617

【问题讨论】:

标签: mysql python-3.x sqlalchemy pyramid mysql-connector


【解决方案1】:

这与您的 SQLAlchemy 设置无关;这是您的模板引擎 (Chameleon) 编译成 Python 代码以提高速度并失败,因为您的模板中有一个 NUL 字符。

使用文本编辑器检查您的模板,并确保文件中没有不可打印的字节。

【讨论】:

  • 你是对的,这与我们的 SQLAlchemy 没有任何关系。我尝试从其他页面中删除所有内容,但仍然出现相同的错误。现在我认为这与我们使用的 mysql 连接器有更多关系,因为当我们将完全相同的代码用于 sqllite 数据库而不是 mysql 时,它可以正常工作。当我们改变它时,它会给出上面的错误。我似乎无法弄清楚我可能在哪里犯了错误,你介意看看我的项目吗?我会把它上传到我的公共保管箱文件夹:dl.dropbox.com/u/6752022/Psqllite.zip希望你能弄明白。
【解决方案2】:

我无法在 Linux 上重现此内容。我的猜测是它是特定于 Windows 的,并且是 Python 中的一个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多