-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
added reflection method #7322
added reflection method #7322
Conversation
@@ -90,6 +90,13 @@ jl_binding_t *jl_get_binding_wr(jl_module_t *m, jl_sym_t *var) | |||
return *bp; | |||
} | |||
|
|||
// return module of binding | |||
DLLEXPORT jl_module_t *jl_get_module_of_binding(jl_sym_t *var) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should take a module argument instead of only using the current module.
Maybe this should be added to |
I'd like to begin printing named objects like functions and types with fully qualified names, which would largely eliminate this problem without requiring reflection (although allowing reflection is still good). |
Variables were the main reason I looked into this. Would variables also be printed like so: julia> e
Base.e = 2.7182818284590... Maybe a bit too cluttered? |
No, because there is no way to do that --- values do not have canonical names. If I say |
Generic functions and types are special in that they do have canonical names based on where they were first defined. Most kinds of values do not have this property. (Where was |
I give a +1 for something like this, at least. I think it makes sense for exported variables to have some kind of reflection to see which module they came from. The function/macro could return module from where the symbol was exported from (if it was). It's somewhat similar to the |
I think |
Values do not carry around where they were defined, but their bindings do. This is what this PR can uncover. @JeffBezanson, thanks for the comments, I'll implement them. |
I added the module to the call signature of
Let me know whether this is ok, and I still needs some tests. |
typesof(args...) = map(a->(isa(a,Type) ? Type{a} : typeof(a)), args) | ||
|
||
function gen_call_with_extracted_types(fcn, ex0) | ||
if isa(ex0, Symbol) | ||
return :($fcn(symbol($(string(ex0)))) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I need to preserve ex0
as symbol. Is there a better way to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$(Expr(:quote, ex0))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or:
Meta.quot(:ex0)
Here the complete thing. It is ready to be merged as far as I am concerned. A few things to ponder and maybe update in this PR:
|
Ping. Any interest in this, should I add/change something? Or should I close the PR? |
@@ -1 +1 @@ | |||
Subproject commit fe3db7224daf4798ac950f7aa2c6ae7b60b85491 | |||
Subproject commit 85361bb67d6eedb8ee956b5c09f50ae547cf7b7a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I once changed that to get python 3.0 compatibility and forgot about it.
89d4140
to
4ab7eaf
Compare
@nolta fixed. |
2ef98c5
to
0388647
Compare
// return module of binding | ||
DLLEXPORT jl_module_t *jl_get_module_of_binding(jl_module_t *m, jl_sym_t *var) | ||
{ | ||
jl_binding_t *b = (jl_binding_t*)ptrhash_get(&m->bindings, var); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to use jl_get_binding
and check for NULL.
4ab7eaf
to
ad62b17
Compare
jl_binding_t *b = jl_get_binding((*bp)->owner, var); | ||
return b->owner; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeffBezanson: I'm not sure this addresses your concerns. It certainly does not check for NULL but maybe the HT_NOTFOUND is good too? This is the best I could come up with my trial-and-error approach. If this is not good either explain more, or give me a test case which fails due to the bug, or update it yourself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jl_get_binding
might return NULL if the symbol is unbound. You should only need to call jl_get_binding
, check for NULL, and otherwise return b->owner
. The ptrhash_bp
call is unnecessary and there are other conditions you would need to check (duplicating jl_get_binding
-- see its definition in modules.c
)
Rebased and updated according to Jeff's line-comment. |
Looks fine to me now, other than some whitespace error somewhere (see travis) |
0ae3d6e
to
6a6f6e7
Compare
Squashed the commits and made a couple of cosmetical changes. But I cannot figure out what error Travis is reporting. (Build runs fine on my Linux machine.) Sorry, but could you explain on how to read: https://travis-ci.org/JuliaLang/julia/jobs/46417667 ? |
6a6f6e7
to
216d361
Compare
Ok, my bad: some blocker in Firefox made the Travis site not show the actual error report, thus I was confused. Now fixed the whitespace issue. |
Bump. I think this is ready to be merged. |
a75a27f
to
befc5f2
Compare
Fixed white-space issue. |
Use with `@which variable`. Includes tests and update of docs.
befc5f2
to
e75f2ce
Compare
Wanted to use this functionality just now, so bumping. |
@@ -222,6 +222,9 @@ code_warntype(f, t::(Type...)) = code_warntype(STDOUT, f, t) | |||
typesof(args...) = map(a->(isa(a,Type) ? Type{a} : typeof(a)), args) | |||
|
|||
function gen_call_with_extracted_types(fcn, ex0) | |||
if isa(ex0, Symbol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
odd 5-space indent here... also seems strange to me that this isn't using dispatch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, just a bit too late & sorry. Fixed in #10944
as suggested by @tkelman
Small cosmetic correction to merged PR #7322
as suggested by @tkelman
As far as I know, when using
using
it is not easy to find out which module a particular binding came from. At least when the object the binding points to does not contain that data, like for instance a variable.This adds a method and a macro to do that:
If this is of interest, someone should have a look at the c-code! And I will add some test. Should it be exported? If so
function_module
should probably be exported too.