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

get_name() changes to handle class property case #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion pytool/json.py
Expand Up @@ -10,7 +10,11 @@
"""
from datetime import datetime

import simplejson as json
try:
import simplejson as json
except:
import json
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplejson is required for Pytool. See the comment about the 'for_json()' hook on line 75.


# Conditionally handle bson import so we don't have to depend on pymongo
try:
import bson
Expand Down
14 changes: 8 additions & 6 deletions pytool/lang.py
Expand Up @@ -31,10 +31,12 @@ def get_name(frame):
maybe_cls = frame.f_locals[varname]

# Get the actual method, if it exists on the class
maybe_func = getattr(maybe_cls, frame.f_code.co_name)

if isinstance(maybe_cls, type):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try replacing this line with:

maybe_func = object.__getattribute__(maybe_cls, frame.f_code.co_name)

... which should avoid recursion issues while still using the standard MRO.

maybe_func = maybe_cls.__dict__[frame.f_code.co_name]
else:
maybe_func = maybe_cls.__class__.__dict__[frame.f_code.co_name]
# If we have self, or a classmethod, we need the class name
if (varname == 'self' or maybe_func.im_self == maybe_cls):
if (varname in ('self', 'cls') or maybe_func.im_self == maybe_cls):
cls_name = (getattr(maybe_cls, '__name__', None)
or getattr(getattr(maybe_cls, '__class__', None),
'__name__', None))
Expand Down Expand Up @@ -236,7 +238,7 @@ class UNSET(object):
>>> # Is good for checking default values
>>> if {}.get('example', UNSET) is UNSET:
... print "Key is missing."
...
...
Key is missing.
>>> # Has no length
>>> len(UNSET)
Expand Down Expand Up @@ -285,7 +287,7 @@ class Namespace(object):
>>> # Namespaces are iterable
>>> for name, value in myns:
... print name, value
...
...
hello world
example.value True
>>> # Namespaces that are empty evaluate as False
Expand All @@ -300,7 +302,7 @@ class Namespace(object):
>>> class MyDescriptor(object):
... def __get__(self, instance, owner):
... return 'Hello World'
...
...
>>> myns.descriptor = MyDescriptor()
>>> myns.descriptor
'Hello World'
Expand Down
23 changes: 23 additions & 0 deletions test/test_lang.py
Expand Up @@ -35,6 +35,29 @@ def test(self):
Test().test()


def test_get_name_class_method():
class Test(object):
@classmethod
def test(cls):
frame = inspect.currentframe()
eq_(pytool.lang.get_name(frame),
'test.test_lang.Test.test')
del frame

Test.test()


def test_get_name_class_property():
class Test(object):
@property
def test(self):
frame = inspect.currentframe()
this_name = pytool.lang.get_name(frame)
del frame
return this_name
eq_(Test().test, 'test.test_lang.Test.test')


def test_classproperty():
class Test(object):
value = 'Test'
Expand Down