[python-ldap] SyntaxError with pytest import of ldap in Django

Alan Rominger arominge at redhat.com
Tue Aug 23 15:53:25 EDT 2016


The recently-released pytest version 3.0.0 will include things in
`globals()` that can not be directly evaluated. In particular,
"@py_builtins" and "@pytest_ar" will be returned in the list from `dir()`,
causing a traceback when running tests in a project that uses pytest-django
and imports ldap. The traceback I've been seeing ends with this:

  File "<venv directory>/lib64/python2.7/site-packages/ldap/schema/subentry.py",
line 19, in <module>
    o = eval(_name)
  File "<string>", line 1
    @py_builtins
    ^
SyntaxError: invalid syntax



My suggestion is to do nothing when a variable in the current local scope
can not be evaluated. This would require a small change in the
ldap/schema/subentry.py file. For instance, replace this code:

for _name in dir():
  o = eval(_name)
  if hasattr(o,'schema_attribute'):
    SCHEMA_CLASS_MAPPING[o.schema_attribute] = o
    SCHEMA_ATTR_MAPPING[o] = o.schema_attribute

with this:

for _name in dir():
  try:
    o = eval(_name)
  except SyntaxError:
    continue
  if hasattr(o,'schema_attribute'):
    SCHEMA_CLASS_MAPPING[o.schema_attribute] = o
    SCHEMA_ATTR_MAPPING[o] = o.schema_attribute

Alternatively, if you wanted to be sure that you picked up every variable,
the following line replacement will still work as far as I have tested. But
if all you need are variables from the imports from ldap.schema.models, the
eval() method will pick all of those up either way.

  o = globals()[_name]

It would be great if this could get into a future release of python-ldap,
so that people using this kind of test environment could continue to test
with up-to-date versions of their dependencies.

Best,
Alan Rominger
github: AlanCoding
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20160823/9820df6d/attachment.html>


More information about the python-ldap mailing list